From 7be90c2644ad344500daf3c0a4cf34572c86dfe1 Mon Sep 17 00:00:00 2001 From: vansh284 <65393463+vansh284@users.noreply.github.com> Date: Mon, 31 Mar 2025 01:56:11 +0200 Subject: [PATCH] Improve completions for exact matches (Issue #14794) (#15387) Fixes #14794. # Description # User-Facing Changes Makes it so that (even if) the command ends in a slash, exact matches are still preferred over partial matches. For example, `foo/bar/as` -> `foo/bar/asdf` but not `foo/bars/asdf`. # Tests + Formatting # After Submitting --------- Co-authored-by: Yash Thakur <45539777+ysthakur@users.noreply.github.com> --- crates/nu-cli/src/completions/completion_common.rs | 2 +- crates/nu-cli/tests/completions/mod.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/nu-cli/src/completions/completion_common.rs b/crates/nu-cli/src/completions/completion_common.rs index c40a57527b..3b9313ad37 100644 --- a/crates/nu-cli/src/completions/completion_common.rs +++ b/crates/nu-cli/src/completions/completion_common.rs @@ -98,7 +98,7 @@ fn complete_rec( } // For https://github.com/nushell/nushell/issues/13204 - if isdir && options.match_algorithm == MatchAlgorithm::Prefix { + if options.match_algorithm == MatchAlgorithm::Prefix { let exact_match = if options.case_sensitive { entry_name.eq(base) } else { diff --git a/crates/nu-cli/tests/completions/mod.rs b/crates/nu-cli/tests/completions/mod.rs index 10a207b88c..efd561f3aa 100644 --- a/crates/nu-cli/tests/completions/mod.rs +++ b/crates/nu-cli/tests/completions/mod.rs @@ -2224,6 +2224,16 @@ fn exact_match() { &vec![file(dir.join("partial").join("hello.txt")).as_str()], &suggestions, ); + + let target_dir = format!("open {}", file(dir.join("partial").join("hello"))); // No trailing slash + let suggestions = completer.complete(&target_dir, target_dir.len()); + + // Despite no trailing slash, since it's an exact match, only 'partial/hello.txt' should be suggested, not + // 'partial-a/hello' and stuff. See issue #14794 for details. + match_suggestions( + &vec![file(dir.join("partial").join("hello.txt")).as_str()], + &suggestions, + ); } #[ignore = "was reverted, still needs fixing"]