make cd command complete only folders (#2431)

This commit is contained in:
gorogoroumaru 2020-08-29 03:38:30 +09:00 committed by GitHub
parent 11ea5e61fc
commit ee71a35786
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 {