Skip to content

Commit 2925547

Browse files
Simplify and clean up query predicate operators (#3052)
1 parent b9191ee commit 2925547

File tree

17 files changed

+132
-129
lines changed

17 files changed

+132
-129
lines changed

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
import type { Range, TextDocument } from "@cursorless/common";
2-
import type { Point, TreeCursor } from "web-tree-sitter";
3-
4-
/**
5-
* Simple representation of the tree sitter syntax node. Used by
6-
* {@link MutableQueryCapture} to avoid using range/text and other mutable
7-
* parameters directly from the node.
8-
*/
9-
export interface SimpleSyntaxNode {
10-
readonly id: number;
11-
readonly type: string;
12-
readonly isNamed: boolean;
13-
readonly parent: SimpleSyntaxNode | null;
14-
readonly children: Array<SimpleChildSyntaxNode>;
15-
walk(): TreeCursor;
16-
}
17-
18-
/**
19-
* Add start and end position to the simple syntax node. Used by the `child-range!` predicate.
20-
*/
21-
interface SimpleChildSyntaxNode extends SimpleSyntaxNode {
22-
readonly startPosition: Point;
23-
readonly endPosition: Point;
24-
readonly text: string;
25-
}
2+
import type { Node } from "web-tree-sitter";
263

274
/**
285
* A capture of a query pattern against a syntax tree. Often corresponds to a
@@ -69,8 +46,9 @@ export interface QueryMatch {
6946
export interface MutableQueryCapture extends QueryCapture {
7047
/**
7148
* The tree-sitter node that was captured.
49+
* This may be undefined if the range has been modified by a query predicate.
7250
*/
73-
readonly node: SimpleSyntaxNode;
51+
node: Node | undefined;
7452

7553
readonly document: TextDocument;
7654
range: Range;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { SimpleSyntaxNode } from "./QueryCapture";
1+
import type { Node } from "web-tree-sitter";
22

33
export function getChildNodesForFieldName(
4-
node: SimpleSyntaxNode,
4+
node: Node,
55
fieldName: string,
6-
): SimpleSyntaxNode[] {
6+
): Node[] {
77
const nodes = [];
88
const treeCursor = node.walk();
99
let hasNext = treeCursor.gotoFirstChild();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Node } from "web-tree-sitter";
2+
import type { MutableQueryCapture } from "./QueryCapture";
3+
4+
export function getNode(capture: MutableQueryCapture): Node {
5+
if (capture.node == null) {
6+
throw Error(
7+
`Capture ${capture.name} has no node. The range of the capture has already been updated and no longer matches a specific node.`,
8+
);
9+
}
10+
return capture.node;
11+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SimpleSyntaxNode } from "./QueryCapture";
1+
import type { Node } from "web-tree-sitter";
22

33
/**
44
* Checks if a node is at an even index within its parent's field.
@@ -7,7 +7,7 @@ import type { SimpleSyntaxNode } from "./QueryCapture";
77
* @param fieldName - The name of the field in the parent node.
88
* @returns True if the node is at an even index, false otherwise.
99
*/
10-
export function isEven(node: SimpleSyntaxNode, fieldName: string): boolean {
10+
export function isEven(node: Node, fieldName: string): boolean {
1111
if (node.parent == null) {
1212
throw Error("Node has no parent");
1313
}

0 commit comments

Comments
 (0)