Skip to content

Commit 833cbff

Browse files
Migrate value
1 parent 93d7501 commit 833cbff

File tree

6 files changed

+71
-228
lines changed

6 files changed

+71
-228
lines changed

data/fixtures/recorded/languages/rust/clearValue3.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

data/fixtures/recorded/languages/rust/clearValue6.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

data/fixtures/recorded/languages/rust/clearValue8.yml

Lines changed: 0 additions & 37 deletions
This file was deleted.

data/fixtures/recorded/languages/rust/clearValue9.yml

Lines changed: 0 additions & 35 deletions
This file was deleted.

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

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,6 @@ import {
2121
} from "../util/nodeSelectors";
2222
import { elseExtractor, elseIfExtractor } from "./elseIfExtractor";
2323

24-
// Generated by the following command:
25-
// `curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-rust/36ae187ed6dd3803a8a89dbb54f3124c8ee74662/src/node-types.STATEMENT_TYPES | jq '[.[] | select(.type == "_declaration_statement") | .subtypes[].type, "expression_statement"]'`
26-
const STATEMENT_TYPES = [
27-
"associated_type",
28-
"attribute_item",
29-
"const_item",
30-
"empty_statement",
31-
"enum_item",
32-
"extern_crate_declaration",
33-
"foreign_mod_item",
34-
"impl_item",
35-
"inner_attribute_item",
36-
"let_declaration",
37-
"macro_definition",
38-
"macro_invocation",
39-
"function_item",
40-
"function_signature_item",
41-
"mod_item",
42-
"static_item",
43-
"struct_item",
44-
"trait_item",
45-
"type_item",
46-
"union_item",
47-
"use_declaration",
48-
"expression_statement",
49-
];
50-
5124
/**
5225
* Returns "impl_item[type]" node higher in the chain
5326
* @param node The node which we will start our search from
@@ -78,60 +51,6 @@ function traitBoundExtractor(
7851
};
7952
}
8053

81-
/**
82-
* Returns the return value node for a given block if we are in a block that has
83-
* a return value. If the return value expression uses the return keyword then
84-
* we return the value itself otherwise we just return the expression
85-
* @param node The node which we might match
86-
* @returns The return value node
87-
*/
88-
function returnValueFinder(node: Node) {
89-
if (node.type !== "block") {
90-
return null;
91-
}
92-
93-
const { lastNamedChild } = node;
94-
95-
// The return expression will always be the last statement or expression in
96-
// the block
97-
if (lastNamedChild == null) {
98-
return null;
99-
}
100-
101-
// If the final name child is an expression statement not a raw expression
102-
// then we only treat it as a return value if it is a return expression.
103-
// Otherwise it is just a normal statement that doesn't return anything
104-
if (lastNamedChild.type === "expression_statement") {
105-
const expression = lastNamedChild.child(0)!;
106-
107-
if (expression.type === "return_expression") {
108-
return expression.child(1);
109-
}
110-
111-
return null;
112-
}
113-
114-
// Any other type of statement is not a return statement so we should not
115-
// match it
116-
if (STATEMENT_TYPES.includes(lastNamedChild.type)) {
117-
return null;
118-
}
119-
120-
// NB: At this point we have now excluded all statement types of the only other
121-
// possible node is an expression node
122-
123-
// If it is a return expression then we zoom down to the actual value of the
124-
// return expression. This happens when they say `return foo` with no
125-
// trailing semicolon
126-
if (lastNamedChild.type === "return_expression") {
127-
return lastNamedChild.child(1);
128-
}
129-
130-
// At this point it is an expression which is not a return expression so we
131-
// just return it as the return value of the block
132-
return lastNamedChild;
133-
}
134-
13554
const nodeMatchers: Partial<
13655
Record<SimpleScopeTypeType, NodeMatcherAlternative>
13756
> = {
@@ -196,15 +115,6 @@ const nodeMatchers: Partial<
196115
),
197116
trailingMatcher(["field_initializer[name]", "field_pattern[name]"], [":"]),
198117
),
199-
value: cascadingMatcher(
200-
leadingMatcher(["let_declaration[value]"], ["="]),
201-
leadingMatcher(
202-
["field_initializer[value]", "field_pattern[pattern]"],
203-
[":"],
204-
),
205-
patternMatcher("meta_item[value]", "const_item[value]"),
206-
matcher(returnValueFinder),
207-
),
208118
branch: cascadingMatcher(
209119
patternMatcher("match_arm"),
210120
matcher(patternFinder("else_clause"), elseExtractor("if_expression")),

queries/rust.scm

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,85 @@
109109

110110
;;!! let Foo {aaa: 1, bbb: 2}
111111
;;! ^^^ ^^^
112+
;;! ^ ^
112113
(field_initializer
113-
name: (_) @collectionKey
114-
value: (_) @_.trailing.startOf
114+
name: (_) @collectionKey @value.leading.endOf
115+
value: (_) @value @collectionKey.trailing.startOf
115116
) @_.domain
116117

117118
;;!! Foo {aaa: 1, bbb: 2}
118119
;;! ^^^ ^^^
120+
;;! ^ ^
119121
(field_pattern
120-
name: (_) @collectionKey
121-
pattern: (_) @_.trailing.startOf
122+
name: (_) @collectionKey @value.leading.endOf
123+
pattern: (_) @value @collectionKey.trailing.startOf
122124
) @_.domain
123125

126+
;;!! const foo: u8 = 2;
127+
;;! ^
128+
(const_item
129+
(_) @value.leading.endOf
130+
.
131+
value: (_) @value
132+
) @_.domain
133+
134+
;;!! let foo = 2;
135+
;;! ^
136+
(let_declaration
137+
(_) @value.leading.endOf
138+
.
139+
value: (_) @value
140+
) @_.domain
141+
142+
;;!! #[cfg_attr(feature = "foo")]
143+
;;! ^^^^^
144+
(meta_item
145+
(_) @value.leading.endOf
146+
value: (_) @value
147+
) @_.domain
148+
149+
;;!! return 2;
150+
;;! ^
151+
(return_expression
152+
(_) @value
153+
) @_.domain
154+
155+
;; Implicit return value at end of function body
156+
(function_item
157+
body: (_
158+
(_) @value
159+
.
160+
)
161+
(#not-type?
162+
@value
163+
;; Exclude return expression
164+
return_expression
165+
;; Exclude all statements
166+
associated_type
167+
attribute_item
168+
const_item
169+
empty_statement
170+
enum_item
171+
extern_crate_declaration
172+
foreign_mod_item
173+
impl_item
174+
inner_attribute_item
175+
let_declaration
176+
macro_definition
177+
macro_invocation
178+
function_item
179+
function_signature_item
180+
mod_item
181+
static_item
182+
struct_item
183+
trait_item
184+
type_item
185+
union_item
186+
use_declaration
187+
expression_statement
188+
)
189+
)
190+
124191
operator: [
125192
"<"
126193
"<<"

0 commit comments

Comments
 (0)