Skip to content

Commit c9e0c37

Browse files
Refactoring
1 parent 98b02db commit c9e0c37

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class TreeSitterQuery {
9595
start?: Position,
9696
end?: Position,
9797
): QueryMatch[] {
98-
const isTesting = ide().runMode === "test";
98+
const checkCaptures = ide().runMode !== "production";
9999
const results: QueryMatch[] = [];
100100

101101
const matches = this.query.matches(
@@ -110,7 +110,7 @@ export class TreeSitterQuery {
110110
const mutableMatch = createMutableQueryMatch(document, match);
111111

112112
if (this.runPredicates(mutableMatch)) {
113-
results.push(createQueryMatch(mutableMatch, isTesting));
113+
results.push(createQueryMatch(mutableMatch, checkCaptures));
114114
}
115115
}
116116

@@ -147,10 +147,10 @@ function createMutableQueryMatch(
147147

148148
function createQueryMatch(
149149
match: MutableQueryMatch,
150-
isTesting: boolean,
150+
checkCaptures: boolean,
151151
): QueryMatch {
152152
const result: MutableQueryCapture[] = [];
153-
const resultMap = new Map<
153+
const map = new Map<
154154
string,
155155
{ acc: MutableQueryCapture; captures: MutableQueryCapture[] }
156156
>();
@@ -163,37 +163,35 @@ function createQueryMatch(
163163
for (const capture of match.captures) {
164164
const name = normalizeCaptureName(capture.name);
165165
const range = getStartOfEndOfRange(capture);
166-
const existing = resultMap.get(name);
166+
const existing = map.get(name);
167167

168168
if (existing == null) {
169-
const accumulator = {
169+
const captures = [capture];
170+
const acc = {
170171
...capture,
171172
name,
172173
range,
174+
hasError: () => captures.some((c) => c.hasError()),
173175
};
174-
result.push(accumulator);
175-
resultMap.set(name, {
176-
acc: accumulator,
177-
captures: [capture],
178-
});
176+
result.push(acc);
177+
map.set(name, { acc, captures });
179178
} else {
180179
existing.acc.range = existing.acc.range.union(range);
181180
existing.acc.allowMultiple =
182181
existing.acc.allowMultiple || capture.allowMultiple;
183182
existing.acc.insertionDelimiter =
184183
existing.acc.insertionDelimiter ?? capture.insertionDelimiter;
185-
existing.acc.hasError = () => existing.captures.some((c) => c.hasError());
186184
existing.captures.push(capture);
187185
}
188186
}
189187

190-
if (isTesting) {
191-
for (const captureGroup of resultMap.values()) {
188+
if (checkCaptures) {
189+
for (const captureGroup of map.values()) {
192190
const capturesAreValid = checkCaptureStartEnd(
193191
rewriteStartOfEndOf(captureGroup.captures),
194192
ide().messages,
195193
);
196-
if (!capturesAreValid) {
194+
if (!capturesAreValid && ide().runMode === "test") {
197195
throw new Error("Invalid captures");
198196
}
199197
}

0 commit comments

Comments
 (0)