diff --git a/crates/nu-cli/src/completion/command.rs b/crates/nu-cli/src/completion/command.rs index 3d062962d8..6562e4de2a 100644 --- a/crates/nu-cli/src/completion/command.rs +++ b/crates/nu-cli/src/completion/command.rs @@ -1,5 +1,5 @@ use std::iter::FromIterator; -use std::path::{Path, PathBuf}; +use std::path::Path; use indexmap::set::IndexSet; @@ -34,28 +34,17 @@ impl Completer { if partial != "" { let path_completer = crate::completion::path::Completer; - let path_results = path_completer.complete(ctx, partial); - suggestions.extend(path_results.into_iter().filter(|suggestion| { - // TODO better path abstractions to avoid a mess like this - let path = { - #[cfg(feature = "directories")] - { - let home_prefix = format!("~{}", std::path::MAIN_SEPARATOR); - if let Some(mut home) = dirs::home_dir() { - home.push(suggestion.replacement.replacen(&home_prefix, "", 1)); - home - } else { - PathBuf::from(&suggestion.replacement) - } - } - #[cfg(not(feature = "directories"))] - { - PathBuf::from(&suggestion.replacement) - } - }; + let path_results = path_completer.path_suggestions(ctx, partial); + let iter = path_results.into_iter().filter_map(|path_suggestion| { + let path = path_suggestion.path; + if path.is_dir() || is_executable(&path) { + Some(path_suggestion.suggestion) + } else { + None + } + }); - path.is_dir() || is_executable(&path) - })); + suggestions.extend(iter); } suggestions diff --git a/crates/nu-cli/src/completion/path.rs b/crates/nu-cli/src/completion/path.rs index 80838f11b7..6b6d878663 100644 --- a/crates/nu-cli/src/completion/path.rs +++ b/crates/nu-cli/src/completion/path.rs @@ -6,8 +6,13 @@ const SEP: char = std::path::MAIN_SEPARATOR; pub struct Completer; +pub struct PathSuggestion { + pub(crate) path: PathBuf, + pub(crate) suggestion: Suggestion, +} + impl Completer { - pub fn complete(&self, _ctx: &Context<'_>, partial: &str) -> Vec { + pub fn path_suggestions(&self, _ctx: &Context<'_>, partial: &str) -> Vec { let expanded = nu_parser::expand_ndots(partial); let expanded = expanded.as_ref(); @@ -43,9 +48,12 @@ impl Completer { file_name.push(std::path::MAIN_SEPARATOR); } - Some(Suggestion { - replacement: path, - display: file_name, + Some(PathSuggestion { + path: entry.path(), + suggestion: Suggestion { + replacement: path, + display: file_name, + }, }) } else { None @@ -57,4 +65,11 @@ impl Completer { Vec::new() } } + + pub fn complete(&self, ctx: &Context<'_>, partial: &str) -> Vec { + self.path_suggestions(ctx, partial) + .into_iter() + .map(|v| v.suggestion) + .collect() + } }