Provide path as part of the suggestion from the path completer. (#2497)

This commit is contained in:
Jason Gedge 2020-09-04 22:10:26 -04:00 committed by GitHub
parent 16f85f32a2
commit 56f85b3108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 26 deletions

View File

@ -1,5 +1,5 @@
use std::iter::FromIterator; use std::iter::FromIterator;
use std::path::{Path, PathBuf}; use std::path::Path;
use indexmap::set::IndexSet; use indexmap::set::IndexSet;
@ -34,28 +34,17 @@ impl Completer {
if partial != "" { if partial != "" {
let path_completer = crate::completion::path::Completer; let path_completer = crate::completion::path::Completer;
let path_results = path_completer.complete(ctx, partial); let path_results = path_completer.path_suggestions(ctx, partial);
suggestions.extend(path_results.into_iter().filter(|suggestion| { let iter = path_results.into_iter().filter_map(|path_suggestion| {
// TODO better path abstractions to avoid a mess like this let path = path_suggestion.path;
let path = { if path.is_dir() || is_executable(&path) {
#[cfg(feature = "directories")] Some(path_suggestion.suggestion)
{ } else {
let home_prefix = format!("~{}", std::path::MAIN_SEPARATOR); None
if let Some(mut home) = dirs::home_dir() { }
home.push(suggestion.replacement.replacen(&home_prefix, "", 1)); });
home
} else {
PathBuf::from(&suggestion.replacement)
}
}
#[cfg(not(feature = "directories"))]
{
PathBuf::from(&suggestion.replacement)
}
};
path.is_dir() || is_executable(&path) suggestions.extend(iter);
}));
} }
suggestions suggestions

View File

@ -6,8 +6,13 @@ const SEP: char = std::path::MAIN_SEPARATOR;
pub struct Completer; pub struct Completer;
pub struct PathSuggestion {
pub(crate) path: PathBuf,
pub(crate) suggestion: Suggestion,
}
impl Completer { impl Completer {
pub fn complete(&self, _ctx: &Context<'_>, partial: &str) -> Vec<Suggestion> { pub fn path_suggestions(&self, _ctx: &Context<'_>, partial: &str) -> Vec<PathSuggestion> {
let expanded = nu_parser::expand_ndots(partial); let expanded = nu_parser::expand_ndots(partial);
let expanded = expanded.as_ref(); let expanded = expanded.as_ref();
@ -43,9 +48,12 @@ impl Completer {
file_name.push(std::path::MAIN_SEPARATOR); file_name.push(std::path::MAIN_SEPARATOR);
} }
Some(Suggestion { Some(PathSuggestion {
replacement: path, path: entry.path(),
display: file_name, suggestion: Suggestion {
replacement: path,
display: file_name,
},
}) })
} else { } else {
None None
@ -57,4 +65,11 @@ 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()
}
} }