Skip to content

Commit 1bd3cca

Browse files
Move parse predicates with error handling to separate file
1 parent 9b3a05f commit 1bd3cca

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

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

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Position, TextDocument } from "@cursorless/common";
2-
import { showError, type TreeSitter } from "@cursorless/common";
2+
import { type TreeSitter } from "@cursorless/common";
33
import type * as treeSitter from "web-tree-sitter";
44
import { ide } from "../../singletons/ide.singleton";
55
import { getNodeRange } from "../../util/nodeSelectors";
@@ -11,9 +11,8 @@ import type {
1111
import { checkCaptureStartEnd } from "./checkCaptureStartEnd";
1212
import { isContainedInErrorNode } from "./isContainedInErrorNode";
1313
import { normalizeCaptureName } from "./normalizeCaptureName";
14-
import { parsePredicates } from "./parsePredicates";
14+
import { parsePredicatesWithErrorHandling } from "./parsePredicatesWithErrorHandling";
1515
import { positionToPoint } from "./positionToPoint";
16-
import { predicateToString } from "./predicateToString";
1716
import {
1817
getStartOfEndOfRange,
1918
rewriteStartOfEndOf,
@@ -46,31 +45,7 @@ export class TreeSitterQuery {
4645
treeSitter: TreeSitter,
4746
query: treeSitter.Query,
4847
) {
49-
const { errors, predicates } = parsePredicates(query.predicates);
50-
51-
if (errors.length > 0) {
52-
for (const error of errors) {
53-
const context = [
54-
`language ${languageId}`,
55-
`pattern ${error.patternIdx}`,
56-
`predicate \`${predicateToString(
57-
query.predicates[error.patternIdx][error.predicateIdx],
58-
)}\``,
59-
].join(", ");
60-
61-
void showError(
62-
ide().messages,
63-
"TreeSitterQuery.parsePredicates",
64-
`Error parsing predicate for ${context}: ${error.error}`,
65-
);
66-
}
67-
68-
// We show errors to the user, but we don't want to crash the extension
69-
// unless we're in test mode
70-
if (ide().runMode === "test") {
71-
throw new Error("Invalid predicates");
72-
}
73-
}
48+
const predicates = parsePredicatesWithErrorHandling(languageId, query);
7449

7550
return new TreeSitterQuery(treeSitter, query, predicates);
7651
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { showError } from "@cursorless/common";
2+
import type { Query } from "web-tree-sitter";
3+
import { ide } from "../../singletons/ide.singleton";
4+
import { parsePredicates } from "./parsePredicates";
5+
import { predicateToString } from "./predicateToString";
6+
7+
export function parsePredicatesWithErrorHandling(
8+
languageId: string,
9+
query: Query,
10+
) {
11+
const { errors, predicates } = parsePredicates(query.predicates);
12+
13+
if (errors.length > 0) {
14+
for (const error of errors) {
15+
const context = [
16+
`language ${languageId}`,
17+
`pattern ${error.patternIdx}`,
18+
`predicate \`${predicateToString(
19+
query.predicates[error.patternIdx][error.predicateIdx],
20+
)}\``,
21+
].join(", ");
22+
23+
void showError(
24+
ide().messages,
25+
"TreeSitterQuery.parsePredicates",
26+
`Error parsing predicate for ${context}: ${error.error}`,
27+
);
28+
}
29+
30+
// We show errors to the user, but we don't want to crash the extension
31+
// unless we're in test mode
32+
if (ide().runMode === "test") {
33+
throw new Error("Invalid predicates");
34+
}
35+
}
36+
37+
return predicates;
38+
}

0 commit comments

Comments
 (0)