diff --git a/crates/nu-cli/src/shell/completer.rs b/crates/nu-cli/src/shell/completer.rs index 1c5ce81666..c45157fb02 100644 --- a/crates/nu-cli/src/shell/completer.rs +++ b/crates/nu-cli/src/shell/completer.rs @@ -1,5 +1,6 @@ use crate::completion::{self, Suggestion}; use crate::context; +use std::fs::metadata; pub(crate) struct NuCompleter {} @@ -47,7 +48,12 @@ impl NuCompleter { LocationType::Argument(_cmd, _arg_name) => { // TODO use cmd and arg_name to narrow things down further let path_completer = crate::completion::path::Completer::new(); - path_completer.complete(context, partial) + let completed_paths = path_completer.complete(context, partial); + if &line[..2] == "cd" { + autocomplete_only_folders(completed_paths) + } else { + completed_paths + } } LocationType::Variable => Vec::new(), @@ -62,6 +68,18 @@ impl NuCompleter { } } +fn autocomplete_only_folders(completed_paths: Vec) -> Vec { + let mut result = Vec::new(); + for path in completed_paths { + let filepath = path.replacement.clone(); + let md = metadata(filepath).expect("Expect filepath"); + if md.is_dir() { + result.push(path); + } + } + result +} + fn requote(item: Suggestion) -> Suggestion { let unescaped = rustyline::completion::unescape(&item.replacement, Some('\\')); if unescaped != item.replacement {