Skip to content

Commit 89cc422

Browse files
Migrate type
1 parent a48730d commit 89cc422

File tree

2 files changed

+54
-89
lines changed

2 files changed

+54
-89
lines changed

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

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,10 @@
1-
import type { SimpleScopeTypeType, TextEditor } from "@cursorless/common";
2-
import type { Node } from "web-tree-sitter";
3-
import type {
4-
NodeMatcherAlternative,
5-
SelectionWithContext,
6-
} from "../typings/Types";
7-
import { patternFinder } from "../util/nodeFinders";
8-
import {
9-
argumentMatcher,
10-
cascadingMatcher,
11-
createPatternMatchers,
12-
leadingMatcher,
13-
matcher,
14-
patternMatcher,
15-
} from "../util/nodeMatchers";
16-
import {
17-
makeNodePairSelection,
18-
makeRangeFromPositions,
19-
} from "../util/nodeSelectors";
20-
21-
/**
22-
* Returns "impl_item[type]" node higher in the chain
23-
* @param node The node which we will start our search from
24-
* @returns node or null
25-
*/
26-
function implItemTypeFinder(node: Node) {
27-
if (
28-
node.parent?.type === "impl_item" &&
29-
node.parent?.childForFieldName("type")?.equals(node)
30-
) {
31-
return node;
32-
}
33-
return null;
34-
}
35-
36-
function traitBoundExtractor(
37-
editor: TextEditor,
38-
node: Node,
39-
): SelectionWithContext {
40-
return {
41-
selection: makeNodePairSelection(node.children[1], node.lastNamedChild!),
42-
context: {
43-
leadingDelimiterRange: makeRangeFromPositions(
44-
node.children[0].startPosition,
45-
node.children[1].startPosition,
46-
),
47-
},
48-
};
49-
}
1+
import type { SimpleScopeTypeType } from "@cursorless/common";
2+
import type { NodeMatcherAlternative } from "../typings/Types";
3+
import { argumentMatcher, createPatternMatchers } from "../util/nodeMatchers";
504

515
const nodeMatchers: Partial<
526
Record<SimpleScopeTypeType, NodeMatcherAlternative>
537
> = {
54-
type: cascadingMatcher(
55-
leadingMatcher(
56-
[
57-
"let_declaration[type]",
58-
"parameter[type]",
59-
"field_declaration[type]",
60-
"const_item[type]",
61-
],
62-
[":"],
63-
),
64-
matcher(
65-
patternFinder(
66-
"constrained_type_parameter[bounds]",
67-
"where_predicate[bounds]",
68-
),
69-
traitBoundExtractor,
70-
),
71-
leadingMatcher(["function_item[return_type]"], ["->"]),
72-
matcher(implItemTypeFinder),
73-
patternMatcher(
74-
"struct_item",
75-
"trait_item",
76-
"impl_item",
77-
"array_type[element]",
78-
),
79-
),
808
argumentOrParameter: argumentMatcher(
819
"arguments",
8210
"parameters",

queries/rust.scm

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,30 +113,53 @@
113113
name: (_) @className @name
114114
) @_.domain
115115

116+
;;!! fn foo() {}
117+
;;! ^^^^^^^^^^^
116118
(function_item
117119
name: (_) @functionName @name
118120
) @namedFunction @_.domain
119121

120-
(field_declaration
121-
name: (_) @name
122+
;;!! fn foo() -> int {}
123+
;;! ^^^
124+
(function_item
125+
parameters: (_) @_.leading.endOf
126+
return_type: (_)? @type
122127
) @_.domain
123128

124-
;;!! where T: Display + Clone
129+
;;!! fn foo<T: Display>() {}
130+
;;! ^
131+
;;! ^^^^^^^
132+
(constrained_type_parameter
133+
left: (_) @name
134+
(trait_bounds
135+
":" @type.leading.endOf
136+
(_) @type.start
137+
(_) @type.end
138+
)
139+
) @_.domain
140+
141+
;;!! where T: Display
125142
;;! ^
143+
;;! ^^^^^^^
126144
(where_predicate
127145
left: (_) @name
146+
(trait_bounds
147+
":" @type.leading.endOf
148+
(_) @type.start
149+
(_) @type.end
150+
)
151+
) @_.domain
152+
153+
(field_declaration
154+
name: (_) @name @type.leading.endOf
155+
type: (_) @type
128156
) @_.domain
129157

130158
;;!! (t: &T, u: &U)
131159
;;! ^ ^
132160
(parameter
133-
pattern: (_) @name
134-
) @_.domain
135-
136-
;;!! <T: Display, U: Clone>
137-
;;! ^ ^
138-
(constrained_type_parameter
139-
left: (_) @name
161+
pattern: (_) @name @type.leading.endOf
162+
type: (_) @type
140163
) @_.domain
141164

142165
[
@@ -191,8 +214,8 @@
191214
;;!! const foo: u8 = 2;
192215
;;! ^
193216
(const_item
194-
name: (_) @name
195-
type: (_) @value.leading.endOf
217+
name: (_) @name @type.leading.endOf
218+
type: (_) @type @value.leading.endOf
196219
value: (_) @value
197220
) @_.domain
198221

@@ -209,8 +232,8 @@
209232
;;! ^^^
210233
;;! ^
211234
(let_declaration
212-
pattern: (_) @name
213-
type: (_) @value.leading.start.endOf
235+
pattern: (_) @name @type.leading.endOf
236+
type: (_) @type @value.leading.start.endOf
214237
value: (_) @value
215238
) @_.domain
216239

@@ -291,6 +314,20 @@
291314
;;! ^^^^^^^
292315
(match_arm) @branch
293316

317+
[
318+
(struct_item)
319+
(trait_item)
320+
(impl_item)
321+
] @type
322+
323+
(impl_item
324+
type: (_) @type
325+
)
326+
327+
(array_type
328+
element: (_) @type
329+
) @_.domain
330+
294331
operator: [
295332
"<"
296333
"<<"

0 commit comments

Comments
 (0)