diff options
author | Michael Davis | 2024-01-16 18:59:48 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2024-01-18 01:57:53 +0000 |
commit | 1f916e65cff4459698d465b2f4558da1e1bf6e44 (patch) | |
tree | 5b6768e3069085bc5ba995efa95fdc73241ed0f6 /helix-term | |
parent | af8e524a7d06253fa854bf8954f64312e11d0ea0 (diff) |
Create helix-stdx crate for stdlib extensions
helix-stdx is meant to carry extensions to the stdlib or low-level
dependencies that are useful in all other crates. This commit starts
with all of the path functions from helix-core and the CWD tracking that
lived in helix-loader.
The CWD tracking in helix-loader was previously unable to call the
canonicalization functions in helix-core. Switching to our custom
canonicalization code should make no noticeable difference though
since `std::env::current_dir` returns a canonicalized path with
symlinks resolved (at least on unix).
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/Cargo.toml | 1 | ||||
-rw-r--r-- | helix-term/src/application.rs | 3 | ||||
-rw-r--r-- | helix-term/src/commands.rs | 10 | ||||
-rw-r--r-- | helix-term/src/commands/dap.rs | 2 | ||||
-rw-r--r-- | helix-term/src/commands/lsp.rs | 7 | ||||
-rw-r--r-- | helix-term/src/commands/typed.rs | 16 | ||||
-rw-r--r-- | helix-term/src/main.rs | 6 | ||||
-rw-r--r-- | helix-term/src/ui/mod.rs | 4 | ||||
-rw-r--r-- | helix-term/src/ui/picker.rs | 2 | ||||
-rw-r--r-- | helix-term/tests/test/commands/write.rs | 11 | ||||
-rw-r--r-- | helix-term/tests/test/splits.rs | 8 |
11 files changed, 36 insertions, 34 deletions
diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index 80bda2b6..21c35553 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -23,6 +23,7 @@ name = "hx" path = "src/main.rs" [dependencies] +helix-stdx = { path = "../helix-stdx" } helix-core = { path = "../helix-core" } helix-event = { path = "../helix-event" } helix-view = { path = "../helix-view" } diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 1b0a06dd..290441b4 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -1,11 +1,12 @@ use arc_swap::{access::Map, ArcSwap}; use futures_util::Stream; -use helix_core::{path::get_relative_path, pos_at_coords, syntax, Selection}; +use helix_core::{pos_at_coords, syntax, Selection}; use helix_lsp::{ lsp::{self, notification::Notification}, util::lsp_range_to_range, LspProgressMap, }; +use helix_stdx::path::get_relative_path; use helix_view::{ align_view, document::DocumentSavedEventResult, diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 937326f6..53783e4e 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2169,7 +2169,7 @@ fn global_search(cx: &mut Context) { type Data = Option<PathBuf>; fn format(&self, current_path: &Self::Data) -> Row { - let relative_path = helix_core::path::get_relative_path(&self.path) + let relative_path = helix_stdx::path::get_relative_path(&self.path) .to_string_lossy() .into_owned(); if current_path @@ -2218,7 +2218,7 @@ fn global_search(cx: &mut Context) { .case_smart(smart_case) .build(regex.as_str()) { - let search_root = helix_loader::current_working_dir(); + let search_root = helix_stdx::env::current_working_dir(); if !search_root.exists() { cx.editor .set_error("Current working directory does not exist"); @@ -2731,7 +2731,7 @@ fn file_picker_in_current_buffer_directory(cx: &mut Context) { } fn file_picker_in_current_directory(cx: &mut Context) { - let cwd = helix_loader::current_working_dir(); + let cwd = helix_stdx::env::current_working_dir(); if !cwd.exists() { cx.editor .set_error("Current working directory does not exist"); @@ -2759,7 +2759,7 @@ fn buffer_picker(cx: &mut Context) { let path = self .path .as_deref() - .map(helix_core::path::get_relative_path); + .map(helix_stdx::path::get_relative_path); let path = match path.as_deref().and_then(Path::to_str) { Some(path) => path, None => SCRATCH_BUFFER_NAME, @@ -2826,7 +2826,7 @@ fn jumplist_picker(cx: &mut Context) { let path = self .path .as_deref() - .map(helix_core::path::get_relative_path); + .map(helix_stdx::path::get_relative_path); let path = match path.as_deref().and_then(Path::to_str) { Some(path) => path, None => SCRATCH_BUFFER_NAME, diff --git a/helix-term/src/commands/dap.rs b/helix-term/src/commands/dap.rs index dec25cbd..d62b0a4e 100644 --- a/helix-term/src/commands/dap.rs +++ b/helix-term/src/commands/dap.rs @@ -217,7 +217,7 @@ pub fn dap_start_impl( } } - args.insert("cwd", to_value(helix_loader::current_working_dir())?); + args.insert("cwd", to_value(helix_stdx::env::current_working_dir())?); let args = to_value(args).unwrap(); diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 0096e6aa..051cdcd3 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -17,9 +17,8 @@ use tui::{ use super::{align_view, push_jump, Align, Context, Editor, Open}; -use helix_core::{ - path, syntax::LanguageServerFeature, text_annotations::InlineAnnotation, Selection, -}; +use helix_core::{syntax::LanguageServerFeature, text_annotations::InlineAnnotation, Selection}; +use helix_stdx::path; use helix_view::{ document::{DocumentInlayHints, DocumentInlayHintsId, Mode}, editor::Action, @@ -1018,7 +1017,7 @@ fn goto_impl( locations: Vec<lsp::Location>, offset_encoding: OffsetEncoding, ) { - let cwdir = helix_loader::current_working_dir(); + let cwdir = helix_stdx::env::current_working_dir(); match locations.as_slice() { [location] => { diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index eb88e041..ee02a7d2 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -7,7 +7,7 @@ use super::*; use helix_core::fuzzy::fuzzy_match; use helix_core::indent::MAX_INDENT; -use helix_core::{encoding, line_ending, path::get_canonicalized_path, shellwords::Shellwords}; +use helix_core::{encoding, line_ending, shellwords::Shellwords}; use helix_lsp::{OffsetEncoding, Url}; use helix_view::document::DEFAULT_LANGUAGE_NAME; use helix_view::editor::{Action, CloseError, ConfigEvent}; @@ -111,7 +111,7 @@ fn open(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) -> ensure!(!args.is_empty(), "wrong argument count"); for arg in args { let (path, pos) = args::parse_file(arg); - let path = helix_core::path::expand_tilde(&path); + let path = helix_stdx::path::expand_tilde(&path); // If the path is a directory, open a file picker on that directory and update the status // message if let Ok(true) = std::fs::canonicalize(&path).map(|p| p.is_dir()) { @@ -1079,18 +1079,17 @@ fn change_current_directory( return Ok(()); } - let dir = helix_core::path::expand_tilde( + let dir = helix_stdx::path::expand_tilde( args.first() .context("target directory not provided")? - .as_ref() .as_ref(), ); - helix_loader::set_current_working_dir(dir)?; + helix_stdx::env::set_current_working_dir(dir)?; cx.editor.set_status(format!( "Current working directory is now {}", - helix_loader::current_working_dir().display() + helix_stdx::env::current_working_dir().display() )); Ok(()) } @@ -1104,7 +1103,7 @@ fn show_current_directory( return Ok(()); } - let cwd = helix_loader::current_working_dir(); + let cwd = helix_stdx::env::current_working_dir(); let message = format!("Current working directory is {}", cwd.display()); if cwd.exists() { @@ -2409,7 +2408,8 @@ fn move_buffer( ensure!(args.len() == 1, format!(":move takes one argument")); let doc = doc!(cx.editor); - let new_path = get_canonicalized_path(&PathBuf::from(args.first().unwrap().to_string())); + let new_path = + helix_stdx::path::canonicalize(&PathBuf::from(args.first().unwrap().to_string())); let old_path = doc .path() .ok_or_else(|| anyhow!("Scratch buffer cannot be moved. Use :write instead"))? diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index a62c54a4..132ee796 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -118,16 +118,16 @@ FLAGS: // Before setting the working directory, resolve all the paths in args.files for (path, _) in args.files.iter_mut() { - *path = helix_core::path::get_canonicalized_path(path); + *path = helix_stdx::path::canonicalize(&path); } // NOTE: Set the working directory early so the correct configuration is loaded. Be aware that // Application::new() depends on this logic so it must be updated if this changes. if let Some(path) = &args.working_directory { - helix_loader::set_current_working_dir(path)?; + helix_stdx::env::set_current_working_dir(path)?; } else if let Some((path, _)) = args.files.first().filter(|p| p.0.is_dir()) { // If the first file is a directory, it will be the working directory unless -w was specified - helix_loader::set_current_working_dir(path)?; + helix_stdx::env::set_current_working_dir(path)?; } let config = match Config::load_default() { diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 660bbfea..efa2473e 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -409,7 +409,7 @@ pub mod completers { use std::path::Path; let is_tilde = input == "~"; - let path = helix_core::path::expand_tilde(Path::new(input)); + let path = helix_stdx::path::expand_tilde(Path::new(input)); let (dir, file_name) = if input.ends_with(std::path::MAIN_SEPARATOR) { (path, None) @@ -430,7 +430,7 @@ pub mod completers { match path.parent() { Some(path) if !path.as_os_str().is_empty() => path.to_path_buf(), // Path::new("h")'s parent is Some("")... - _ => helix_loader::current_working_dir(), + _ => helix_stdx::env::current_working_dir(), } }; diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 08a367ba..4be5a11e 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -63,7 +63,7 @@ impl PathOrId { fn get_canonicalized(self) -> Self { use PathOrId::*; match self { - Path(path) => Path(helix_core::path::get_canonicalized_path(&path)), + Path(path) => Path(helix_stdx::path::canonicalize(path)), Id(id) => Id(id), } } diff --git a/helix-term/tests/test/commands/write.rs b/helix-term/tests/test/commands/write.rs index 376ba5e7..adc721c5 100644 --- a/helix-term/tests/test/commands/write.rs +++ b/helix-term/tests/test/commands/write.rs @@ -3,7 +3,8 @@ use std::{ ops::RangeInclusive, }; -use helix_core::{diagnostic::Severity, path::get_normalized_path}; +use helix_core::diagnostic::Severity; +use helix_stdx::path; use helix_view::doc; use super::*; @@ -23,7 +24,7 @@ async fn test_write_quit_fail() -> anyhow::Result<()> { assert_eq!(1, docs.len()); let doc = docs.pop().unwrap(); - assert_eq!(Some(&get_normalized_path(file.path())), doc.path()); + assert_eq!(Some(&path::normalize(file.path())), doc.path()); assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1); }), false, @@ -269,7 +270,7 @@ async fn test_write_scratch_to_new_path() -> anyhow::Result<()> { assert_eq!(1, docs.len()); let doc = docs.pop().unwrap(); - assert_eq!(Some(&get_normalized_path(file.path())), doc.path()); + assert_eq!(Some(&path::normalize(file.path())), doc.path()); }), false, ) @@ -341,7 +342,7 @@ async fn test_write_new_path() -> anyhow::Result<()> { Some(&|app| { let doc = doc!(app.editor); assert!(!app.editor.is_err()); - assert_eq!(&get_normalized_path(file1.path()), doc.path().unwrap()); + assert_eq!(&path::normalize(file1.path()), doc.path().unwrap()); }), ), ( @@ -349,7 +350,7 @@ async fn test_write_new_path() -> anyhow::Result<()> { Some(&|app| { let doc = doc!(app.editor); assert!(!app.editor.is_err()); - assert_eq!(&get_normalized_path(file2.path()), doc.path().unwrap()); + assert_eq!(&path::normalize(file2.path()), doc.path().unwrap()); assert!(app.editor.document_by_path(file1.path()).is_none()); }), ), diff --git a/helix-term/tests/test/splits.rs b/helix-term/tests/test/splits.rs index f010c86b..3b66c048 100644 --- a/helix-term/tests/test/splits.rs +++ b/helix-term/tests/test/splits.rs @@ -1,6 +1,6 @@ use super::*; -use helix_core::path::get_normalized_path; +use helix_stdx::path; #[tokio::test(flavor = "multi_thread")] async fn test_split_write_quit_all() -> anyhow::Result<()> { @@ -27,21 +27,21 @@ async fn test_split_write_quit_all() -> anyhow::Result<()> { let doc1 = docs .iter() - .find(|doc| doc.path().unwrap() == &get_normalized_path(file1.path())) + .find(|doc| doc.path().unwrap() == &path::normalize(file1.path())) .unwrap(); assert_eq!("hello1", doc1.text().to_string()); let doc2 = docs .iter() - .find(|doc| doc.path().unwrap() == &get_normalized_path(file2.path())) + .find(|doc| doc.path().unwrap() == &path::normalize(file2.path())) .unwrap(); assert_eq!("hello2", doc2.text().to_string()); let doc3 = docs .iter() - .find(|doc| doc.path().unwrap() == &get_normalized_path(file3.path())) + .find(|doc| doc.path().unwrap() == &path::normalize(file3.path())) .unwrap(); assert_eq!("hello3", doc3.text().to_string()); |