-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathruby.ts
More file actions
188 lines (179 loc) · 4.05 KB
/
Copy pathruby.ts
File metadata and controls
188 lines (179 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import type { SimpleScopeTypeType } from "@cursorless/common";
import type { Node } from "web-tree-sitter";
import type { NodeMatcherAlternative } from "../typings/Types";
import { patternFinder } from "../util/nodeFinders";
import {
ancestorChainNodeMatcher,
argumentMatcher,
cascadingMatcher,
conditionMatcher,
createPatternMatchers,
leadingMatcher,
matcher,
patternMatcher,
trailingMatcher,
} from "../util/nodeMatchers";
// Generated by the following command:
// curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-ruby/1ebfdb288842dae5a9233e2509a135949023dd82/src/node-types.json \
// | jq '[.[] | select((.type == "_statement" or .type == "_simple_statement") and .type != "_expression") | .subtypes[] | select(.type != "_expression") | .type ]'
const STATEMENT_TYPES = [
"alias",
"begin_block",
"end_block",
"if_modifier",
"rescue_modifier",
"undef",
"unless_modifier",
"until_modifier",
"while_modifier",
];
// Generated by the following command:
// > curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-ruby/1ebfdb288842dae5a9233e2509a135949023dd82/src/node-types.json \
// | jq '[.[] | select(.type == _expression or .type == _arg or .type == _primary or .type == _lhs or .type == _simple_numeric or .type == _variable or .type == _nonlocal_variable) | .subtypes[].type | select(startswith(_) | not)] | sort | unique'
const EXPRESSION_TYPES = [
"array",
"assignment",
"begin",
"binary",
"break",
"call",
"case",
"case_match",
"chained_string",
"character",
"class",
"class_variable",
"complex",
"conditional",
"constant",
"delimited_symbol",
"element_reference",
"false",
"float",
"for",
"global_variable",
"hash",
"heredoc_beginning",
"identifier",
"if",
"instance_variable",
"integer",
"lambda",
"method",
"module",
"next",
"nil",
"operator_assignment",
"parenthesized_statements",
"range",
"rational",
"redo",
"regex",
"retry",
"return",
"scope_resolution",
"self",
"simple_symbol",
"singleton_class",
"singleton_method",
"string",
"string_array",
"subshell",
"super",
"symbol_array",
"true",
"unary",
"unless",
"until",
"while",
"yield",
];
const EXPRESSION_STATEMENT_PARENT_TYPES = [
"begin_block",
"begin",
"block_body",
"block",
"body_statement",
"do_block",
"do",
"else",
"end_block",
"ensure",
"heredoc_beginning",
"interpolation",
"lambda",
"method",
"parenthesized_statements",
"program",
"singleton_class",
"singleton_method",
"then",
];
const assignmentOperators = [
"=",
"+=",
"-=",
"*=",
"**=",
"/=",
"||=",
"|=",
"&&=",
"&=",
"%=",
">>=",
"<<=",
"^=",
];
const mapKeyValueSeparators = [":", "=>"];
function blockFinder(node: Node) {
if (node.type !== "call") {
return null;
}
const receiver = node.childForFieldName("receiver");
const method = node.childForFieldName("method");
const block = node.childForFieldName("block");
if (
(receiver?.text === "Proc" && method?.text === "new") ||
(receiver == null && method?.text === "lambda")
) {
return node;
}
return block;
}
const nodeMatchers: Partial<
Record<SimpleScopeTypeType, NodeMatcherAlternative>
> = {
statement: cascadingMatcher(
patternMatcher(...STATEMENT_TYPES),
ancestorChainNodeMatcher(
[
patternFinder(...EXPRESSION_STATEMENT_PARENT_TYPES),
patternFinder(...EXPRESSION_TYPES),
],
1,
),
),
anonymousFunction: cascadingMatcher(
patternMatcher("lambda", "do_block"),
matcher(blockFinder),
),
condition: conditionMatcher("*[condition]"),
argumentOrParameter: argumentMatcher(
"lambda_parameters",
"method_parameters",
"block_parameters",
"argument_list",
),
collectionKey: trailingMatcher(["pair[key]"], [":"]),
value: leadingMatcher(
[
"pair[value]",
"assignment[right]",
"operator_assignment[right]",
"return.argument_list!",
],
assignmentOperators.concat(mapKeyValueSeparators),
),
};
export const patternMatchers = createPatternMatchers(nodeMatchers);