Repository navigation
Expand file tree
/
Copy pathTreeSitterQuery.ts
More file actions
137 lines (122 loc) · 4.57 KB
/
Copy pathTreeSitterQuery.ts
File metadata and controls
137 lines (122 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Position, TextDocument, showError } from "@cursorless/common";
import { Point, Query } from "web-tree-sitter";
import { ide } from "../../singletons/ide.singleton";
import { TreeSitter } from "../../typings/TreeSitter";
import { getNodeRange } from "../../util/nodeSelectors";
import { MutableQueryMatch, QueryCapture, QueryMatch } from "./QueryCapture";
import { parsePredicates } from "./parsePredicates";
import { predicateToString } from "./predicateToString";
import { groupBy, uniq } from "lodash";
import { checkCaptureStartEnd } from "./checkCaptureStartEnd";
import { rewriteStartOfEndOf } from "./rewriteStartOfEndOf";
/**
* Wrapper around a tree-sitter query that provides a more convenient API, and
* defines our own custom predicate operators
*/
export class TreeSitterQuery {
private constructor(
private treeSitter: TreeSitter,
/**
* The raw tree-sitter query as parsed by tree-sitter from the query file
*/
private query: Query,
/**
* The predicates for each pattern in the query. Each element of the outer
* array corresponds to a pattern, and each element of the inner array
* corresponds to a predicate for that pattern.
*/
private patternPredicates: ((match: MutableQueryMatch) => boolean)[][],
) {}
static create(languageId: string, treeSitter: TreeSitter, query: Query) {
const { errors, predicates } = parsePredicates(query.predicates);
if (errors.length > 0) {
for (const error of errors) {
const context = [
`language ${languageId}`,
`pattern ${error.patternIdx}`,
`predicate \`${predicateToString(
query.predicates[error.patternIdx][error.predicateIdx],
)}\``,
].join(", ");
showError(
ide().messages,
"TreeSitterQuery.parsePredicates",
`Error parsing predicate for ${context}: ${error.error}`,
);
}
// We show errors to the user, but we don't want to crash the extension
// unless we're in test mode
if (ide().runMode === "test") {
throw new Error("Invalid predicates");
}
}
return new TreeSitterQuery(treeSitter, query, predicates);
}
matches(
document: TextDocument,
start?: Position,
end?: Position,
): QueryMatch[] {
return this.query
.matches(this.treeSitter.getTree(document).rootNode, {
startPosition: start == null ? undefined : positionToPoint(start),
endPosition: end == null ? undefined : positionToPoint(end),
})
.map(
({ pattern, captures }): MutableQueryMatch => ({
patternIdx: pattern,
captures: captures.map(({ name, node }) => ({
name,
node,
document,
range: getNodeRange(node),
insertionDelimiter: undefined,
allowMultiple: false,
})),
}),
)
.filter((match) =>
this.patternPredicates[match.patternIdx].every((predicate) =>
predicate(match),
),
)
.map((match): QueryMatch => {
// Merge the ranges of all captures with the same name into a single
// range and return one capture with that name. We consider captures
// with names `@foo`, `@foo.start`, and `@foo.end` to have the same
// name, for which we'd return a capture with name `foo`.
const captures: QueryCapture[] = Object.entries(
groupBy(match.captures, ({ name }) => normalizeCaptureName(name)),
).map(([name, captures]) => {
captures = rewriteStartOfEndOf(captures);
const capturesAreValid = checkCaptureStartEnd(
captures,
ide().messages,
);
if (!capturesAreValid && ide().runMode === "test") {
throw new Error("Invalid captures");
}
return {
name,
range: captures
.map(({ range }) => range)
.reduce((accumulator, range) => range.union(accumulator)),
allowMultiple: captures.some((capture) => capture.allowMultiple),
insertionDelimiter: captures.find(
(capture) => capture.insertionDelimiter != null,
)?.insertionDelimiter,
};
});
return { ...match, captures };
});
}
get captureNames() {
return uniq(this.query.captureNames.map(normalizeCaptureName));
}
}
function normalizeCaptureName(name: string): string {
return name.replace(/(\.(start|end))?(\.(startOf|endOf))?$/, "");
}
function positionToPoint(start: Position): Point {
return { row: start.line, column: start.character };
}