Exclude deprecated commands from completions (#9612)

# Description
We previously simply searched all commands in the working set. As our
deprecated/removed subcommands are documented by stub commands that
don't do anything apart from providing a message, they were still
included.
With this change we check the `Signature.category` to not be
`Category::Deprecated`.

## Note on performance
Making this change will exercise `Command.signature()` more
frequently! As the rust-implemented commands include their builders here
this probably will cause a number of extra allocations. There is
actually no valid reason why the commands should construct a new
`Signature` for each call to `Command.signature()`.
This will introduce some overhead to generate the completions for
commands.
# User-Facing Changes
Example: `str <TAB>`


![grafik](https://github.com/nushell/nushell/assets/15833959/4d5ec5fe-aa93-45af-aa60-3854a20fcb04)
This commit is contained in:
Stefan Holderbach 2023-07-05 23:13:16 +02:00 committed by GitHub
parent 406b606398
commit 881c3495c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View file

@ -89,7 +89,7 @@ impl CommandCompletion {
let filter_predicate = |command: &[u8]| match_algorithm.matches_u8(command, partial); let filter_predicate = |command: &[u8]| match_algorithm.matches_u8(command, partial);
let mut results = working_set let mut results = working_set
.find_commands_by_predicate(filter_predicate) .find_commands_by_predicate(filter_predicate, true)
.into_iter() .into_iter()
.map(move |x| Suggestion { .map(move |x| Suggestion {
value: String::from_utf8_lossy(&x.0).to_string(), value: String::from_utf8_lossy(&x.0).to_string(),

View file

@ -6,7 +6,7 @@ use crate::{
ast::Block, BlockId, Config, DeclId, Example, FileId, Module, ModuleId, OverlayId, ShellError, ast::Block, BlockId, Config, DeclId, Example, FileId, Module, ModuleId, OverlayId, ShellError,
Signature, Span, Type, VarId, Variable, VirtualPathId, Signature, Span, Type, VarId, Variable, VirtualPathId,
}; };
use crate::{ParseError, Value}; use crate::{Category, ParseError, Value};
use core::panic; use core::panic;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@ -708,6 +708,7 @@ impl EngineState {
pub fn find_commands_by_predicate( pub fn find_commands_by_predicate(
&self, &self,
predicate: impl Fn(&[u8]) -> bool, predicate: impl Fn(&[u8]) -> bool,
ignore_deprecated: bool,
) -> Vec<(Vec<u8>, Option<String>)> { ) -> Vec<(Vec<u8>, Option<String>)> {
let mut output = vec![]; let mut output = vec![];
@ -715,6 +716,9 @@ impl EngineState {
for decl in &overlay_frame.decls { for decl in &overlay_frame.decls {
if overlay_frame.visibility.is_decl_id_visible(decl.1) && predicate(&decl.0 .0) { if overlay_frame.visibility.is_decl_id_visible(decl.1) && predicate(&decl.0 .0) {
let command = self.get_decl(*decl.1); let command = self.get_decl(*decl.1);
if ignore_deprecated && command.signature().category == Category::Deprecated {
continue;
}
output.push((decl.0 .0.clone(), Some(command.usage().to_string()))); output.push((decl.0 .0.clone(), Some(command.usage().to_string())));
} }
} }
@ -1755,6 +1759,7 @@ impl<'a> StateWorkingSet<'a> {
pub fn find_commands_by_predicate( pub fn find_commands_by_predicate(
&self, &self,
predicate: impl Fn(&[u8]) -> bool, predicate: impl Fn(&[u8]) -> bool,
ignore_deprecated: bool,
) -> Vec<(Vec<u8>, Option<String>)> { ) -> Vec<(Vec<u8>, Option<String>)> {
let mut output = vec![]; let mut output = vec![];
@ -1766,13 +1771,19 @@ impl<'a> StateWorkingSet<'a> {
if overlay_frame.visibility.is_decl_id_visible(decl.1) && predicate(&decl.0 .0) if overlay_frame.visibility.is_decl_id_visible(decl.1) && predicate(&decl.0 .0)
{ {
let command = self.get_decl(*decl.1); let command = self.get_decl(*decl.1);
if ignore_deprecated && command.signature().category == Category::Deprecated
{
continue;
}
output.push((decl.0 .0.clone(), Some(command.usage().to_string()))); output.push((decl.0 .0.clone(), Some(command.usage().to_string())));
} }
} }
} }
} }
let mut permanent = self.permanent_state.find_commands_by_predicate(predicate); let mut permanent = self
.permanent_state
.find_commands_by_predicate(predicate, ignore_deprecated);
output.append(&mut permanent); output.append(&mut permanent);