Skip to content

Commit

Permalink
feat(commands): open urls with goto_file command
Browse files Browse the repository at this point in the history
Add capability for `goto_file` command to open an URL under cursor.

Fixes: helix-editor#1472
Superseds: helix-editor#4398
  • Loading branch information
matoous committed Mar 31, 2023
1 parent ec55b4d commit 9828138
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 30 deletions.
86 changes: 59 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions helix-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ log = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
toml = "0.7"
url = "2.3.1"

imara-diff = "0.1.0"

Expand Down
1 change: 1 addition & 0 deletions helix-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use encoding_rs as encoding;
pub use url;

pub mod auto_pairs;
pub mod chars;
Expand Down
2 changes: 2 additions & 0 deletions helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ ignore = "0.4"
pulldown-cmark = { version = "0.9", default-features = false }
# file type detection
content_inspector = "0.2.4"
# openning URLs
open = "3.2.0"

# config
toml = "0.7"
Expand Down
13 changes: 12 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use helix_core::{
textobject,
tree_sitter::Node,
unicode::width::UnicodeWidthChar,
url::Url,
visual_offset_from_block, LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice,
Selection, SmallVec, Tendril, Transaction,
};
Expand Down Expand Up @@ -319,7 +320,7 @@ impl MappableCommand {
goto_implementation, "Goto implementation",
goto_file_start, "Goto line number <n> else file start",
goto_file_end, "Goto file end",
goto_file, "Goto files in selection",
goto_file, "Goto files/URLs in selection",
goto_file_hsplit, "Goto files in selection (hsplit)",
goto_file_vsplit, "Goto files in selection (vsplit)",
goto_reference, "Goto references",
Expand Down Expand Up @@ -1153,6 +1154,16 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
for sel in paths {
let p = sel.trim();
if !p.is_empty() {
if let Ok(url) = Url::parse(p) {
if let Err(e) = open::that(url.as_str()) {
cx.editor.set_error(format!(
"Open file failed for url '{}': {:?}",
url.as_str(),
e
));
}
return;
}
if let Err(e) = cx.editor.open(&PathBuf::from(p), action) {
cx.editor.set_error(format!("Open file failed: {:?}", e));
}
Expand Down
1 change: 0 additions & 1 deletion helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ helix-vcs = { version = "0.6", path = "../helix-vcs" }

# Conversion traits
once_cell = "1.17"
url = "2"

arc-swap = { version = "1.6.0" }

Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ where
*mut_ref = f(mem::take(mut_ref));
}

use helix_core::url::Url;
use helix_lsp::lsp;
use url::Url;

impl Document {
pub fn from(
Expand Down

0 comments on commit 9828138

Please sign in to comment.