Skip to content
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

Handle angle brackets within an error node #396

Merged
merged 3 commits into from
Dec 17, 2021
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 @@ -6,6 +6,7 @@ import {
SurroundingPairDirection,
} from "../../../typings/Types";
import { getNodeRange } from "../../../util/nodeSelectors";
import { isContainedInErrorNode } from "../../../util/treeSitterUtils";
import { extractSelectionFromSurroundingPairOffsets } from "./extractSelectionFromSurroundingPairOffsets";
import { findSurroundingPairCore } from "./findSurroundingPairCore";
import { getIndividualDelimiters } from "./getIndividualDelimiters";
Expand Down Expand Up @@ -205,7 +206,8 @@ function findSurroundingPairContainedInNode(
// looking at its position within its parent node.
if (
delimiterInfo.delimiter === "angleBrackets" &&
inferDelimiterSide(delimiterNode) !== delimiterInfo.side
inferDelimiterSide(delimiterNode) !== delimiterInfo.side &&
!isContainedInErrorNode(delimiterNode)
) {
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: typescript
command:
version: 1
spokenForm: clear pair
action: clearAndSetSelection
targets:
- type: primitive
modifier: {type: surroundingPair, delimiter: any}
initialState:
documentContents: foo<bar>
selections:
- anchor: {line: 0, character: 6}
active: {line: 0, character: 6}
marks: {}
finalState:
documentContents: foo
selections:
- anchor: {line: 0, character: 3}
active: {line: 0, character: 3}
thatMark:
- anchor: {line: 0, character: 3}
active: {line: 0, character: 3}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}, isImplicit: false}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: typescript
command:
version: 1
spokenForm: clear pair
action: clearAndSetSelection
targets:
- type: primitive
modifier: {type: surroundingPair, delimiter: any}
initialState:
documentContents: foo<>
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
marks: {}
finalState:
documentContents: foo
selections:
- anchor: {line: 0, character: 3}
active: {line: 0, character: 3}
thatMark:
- anchor: {line: 0, character: 3}
active: {line: 0, character: 3}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}, isImplicit: false}]
30 changes: 30 additions & 0 deletions src/util/treeSitterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,33 @@ export function getChildNodesForFieldName(

return ret;
}

/**
* Returns a list of the node's ancestors, including the node itself if
* `includeNode` is `true`
* @param node The node to iterate ancestors from
* @param includeNode Whether to include the node itself in the returned list
* @returns A list of ancestors possibly including the includeNode node itself
*/
export function getAncestors(node: SyntaxNode, includeNode: boolean = true) {
const ancestors: SyntaxNode[] = includeNode ? [node] : [];

for (
let currentNode: SyntaxNode | null = node.parent;
currentNode != null;
currentNode = currentNode.parent
) {
ancestors.push(currentNode);
}

return ancestors;
}

/**
* Determines whether the given node or one of its ancestors is an error node
* @param node The node to check
* @returns True if the given node is contained in an error node
*/
export function isContainedInErrorNode(node: SyntaxNode) {
return getAncestors(node).some((ancestor) => ancestor.type === "ERROR");
}