Made a change to completion resolution order (#5440)

* Made a change to completion resolution order

* Potential fix for completion (remove file paths from command completer)

* Updating formatting

* Removed commented out code for readability

* Fixed compile error on merge
This commit is contained in:
Robert O'Shea 2022-05-13 14:15:24 +01:00 committed by GitHub
parent 852de79212
commit 1aec4a343a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 84 deletions

View File

@ -1,7 +1,5 @@
use crate::completions::{
file_completions::file_path_completion, Completer, CompletionOptions, MatchAlgorithm, SortBy,
};
use nu_parser::{unescape_unquote_string, FlatShape};
use crate::completions::{Completer, CompletionOptions, MatchAlgorithm, SortBy};
use nu_parser::FlatShape;
use nu_protocol::{
engine::{EngineState, StateWorkingSet},
Span,
@ -12,7 +10,6 @@ use std::sync::Arc;
pub struct CommandCompletion {
engine_state: Arc<EngineState>,
flattened: Vec<(Span, FlatShape)>,
flat_idx: usize,
flat_shape: FlatShape,
}
@ -21,13 +18,11 @@ impl CommandCompletion {
engine_state: Arc<EngineState>,
_: &StateWorkingSet,
flattened: Vec<(Span, FlatShape)>,
flat_idx: usize,
flat_shape: FlatShape,
) -> Self {
Self {
engine_state,
flattened,
flat_idx,
flat_shape,
}
}
@ -161,7 +156,7 @@ impl Completer for CommandCompletion {
fn fetch(
&mut self,
working_set: &StateWorkingSet,
prefix: Vec<u8>,
_prefix: Vec<u8>,
span: Span,
offset: usize,
pos: usize,
@ -214,66 +209,8 @@ impl Completer for CommandCompletion {
vec![]
};
let cwd = if let Some(d) = self.engine_state.get_env_var("PWD") {
match d.as_string() {
Ok(s) => s,
Err(_) => "".to_string(),
}
} else {
"".to_string()
};
let preceding_byte = if span.start > offset {
working_set
.get_span_contents(Span {
start: span.start - 1,
end: span.start,
})
.to_vec()
} else {
vec![]
};
// let prefix = working_set.get_span_contents(flat.0);
let prefix = String::from_utf8_lossy(&prefix).to_string();
file_path_completion(span, &prefix, &cwd, options)
subcommands
.into_iter()
.map(move |x| {
if self.flat_idx == 0 {
// We're in the command position
if (x.1.starts_with('"') || x.1.starts_with('\'') || x.1.starts_with('`'))
&& !matches!(preceding_byte.get(0), Some(b'^'))
{
let (trimmed, _) = unescape_unquote_string(x.1.as_bytes(), span);
let expanded = nu_path::canonicalize_with(trimmed, &cwd);
if let Ok(expanded) = expanded {
if is_executable::is_executable(expanded) {
(x.0, format!("^{}", x.1))
} else {
(x.0, x.1)
}
} else {
(x.0, x.1)
}
} else {
(x.0, x.1)
}
} else {
(x.0, x.1)
}
})
.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: false,
})
.chain(subcommands.into_iter())
.chain(commands.into_iter())
.collect::<Vec<_>>()
}

View File

@ -176,37 +176,39 @@ impl NuCompleter {
pos,
);
}
FlatShape::Filepath
| FlatShape::GlobPattern
| FlatShape::ExternalArg => {
let mut completer = FileCompletion::new(self.engine_state.clone());
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
offset,
pos,
);
}
flat_shape => {
let mut completer = CommandCompletion::new(
self.engine_state.clone(),
&working_set,
flattened.clone(),
flat_idx,
// flat_idx,
flat_shape.clone(),
);
return self.process_completion(
let out: Vec<_> = self.process_completion(
&mut completer,
&working_set,
prefix,
prefix.clone(),
new_span,
offset,
pos,
);
if out.is_empty() {
let mut completer =
FileCompletion::new(self.engine_state.clone());
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
offset,
pos,
);
}
return out;
}
};
}