From de2b752771f5d1d5672983a00a3676a246196295 Mon Sep 17 00:00:00 2001 From: Yash Thakur <45539777+ysthakur@users.noreply.github.com> Date: Fri, 5 Jul 2024 18:58:35 -0400 Subject: [PATCH] Fix variable completion sort order (#13306) # Description My last PR (https://github.com/nushell/nushell/pull/13242) made it so that the last branch in the variable completer doesn't sort suggestions. Sorry about that. This should fix it. # User-Facing Changes Variables will now be sorted properly. # Tests + Formatting Added one test case to verify this won't happen again. # After Submitting --- crates/nu-cli/src/completions/variable_completions.rs | 2 ++ crates/nu-cli/tests/completions/mod.rs | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/nu-cli/src/completions/variable_completions.rs b/crates/nu-cli/src/completions/variable_completions.rs index ab04da13ea..72a69e942c 100644 --- a/crates/nu-cli/src/completions/variable_completions.rs +++ b/crates/nu-cli/src/completions/variable_completions.rs @@ -229,6 +229,8 @@ impl Completer for VariableCompletion { } } + output = sort_suggestions(&prefix_str, output, SortBy::Ascending); + output.dedup(); // TODO: Removes only consecutive duplicates, is it intended? output diff --git a/crates/nu-cli/tests/completions/mod.rs b/crates/nu-cli/tests/completions/mod.rs index 03f37925f5..a7f88dc3b0 100644 --- a/crates/nu-cli/tests/completions/mod.rs +++ b/crates/nu-cli/tests/completions/mod.rs @@ -107,7 +107,7 @@ fn subcommand_completer() -> NuCompleter { } #[test] -fn variables_dollar_sign_with_varialblecompletion() { +fn variables_dollar_sign_with_variablecompletion() { let (_, _, engine, stack) = new_engine(); let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); @@ -907,6 +907,11 @@ fn variables_completions() { // Match results match_suggestions(expected, suggestions); + + let suggestions = completer.complete("$", 1); + let expected: Vec = vec!["$actor".into(), "$env".into(), "$in".into(), "$nu".into()]; + + match_suggestions(expected, suggestions); } #[test]