diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 735a62c1b2cad..6056e3410548f 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -47,9 +47,41 @@ pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option { /// * Top-most folder containing a root marker if not git repository detected /// * Current working directory as fallback pub fn find_root(root: Option<&str>, root_markers: &[String]) -> Option { - helix_loader::find_root_impl(root, root_markers) - .first() - .cloned() + let current_dir = std::env::current_dir().expect("unable to determine current directory"); + + let root = match root { + Some(root) => { + let root = std::path::Path::new(root); + if root.is_absolute() { + root.to_path_buf() + } else { + current_dir.join(root) + } + } + None => current_dir.clone(), + }; + + let mut top_marker = None; + for ancestor in root.ancestors() { + if root_markers + .iter() + .any(|marker| ancestor.join(marker).exists()) + { + top_marker = Some(ancestor); + } + + // Don't go higher than repo if we're in one + if ancestor.join(".git").is_dir() { + // Top marker is repo root if not root marker is detected + if top_marker.is_none() { + top_marker = Some(ancestor); + } + break; + } + } + + // Return the found top marker or the current_dir as fallback + top_marker.map(|a| a.to_path_buf()).or(Some(current_dir)) } pub use ropey::{str_utils, Rope, RopeBuilder, RopeSlice}; diff --git a/helix-loader/src/lib.rs b/helix-loader/src/lib.rs index 3c9905f5a14b4..46f6db8f1af5d 100644 --- a/helix-loader/src/lib.rs +++ b/helix-loader/src/lib.rs @@ -59,7 +59,7 @@ pub fn config_dir() -> PathBuf { } pub fn local_config_dirs() -> Vec { - let directories = find_root_impl(None, &[".helix".to_string()]) + let directories = find_local_config_dirs() .into_iter() .map(|path| path.join(".helix")) .collect(); @@ -90,32 +90,16 @@ pub fn log_file() -> PathBuf { cache_dir().join("helix.log") } -pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec { +pub fn find_local_config_dirs() -> Vec { let current_dir = std::env::current_dir().expect("unable to determine current directory"); let mut directories = Vec::new(); - let root = match root { - Some(root) => { - let root = std::path::Path::new(root); - if root.is_absolute() { - root.to_path_buf() - } else { - current_dir.join(root) - } - } - None => current_dir, - }; - - for ancestor in root.ancestors() { - // don't go higher than repo + for ancestor in current_dir.ancestors() { + // Don't go higher than repo if we're in one if ancestor.join(".git").is_dir() { - // Use workspace if detected from marker directories.push(ancestor.to_path_buf()); break; - } else if root_markers - .iter() - .any(|marker| ancestor.join(marker).exists()) - { + } else if ancestor.join(".helix").is_dir() { directories.push(ancestor.to_path_buf()); } }