Early return on subcommands (#4443)

* Early return on subcommands

* More streamlining
This commit is contained in:
JT 2022-02-12 11:39:38 -05:00 committed by GitHub
parent 9829e449e3
commit eceb2d5106
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -118,6 +118,7 @@ impl NuCompleter {
working_set: &StateWorkingSet, working_set: &StateWorkingSet,
span: Span, span: Span,
offset: usize, offset: usize,
find_externals: bool,
) -> Vec<(reedline::Span, String)> { ) -> Vec<(reedline::Span, String)> {
let prefix = working_set.get_span_contents(span); let prefix = working_set.get_span_contents(span);
@ -137,6 +138,7 @@ impl NuCompleter {
let prefix = working_set.get_span_contents(span); let prefix = working_set.get_span_contents(span);
let prefix = String::from_utf8_lossy(prefix).to_string(); let prefix = String::from_utf8_lossy(prefix).to_string();
if find_externals {
let results_external = let results_external =
self.external_command_completion(&prefix) self.external_command_completion(&prefix)
.into_iter() .into_iter()
@ -159,6 +161,9 @@ impl NuCompleter {
} }
results results
} else {
results
}
} }
fn completion_helper(&self, line: &str, pos: usize) -> Vec<(reedline::Span, String)> { fn completion_helper(&self, line: &str, pos: usize) -> Vec<(reedline::Span, String)> {
@ -219,7 +224,7 @@ impl NuCompleter {
} }
match &flat.1 { match &flat.1 {
nu_parser::FlatShape::Custom(custom_completion) => { FlatShape::Custom(custom_completion) => {
//let prefix = working_set.get_span_contents(flat.0).to_vec(); //let prefix = working_set.get_span_contents(flat.0).to_vec();
let (block, ..) = parse( let (block, ..) = parse(
@ -270,21 +275,31 @@ impl NuCompleter {
return v; return v;
} }
flat_shape => { FlatShape::Filepath | FlatShape::GlobPattern => {
let commands = let cwd = if let Some(d) = self.engine_state.env_vars.get("PWD")
if matches!(flat_shape, nu_parser::FlatShape::External)
|| matches!(
flat_shape,
nu_parser::FlatShape::InternalCall
)
|| ((new_span.end - new_span.start) == 0)
{ {
// we're in a gap or at a command match d.as_string() {
self.complete_commands(&working_set, new_span, offset) Ok(s) => s,
Err(_) => "".to_string(),
}
} else { } else {
vec![] "".to_string()
}; };
let prefix = String::from_utf8_lossy(&prefix).to_string();
return file_path_completion(new_span, &prefix, &cwd)
.into_iter()
.map(move |x| {
(
reedline::Span {
start: x.0.start - offset,
end: x.0.end - offset,
},
x.1,
)
})
.collect();
}
flat_shape => {
let last = flattened let last = flattened
.iter() .iter()
.rev() .rev()
@ -310,6 +325,30 @@ impl NuCompleter {
end: pos, end: pos,
}, },
offset, offset,
false,
)
} else {
vec![]
};
if !subcommands.is_empty() {
return subcommands;
}
let commands =
if matches!(flat_shape, nu_parser::FlatShape::External)
|| matches!(
flat_shape,
nu_parser::FlatShape::InternalCall
)
|| ((new_span.end - new_span.start) == 0)
{
// we're in a gap or at a command
self.complete_commands(
&working_set,
new_span,
offset,
true,
) )
} else { } else {
vec![] vec![]
@ -337,7 +376,7 @@ impl NuCompleter {
}; };
// let prefix = working_set.get_span_contents(flat.0); // let prefix = working_set.get_span_contents(flat.0);
let prefix = String::from_utf8_lossy(&prefix).to_string(); let prefix = String::from_utf8_lossy(&prefix).to_string();
let mut output = file_path_completion(new_span, &prefix, &cwd) let output = file_path_completion(new_span, &prefix, &cwd)
.into_iter() .into_iter()
.map(move |x| { .map(move |x| {
if flat_idx == 0 { if flat_idx == 0 {
@ -379,7 +418,7 @@ impl NuCompleter {
.chain(subcommands.into_iter()) .chain(subcommands.into_iter())
.chain(commands.into_iter()) .chain(commands.into_iter())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
output.dedup_by(|a, b| a.1 == b.1); //output.dedup_by(|a, b| a.1 == b.1);
return output; return output;
} }