From 51265b262dd0a0ab02ffd828dcfce063d15d1ce5 Mon Sep 17 00:00:00 2001 From: Bahex Date: Tue, 22 Jul 2025 17:24:21 +0300 Subject: [PATCH] fix: highlighting a where command with invalid arguments can duplicate text (#16192) - fixes #16129 # Description ## Problem Parsing a multi span value as RowCondition always produces a RowCondition expression that covers all the spans. Which is problematic inside OneOf and can produce multiple expressions with overlapping spans. Which results in funky highlighting that duplicates text. ## Solution Only reason for including `SyntaxShape::Closure` in the signature (#15697) was for documentation purposes, making it clear in `help` texts that a closure can be used as the argument. As our current parser is shape directed, simplifying the command signature means simplifies the parsing, so using a RowCondition on its own, and instead making it always look like a union with `closure(any)` solves the issue without any changes in the parser. Also, RowCondition always accepts closure values anyway, so its textual representation should indicate that without the need to wrap it in OneOf. Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com> --- crates/nu-cli/tests/commands/nu_highlight.rs | 6 ++++++ crates/nu-command/src/filters/where_.rs | 5 +---- crates/nu-protocol/src/syntax_shape.rs | 6 +++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/nu-cli/tests/commands/nu_highlight.rs b/crates/nu-cli/tests/commands/nu_highlight.rs index bd185a8634..c7d2e07c80 100644 --- a/crates/nu-cli/tests/commands/nu_highlight.rs +++ b/crates/nu-cli/tests/commands/nu_highlight.rs @@ -5,3 +5,9 @@ fn nu_highlight_not_expr() { let actual = nu!("'not false' | nu-highlight | ansi strip"); assert_eq!(actual.out, "not false"); } + +#[test] +fn nu_highlight_where_row_condition() { + let actual = nu!("'ls | where a b 12345(' | nu-highlight | ansi strip"); + assert_eq!(actual.out, "ls | where a b 12345("); +} diff --git a/crates/nu-command/src/filters/where_.rs b/crates/nu-command/src/filters/where_.rs index b77dd13a9a..467b8530ae 100644 --- a/crates/nu-command/src/filters/where_.rs +++ b/crates/nu-command/src/filters/where_.rs @@ -43,10 +43,7 @@ Row conditions cannot be stored in a variable. To pass a condition with a variab ]) .required( "condition", - SyntaxShape::OneOf(vec![ - SyntaxShape::RowCondition, - SyntaxShape::Closure(Some(vec![SyntaxShape::Any])), - ]), + SyntaxShape::RowCondition, "Filter row condition or closure.", ) .allow_variants_without_examples(true) diff --git a/crates/nu-protocol/src/syntax_shape.rs b/crates/nu-protocol/src/syntax_shape.rs index 44dd4903d3..7f1e2f95b6 100644 --- a/crates/nu-protocol/src/syntax_shape.rs +++ b/crates/nu-protocol/src/syntax_shape.rs @@ -239,7 +239,11 @@ impl Display for SyntaxShape { SyntaxShape::Duration => write!(f, "duration"), SyntaxShape::DateTime => write!(f, "datetime"), SyntaxShape::Operator => write!(f, "operator"), - SyntaxShape::RowCondition => write!(f, "condition"), + SyntaxShape::RowCondition => write!( + f, + "oneof", + SyntaxShape::Closure(Some(vec![SyntaxShape::Any])) + ), SyntaxShape::MathExpression => write!(f, "variable"), SyntaxShape::VarWithOptType => write!(f, "vardecl"), SyntaxShape::Signature => write!(f, "signature"),