Skip to content

Commit f08b107

Browse files
Updates for newer Tree sitter version (#2912)
Should be merged after cursorless-dev/vscode-parse-tree#104
1 parent 629fac3 commit f08b107

30 files changed

+1192
-432
lines changed
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
import type { Range, TextDocument } from "@cursorless/common";
2-
import type { Language, SyntaxNode, Tree } from "web-tree-sitter";
2+
import type { Node, Query, Tree } from "web-tree-sitter";
33

44
export interface TreeSitter {
55
/**
66
* Function to access nodes in the tree sitter.
77
*/
8-
getNodeAtLocation(document: TextDocument, range: Range): SyntaxNode;
8+
getNodeAtLocation(document: TextDocument, range: Range): Node;
99

1010
/**
1111
* Function to access the tree sitter tree.
1212
*/
1313
getTree(document: TextDocument): Tree;
1414

15-
/**
16-
* Gets a language if it is loaded
17-
*
18-
* @param languageId The language id of the language to get
19-
* @returns The language if it is already loaded
20-
*/
21-
getLanguage(languageId: string): Language | undefined;
22-
2315
/**
2416
* Loads a language, returning true if it was successfully loaded
2517
*
2618
* @param languageId The language id of the language to load
2719
* @returns `true` if the language was successfully loaded
2820
*/
2921
loadLanguage(languageId: string): Promise<boolean>;
22+
23+
/**
24+
* Create a query if the language is loaded.
25+
*
26+
* @param languageId The language id of the language to get
27+
* @param source The query source
28+
* @returns The query if that language is already loaded
29+
*/
30+
createQuery(languageId: string, source: string): Query | undefined;
3031
}

packages/cursorless-engine/src/disabledComponents/DisabledLanguageDefinitions.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { TextDocument, Range, Listener } from "@cursorless/common";
2-
import type { SyntaxNode } from "web-tree-sitter";
2+
import type { Node } from "web-tree-sitter";
33
import type { LanguageDefinition } from "../languages/LanguageDefinition";
44
import type { LanguageDefinitions } from "../languages/LanguageDefinitions";
55

@@ -16,10 +16,7 @@ export class DisabledLanguageDefinitions implements LanguageDefinitions {
1616
return undefined;
1717
}
1818

19-
getNodeAtLocation(
20-
_document: TextDocument,
21-
_range: Range,
22-
): SyntaxNode | undefined {
19+
getNodeAtLocation(_document: TextDocument, _range: Range): Node | undefined {
2320
return undefined;
2421
}
2522

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Range, TextDocument, TreeSitter } from "@cursorless/common";
2-
import type { Language, SyntaxNode, Tree } from "web-tree-sitter";
2+
import type { Node, Query, Tree } from "web-tree-sitter";
33

44
export class DisabledTreeSitter implements TreeSitter {
55
getTree(_document: TextDocument): Tree {
@@ -10,11 +10,11 @@ export class DisabledTreeSitter implements TreeSitter {
1010
return Promise.resolve(false);
1111
}
1212

13-
getLanguage(_languageId: string): Language | undefined {
13+
getNodeAtLocation(_document: TextDocument, _range: Range): Node {
1414
throw new Error("Tree sitter not provided");
1515
}
1616

17-
getNodeAtLocation(_document: TextDocument, _range: Range): SyntaxNode {
17+
createQuery(_languageId: string, _source: string): Query | undefined {
1818
throw new Error("Tree sitter not provided");
1919
}
2020
}

packages/cursorless-engine/src/languages/LanguageDefinition.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,14 @@ export class LanguageDefinition {
5959
return undefined;
6060
}
6161

62-
const rawQuery = treeSitter
63-
.getLanguage(languageId)!
64-
.query(rawLanguageQueryString);
62+
const rawQuery = treeSitter.createQuery(languageId, rawLanguageQueryString);
63+
64+
if (rawQuery == null) {
65+
throw Error(
66+
`Could not create Tree sitter query for language ${languageId}`,
67+
);
68+
}
69+
6570
const query = TreeSitterQuery.create(languageId, treeSitter, rawQuery);
6671

6772
return new LanguageDefinition(query);

packages/cursorless-engine/src/languages/LanguageDefinitions.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
type TreeSitter,
99
} from "@cursorless/common";
1010
import { toString } from "lodash-es";
11-
import type { SyntaxNode } from "web-tree-sitter";
11+
import type { Node } from "web-tree-sitter";
1212
import { LanguageDefinition } from "./LanguageDefinition";
1313
import { treeSitterQueryCache } from "./TreeSitterQuery/treeSitterQueryCache";
1414

@@ -36,10 +36,7 @@ export interface LanguageDefinitions {
3636
/**
3737
* @deprecated Only for use in legacy containing scope stage
3838
*/
39-
getNodeAtLocation(
40-
document: TextDocument,
41-
range: Range,
42-
): SyntaxNode | undefined;
39+
getNodeAtLocation(document: TextDocument, range: Range): Node | undefined;
4340
}
4441

4542
/**
@@ -163,7 +160,7 @@ export class LanguageDefinitionsImpl
163160
return definition === LANGUAGE_UNDEFINED ? undefined : definition;
164161
}
165162

166-
public getNodeAtLocation(document: TextDocument, range: Range): SyntaxNode {
163+
public getNodeAtLocation(document: TextDocument, range: Range): Node {
167164
return this.treeSitter.getNodeAtLocation(document, range);
168165
}
169166

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type InferSchemaType<T extends HasSchema> = T["schema"];
3232
* Maps from an operand schema output type to the type of the argument that
3333
* will be passed to the `accept` function of the predicate operator. For example:
3434
*
35-
* - `{type: "capture", name: string}` -> `SyntaxNode`
35+
* - `{type: "capture", name: string}` -> `Node`
3636
* - `{type: "integer", value: number}` -> `number`
3737
*/
3838
type PredicateParameterType<T extends SchemaOutputType> = T extends {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PredicateOperand } from "web-tree-sitter";
1+
import type { PredicateStep } from "web-tree-sitter";
22
import type { z } from "zod";
33
import type {
44
AcceptFunctionArgs,
@@ -41,7 +41,7 @@ export abstract class QueryPredicateOperator<T extends HasSchema> {
4141
*
4242
* @param args The arguments to the operator, converted to the types specified
4343
* in the schema. For example, if the schema is `z.tuple([q.node, q.string])`,
44-
* then `args` will be `SyntaxNode, string`.
44+
* then `args` will be `Node, string`.
4545
*/
4646
protected abstract run(
4747
...args: AcceptFunctionArgs<z.infer<InferSchemaType<T>>>
@@ -71,7 +71,7 @@ export abstract class QueryPredicateOperator<T extends HasSchema> {
7171
* @returns Either a predicate function, or a list of error messages if the operands
7272
* were invalid.
7373
*/
74-
createPredicate(inputOperands: PredicateOperand[]): PredicateResult {
74+
createPredicate(inputOperands: PredicateStep[]): PredicateResult {
7575
const result = this.schema.safeParse(inputOperands);
7676

7777
return result.success

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { PredicateOperand } from "web-tree-sitter";
1+
import type { PredicateStep } from "web-tree-sitter";
22
import type { z } from "zod";
33
import { operandToString } from "./predicateToString";
44

55
export function constructZodErrorMessages(
6-
inputOperands: PredicateOperand[],
7-
error: z.ZodError<PredicateOperand[]>,
6+
inputOperands: PredicateStep[],
7+
error: z.ZodError<PredicateStep[]>,
88
): string[] {
99
return error.errors
1010
.filter(
@@ -21,7 +21,7 @@ export function constructZodErrorMessages(
2121
.map((error) => getErrorMessage(inputOperands, error));
2222
}
2323

24-
function getErrorMessage(inputOperands: PredicateOperand[], error: z.ZodIssue) {
24+
function getErrorMessage(inputOperands: PredicateStep[], error: z.ZodIssue) {
2525
if (error.path.length === 0) {
2626
if (error.code === "too_small") {
2727
return "Too few arguments";

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

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

33
/**
44
* Determines whether the given node or one of its ancestors is an error node
55
* @param node The node to check
66
* @returns True if the given node is contained in an error node
77
*/
8-
export function isContainedInErrorNode(node: SyntaxNode) {
8+
export function isContainedInErrorNode(node: Node) {
99
// This node or one of it descendants is an error node
1010
if (node.hasError) {
1111
return true;
1212
}
1313

14-
let ancestorNode: SyntaxNode | null = node.parent;
14+
let ancestorNode: Node | null = node.parent;
1515

1616
while (ancestorNode != null) {
1717
// Ancestral node is an error node

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from "zod";
22
import { assertTypesEqual } from "./assertTypesEqual";
3-
import type { PredicateOperand } from "web-tree-sitter";
3+
import type { PredicateStep } from "web-tree-sitter";
44

55
const string = z.object({ type: z.literal("string"), value: z.string() });
66

@@ -62,9 +62,9 @@ export type SchemaTypes = (typeof q)[keyof typeof q];
6262

6363
/**
6464
* The type of the input to the schema. This should always be
65-
* `PredicateOperand`, as that is what we always get from tree-sitter
65+
* `PredicateStep`, as that is what we always get from tree-sitter
6666
*/
67-
export type SchemaInputType = PredicateOperand;
67+
export type SchemaInputType = PredicateStep;
6868
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
6969
assertTypesEqual<SchemaInputType, z.input<SchemaTypes>, SchemaInputType>;
7070

0 commit comments

Comments
 (0)