Skip to content

Surrounding pairs: Fall back to text based algorithm when error node is detected. #1558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ function processSurroundingPairCore(
try {
node = languageDefinitions.getNodeAtLocation(document, range);

// Error nodes are unreliable and should be ignored. Fall back to text based
// algorithm.
if (nodeHasError(node)) {
return findSurroundingPairTextBased(
editor,
range,
null,
delimiters,
scopeType,
);
}

textFragmentExtractor = getTextFragmentExtractor(document.languageId);
} catch (err) {
if ((err as Error).name === "UnsupportedLanguageError") {
Expand Down Expand Up @@ -148,3 +160,22 @@ function processSurroundingPairCore(
scopeType,
);
}

function nodeHasError(node: SyntaxNode, includeChildren = false): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems quite conservative. Are there test cases that fail if you don't eg check grand-siblings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The added test for example. The error node is a child to an ancestor of the selected node.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed, let's use your proposed version for now and if it becomes bothersome we can revisit. see also #1600

if (nodeIsError(node)) {
return true;
}
if (includeChildren) {
if (node.children.some(nodeIsError)) {
return true;
}
}
if (node.parent != null) {
return nodeHasError(node.parent, true);
}
return false;
}

function nodeIsError(node: SyntaxNode): boolean {
return node.type === "ERROR";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
languageId: python
command:
version: 5
spokenForm: clear pair
action: {name: clearAndSetSelection}
targets:
- type: primitive
modifiers:
- type: containingScope
scopeType: {type: surroundingPair, delimiter: any}
usePrePhraseSnapshot: true
initialState:
documentContents: "def funk(words:list<str>):"
selections:
- anchor: {line: 0, character: 20}
active: {line: 0, character: 20}
marks: {}
finalState:
documentContents: "def funk(words:list):"
selections:
- anchor: {line: 0, character: 19}
active: {line: 0, character: 19}