Fix path completions for cd command. (#2525)

Previously, we weren't expanding `~`, so `std::fs::metadata` was failing. We now
make use of `PathSuggestion` to get the actual path, as represented by a
`PathBuf`.
This commit is contained in:
Jason Gedge 2020-09-09 18:32:20 -04:00 committed by GitHub
parent a63a5adafa
commit 73e65df5f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 14 deletions

View file

@ -70,11 +70,4 @@ impl Completer {
Vec::new()
}
}
pub fn complete(&self, ctx: &Context<'_>, partial: &str) -> Vec<Suggestion> {
self.path_suggestions(ctx, partial)
.into_iter()
.map(|v| v.suggestion)
.collect()
}
}

View file

@ -1,6 +1,6 @@
use crate::completion::path::PathSuggestion;
use crate::completion::{self, Suggestion};
use crate::context;
use std::fs::metadata;
pub(crate) struct NuCompleter {}
@ -71,15 +71,15 @@ impl NuCompleter {
partial
};
let completed_paths = path_completer.complete(context, partial);
let completed_paths = path_completer.path_suggestions(context, partial);
match cmd.as_deref().unwrap_or("") {
"cd" => select_directory_suggestions(completed_paths),
_ => completed_paths,
}
.into_iter()
.map(|suggestion| Suggestion {
replacement: requote(suggestion.replacement),
display: suggestion.display,
.map(|s| Suggestion {
replacement: requote(s.suggestion.replacement),
display: s.suggestion.display,
})
.collect()
}
@ -94,11 +94,13 @@ impl NuCompleter {
}
}
fn select_directory_suggestions(completed_paths: Vec<Suggestion>) -> Vec<Suggestion> {
fn select_directory_suggestions(completed_paths: Vec<PathSuggestion>) -> Vec<PathSuggestion> {
completed_paths
.into_iter()
.filter(|suggestion| {
metadata(&suggestion.replacement)
suggestion
.path
.metadata()
.map(|md| md.is_dir())
.unwrap_or(false)
})