From 5fe97b8d590ea84343bc0ff5d01631e16b4b5c7f Mon Sep 17 00:00:00 2001 From: zc he Date: Sat, 29 Mar 2025 06:00:05 +0800 Subject: [PATCH] fix(completion): completions.external.enable config option not respected (#15443) Fixes #15441 # Description Actually I made a small change to the original behavior: ``` ^foo ``` will still show external commands, regardless of whether it's enabled or not. I think that's the only thing people want to see when they press tab with a `^` prefix. # User-Facing Changes # Tests + Formatting +1 # After Submitting Should I document that minor behavior change somewhere in GitHub.io? --------- Co-authored-by: Yash Thakur <45539777+ysthakur@users.noreply.github.com> --- crates/nu-cli/src/completions/completer.rs | 3 ++- crates/nu-cli/tests/completions/mod.rs | 29 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/crates/nu-cli/src/completions/completer.rs b/crates/nu-cli/src/completions/completer.rs index ec0c10effe..a7a00306b0 100644 --- a/crates/nu-cli/src/completions/completer.rs +++ b/crates/nu-cli/src/completions/completer.rs @@ -486,9 +486,10 @@ impl NuCompleter { externals: bool, strip: bool, ) -> Vec { + let config = self.engine_state.get_config(); let mut command_completions = CommandCompletion { internals, - externals, + externals: !internals || (externals && config.completions.external.enable), }; let (new_span, prefix) = strip_placeholder_if_any(working_set, &span, strip); let ctx = Context::new(working_set, new_span, prefix, offset); diff --git a/crates/nu-cli/tests/completions/mod.rs b/crates/nu-cli/tests/completions/mod.rs index 4e1e5b0fdb..10a207b88c 100644 --- a/crates/nu-cli/tests/completions/mod.rs +++ b/crates/nu-cli/tests/completions/mod.rs @@ -10,7 +10,7 @@ use nu_cli::NuCompleter; use nu_engine::eval_block; use nu_parser::parse; use nu_path::expand_tilde; -use nu_protocol::{debugger::WithoutDebug, engine::StateWorkingSet, PipelineData}; +use nu_protocol::{debugger::WithoutDebug, engine::StateWorkingSet, Config, PipelineData}; use reedline::{Completer, Suggestion}; use rstest::{fixture, rstest}; use support::{ @@ -367,7 +367,7 @@ export def say [ /// External command only if starts with `^` #[test] -fn external_commands_only() { +fn external_commands() { let engine = new_external_engine(); let mut completer = NuCompleter::new( Arc::new(engine), @@ -390,6 +390,31 @@ fn external_commands_only() { match_suggestions(&expected, &suggestions); } +/// Disable external commands except for those start with `^` +#[test] +fn external_commands_disabled() { + let mut engine = new_external_engine(); + + let mut config = Config::default(); + config.completions.external.enable = false; + engine.set_config(config); + + let stack = nu_protocol::engine::Stack::new(); + let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); + let completion_str = "ls; ^sleep"; + let suggestions = completer.complete(completion_str, completion_str.len()); + #[cfg(windows)] + let expected: Vec<_> = vec!["sleep.exe"]; + #[cfg(not(windows))] + let expected: Vec<_> = vec!["sleep"]; + match_suggestions(&expected, &suggestions); + + let completion_str = "sleep"; + let suggestions = completer.complete(completion_str, completion_str.len()); + let expected: Vec<_> = vec!["sleep"]; + match_suggestions(&expected, &suggestions); +} + /// Which completes both internals and externals #[test] fn which_command_completions() {