mirror of
https://github.com/nushell/nushell.git
synced 2025-08-03 12:07:36 +02:00
To avoid resuggesting the same completion, add a space after commands or flags that have been accepted via `Enter`. Don't do that for filepaths or external completions * Add append_whitespace choice for suggestion Signed-off-by: gipsyh <gipsyh.icu@gmail.com> * Fixed `test <path>` appending space. * Update reedline Co-authored-by: sholderbach <sholderbach@users.noreply.github.com>
125 lines
4.1 KiB
Rust
125 lines
4.1 KiB
Rust
use crate::completions::{
|
|
file_path_completion, partial_from, Completer, CompletionOptions, SortBy,
|
|
};
|
|
use nu_protocol::{
|
|
engine::{EngineState, StateWorkingSet},
|
|
Span,
|
|
};
|
|
use reedline::Suggestion;
|
|
use std::sync::Arc;
|
|
const SEP: char = std::path::MAIN_SEPARATOR;
|
|
|
|
#[derive(Clone)]
|
|
pub struct DotNuCompletion {
|
|
engine_state: Arc<EngineState>,
|
|
}
|
|
|
|
impl DotNuCompletion {
|
|
pub fn new(engine_state: Arc<EngineState>) -> Self {
|
|
Self { engine_state }
|
|
}
|
|
}
|
|
|
|
impl Completer for DotNuCompletion {
|
|
fn fetch(
|
|
&mut self,
|
|
_: &StateWorkingSet,
|
|
prefix: Vec<u8>,
|
|
span: Span,
|
|
offset: usize,
|
|
_: usize,
|
|
options: &CompletionOptions,
|
|
) -> Vec<Suggestion> {
|
|
let prefix_str = String::from_utf8_lossy(&prefix).to_string();
|
|
let mut search_dirs: Vec<String> = vec![];
|
|
let (base_dir, mut partial) = partial_from(&prefix_str);
|
|
let mut is_current_folder = false;
|
|
|
|
// Fetch the lib dirs
|
|
let lib_dirs: Vec<String> =
|
|
if let Some(lib_dirs) = self.engine_state.env_vars.get("NU_LIB_DIRS") {
|
|
lib_dirs
|
|
.as_list()
|
|
.into_iter()
|
|
.flat_map(|it| {
|
|
it.iter().map(|x| {
|
|
x.as_path()
|
|
.expect("internal error: failed to convert lib path")
|
|
})
|
|
})
|
|
.map(|it| {
|
|
it.into_os_string()
|
|
.into_string()
|
|
.expect("internal error: failed to convert OS path")
|
|
})
|
|
.collect()
|
|
} else {
|
|
vec![]
|
|
};
|
|
|
|
// Check if the base_dir is a folder
|
|
if base_dir != "./" {
|
|
// Add the base dir into the directories to be searched
|
|
search_dirs.push(base_dir.clone());
|
|
|
|
// Reset the partial adding the basic dir back
|
|
// in order to make the span replace work properly
|
|
let mut base_dir_partial = base_dir;
|
|
base_dir_partial.push_str(&partial);
|
|
|
|
partial = base_dir_partial;
|
|
} else {
|
|
// Fetch the current folder
|
|
let current_folder = if let Some(d) = self.engine_state.env_vars.get("PWD") {
|
|
match d.as_string() {
|
|
Ok(s) => s,
|
|
Err(_) => "".to_string(),
|
|
}
|
|
} else {
|
|
"".to_string()
|
|
};
|
|
is_current_folder = true;
|
|
|
|
// Add the current folder and the lib dirs into the
|
|
// directories to be searched
|
|
search_dirs.push(current_folder);
|
|
search_dirs.extend(lib_dirs);
|
|
}
|
|
|
|
// Fetch the files filtering the ones that ends with .nu
|
|
// and transform them into suggestions
|
|
let output: Vec<Suggestion> = search_dirs
|
|
.into_iter()
|
|
.flat_map(|it| {
|
|
file_path_completion(span, &partial, &it, options.match_algorithm)
|
|
.into_iter()
|
|
.filter(|it| {
|
|
// Different base dir, so we list the .nu files or folders
|
|
if !is_current_folder {
|
|
it.1.ends_with(".nu") || it.1.ends_with(SEP)
|
|
} else {
|
|
// Lib dirs, so we filter only the .nu files
|
|
it.1.ends_with(".nu")
|
|
}
|
|
})
|
|
.map(move |x| Suggestion {
|
|
value: x.1,
|
|
description: None,
|
|
extra: None,
|
|
span: reedline::Span {
|
|
start: x.0.start - offset,
|
|
end: x.0.end - offset,
|
|
},
|
|
append_whitespace: true,
|
|
})
|
|
})
|
|
.collect();
|
|
|
|
output
|
|
}
|
|
|
|
fn get_sort_by(&self) -> SortBy {
|
|
SortBy::LevenshteinDistance
|
|
}
|
|
}
|