From d28a23ebc44de6dcdad077112fd53f7f4937b6ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Sat, 4 Feb 2023 10:07:39 +0000 Subject: [PATCH] feat(commands): open urls with goto_file command Add capability for `goto_file` command to open an URL under cursor. Fixes: https://github.com/helix-editor/helix/issues/1472 Superseds: https://github.com/helix-editor/helix/pull/4398 --- Cargo.lock | 17 +++++++++++++++++ helix-term/Cargo.toml | 2 ++ helix-term/src/commands.rs | 13 ++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 066af52830ade..e5369df27ccd1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1217,6 +1217,7 @@ dependencies = [ "indoc", "log", "once_cell", + "open", "pulldown-cmark", "serde", "serde_json", @@ -1609,6 +1610,16 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" +[[package]] +name = "open" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2078c0039e6a54a0c42c28faa984e115fb4c2d5bf2208f77d1961002df8576f8" +dependencies = [ + "pathdiff", + "windows-sys", +] + [[package]] name = "parking_lot" version = "0.11.2" @@ -1657,6 +1668,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "percent-encoding" version = "2.2.0" diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index 603f37d39ab8f..2be366e78a9ea 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -59,6 +59,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" diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d1dc92236cf0a..e24ca22130f3c 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3,6 +3,7 @@ pub(crate) mod lsp; pub(crate) mod typed; pub use dap::*; +use helix_lsp::Url; use helix_vcs::Hunk; pub use lsp::*; use tui::widgets::Row; @@ -302,7 +303,7 @@ impl MappableCommand { goto_implementation, "Goto implementation", goto_file_start, "Goto line number 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", @@ -1134,6 +1135,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)); }