preserve variable capture spans in blocks (#15334)

Closes #15160

# User-Facing Changes

Certain "variable not found" errors no longer highlight the surrounding
block.

Before:

```nushell
do {
  match foo {
    _ => $in
  }
}

Error: nu:🐚:variable_not_found

  × Variable not found
   ╭─[entry #1:1:1]
 1 │ ╭─▶ do {
 2 │ │     match foo {
 3 │ │       _ => $in
 4 │ │     }
 5 │ ├─▶ }
   · ╰──── variable not found
```

After:

```nushell
Error: nu:🐚:variable_not_found

  × Variable not found
   ╭─[entry #1:3:10]
 2 │   match foo {
 3 │     _ => $in
   ·          ─┬─
   ·           ╰── variable not found
```
This commit is contained in:
Solomon
2025-03-20 18:20:28 +00:00
committed by GitHub
parent 7a6cfa24fc
commit dd56c813f9
6 changed files with 17 additions and 15 deletions

View File

@ -6589,9 +6589,9 @@ pub fn discover_captures_in_expr(
None => {
let block = working_set.get_block(block_id);
if !block.captures.is_empty() {
for capture in &block.captures {
for (capture, span) in &block.captures {
if !seen.contains(capture) {
output.push((*capture, call.head));
output.push((*capture, *span));
}
}
} else {
@ -6905,8 +6905,7 @@ pub fn parse(
&mut captures,
) {
Ok(_) => {
Arc::make_mut(&mut output).captures =
captures.into_iter().map(|(var_id, _)| var_id).collect();
Arc::make_mut(&mut output).captures = captures;
}
Err(err) => working_set.error(err),
}
@ -6961,7 +6960,7 @@ pub fn parse(
&& block_id.get() >= working_set.permanent_state.num_blocks()
{
let block = working_set.get_block_mut(block_id);
block.captures = captures.into_iter().map(|(var_id, _)| var_id).collect();
block.captures = captures;
}
}