From 1e566adcfc24364efeb3cafd23d494cc11ba685e Mon Sep 17 00:00:00 2001 From: zc he Date: Wed, 12 Mar 2025 21:04:20 +0800 Subject: [PATCH] fix(completion): full set of operators for type any (#15303) # Description As elaborated [here](https://github.com/nushell/nushell/issues/13676#issuecomment-2717096417), a full set probably is a more thoughtful approximation for unknown types. # User-Facing Changes # Tests + Formatting Adjusted # After Submitting --- .../src/completions/operator_completions.rs | 24 ++++++++++++++++--- crates/nu-cli/tests/completions/mod.rs | 7 ++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/crates/nu-cli/src/completions/operator_completions.rs b/crates/nu-cli/src/completions/operator_completions.rs index b102b2c327..3d1db30673 100644 --- a/crates/nu-cli/src/completions/operator_completions.rs +++ b/crates/nu-cli/src/completions/operator_completions.rs @@ -37,6 +37,15 @@ fn common_comparison_ops() -> Vec { ] } +fn all_ops_for_immutable() -> Vec { + ast::Comparison::iter() + .map(operator_to_item) + .chain(ast::Math::iter().map(operator_to_item)) + .chain(ast::Boolean::iter().map(operator_to_item)) + .chain(ast::Bits::iter().map(operator_to_item)) + .collect() +} + fn collection_comparison_ops() -> Vec { let mut ops = common_comparison_ops(); ops.push(operator_to_item(Comparison::Has)); @@ -72,6 +81,10 @@ fn bit_ops() -> Vec { ast::Bits::iter().map(operator_to_item).collect() } +fn all_assignment_ops() -> Vec { + ast::Assignment::iter().map(operator_to_item).collect() +} + fn numeric_assignment_ops() -> Vec { ast::Assignment::iter() .filter(|op| !matches!(op, ast::Assignment::ConcatenateAssign)) @@ -154,7 +167,7 @@ fn ops_by_value(value: &Value, mutable: bool) -> Vec { Value::Filesize { .. } | Value::Duration { .. } => valid_value_with_unit_ops(), Value::Range { .. } | Value::Record { .. } => collection_comparison_ops(), Value::List { .. } => valid_list_ops(), - _ => common_comparison_ops(), + _ => all_ops_for_immutable(), }; if mutable { ops.extend(match value { @@ -165,7 +178,11 @@ fn ops_by_value(value: &Value, mutable: bool) -> Vec { Value::String { .. } | Value::Binary { .. } | Value::List { .. } => { concat_assignment_ops() } - _ => vec![operator_to_item(ast::Assignment::Assign)], + Value::Bool { .. } + | Value::Date { .. } + | Value::Range { .. } + | Value::Record { .. } => vec![operator_to_item(ast::Assignment::Assign)], + _ => all_assignment_ops(), }) } ops @@ -223,7 +240,7 @@ impl Completer for OperatorCompletion<'_> { needs_assignment_ops = false; ops_by_value(&value, mutable) } - _ => common_comparison_ops(), + _ => all_ops_for_immutable(), }, _ => common_comparison_ops(), }; @@ -233,6 +250,7 @@ impl Completer for OperatorCompletion<'_> { Type::Int | Type::Float | Type::Number => numeric_assignment_ops(), Type::Filesize | Type::Duration => numeric_assignment_ops(), Type::String | Type::Binary | Type::List(_) => concat_assignment_ops(), + Type::Any => all_assignment_ops(), _ => vec![operator_to_item(ast::Assignment::Assign)], }); } diff --git a/crates/nu-cli/tests/completions/mod.rs b/crates/nu-cli/tests/completions/mod.rs index f0c9fcaef0..8c29c83ebc 100644 --- a/crates/nu-cli/tests/completions/mod.rs +++ b/crates/nu-cli/tests/completions/mod.rs @@ -2345,6 +2345,13 @@ fn assignment_operator_completions(mut custom_completer: NuCompleter) { let expected: Vec<_> = vec!["++", "++="]; let suggestions = custom_completer.complete("$env.config.keybindings +", 25); match_suggestions(&expected, &suggestions); + + // all operators for type any + let suggestions = custom_completer.complete("ls | where name ", 16); + assert_eq!(30, suggestions.len()); + let expected: Vec<_> = vec!["starts-with"]; + let suggestions = custom_completer.complete("ls | where name starts", 22); + match_suggestions(&expected, &suggestions); } #[test]