Skip to content
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
@@ -1,28 +1,5 @@
import type { Range, TextDocument } from "@cursorless/common";
import type { Point, TreeCursor } from "web-tree-sitter";

/**
* Simple representation of the tree sitter syntax node. Used by
* {@link MutableQueryCapture} to avoid using range/text and other mutable
* parameters directly from the node.
*/
export interface SimpleSyntaxNode {
readonly id: number;
readonly type: string;
readonly isNamed: boolean;
readonly parent: SimpleSyntaxNode | null;
readonly children: Array<SimpleChildSyntaxNode>;
walk(): TreeCursor;
}

/**
* Add start and end position to the simple syntax node. Used by the `child-range!` predicate.
*/
interface SimpleChildSyntaxNode extends SimpleSyntaxNode {
readonly startPosition: Point;
readonly endPosition: Point;
readonly text: string;
}
import type { Node } from "web-tree-sitter";

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

readonly document: TextDocument;
range: Range;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { SimpleSyntaxNode } from "./QueryCapture";
import type { Node } from "web-tree-sitter";

export function getChildNodesForFieldName(
node: SimpleSyntaxNode,
node: Node,
fieldName: string,
): SimpleSyntaxNode[] {
): Node[] {
const nodes = [];
const treeCursor = node.walk();
let hasNext = treeCursor.gotoFirstChild();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Node } from "web-tree-sitter";
import type { MutableQueryCapture } from "./QueryCapture";

export function getNode(capture: MutableQueryCapture): Node {
if (capture.node == null) {
throw Error(
`Capture ${capture.name} has no node. The range of the capture has already been updated and no longer matches a specific node.`,
);
}
return capture.node;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SimpleSyntaxNode } from "./QueryCapture";
import type { Node } from "web-tree-sitter";

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