mirror of
https://github.com/nushell/nushell
synced 2025-01-01 15:58:55 +00:00
bb27b9f371
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>
86 lines
2.6 KiB
Rust
86 lines
2.6 KiB
Rust
use crate::completions::{Completer, CompletionOptions};
|
|
use nu_protocol::{
|
|
ast::{Expr, Expression},
|
|
engine::StateWorkingSet,
|
|
Span,
|
|
};
|
|
|
|
use reedline::Suggestion;
|
|
|
|
#[derive(Clone)]
|
|
pub struct FlagCompletion {
|
|
expression: Expression,
|
|
}
|
|
|
|
impl FlagCompletion {
|
|
pub fn new(expression: Expression) -> Self {
|
|
Self { expression }
|
|
}
|
|
}
|
|
|
|
impl Completer for FlagCompletion {
|
|
fn fetch(
|
|
&mut self,
|
|
working_set: &StateWorkingSet,
|
|
prefix: Vec<u8>,
|
|
span: Span,
|
|
offset: usize,
|
|
_: usize,
|
|
options: &CompletionOptions,
|
|
) -> Vec<Suggestion> {
|
|
// Check if it's a flag
|
|
if let Expr::Call(call) = &self.expression.expr {
|
|
let decl = working_set.get_decl(call.decl_id);
|
|
let sig = decl.signature();
|
|
|
|
let mut output = vec![];
|
|
|
|
for named in &sig.named {
|
|
let flag_desc = &named.desc;
|
|
if let Some(short) = named.short {
|
|
let mut named = vec![0; short.len_utf8()];
|
|
short.encode_utf8(&mut named);
|
|
named.insert(0, b'-');
|
|
|
|
if options.match_algorithm.matches_u8(&named, &prefix) {
|
|
output.push(Suggestion {
|
|
value: String::from_utf8_lossy(&named).to_string(),
|
|
description: Some(flag_desc.to_string()),
|
|
extra: None,
|
|
span: reedline::Span {
|
|
start: span.start - offset,
|
|
end: span.end - offset,
|
|
},
|
|
append_whitespace: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
if named.long.is_empty() {
|
|
continue;
|
|
}
|
|
|
|
let mut named = named.long.as_bytes().to_vec();
|
|
named.insert(0, b'-');
|
|
named.insert(0, b'-');
|
|
|
|
if options.match_algorithm.matches_u8(&named, &prefix) {
|
|
output.push(Suggestion {
|
|
value: String::from_utf8_lossy(&named).to_string(),
|
|
description: Some(flag_desc.to_string()),
|
|
extra: None,
|
|
span: reedline::Span {
|
|
start: span.start - offset,
|
|
end: span.end - offset,
|
|
},
|
|
append_whitespace: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
vec![]
|
|
}
|
|
}
|