Skip to content

Commit 1c7bd74

Browse files
Don't discard nodes with non adjacent error siblings (#2860)
Today we discard parse tree nodes if any of the ancestral nodes has a error. That means that any of the ancestral nodes has a descendant that is an error node. That could be something a hundred lines below that isn't really relevant to the node we are evaluating. This updates changes so we only look at error nodes that are adjacent to our ascending path.
1 parent 195dc3e commit 1c7bd74

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
languageId: typescriptreact
2+
command:
3+
version: 7
4+
spokenForm: change pair
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: surroundingPair, delimiter: any}
12+
usePrePhraseSnapshot: false
13+
initialState:
14+
documentContents: |
15+
<div bbb={() => null}>hello &</div>
16+
selections:
17+
- anchor: {line: 0, character: 14}
18+
active: {line: 0, character: 14}
19+
marks: {}
20+
finalState:
21+
documentContents: |
22+
<div bbb=>hello &</div>
23+
selections:
24+
- anchor: {line: 0, character: 9}
25+
active: {line: 0, character: 9}

packages/cursorless-engine/src/languages/TreeSitterQuery/isContainedInErrorNode.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,35 @@ import type { SyntaxNode } from "web-tree-sitter";
66
* @returns True if the given node is contained in an error node
77
*/
88
export function isContainedInErrorNode(node: SyntaxNode) {
9-
let currentNode: SyntaxNode | null = node;
9+
// This node or one of it descendants is an error node
10+
if (node.hasError) {
11+
return true;
12+
}
13+
14+
let ancestorNode: SyntaxNode | null = node.parent;
15+
16+
while (ancestorNode != null) {
17+
// Ancestral node is an error node
18+
if (ancestorNode.isError) {
19+
return true;
20+
}
21+
22+
// Ancestral node has errors, but it was not siblings to the previous node.
23+
// We don't want to discard a node when a sibling that isn't adjacent is
24+
// erroring.
25+
if (ancestorNode.hasError) {
26+
return false;
27+
}
1028

11-
while (currentNode != null) {
12-
if (currentNode.hasError) {
29+
// A adjacent sibling node was causing the problem. ie we are right next to the error node.
30+
if (
31+
ancestorNode.previousSibling?.isError ||
32+
ancestorNode.nextSibling?.isError
33+
) {
1334
return true;
1435
}
15-
currentNode = currentNode.parent;
36+
37+
ancestorNode = ancestorNode.parent;
1638
}
1739

1840
return false;

0 commit comments

Comments
 (0)