From 881c3495c1cab41b59fea0371ab05c7831d04d45 Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Wed, 5 Jul 2023 23:13:16 +0200 Subject: [PATCH] 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 ` ![grafik](https://github.com/nushell/nushell/assets/15833959/4d5ec5fe-aa93-45af-aa60-3854a20fcb04) --- .../nu-cli/src/completions/command_completions.rs | 2 +- crates/nu-protocol/src/engine/engine_state.rs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/crates/nu-cli/src/completions/command_completions.rs b/crates/nu-cli/src/completions/command_completions.rs index 336946210a..0d56332a63 100644 --- a/crates/nu-cli/src/completions/command_completions.rs +++ b/crates/nu-cli/src/completions/command_completions.rs @@ -89,7 +89,7 @@ impl CommandCompletion { let filter_predicate = |command: &[u8]| match_algorithm.matches_u8(command, partial); let mut results = working_set - .find_commands_by_predicate(filter_predicate) + .find_commands_by_predicate(filter_predicate, true) .into_iter() .map(move |x| Suggestion { value: String::from_utf8_lossy(&x.0).to_string(), diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 4dc5e6c905..5a2ea1ed8a 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -6,7 +6,7 @@ use crate::{ ast::Block, BlockId, Config, DeclId, Example, FileId, Module, ModuleId, OverlayId, ShellError, Signature, Span, Type, VarId, Variable, VirtualPathId, }; -use crate::{ParseError, Value}; +use crate::{Category, ParseError, Value}; use core::panic; use std::borrow::Borrow; use std::collections::{HashMap, HashSet}; @@ -708,6 +708,7 @@ impl EngineState { pub fn find_commands_by_predicate( &self, predicate: impl Fn(&[u8]) -> bool, + ignore_deprecated: bool, ) -> Vec<(Vec, Option)> { let mut output = vec![]; @@ -715,6 +716,9 @@ impl EngineState { for decl in &overlay_frame.decls { if overlay_frame.visibility.is_decl_id_visible(decl.1) && predicate(&decl.0 .0) { 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()))); } } @@ -1755,6 +1759,7 @@ impl<'a> StateWorkingSet<'a> { pub fn find_commands_by_predicate( &self, predicate: impl Fn(&[u8]) -> bool, + ignore_deprecated: bool, ) -> Vec<(Vec, Option)> { 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) { 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()))); } } } } - 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);