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.
This commit is contained in:
pwygab 2022-12-22 22:50:59 +08:00 committed by GitHub
parent 5b616770df
commit ead6fbdf9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,8 @@ use reedline::Suggestion;
use std::str; use std::str;
use std::sync::Arc; use std::sync::Arc;
use super::MatchAlgorithm;
#[derive(Clone)] #[derive(Clone)]
pub struct VariableCompletion { pub struct VariableCompletion {
engine_state: Arc<EngineState>, // TODO: Is engine state necessary? It's already a part of working set in fetch() engine_state: Arc<EngineState>, // 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 for suggestion in
nested_suggestions(val.clone(), nested_levels, current_span) nested_suggestions(val.clone(), nested_levels, current_span)
{ {
if options if options.match_algorithm.matches_u8_insensitive(
.match_algorithm options.case_sensitive,
.matches_u8(suggestion.value.as_bytes(), &prefix) suggestion.value.as_bytes(),
{ &prefix,
) {
output.push(suggestion); output.push(suggestion);
} }
} }
@ -86,10 +89,11 @@ impl Completer for VariableCompletion {
} else { } else {
// No nesting provided, return all env vars // No nesting provided, return all env vars
for env_var in env_vars { for env_var in env_vars {
if options if options.match_algorithm.matches_u8_insensitive(
.match_algorithm options.case_sensitive,
.matches_u8(env_var.0.as_bytes(), &prefix) env_var.0.as_bytes(),
{ &prefix,
) {
output.push(Suggestion { output.push(Suggestion {
value: env_var.0, value: env_var.0,
description: None, description: None,
@ -116,10 +120,11 @@ impl Completer for VariableCompletion {
for suggestion in for suggestion in
nested_suggestions(nuval, self.var_context.1.clone(), current_span) nested_suggestions(nuval, self.var_context.1.clone(), current_span)
{ {
if options if options.match_algorithm.matches_u8_insensitive(
.match_algorithm options.case_sensitive,
.matches_u8(suggestion.value.as_bytes(), &prefix) suggestion.value.as_bytes(),
{ &prefix,
) {
output.push(suggestion); output.push(suggestion);
} }
} }
@ -138,10 +143,11 @@ impl Completer for VariableCompletion {
for suggestion in for suggestion in
nested_suggestions(value, self.var_context.1.clone(), current_span) nested_suggestions(value, self.var_context.1.clone(), current_span)
{ {
if options if options.match_algorithm.matches_u8_insensitive(
.match_algorithm options.case_sensitive,
.matches_u8(suggestion.value.as_bytes(), &prefix) suggestion.value.as_bytes(),
{ &prefix,
) {
output.push(suggestion); output.push(suggestion);
} }
} }
@ -153,10 +159,11 @@ impl Completer for VariableCompletion {
// Variable completion (e.g: $en<tab> to complete $env) // Variable completion (e.g: $en<tab> to complete $env)
for builtin in builtins { for builtin in builtins {
if options if options.match_algorithm.matches_u8_insensitive(
.match_algorithm options.case_sensitive,
.matches_u8(builtin.as_bytes(), &prefix) builtin.as_bytes(),
{ &prefix,
) {
output.push(Suggestion { output.push(Suggestion {
value: builtin.to_string(), value: builtin.to_string(),
description: None, description: None,
@ -178,7 +185,11 @@ impl Completer for VariableCompletion {
.rev() .rev()
{ {
for v in &overlay_frame.vars { 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 { output.push(Suggestion {
value: String::from_utf8_lossy(v.0).to_string(), value: String::from_utf8_lossy(v.0).to_string(),
description: None, description: None,
@ -200,7 +211,11 @@ impl Completer for VariableCompletion {
.rev() .rev()
{ {
for v in &overlay_frame.vars { 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 { output.push(Suggestion {
value: String::from_utf8_lossy(v.0).to_string(), value: String::from_utf8_lossy(v.0).to_string(),
description: None, description: None,
@ -281,3 +296,13 @@ fn recursive_value(val: Value, sublevels: Vec<Vec<u8>>) -> Value {
val 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())
}
}
}