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

View File

@ -6,8 +6,13 @@ const SEP: char = std::path::MAIN_SEPARATOR;
pub struct Completer;
pub struct PathSuggestion {
pub(crate) path: PathBuf,
pub(crate) suggestion: Suggestion,
}
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 = expanded.as_ref();
@ -43,9 +48,12 @@ impl Completer {
file_name.push(std::path::MAIN_SEPARATOR);
}
Some(Suggestion {
replacement: path,
display: file_name,
Some(PathSuggestion {
path: entry.path(),
suggestion: Suggestion {
replacement: path,
display: file_name,
},
})
} else {
None
@ -57,4 +65,11 @@ 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()
}
}