Add MatchAlgorithm for completion suggestions (#5244)

* Pass completion options to each fetch() call

* Add MatchAlgorithm to CompletionOptions

* Add unit test for MatchAlgorithm

* Pass completion options to directory completer
This commit is contained in:
Richard
2022-04-23 17:01:19 +02:00
committed by GitHub
parent 667eb27d1b
commit e6a70f9846
12 changed files with 151 additions and 64 deletions

View File

@ -444,12 +444,15 @@ impl EngineState {
None
}
pub fn find_commands_by_prefix(&self, name: &[u8]) -> Vec<(Vec<u8>, Option<String>)> {
pub fn find_commands_by_predicate(
&self,
predicate: impl Fn(&[u8]) -> bool,
) -> Vec<(Vec<u8>, Option<String>)> {
let mut output = vec![];
for scope in self.scope.iter().rev() {
for decl in &scope.decls {
if decl.0.starts_with(name) {
if predicate(decl.0) {
let command = self.get_decl(*decl.1);
output.push((decl.0.clone(), Some(command.usage().to_string())));
}
@ -459,12 +462,12 @@ impl EngineState {
output
}
pub fn find_aliases_by_prefix(&self, name: &[u8]) -> Vec<Vec<u8>> {
pub fn find_aliases_by_predicate(&self, predicate: impl Fn(&[u8]) -> bool) -> Vec<Vec<u8>> {
self.scope
.iter()
.rev()
.flat_map(|scope| &scope.aliases)
.filter(|decl| decl.0.starts_with(name))
.filter(|decl| predicate(decl.0))
.map(|decl| decl.0.clone())
.collect()
}
@ -1315,34 +1318,40 @@ impl<'a> StateWorkingSet<'a> {
}
}
pub fn find_commands_by_prefix(&self, name: &[u8]) -> Vec<(Vec<u8>, Option<String>)> {
pub fn find_commands_by_predicate(
&self,
predicate: impl Fn(&[u8]) -> bool,
) -> Vec<(Vec<u8>, Option<String>)> {
let mut output = vec![];
for scope in self.delta.scope.iter().rev() {
for decl in &scope.decls {
if decl.0.starts_with(name) {
if predicate(decl.0) {
let command = self.get_decl(*decl.1);
output.push((decl.0.clone(), Some(command.usage().to_string())));
}
}
}
let mut permanent = self.permanent_state.find_commands_by_prefix(name);
let mut permanent = self.permanent_state.find_commands_by_predicate(predicate);
output.append(&mut permanent);
output
}
pub fn find_aliases_by_prefix(&self, name: &[u8]) -> Vec<Vec<u8>> {
pub fn find_aliases_by_predicate(
&self,
predicate: impl Fn(&[u8]) -> bool + Copy,
) -> Vec<Vec<u8>> {
self.delta
.scope
.iter()
.rev()
.flat_map(|scope| &scope.aliases)
.filter(|decl| decl.0.starts_with(name))
.filter(|decl| predicate(decl.0))
.map(|decl| decl.0.clone())
.chain(self.permanent_state.find_aliases_by_prefix(name))
.chain(self.permanent_state.find_aliases_by_predicate(predicate))
.collect()
}