From ead6fbdf9c8bf8e9818e063294e507e5e22d689f Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Thu, 22 Dec 2022 22:50:59 +0800 Subject: [PATCH] let case_insensitive option work for variable completion as well (#7539) # Description Fixes #7529. # User-Facing Changes _(List of all changes that impact the user experience here. This helps us keep track of breaking changes.)_ # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --- .../src/completions/variable_completions.rs | 69 +++++++++++++------ 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/crates/nu-cli/src/completions/variable_completions.rs b/crates/nu-cli/src/completions/variable_completions.rs index d10f551572..0c46154a29 100644 --- a/crates/nu-cli/src/completions/variable_completions.rs +++ b/crates/nu-cli/src/completions/variable_completions.rs @@ -9,6 +9,8 @@ use reedline::Suggestion; use std::str; use std::sync::Arc; +use super::MatchAlgorithm; + #[derive(Clone)] pub struct VariableCompletion { engine_state: Arc, // TODO: Is engine state necessary? It's already a part of working set in fetch() @@ -73,10 +75,11 @@ impl Completer for VariableCompletion { for suggestion in nested_suggestions(val.clone(), nested_levels, current_span) { - if options - .match_algorithm - .matches_u8(suggestion.value.as_bytes(), &prefix) - { + if options.match_algorithm.matches_u8_insensitive( + options.case_sensitive, + suggestion.value.as_bytes(), + &prefix, + ) { output.push(suggestion); } } @@ -86,10 +89,11 @@ impl Completer for VariableCompletion { } else { // No nesting provided, return all env vars for env_var in env_vars { - if options - .match_algorithm - .matches_u8(env_var.0.as_bytes(), &prefix) - { + if options.match_algorithm.matches_u8_insensitive( + options.case_sensitive, + env_var.0.as_bytes(), + &prefix, + ) { output.push(Suggestion { value: env_var.0, description: None, @@ -116,10 +120,11 @@ impl Completer for VariableCompletion { for suggestion in nested_suggestions(nuval, self.var_context.1.clone(), current_span) { - if options - .match_algorithm - .matches_u8(suggestion.value.as_bytes(), &prefix) - { + if options.match_algorithm.matches_u8_insensitive( + options.case_sensitive, + suggestion.value.as_bytes(), + &prefix, + ) { output.push(suggestion); } } @@ -138,10 +143,11 @@ impl Completer for VariableCompletion { for suggestion in nested_suggestions(value, self.var_context.1.clone(), current_span) { - if options - .match_algorithm - .matches_u8(suggestion.value.as_bytes(), &prefix) - { + if options.match_algorithm.matches_u8_insensitive( + options.case_sensitive, + suggestion.value.as_bytes(), + &prefix, + ) { output.push(suggestion); } } @@ -153,10 +159,11 @@ impl Completer for VariableCompletion { // Variable completion (e.g: $en to complete $env) for builtin in builtins { - if options - .match_algorithm - .matches_u8(builtin.as_bytes(), &prefix) - { + if options.match_algorithm.matches_u8_insensitive( + options.case_sensitive, + builtin.as_bytes(), + &prefix, + ) { output.push(Suggestion { value: builtin.to_string(), description: None, @@ -178,7 +185,11 @@ impl Completer for VariableCompletion { .rev() { for v in &overlay_frame.vars { - if options.match_algorithm.matches_u8(v.0, &prefix) { + if options.match_algorithm.matches_u8_insensitive( + options.case_sensitive, + v.0, + &prefix, + ) { output.push(Suggestion { value: String::from_utf8_lossy(v.0).to_string(), description: None, @@ -200,7 +211,11 @@ impl Completer for VariableCompletion { .rev() { for v in &overlay_frame.vars { - if options.match_algorithm.matches_u8(v.0, &prefix) { + if options.match_algorithm.matches_u8_insensitive( + options.case_sensitive, + v.0, + &prefix, + ) { output.push(Suggestion { value: String::from_utf8_lossy(v.0).to_string(), description: None, @@ -281,3 +296,13 @@ fn recursive_value(val: Value, sublevels: Vec>) -> Value { val } + +impl MatchAlgorithm { + pub fn matches_u8_insensitive(&self, sensitive: bool, haystack: &[u8], needle: &[u8]) -> bool { + if sensitive { + self.matches_u8(haystack, needle) + } else { + self.matches_u8(&haystack.to_ascii_lowercase(), &needle.to_ascii_lowercase()) + } + } +}