Skip to content

Commit 5eb34e9

Browse files
Clean up
1 parent bf9b171 commit 5eb34e9

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { SimpleSyntaxNode } from "./QueryCapture";
2+
3+
/**
4+
* Checks if a node is at an even index within its parent's field.
5+
*
6+
* @param node - The node to check.
7+
* @param fieldName - The name of the field in the parent node.
8+
* @returns True if the node is at an even index, false otherwise.
9+
*/
10+
export function isEven(node: SimpleSyntaxNode, fieldName: string): boolean {
11+
if (node.parent == null) {
12+
throw Error("Node has no parent");
13+
}
14+
15+
const treeCursor = node.parent.walk();
16+
let hasNext = treeCursor.gotoFirstChild();
17+
let even = true;
18+
19+
while (hasNext) {
20+
if (treeCursor.currentFieldName === fieldName) {
21+
if (treeCursor.currentNode.id === node.id) {
22+
return even;
23+
}
24+
even = !even;
25+
}
26+
hasNext = treeCursor.gotoNextSibling();
27+
}
28+
29+
throw Error(`Node not found in parent for field: ${fieldName}`);
30+
}

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ import { z } from "zod";
44
import { makeRangeFromPositions } from "../../util/nodeSelectors";
55
import type { MutableQueryCapture } from "./QueryCapture";
66
import { QueryPredicateOperator } from "./QueryPredicateOperator";
7-
import { getChildNodesForFieldName } from "./getChildNodesForFieldName";
7+
import { isEven } from "./isEven";
88
import { q } from "./operatorArgumentSchemaTypes";
99

1010
/**
11-
* A predicate operator that returns true if the node matches the desired index parity.
12-
* For example, `(#parity? @foo value 0)` will accept the match if the `@foo`
13-
* capture is at index parity 0 (even) among its parents value children.
11+
* A predicate operator that returns true if the node is at an even index within
12+
* its parents field. For example, `(#even? @foo value)` will accept the match
13+
* if the `@foo` capture is at an even index among its parents value children.
1414
*/
15-
class Parity extends QueryPredicateOperator<Parity> {
16-
name = "parity?" as const;
17-
schema = z.tuple([q.node, q.string, q.integer]);
18-
run({ node }: MutableQueryCapture, fieldName: string, parity: 0 | 1) {
19-
if (node.parent == null) {
20-
return false;
21-
}
22-
23-
const children = getChildNodesForFieldName(node.parent, fieldName);
24-
const nodeIndex = children.findIndex(({ id }) => id === node.id);
25-
26-
if (nodeIndex === -1) {
27-
return false;
28-
}
29-
30-
const desiredIndex = Math.floor(nodeIndex / 2) * 2 + parity;
15+
class Even extends QueryPredicateOperator<Even> {
16+
name = "even?" as const;
17+
schema = z.tuple([q.node, q.string]);
18+
run({ node }: MutableQueryCapture, fieldName: string) {
19+
return isEven(node, fieldName);
20+
}
21+
}
3122

32-
return nodeIndex === desiredIndex;
23+
/**
24+
* A predicate operator that returns true if the node is at an odd index within
25+
* its parents field. For example, `(#odd? @foo value)` will accept the match
26+
* if the `@foo` capture is at an odd index among its parents value children.
27+
*/
28+
class Odd extends QueryPredicateOperator<Odd> {
29+
name = "odd?" as const;
30+
schema = z.tuple([q.node, q.string]);
31+
run({ node }: MutableQueryCapture, fieldName: string) {
32+
return !isEven(node, fieldName);
3333
}
3434
}
3535

@@ -429,7 +429,8 @@ class EmptySingleMultiDelimiter extends QueryPredicateOperator<EmptySingleMultiD
429429

430430
export const queryPredicateOperators = [
431431
new Log(),
432-
new Parity(),
432+
new Even(),
433+
new Odd(),
433434
new Text(),
434435
new Type(),
435436
new NotType(),

queries/clojure.scm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@
151151
(map_lit
152152
value: (_) @collectionKey @collectionKey.domain.start @value.domain.start
153153
value: (_) @value @collectionKey.domain.end @value.domain.end
154-
(#parity? @collectionKey value 0)
155-
(#parity? @value value 1)
154+
(#even? @collectionKey value)
155+
(#odd? @value value)
156156
)
157157

158158
;;!! {:foo 1, :bar 2}

0 commit comments

Comments
 (0)