mirror of
https://github.com/nushell/nushell.git
synced 2024-11-23 00:43:33 +01:00
Provide path as part of the suggestion from the path completer. (#2497)
This commit is contained in:
parent
16f85f32a2
commit
56f85b3108
@ -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
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user