diff --git a/crates/nu-cli/src/completions/completion_options.rs b/crates/nu-cli/src/completions/completion_options.rs index a414aafedf..8a6ccf0a63 100644 --- a/crates/nu-cli/src/completions/completion_options.rs +++ b/crates/nu-cli/src/completions/completion_options.rs @@ -24,6 +24,12 @@ pub enum MatchAlgorithm { /// Example: /// "git checkout" is matched by "gco" Fuzzy, + + /// Only show suggestions which contain the substring starting at any place + /// + /// Example: + /// "git checkout" is matched by "check" + Substring } impl MatchAlgorithm { @@ -37,6 +43,7 @@ impl MatchAlgorithm { let matcher = SkimMatcherV2::default(); matcher.fuzzy_match(haystack, needle).is_some() } + MatchAlgorithm::Substring => haystack.contains(needle), } } @@ -51,6 +58,7 @@ impl MatchAlgorithm { let matcher = SkimMatcherV2::default(); matcher.fuzzy_match(&haystack_str, &needle_str).is_some() } + MatchAlgorithm::Substring => haystack.windows(needle.len()).any(|window| window == needle), } } } @@ -60,6 +68,7 @@ impl From for MatchAlgorithm { match value { CompletionAlgorithm::Prefix => MatchAlgorithm::Prefix, CompletionAlgorithm::Fuzzy => MatchAlgorithm::Fuzzy, + CompletionAlgorithm::Substring => MatchAlgorithm::Substring, } } } @@ -71,6 +80,7 @@ impl TryFrom for MatchAlgorithm { match value.as_str() { "prefix" => Ok(Self::Prefix), "fuzzy" => Ok(Self::Fuzzy), + "substring" => Ok(Self::Substring), _ => Err(InvalidMatchAlgorithm::Unknown), } } diff --git a/crates/nu-cli/src/completions/custom_completions.rs b/crates/nu-cli/src/completions/custom_completions.rs index 0d2c674ef9..db38e9f6d8 100644 --- a/crates/nu-cli/src/completions/custom_completions.rs +++ b/crates/nu-cli/src/completions/custom_completions.rs @@ -157,7 +157,7 @@ fn filter( } } }, - MatchAlgorithm::Fuzzy => options + MatchAlgorithm::Fuzzy | MatchAlgorithm::Substring => options .match_algorithm .matches_u8(it.suggestion.value.as_bytes(), prefix), }) diff --git a/crates/nu-protocol/src/config/completer.rs b/crates/nu-protocol/src/config/completer.rs index 67bde52e27..db865f2ff8 100644 --- a/crates/nu-protocol/src/config/completer.rs +++ b/crates/nu-protocol/src/config/completer.rs @@ -11,6 +11,7 @@ pub enum CompletionAlgorithm { #[default] Prefix, Fuzzy, + Substring, } impl FromStr for CompletionAlgorithm { @@ -20,6 +21,7 @@ impl FromStr for CompletionAlgorithm { match s.to_ascii_lowercase().as_str() { "prefix" => Ok(Self::Prefix), "fuzzy" => Ok(Self::Fuzzy), + "substring" => Ok(Self::Substring), _ => Err("expected either 'prefix' or 'fuzzy'"), } } @@ -30,6 +32,7 @@ impl ReconstructVal for CompletionAlgorithm { let str = match self { CompletionAlgorithm::Prefix => "prefix", CompletionAlgorithm::Fuzzy => "fuzzy", + CompletionAlgorithm::Substring => "substring", }; Value::string(str, span) } diff --git a/crates/nu-utils/src/sample_config/default_config.nu b/crates/nu-utils/src/sample_config/default_config.nu index 0c78cf9ca1..e328447d1a 100644 --- a/crates/nu-utils/src/sample_config/default_config.nu +++ b/crates/nu-utils/src/sample_config/default_config.nu @@ -205,7 +205,7 @@ $env.config = { case_sensitive: false # set to true to enable case-sensitive completions quick: true # set this to false to prevent auto-selecting completions when only one remains partial: true # set this to false to prevent partial filling of the prompt - algorithm: "prefix" # prefix or fuzzy + algorithm: "prefix" # prefix or fuzzy or substring external: { enable: true # set to false to prevent nushell looking into $env.PATH to find more suggestions, `false` recommended for WSL users as this look up may be very slow max_results: 100 # setting it lower can improve completion performance at the cost of omitting some options