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