Skip to content

Commit 881c349

Browse files
authored
Exclude deprecated commands from completions (nushell#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)
1 parent 406b606 commit 881c349

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

crates/nu-cli/src/completions/command_completions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl CommandCompletion {
8989
let filter_predicate = |command: &[u8]| match_algorithm.matches_u8(command, partial);
9090

9191
let mut results = working_set
92-
.find_commands_by_predicate(filter_predicate)
92+
.find_commands_by_predicate(filter_predicate, true)
9393
.into_iter()
9494
.map(move |x| Suggestion {
9595
value: String::from_utf8_lossy(&x.0).to_string(),

crates/nu-protocol/src/engine/engine_state.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
ast::Block, BlockId, Config, DeclId, Example, FileId, Module, ModuleId, OverlayId, ShellError,
77
Signature, Span, Type, VarId, Variable, VirtualPathId,
88
};
9-
use crate::{ParseError, Value};
9+
use crate::{Category, ParseError, Value};
1010
use core::panic;
1111
use std::borrow::Borrow;
1212
use std::collections::{HashMap, HashSet};
@@ -708,13 +708,17 @@ impl EngineState {
708708
pub fn find_commands_by_predicate(
709709
&self,
710710
predicate: impl Fn(&[u8]) -> bool,
711+
ignore_deprecated: bool,
711712
) -> Vec<(Vec<u8>, Option<String>)> {
712713
let mut output = vec![];
713714

714715
for overlay_frame in self.active_overlays(&[]).rev() {
715716
for decl in &overlay_frame.decls {
716717
if overlay_frame.visibility.is_decl_id_visible(decl.1) && predicate(&decl.0 .0) {
717718
let command = self.get_decl(*decl.1);
719+
if ignore_deprecated && command.signature().category == Category::Deprecated {
720+
continue;
721+
}
718722
output.push((decl.0 .0.clone(), Some(command.usage().to_string())));
719723
}
720724
}
@@ -1755,6 +1759,7 @@ impl<'a> StateWorkingSet<'a> {
17551759
pub fn find_commands_by_predicate(
17561760
&self,
17571761
predicate: impl Fn(&[u8]) -> bool,
1762+
ignore_deprecated: bool,
17581763
) -> Vec<(Vec<u8>, Option<String>)> {
17591764
let mut output = vec![];
17601765

@@ -1766,13 +1771,19 @@ impl<'a> StateWorkingSet<'a> {
17661771
if overlay_frame.visibility.is_decl_id_visible(decl.1) && predicate(&decl.0 .0)
17671772
{
17681773
let command = self.get_decl(*decl.1);
1774+
if ignore_deprecated && command.signature().category == Category::Deprecated
1775+
{
1776+
continue;
1777+
}
17691778
output.push((decl.0 .0.clone(), Some(command.usage().to_string())));
17701779
}
17711780
}
17721781
}
17731782
}
17741783

1775-
let mut permanent = self.permanent_state.find_commands_by_predicate(predicate);
1784+
let mut permanent = self
1785+
.permanent_state
1786+
.find_commands_by_predicate(predicate, ignore_deprecated);
17761787

17771788
output.append(&mut permanent);
17781789

0 commit comments

Comments
 (0)