Skip to content

Commit 9e89672

Browse files
Migrate if statement
1 parent 816f936 commit 9e89672

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ import type { MutableQueryCapture } from "./QueryCapture";
66
import { QueryPredicateOperator } from "./QueryPredicateOperator";
77
import { q } from "./operatorArgumentSchemaTypes";
88

9+
/**
10+
* A predicate operator that returns true if the node matches the given text.
11+
* For example, `(#text? @foo bar)` will accept the match if the `@foo`
12+
* captures text is `bar`. It is acceptable to pass in multiple texts, e.g.
13+
* `(#text? @foo bar baz)`.
14+
*/
15+
class Text extends QueryPredicateOperator<Text> {
16+
name = "text?" as const;
17+
schema = z.tuple([q.node, q.string]).rest(q.string);
18+
run({ node, document, range }: MutableQueryCapture, ...texts: string[]) {
19+
const text = document.getText(range);
20+
return texts.includes(text);
21+
}
22+
}
23+
924
/**
1025
* A predicate operator that returns true if the node is of the given type.
1126
* For example, `(#type? @foo string)` will accept the match if the `@foo`
@@ -388,6 +403,7 @@ class EmptySingleMultiDelimiter extends QueryPredicateOperator<EmptySingleMultiD
388403

389404
export const queryPredicateOperators = [
390405
new Log(),
406+
new Text(),
391407
new Type(),
392408
new NotType(),
393409
new TrimEnd(),

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,6 @@ const functionNameMatcher = chainedMatcher([
111111
(functionNode) => getValueNodes(functionNode)[1],
112112
]);
113113

114-
const ifStatementFinder = functionNameBasedFinder(
115-
"if",
116-
"if-let",
117-
"when",
118-
"when-let",
119-
);
120-
121-
const ifStatementMatcher = matcher(ifStatementFinder);
122-
123114
const nodeMatchers: Partial<
124115
Record<SimpleScopeTypeType, NodeMatcherAlternative>
125116
> = {
@@ -150,13 +141,6 @@ const nodeMatchers: Partial<
150141
functionNameBasedMatcher("fn"),
151142
patternMatcher("anon_fn_lit"),
152143
),
153-
154-
ifStatement: ifStatementMatcher,
155-
156-
condition: chainedMatcher([
157-
ifStatementFinder,
158-
(node) => getValueNodes(node)[1],
159-
]),
160144
};
161145

162146
export default createPatternMatchers(nodeMatchers);

queries/clojure.scm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,13 @@
7979
open: "{" @collectionItem.iteration.start.endOf
8080
close: "}" @collectionItem.iteration.end.startOf
8181
) @collectionItem.iteration.domain
82+
83+
;;!! (if true "hello")
84+
;;! ^^^^^^^^^^^^^^^^^
85+
;;! ^^^^
86+
(list_lit
87+
value: (_) @_dummy
88+
.
89+
value: (_) @condition
90+
(#text? @_dummy "if" "if-let" "when" "when-let")
91+
) @ifStatement @condition.domain

0 commit comments

Comments
 (0)