From 8b4d710b0b99309c08a474403676868ff53c8cee Mon Sep 17 00:00:00 2001 From: Yash Thakur <45539777+ysthakur@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:56:35 -0500 Subject: [PATCH] Fix highlighting of spread subexpressions in records (#11202) # Description It turns out that I left a bug in [#11144](https://github.com/nushell/nushell/pull/11144/), which introduced a spread operator in record literals. When highlighting subexpressions that are spread inside records, the spread operator and the token before it are insert twice. Currently, when you type `{ ...() }`, this is what you'll see: ![image](https://github.com/nushell/nushell/assets/45539777/9a76647a-6bbe-426e-95bc-50becf2fa537) With the PR, the behavior is as expected: ![image](https://github.com/nushell/nushell/assets/45539777/36bdab23-3252-4500-8317-51278da0e869) I'm still not sure how `FlatShape` works, I just copied the existing logic for flattening key-value pairs in records, so it's possible there's still issues, but I haven't found any yet (tried spreading subexpressions, variables, and records). # User-Facing Changes Highlighting for subexpressions spread inside records should no longer be screwed up. # Tests + Formatting Is there any way to test flattening/syntax highlighting? # After Submitting --- crates/nu-parser/src/flatten.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/nu-parser/src/flatten.rs b/crates/nu-parser/src/flatten.rs index 26cd1e5bf490b..e4222fee04fdd 100644 --- a/crates/nu-parser/src/flatten.rs +++ b/crates/nu-parser/src/flatten.rs @@ -443,8 +443,15 @@ pub fn flatten_expression( output.push((Span::new(last_end, op_span.start), FlatShape::Record)); } output.push((*op_span, FlatShape::Operator)); + last_end = op_span.end; let flattened_inner = flatten_expression(working_set, record); + if let Some(first) = flattened_inner.first() { + if first.0.start > last_end { + output + .push((Span::new(last_end, first.0.start), FlatShape::Record)); + } + } if let Some(last) = flattened_inner.last() { last_end = last.0.end; }