diff --git a/helix-core/src/path.rs b/helix-core/src/path.rs index efa46c46e98f9..de57ff98f5838 100644 --- a/helix-core/src/path.rs +++ b/helix-core/src/path.rs @@ -96,12 +96,10 @@ pub fn get_canonicalized_path(path: &Path) -> std::io::Result { Ok(get_normalized_path(path.as_path())) } -pub fn get_relative_path(path: &Path) -> PathBuf { +pub fn get_relative_path(path: &Path) -> std::io::Result { let path = PathBuf::from(path); let path = if path.is_absolute() { - let cwdir = std::env::current_dir() - .map(|path| get_normalized_path(&path)) - .expect("couldn't determine current directory"); + let cwdir = std::env::current_dir().map(|path| get_normalized_path(&path))?; get_normalized_path(&path) .strip_prefix(cwdir) .map(PathBuf::from) @@ -109,7 +107,7 @@ pub fn get_relative_path(path: &Path) -> PathBuf { } else { path }; - fold_home_dir(&path) + Ok(fold_home_dir(&path)) } /// Returns a truncated filepath where the basepart of the path is reduced to the first diff --git a/helix-loader/src/lib.rs b/helix-loader/src/lib.rs index ad4ad899db67c..fbac4b7d1580a 100644 --- a/helix-loader/src/lib.rs +++ b/helix-loader/src/lib.rs @@ -217,7 +217,7 @@ pub fn merge_toml_values(left: toml::Value, right: toml::Value, merge_depth: usi /// If no workspace was found returns (CWD, true). /// Otherwise (workspace, false) is returned pub fn find_workspace() -> (PathBuf, bool) { - let current_dir = std::env::current_dir().expect("unable to determine current directory"); + let current_dir = std::env::current_dir().unwrap_or_default(); for ancestor in current_dir.ancestors() { if ancestor.join(".git").exists() || ancestor.join(".helix").exists() { return (ancestor.to_owned(), false); diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 7e9684827a9cd..9df02ab9f64b4 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -585,9 +585,10 @@ impl Application { } // TODO: fix being overwritten by lsp + let path = get_relative_path(&doc_save_event.path).unwrap_or(doc_save_event.path); self.editor.set_status(format!( "'{}' written, {}L {}B", - get_relative_path(&doc_save_event.path).to_string_lossy(), + path.to_string_lossy(), lines, bytes )); diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b7a22c7b24d6c..160cbeeb6f8a7 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2032,6 +2032,7 @@ fn global_search(cx: &mut Context) { fn format(&self, current_path: &Self::Data) -> Row { let relative_path = helix_core::path::get_relative_path(&self.path) + .unwrap_or_else(|_| self.path.to_owned()) .to_string_lossy() .into_owned(); if current_path @@ -2065,7 +2066,7 @@ fn global_search(cx: &mut Context) { .map(|comp| (0.., std::borrow::Cow::Owned(comp.clone()))) .collect() }, - move |_editor, regex, event| { + move |editor, regex, event| { if event != PromptEvent::Validate { return; } @@ -2078,8 +2079,13 @@ fn global_search(cx: &mut Context) { .binary_detection(BinaryDetection::quit(b'\x00')) .build(); - let search_root = std::env::current_dir() - .expect("Global search error: Failed to get current dir"); + let search_root = match std::env::current_dir() { + Ok(cwd) => cwd, + Err(err) => { + editor.set_error(format!("Global search error: {err}")); + return; + } + }; let dedup_symlinks = file_picker_config.deduplicate_links; let absolute_root = search_root .canonicalize() @@ -2151,7 +2157,9 @@ fn global_search(cx: &mut Context) { let call: job::Callback = Callback::EditorCompositor(Box::new( move |editor: &mut Editor, compositor: &mut Compositor| { if all_matches.is_empty() { - editor.set_status("No matches found"); + if !editor.is_err() { + editor.set_status("No matches found"); + } return; } @@ -2539,14 +2547,15 @@ fn buffer_picker(cx: &mut Context) { type Data = (); fn format(&self, _data: &Self::Data) -> Row { - let path = self - .path - .as_deref() - .map(helix_core::path::get_relative_path); - let path = match path.as_deref().and_then(Path::to_str) { - Some(path) => path, - None => SCRATCH_BUFFER_NAME, - }; + let path = self.path.as_deref().map_or_else( + || SCRATCH_BUFFER_NAME.to_owned(), + |path| { + helix_core::path::get_relative_path(path) + .unwrap_or_else(|_| path.into()) + .to_string_lossy() + .into_owned() + }, + ); let mut flags = String::new(); if self.is_modified { @@ -2556,7 +2565,7 @@ fn buffer_picker(cx: &mut Context) { flags.push('*'); } - Row::new([self.id.to_string(), flags, path.to_string()]) + Row::new([self.id.to_string(), flags, path]) } } @@ -2610,14 +2619,15 @@ fn jumplist_picker(cx: &mut Context) { type Data = (); fn format(&self, _data: &Self::Data) -> Row { - let path = self - .path - .as_deref() - .map(helix_core::path::get_relative_path); - let path = match path.as_deref().and_then(Path::to_str) { - Some(path) => path, - None => SCRATCH_BUFFER_NAME, - }; + let path = self.path.as_deref().map_or_else( + || SCRATCH_BUFFER_NAME.to_owned(), + |path| { + helix_core::path::get_relative_path(path) + .unwrap_or_else(|_| path.into()) + .to_string_lossy() + .into_owned() + }, + ); let mut flags = Vec::new(); if self.is_current { diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 8c3fd13b558cf..1dbc194bc8e78 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -116,7 +116,7 @@ impl ui::menu::Item for SymbolInformationItem { } else { match self.symbol.location.uri.to_file_path() { Ok(path) => { - let get_relative_path = path::get_relative_path(path.as_path()); + let get_relative_path = path::get_relative_path(path.as_path()).unwrap_or(path); format!( "{} ({})", &self.symbol.name, diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index ec328ec55cea3..8a72f0d6fa47c 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -476,7 +476,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("")... - _ => std::env::current_dir().expect("couldn't determine current directory"), + _ => std::env::current_dir().unwrap_or_default(), } }; diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index bd3c465d41dde..42f86f2f93e01 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -1546,10 +1546,12 @@ impl Document { &self.selections } + // Get the relative path of the document. + // Returns its absolute path in case the relative one could not be computed pub fn relative_path(&self) -> Option { self.path .as_deref() - .map(helix_core::path::get_relative_path) + .map(|path| helix_core::path::get_relative_path(path).unwrap_or_else(|_| path.into())) } pub fn display_name(&self) -> Cow<'static, str> {