From 98281388d4db5addc0ae7d36f2efa88a8c2611da 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 | 86 ++++++++++++++++++++++++++------------ helix-core/Cargo.toml | 1 + helix-core/src/lib.rs | 1 + helix-term/Cargo.toml | 2 + helix-term/src/commands.rs | 13 +++++- helix-view/Cargo.toml | 1 - helix-view/src/document.rs | 2 +- 7 files changed, 76 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b7ed2c9c81825..08d49dbc6312f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -260,7 +260,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.11", + "syn 2.0.12", ] [[package]] @@ -277,7 +277,7 @@ checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.11", + "syn 2.0.12", ] [[package]] @@ -359,7 +359,7 @@ checksum = "50d6a0976c999d473fe89ad888d5a284e55366d9dc9038b1ba2aa15128c4afa0" dependencies = [ "errno-dragonfly", "libc", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -420,7 +420,7 @@ dependencies = [ "cfg-if", "libc", "redox_syscall 0.2.16", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -450,15 +450,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86d7a0c1aa76363dac491de0ee99faf6941128376f1cf96f07db7603b7de69dd" +checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" [[package]] name = "futures-executor" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1997dd9df74cdac935c76252744c1ed5794fac083242ea4fe77ef3ed60ba0f83" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" dependencies = [ "futures-core", "futures-task", @@ -467,15 +467,15 @@ dependencies = [ [[package]] name = "futures-task" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd65540d33b37b16542a0438c12e6aeead10d4ac5d05bd3f805b8f35ab592879" +checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" [[package]] name = "futures-util" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab" +checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ "futures-core", "futures-task", @@ -1106,6 +1106,7 @@ dependencies = [ "unicode-general-category", "unicode-segmentation", "unicode-width", + "url", ] [[package]] @@ -1190,6 +1191,7 @@ dependencies = [ "libc", "log", "once_cell", + "open", "pulldown-cmark", "serde", "serde_json", @@ -1261,7 +1263,6 @@ dependencies = [ "tokio", "tokio-stream", "toml", - "url", "which", ] @@ -1399,7 +1400,7 @@ checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" dependencies = [ "hermit-abi 0.3.1", "libc", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -1450,9 +1451,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd550e73688e6d578f0ac2119e32b797a327631a42f9433e59d02e139c8df60d" +checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" [[package]] name = "lock_api" @@ -1525,7 +1526,7 @@ dependencies = [ "libc", "log", "wasi", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -1594,6 +1595,16 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[package]] +name = "open" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2078c0039e6a54a0c42c28faa984e115fb4c2d5bf2208f77d1961002df8576f8" +dependencies = [ + "pathdiff", + "windows-sys 0.42.0", +] + [[package]] name = "parking_lot" version = "0.12.1" @@ -1614,9 +1625,15 @@ dependencies = [ "libc", "redox_syscall 0.2.16", "smallvec", - "windows-sys", + "windows-sys 0.45.0", ] +[[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" @@ -1770,7 +1787,7 @@ dependencies = [ "io-lifetimes", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -1817,7 +1834,7 @@ checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" dependencies = [ "proc-macro2", "quote", - "syn 2.0.11", + "syn 2.0.12", ] [[package]] @@ -1839,7 +1856,7 @@ checksum = "bcec881020c684085e55a25f7fd888954d56609ef363479dc5a1305eb0d40cab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.11", + "syn 2.0.12", ] [[package]] @@ -1981,9 +1998,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.11" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21e3787bb71465627110e7d87ed4faaa36c1f61042ee67badb9e2ef173accc40" +checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" dependencies = [ "proc-macro2", "quote", @@ -2000,7 +2017,7 @@ dependencies = [ "fastrand", "redox_syscall 0.3.5", "rustix", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -2049,7 +2066,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.11", + "syn 2.0.12", ] [[package]] @@ -2131,7 +2148,7 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -2142,7 +2159,7 @@ checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce" dependencies = [ "proc-macro2", "quote", - "syn 2.0.11", + "syn 2.0.12", ] [[package]] @@ -2417,6 +2434,21 @@ dependencies = [ "windows-targets 0.47.0", ] +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + [[package]] name = "windows-sys" version = "0.45.0" diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml index e5c5f8f1a24ec..6593144d620da 100644 --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -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" diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index b67e2c8a38e23..eb096e795b2ff 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -1,4 +1,5 @@ pub use encoding_rs as encoding; +pub use url; pub mod auto_pairs; pub mod chars; diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index 5222ddaa15f64..b13d7d3760ead 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -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" diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b55f1ab72bd7b..423ae0e1a6806 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -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, }; @@ -319,7 +320,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", @@ -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)); } diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index 4f7b08edd191b..64deed2d8141e 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -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" } diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index eca6002653f54..c44f0b2df596a 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -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(