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>
This commit is contained in:
Bahex
2025-07-22 17:24:21 +03:00
committed by GitHub
parent 889fe7f97b
commit 51265b262d
3 changed files with 12 additions and 5 deletions

View File

@ -5,3 +5,9 @@ fn nu_highlight_not_expr() {
let actual = nu!("'not false' | nu-highlight | ansi strip"); let actual = nu!("'not false' | nu-highlight | ansi strip");
assert_eq!(actual.out, "not false"); 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(");
}

View File

@ -43,10 +43,7 @@ Row conditions cannot be stored in a variable. To pass a condition with a variab
]) ])
.required( .required(
"condition", "condition",
SyntaxShape::OneOf(vec![
SyntaxShape::RowCondition, SyntaxShape::RowCondition,
SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
]),
"Filter row condition or closure.", "Filter row condition or closure.",
) )
.allow_variants_without_examples(true) .allow_variants_without_examples(true)

View File

@ -239,7 +239,11 @@ impl Display for SyntaxShape {
SyntaxShape::Duration => write!(f, "duration"), SyntaxShape::Duration => write!(f, "duration"),
SyntaxShape::DateTime => write!(f, "datetime"), SyntaxShape::DateTime => write!(f, "datetime"),
SyntaxShape::Operator => write!(f, "operator"), SyntaxShape::Operator => write!(f, "operator"),
SyntaxShape::RowCondition => write!(f, "condition"), SyntaxShape::RowCondition => write!(
f,
"oneof<condition, {}>",
SyntaxShape::Closure(Some(vec![SyntaxShape::Any]))
),
SyntaxShape::MathExpression => write!(f, "variable"), SyntaxShape::MathExpression => write!(f, "variable"),
SyntaxShape::VarWithOptType => write!(f, "vardecl"), SyntaxShape::VarWithOptType => write!(f, "vardecl"),
SyntaxShape::Signature => write!(f, "signature"), SyntaxShape::Signature => write!(f, "signature"),