forked from extern/nushell
make cd command complete only folders (#2431)
This commit is contained in:
parent
11ea5e61fc
commit
ee71a35786
@ -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<Suggestion>) -> Vec<Suggestion> {
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user