diff --git a/data/fixtures/recorded/surroundingPair/changePair.yml b/data/fixtures/recorded/surroundingPair/changePair.yml
new file mode 100644
index 0000000000..3bfc52df27
--- /dev/null
+++ b/data/fixtures/recorded/surroundingPair/changePair.yml
@@ -0,0 +1,25 @@
+languageId: typescriptreact
+command:
+ version: 7
+ spokenForm: change pair
+ action:
+ name: clearAndSetSelection
+ target:
+ type: primitive
+ modifiers:
+ - type: containingScope
+ scopeType: {type: surroundingPair, delimiter: any}
+ usePrePhraseSnapshot: false
+initialState:
+ documentContents: |
+
null}>hello &
+ selections:
+ - anchor: {line: 0, character: 14}
+ active: {line: 0, character: 14}
+ marks: {}
+finalState:
+ documentContents: |
+ hello &
+ selections:
+ - anchor: {line: 0, character: 9}
+ active: {line: 0, character: 9}
diff --git a/packages/cursorless-engine/src/languages/TreeSitterQuery/isContainedInErrorNode.ts b/packages/cursorless-engine/src/languages/TreeSitterQuery/isContainedInErrorNode.ts
index 8b3c46a9bc..463d97e2e8 100644
--- a/packages/cursorless-engine/src/languages/TreeSitterQuery/isContainedInErrorNode.ts
+++ b/packages/cursorless-engine/src/languages/TreeSitterQuery/isContainedInErrorNode.ts
@@ -6,13 +6,35 @@ import type { SyntaxNode } from "web-tree-sitter";
* @returns True if the given node is contained in an error node
*/
export function isContainedInErrorNode(node: SyntaxNode) {
- let currentNode: SyntaxNode | null = node;
+ // This node or one of it descendants is an error node
+ if (node.hasError) {
+ return true;
+ }
+
+ let ancestorNode: SyntaxNode | null = node.parent;
+
+ while (ancestorNode != null) {
+ // Ancestral node is an error node
+ if (ancestorNode.isError) {
+ return true;
+ }
+
+ // Ancestral node has errors, but it was not siblings to the previous node.
+ // We don't want to discard a node when a sibling that isn't adjacent is
+ // erroring.
+ if (ancestorNode.hasError) {
+ return false;
+ }
- while (currentNode != null) {
- if (currentNode.hasError) {
+ // A adjacent sibling node was causing the problem. ie we are right next to the error node.
+ if (
+ ancestorNode.previousSibling?.isError ||
+ ancestorNode.nextSibling?.isError
+ ) {
return true;
}
- currentNode = currentNode.parent;
+
+ ancestorNode = ancestorNode.parent;
}
return false;