@plannotator/sem-jiff 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/LICENSE-APACHE +191 -0
- package/LICENSE-MIT +21 -0
- package/README.md +110 -2
- package/THIRD-PARTY-NOTICES +367 -0
- package/dist/candidate.d.ts +14 -0
- package/dist/candidate.js +1 -0
- package/dist/classifier.d.ts +25 -0
- package/dist/classifier.js +32 -0
- package/dist/copy-wasm-bin.d.ts +2 -0
- package/dist/copy-wasm-bin.js +11 -0
- package/dist/copy-wasm.d.ts +3 -0
- package/dist/copy-wasm.js +43 -0
- package/dist/coverage.d.ts +7 -0
- package/dist/coverage.js +43 -0
- package/dist/edits.d.ts +10 -0
- package/dist/edits.js +230 -0
- package/dist/extract.d.ts +5 -0
- package/dist/extract.js +24 -0
- package/dist/grammars.d.ts +22 -0
- package/dist/grammars.js +37 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +493 -0
- package/dist/inline.d.ts +21 -0
- package/dist/inline.js +723 -0
- package/dist/languages.d.ts +2 -0
- package/dist/languages.js +28 -0
- package/dist/linediff.d.ts +27 -0
- package/dist/linediff.js +163 -0
- package/dist/opinions.d.ts +84 -0
- package/dist/opinions.js +201 -0
- package/dist/pairing.d.ts +10 -0
- package/dist/pairing.js +137 -0
- package/dist/parse.d.ts +118 -0
- package/dist/parse.js +1274 -0
- package/dist/pierre-types.d.ts +101 -0
- package/dist/pierre-types.js +5 -0
- package/dist/runtime.d.ts +46 -0
- package/dist/runtime.js +51 -0
- package/dist/structure.d.ts +13 -0
- package/dist/structure.js +894 -0
- package/dist/summary.d.ts +19 -0
- package/dist/summary.js +15 -0
- package/dist/syntax.d.ts +67 -0
- package/dist/syntax.js +412 -0
- package/dist/textdiff.d.ts +15 -0
- package/dist/textdiff.js +64 -0
- package/dist/textmoves.d.ts +8 -0
- package/dist/textmoves.js +143 -0
- package/package.json +108 -4
- package/src/candidate.ts +10 -0
- package/src/grammars.ts +44 -0
- package/src/opinions.ts +271 -0
- package/wasm/SHA256SUMS +12 -0
- package/wasm/tree-sitter-c_sharp.wasm +0 -0
- package/wasm/tree-sitter-go.wasm +0 -0
- package/wasm/tree-sitter-java.wasm +0 -0
- package/wasm/tree-sitter-kotlin.wasm +0 -0
- package/wasm/tree-sitter-php.wasm +0 -0
- package/wasm/tree-sitter-python.wasm +0 -0
- package/wasm/tree-sitter-ruby.wasm +0 -0
- package/wasm/tree-sitter-rust.wasm +0 -0
- package/wasm/tree-sitter-swift.wasm +0 -0
- package/wasm/tree-sitter-tsx.wasm +0 -0
- package/wasm/tree-sitter-typescript.wasm +0 -0
- package/wasm/web-tree-sitter.wasm +0 -0
package/dist/parse.js
ADDED
|
@@ -0,0 +1,1274 @@
|
|
|
1
|
+
// Parser setup and per-language knowledge: what a function is, where its
|
|
2
|
+
// statements live, what a call and a return look like.
|
|
3
|
+
import { Language, Parser } from 'web-tree-sitter';
|
|
4
|
+
import { currentSetup, resolveInNode, rule } from "./runtime.js";
|
|
5
|
+
export { languageForPath } from "./languages.js";
|
|
6
|
+
/** Comment node types across grammars (Rust splits line and block comments; Swift calls block comments multiline_comment). */
|
|
7
|
+
export const COMMENT_TYPES = new Set(['comment', 'line_comment', 'block_comment', 'multiline_comment']);
|
|
8
|
+
export const isComment = (n) => !!n && COMMENT_TYPES.has(n.type);
|
|
9
|
+
/** The node holds an error, or a missing token that is not harmless for its language. */
|
|
10
|
+
export function brokenTree(n, cfg) {
|
|
11
|
+
if (!n.hasError)
|
|
12
|
+
return false;
|
|
13
|
+
if (!cfg.harmlessMissing?.size && !cfg.harmlessError)
|
|
14
|
+
return true;
|
|
15
|
+
const stack = [n];
|
|
16
|
+
while (stack.length) {
|
|
17
|
+
const x = stack.pop();
|
|
18
|
+
if (x.isError && cfg.harmlessError?.(x))
|
|
19
|
+
continue;
|
|
20
|
+
if (x.isError || (x.isMissing && !cfg.harmlessMissing?.has(x.type)))
|
|
21
|
+
return true;
|
|
22
|
+
if (!x.hasError)
|
|
23
|
+
continue;
|
|
24
|
+
const children = x.children.filter((c) => !!c);
|
|
25
|
+
// A missing token of a hidden rule (`_class_member_semi`) is not among the children: read it from the tree's text form.
|
|
26
|
+
if (!children.some((c) => c.hasError || c.isMissing)) {
|
|
27
|
+
const missing = [...x.toString().matchAll(/\(MISSING ("(?:[^"\\]|\\.)*"|[\w]+)\)/g)].map((m) => m[1]);
|
|
28
|
+
if (/\((ERROR|UNEXPECTED)\b/.test(x.toString()) || missing.some((m) => !cfg.harmlessMissing?.has(m)))
|
|
29
|
+
return true;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
stack.push(...children);
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Imports are declarations too, named by what they import (the module path), so
|
|
38
|
+
* re-sorted imports pair up and show as moved rather than deleted and re-added.
|
|
39
|
+
* Adding a language's imports takes only the key: the text of its module path.
|
|
40
|
+
*/
|
|
41
|
+
export const importDeclaration = (node, key, prefix = '') => ({
|
|
42
|
+
node,
|
|
43
|
+
kind: 'import',
|
|
44
|
+
name: prefix + unquote(key).replace(/\s+/g, ''),
|
|
45
|
+
});
|
|
46
|
+
const unquote = (s) => s.replace(/^(['"`])(.*)\1$/, '$2');
|
|
47
|
+
/** The expression body of a one-line arrow function (`(x) => x + 1`). */
|
|
48
|
+
const FUNCTION_VALUE = /^(arrow_function|function_expression|function|generator_function)$/;
|
|
49
|
+
/**
|
|
50
|
+
* The function an Effect constructor wraps: the first function argument of `X.gen(...)`,
|
|
51
|
+
* `X.fn(...)`, `X.fnUntraced(...)` or `X.suspend(...)`, also when the name comes first
|
|
52
|
+
* (`Effect.fn("load")(function* () {...})`). Null for any other call.
|
|
53
|
+
*/
|
|
54
|
+
function effectFunction(call) {
|
|
55
|
+
const callee = call.childForFieldName('function');
|
|
56
|
+
const wraps = (c) => c?.type === 'member_expression' && /^(gen|fn|fnUntraced|suspend)$/.test(c.childForFieldName('property')?.text ?? '');
|
|
57
|
+
if (!wraps(callee) && !(callee?.type === 'call_expression' && wraps(callee.childForFieldName('function'))))
|
|
58
|
+
return null;
|
|
59
|
+
for (const a of call.childForFieldName('arguments')?.namedChildren ?? [])
|
|
60
|
+
if (a && FUNCTION_VALUE.test(a.type))
|
|
61
|
+
return a;
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A top-level constant made by handing a function to a factory is that function:
|
|
66
|
+
* `export const withRetry = dual(2, (self, n) => {...})`,
|
|
67
|
+
* `export const view = makeView((model, h) => ...)`, `const subs = Subscription.make<M>()((entry) => ...)`.
|
|
68
|
+
* Only at module level, only when the function is the last argument, and never for collection or
|
|
69
|
+
* promise methods (`const rows = items.map((x) => ...)` holds a value, not a function).
|
|
70
|
+
*/
|
|
71
|
+
const VALUE_METHODS = /^(map|flatMap|filter|reduce|reduceRight|forEach|find|findIndex|findLast|some|every|sort|toSorted|then|catch|finally|from|all|race|any|allSettled|keys|values|entries|fromEntries|assign|freeze|of|pipe|setTimeout|setInterval|queueMicrotask|describe|it|test|beforeEach|afterEach|beforeAll|afterAll|computed|watch|effect)$/;
|
|
72
|
+
function factoryFunction(decl, call) {
|
|
73
|
+
if (!rule('FACTORY_FUNCTIONS'))
|
|
74
|
+
return null;
|
|
75
|
+
const statement = decl.parent?.type === 'lexical_declaration' || decl.parent?.type === 'variable_declaration' ? decl.parent : null;
|
|
76
|
+
const top = statement?.parent?.type === 'export_statement' ? statement.parent.parent : statement?.parent;
|
|
77
|
+
if (top?.type !== 'program')
|
|
78
|
+
return null;
|
|
79
|
+
let callee = call.childForFieldName('function');
|
|
80
|
+
// `make<M>()((entry) => ...)`: the factory is the inner call.
|
|
81
|
+
while (callee?.type === 'call_expression')
|
|
82
|
+
callee = callee.childForFieldName('function');
|
|
83
|
+
const name = callee?.type === 'member_expression' ? callee.childForFieldName('property')?.text : callee?.text;
|
|
84
|
+
if (!name || VALUE_METHODS.test(name))
|
|
85
|
+
return null;
|
|
86
|
+
const args = call.childForFieldName('arguments')?.namedChildren.filter((a) => !!a && !isComment(a)) ?? [];
|
|
87
|
+
const last = args.at(-1);
|
|
88
|
+
return last && FUNCTION_VALUE.test(last.type) ? last : null;
|
|
89
|
+
}
|
|
90
|
+
const isArrowExpressionBody = (n) => n.type !== 'statement_block' && n.parent?.type === 'arrow_function' && n.parent.childForFieldName('body')?.id === n.id;
|
|
91
|
+
const tsLike = (id, file) => ({
|
|
92
|
+
id,
|
|
93
|
+
wasm: `tree-sitter-typescript/${file}`,
|
|
94
|
+
// `const f = () => { ... }`, class fields and object properties holding arrow
|
|
95
|
+
// functions (`perform: (elements) => { ... }`) are functions too.
|
|
96
|
+
functionTypes: new Set(['function_declaration', 'method_definition', 'generator_function_declaration', 'variable_declarator', 'public_field_definition', 'pair']),
|
|
97
|
+
callTypes: new Set(['call_expression']),
|
|
98
|
+
blockTypes: new Set(['statement_block']),
|
|
99
|
+
// A one-line arrow (`const f = (x) => x.a + 1`) returns its expression body.
|
|
100
|
+
isReturn: (s, last) => s.type === 'return_statement' || (!!last && isArrowExpressionBody(s)),
|
|
101
|
+
// Decorators above a class member (`@Get(':id')`) belong to it, like a doc comment.
|
|
102
|
+
attachedTypes: new Set(['comment', 'decorator']),
|
|
103
|
+
nameOf: (fn) => (fn.type === 'pair' ? (fn.childForFieldName('key')?.text.replace(/^['"]|['"]$/g, '') ?? null) : (fn.childForFieldName('name')?.text ?? null)),
|
|
104
|
+
bodyOf: (fn) => {
|
|
105
|
+
let value = fn.childForFieldName('value');
|
|
106
|
+
// A function handed to a wrapper is still this one: `const Row = memo((props) => {...})`,
|
|
107
|
+
// `const save = useCallback(async () => {...}, [deps])`, `forwardRef(function X() {...})`.
|
|
108
|
+
// Only wrappers that define a function: `items.map((x) => ...)` holds a value, not a function.
|
|
109
|
+
if (value?.type === 'call_expression' && /^(React\.)?(memo|forwardRef|useCallback|observer)$/.test(value.childForFieldName('function')?.text ?? '')) {
|
|
110
|
+
const first = value.childForFieldName('arguments')?.namedChild(0);
|
|
111
|
+
value = first && FUNCTION_VALUE.test(first.type) ? first : value;
|
|
112
|
+
}
|
|
113
|
+
// Effect defines functions the same way: `const load = Effect.gen(function* () {...})`,
|
|
114
|
+
// `Effect.fn("load")(function* (id) {...})`, `Effect.suspend(() => ...)`.
|
|
115
|
+
if (value?.type === 'call_expression')
|
|
116
|
+
value = effectFunction(value) ?? value;
|
|
117
|
+
if (value?.type === 'call_expression' && fn.type === 'variable_declarator')
|
|
118
|
+
value = factoryFunction(fn, value) ?? value;
|
|
119
|
+
if (fn.type === 'variable_declarator' || fn.type === 'public_field_definition' || fn.type === 'pair') {
|
|
120
|
+
if (!value || !FUNCTION_VALUE.test(value.type))
|
|
121
|
+
return null;
|
|
122
|
+
const body = value.childForFieldName('body');
|
|
123
|
+
return body?.type === 'statement_block' || (body && value.type === 'arrow_function') ? body : null;
|
|
124
|
+
}
|
|
125
|
+
return fn.childForFieldName('body');
|
|
126
|
+
},
|
|
127
|
+
statementsOf: (block) => (isArrowExpressionBody(block) ? [block] : block.namedChildren.filter((n) => n !== null && !isComment(n))),
|
|
128
|
+
calleeOf: (call) => {
|
|
129
|
+
const f = call.childForFieldName('function');
|
|
130
|
+
if (!f)
|
|
131
|
+
return null;
|
|
132
|
+
return f.type === 'member_expression' ? (f.childForFieldName('property')?.text ?? null) : f.text;
|
|
133
|
+
},
|
|
134
|
+
returnValueOf: (ret) => (isArrowExpressionBody(ret) ? ret : (ret.namedChildren.find((n) => n !== null && n.type !== 'comment') ?? null)),
|
|
135
|
+
identifierTypes: new Set(['identifier', 'property_identifier', 'type_identifier', 'shorthand_property_identifier', 'private_property_identifier']),
|
|
136
|
+
declarationsIn: (container) => {
|
|
137
|
+
const out = [];
|
|
138
|
+
for (const child of container.namedChildren) {
|
|
139
|
+
if (!child || child.type === 'comment')
|
|
140
|
+
continue;
|
|
141
|
+
if (child.type === 'import_statement') {
|
|
142
|
+
// `import type { X } from 'm'` and `import { Y } from 'm'` are different imports.
|
|
143
|
+
const source = child.childForFieldName('source');
|
|
144
|
+
const type = child.children.some((c) => c?.type === 'type') ? 'type ' : '';
|
|
145
|
+
if (source)
|
|
146
|
+
out.push(importDeclaration(child, source.text, type));
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
// Re-exports (`export { a } from 'm'`, `export * from 'm'`) are imports too.
|
|
150
|
+
const reexport = child.type === 'export_statement' && !child.childForFieldName('declaration') ? child.childForFieldName('source') : null;
|
|
151
|
+
if (reexport) {
|
|
152
|
+
out.push({ ...importDeclaration(child, reexport.text), kind: 'export-from' });
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
// `export ...` wraps the declaration; the whole statement is the entity.
|
|
156
|
+
const decl = child.type === 'export_statement' ? (child.childForFieldName('declaration') ?? child) : child;
|
|
157
|
+
let name = decl.childForFieldName('name')?.text ?? null;
|
|
158
|
+
if (!name && (decl.type === 'lexical_declaration' || decl.type === 'variable_declaration')) {
|
|
159
|
+
name = decl.namedChildren.find((n) => n?.type === 'variable_declarator')?.childForFieldName('name')?.text ?? null;
|
|
160
|
+
}
|
|
161
|
+
if (!name)
|
|
162
|
+
continue;
|
|
163
|
+
const body = decl.childForFieldName('body');
|
|
164
|
+
// Namespaces (`namespace N { ... }`) hold declarations like a class body does.
|
|
165
|
+
const members = body && (body.type === 'class_body' || body.type === 'interface_body' || (body.type === 'statement_block' && /module/.test(decl.type))) ? body : undefined;
|
|
166
|
+
out.push({ node: child, kind: decl.type, name, members });
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
/** Rust items that declare one named thing; `impl` and `use` are handled on their own. */
|
|
172
|
+
const RUST_ITEMS = new Set([
|
|
173
|
+
'function_item', 'function_signature_item', 'struct_item', 'enum_item', 'union_item', 'trait_item', 'mod_item',
|
|
174
|
+
'const_item', 'static_item', 'type_item', 'macro_definition', 'extern_crate_declaration',
|
|
175
|
+
]);
|
|
176
|
+
/** A Rust expression as the last thing in a block is the block's value (a return without `return`). */
|
|
177
|
+
const RUST_NOT_TAIL = new Set(['let_declaration', 'expression_statement', 'empty_statement', 'macro_definition', 'attribute_item', 'inner_attribute_item']);
|
|
178
|
+
const rust = {
|
|
179
|
+
id: 'rust',
|
|
180
|
+
wasm: 'tree-sitter-rust/tree-sitter-rust.wasm',
|
|
181
|
+
functionTypes: new Set(['function_item']),
|
|
182
|
+
callTypes: new Set(['call_expression']),
|
|
183
|
+
blockTypes: new Set(['block']),
|
|
184
|
+
isReturn: (s, last) => (s.type === 'expression_statement' && s.namedChild(0)?.type === 'return_expression') ||
|
|
185
|
+
s.type === 'return_expression' ||
|
|
186
|
+
(last && !RUST_NOT_TAIL.has(s.type) && !s.type.endsWith('_item')),
|
|
187
|
+
attachedTypes: new Set(['line_comment', 'block_comment', 'attribute_item']),
|
|
188
|
+
nameOf: (fn) => fn.childForFieldName('name')?.text ?? null,
|
|
189
|
+
bodyOf: (fn) => fn.childForFieldName('body'),
|
|
190
|
+
statementsOf: (block) => block.namedChildren.filter((n) => n !== null && !isComment(n) && n.type !== 'label'),
|
|
191
|
+
calleeOf: (call) => {
|
|
192
|
+
let f = call.childForFieldName('function');
|
|
193
|
+
if (f?.type === 'generic_function')
|
|
194
|
+
f = f.childForFieldName('function');
|
|
195
|
+
if (!f)
|
|
196
|
+
return null;
|
|
197
|
+
if (f.type === 'field_expression')
|
|
198
|
+
return f.childForFieldName('field')?.text ?? null;
|
|
199
|
+
if (f.type === 'scoped_identifier')
|
|
200
|
+
return f.childForFieldName('name')?.text ?? null;
|
|
201
|
+
return f.text;
|
|
202
|
+
},
|
|
203
|
+
returnValueOf: (ret) => {
|
|
204
|
+
const r = ret.type === 'expression_statement' ? ret.namedChild(0) : ret;
|
|
205
|
+
if (r?.type === 'return_expression')
|
|
206
|
+
return r.namedChildren.find((n) => n !== null && !isComment(n)) ?? null;
|
|
207
|
+
return r;
|
|
208
|
+
},
|
|
209
|
+
identifierTypes: new Set(['identifier', 'field_identifier', 'type_identifier', 'shorthand_field_identifier']),
|
|
210
|
+
declarationsIn: (container) => {
|
|
211
|
+
const out = [];
|
|
212
|
+
for (const child of container.namedChildren) {
|
|
213
|
+
if (!child || isComment(child))
|
|
214
|
+
continue;
|
|
215
|
+
if (child.type === 'use_declaration') {
|
|
216
|
+
// `use a::b::{C, D}` is keyed by its path (`a::b`), so re-sorting the braces is an edit of one import.
|
|
217
|
+
const arg = child.childForFieldName('argument');
|
|
218
|
+
const key = arg?.type === 'scoped_use_list' ? (arg.childForFieldName('path')?.text ?? arg.text) + '::{}' : arg?.text;
|
|
219
|
+
if (key)
|
|
220
|
+
out.push(importDeclaration(child, key));
|
|
221
|
+
}
|
|
222
|
+
else if (child.type === 'impl_item') {
|
|
223
|
+
// `impl Trait for Type` and `impl Type` are named by what they implement.
|
|
224
|
+
const type = child.childForFieldName('type')?.text ?? '';
|
|
225
|
+
const trait = child.childForFieldName('trait')?.text;
|
|
226
|
+
const body = child.childForFieldName('body') ?? undefined;
|
|
227
|
+
out.push({ node: child, kind: child.type, name: (trait ? `${trait} for ${type}` : type).replace(/\s+/g, ''), members: body });
|
|
228
|
+
}
|
|
229
|
+
else if (RUST_ITEMS.has(child.type)) {
|
|
230
|
+
const name = child.childForFieldName('name')?.text;
|
|
231
|
+
const body = child.childForFieldName('body');
|
|
232
|
+
const members = body?.type === 'declaration_list' ? body : undefined;
|
|
233
|
+
if (name)
|
|
234
|
+
out.push({ node: child, kind: child.type, name, members });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
/** Top-level-ish declarations shared by the C-family configs below: kind, name, and a body holding members. */
|
|
241
|
+
const named = (node, kind = node.type, name = node.childForFieldName('name')?.text) => {
|
|
242
|
+
if (!name)
|
|
243
|
+
return null;
|
|
244
|
+
const body = node.childForFieldName('body');
|
|
245
|
+
// Python class bodies are plain `block`s, like function bodies: only a class's block holds members.
|
|
246
|
+
const holds = body && (MEMBER_BODIES.has(body.type) || (body.type === 'block' && node.type === 'class_definition'));
|
|
247
|
+
return { node, kind, name, members: holds ? body : undefined };
|
|
248
|
+
};
|
|
249
|
+
const MEMBER_BODIES = new Set([
|
|
250
|
+
'class_body', 'interface_body', 'enum_body', 'annotation_type_body', // Java
|
|
251
|
+
'declaration_list', // C#, Rust and PHP
|
|
252
|
+
'enum_declaration_list', // PHP
|
|
253
|
+
]);
|
|
254
|
+
/** The first declared variable's name in a field or variable declaration. */
|
|
255
|
+
const firstDeclarator = (n) => {
|
|
256
|
+
const walk = (x) => {
|
|
257
|
+
if (x.type === 'variable_declarator')
|
|
258
|
+
return x;
|
|
259
|
+
for (const c of x.namedChildren) {
|
|
260
|
+
const r = c && walk(c);
|
|
261
|
+
if (r)
|
|
262
|
+
return r;
|
|
263
|
+
}
|
|
264
|
+
return null;
|
|
265
|
+
};
|
|
266
|
+
const d = walk(n);
|
|
267
|
+
return (d?.childForFieldName('name') ?? d?.namedChild(0))?.text;
|
|
268
|
+
};
|
|
269
|
+
const withoutKeyword = (text, keyword) => text.replace(new RegExp(`^\\s*(global\\s+)?${keyword}\\s+`), '').replace(/;\s*$/, '');
|
|
270
|
+
const python = {
|
|
271
|
+
id: 'python',
|
|
272
|
+
wasm: 'tree-sitter-python/tree-sitter-python.wasm',
|
|
273
|
+
functionTypes: new Set(['function_definition']),
|
|
274
|
+
callTypes: new Set(['call']),
|
|
275
|
+
blockTypes: new Set(['block']),
|
|
276
|
+
isReturn: (s) => s.type === 'return_statement',
|
|
277
|
+
attachedTypes: new Set(['comment']),
|
|
278
|
+
nameOf: (fn) => fn.childForFieldName('name')?.text ?? null,
|
|
279
|
+
bodyOf: (fn) => fn.childForFieldName('body'),
|
|
280
|
+
statementsOf: (block) => block.namedChildren.filter((n) => n !== null && !isComment(n)),
|
|
281
|
+
calleeOf: (call) => {
|
|
282
|
+
const f = call.childForFieldName('function');
|
|
283
|
+
if (!f)
|
|
284
|
+
return null;
|
|
285
|
+
return f.type === 'attribute' ? (f.childForFieldName('attribute')?.text ?? null) : f.text;
|
|
286
|
+
},
|
|
287
|
+
returnValueOf: (ret) => ret.namedChildren.find((n) => n !== null && !isComment(n)) ?? null,
|
|
288
|
+
identifierTypes: new Set(['identifier']),
|
|
289
|
+
declarationsIn: (container) => {
|
|
290
|
+
const out = [];
|
|
291
|
+
for (const child of container.namedChildren) {
|
|
292
|
+
if (!child || isComment(child))
|
|
293
|
+
continue;
|
|
294
|
+
if (child.type === 'import_statement') {
|
|
295
|
+
// `import a, b.c` is keyed by what it names.
|
|
296
|
+
out.push(importDeclaration(child, child.namedChildren.map((n) => n?.text ?? '').join(',')));
|
|
297
|
+
}
|
|
298
|
+
else if (child.type === 'import_from_statement') {
|
|
299
|
+
const module = child.childForFieldName('module_name')?.text;
|
|
300
|
+
if (module)
|
|
301
|
+
out.push(importDeclaration(child, module, 'from '));
|
|
302
|
+
}
|
|
303
|
+
else if (child.type === 'function_definition' || child.type === 'class_definition') {
|
|
304
|
+
const d = named(child);
|
|
305
|
+
if (d)
|
|
306
|
+
out.push(d);
|
|
307
|
+
}
|
|
308
|
+
else if (child.type === 'decorated_definition') {
|
|
309
|
+
// The decorators belong to the definition: the whole thing is the entity.
|
|
310
|
+
const def = child.childForFieldName('definition');
|
|
311
|
+
const d = def && named(def);
|
|
312
|
+
if (d)
|
|
313
|
+
out.push({ ...d, node: child });
|
|
314
|
+
}
|
|
315
|
+
else if (child.type === 'expression_statement' && child.namedChild(0)?.type === 'assignment') {
|
|
316
|
+
// Module and class constants: `NAME = ...`.
|
|
317
|
+
const left = child.namedChild(0).childForFieldName('left');
|
|
318
|
+
if (left?.type === 'identifier')
|
|
319
|
+
out.push({ node: child, kind: 'assignment', name: left.text });
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return out;
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
/**
|
|
326
|
+
* Java and C#: the classes (and C# namespaces) that hold a line, outermost first, as a context for the decided
|
|
327
|
+
* rule: code moved into another class, or into or out of a nested class, now means something else (another
|
|
328
|
+
* `this`, another name for every caller), so it is an edit, not a move. A class's own first and last lines count
|
|
329
|
+
* as inside it, so a whole class moved stays a move; the parts of a C# partial class share one context.
|
|
330
|
+
*/
|
|
331
|
+
const classLines = new WeakMap();
|
|
332
|
+
function classContext(root, line, containers) {
|
|
333
|
+
const key = root.tree;
|
|
334
|
+
let lines = classLines.get(key);
|
|
335
|
+
if (!lines)
|
|
336
|
+
classLines.set(key, (lines = root.text.split('\n')));
|
|
337
|
+
const text = lines[line - 1] ?? '';
|
|
338
|
+
const column = Math.max(0, text.search(/\S/));
|
|
339
|
+
const names = [];
|
|
340
|
+
for (let p = root.descendantForPosition({ row: line - 1, column }, { row: line - 1, column: column + 1 }); p; p = p.parent) {
|
|
341
|
+
if (!containers.has(p.type))
|
|
342
|
+
continue;
|
|
343
|
+
// `Foo` and `Foo<T>` are different classes.
|
|
344
|
+
const params = p.namedChildren.find((c) => c?.type === 'type_parameter_list' || c?.type === 'type_parameters')?.namedChildren.filter((c) => c?.type === 'type_parameter').length ?? 0;
|
|
345
|
+
names.unshift(`${p.type.replace(/_declaration$/, '')} ${p.childForFieldName('name')?.text ?? ''}${params ? `\`${params}` : ''}`);
|
|
346
|
+
}
|
|
347
|
+
// A C# file-scoped `namespace X;` covers every line after it.
|
|
348
|
+
const scoped = root.namedChildren.find((c) => c?.type === 'file_scoped_namespace_declaration' && c.startPosition.row < line - 1);
|
|
349
|
+
if (scoped && !names.some((n) => n.startsWith('file_scoped_namespace')))
|
|
350
|
+
names.unshift(`namespace ${scoped.childForFieldName('name')?.text ?? ''}`);
|
|
351
|
+
return names.join(' > ');
|
|
352
|
+
}
|
|
353
|
+
const JAVA_CONTAINERS = new Set(['class_declaration', 'interface_declaration', 'enum_declaration', 'record_declaration', 'annotation_type_declaration']);
|
|
354
|
+
const CSHARP_CONTAINERS = new Set(['class_declaration', 'struct_declaration', 'interface_declaration', 'record_declaration', 'enum_declaration', 'namespace_declaration']);
|
|
355
|
+
/** C# types that can be written `partial`: all parts of one in a file are one type. */
|
|
356
|
+
const CSHARP_TYPES = new Set(['class_declaration', 'struct_declaration', 'interface_declaration', 'record_declaration']);
|
|
357
|
+
const java = {
|
|
358
|
+
id: 'java',
|
|
359
|
+
wasm: 'tree-sitter-java/tree-sitter-java.wasm',
|
|
360
|
+
functionTypes: new Set(['method_declaration', 'constructor_declaration']),
|
|
361
|
+
callTypes: new Set(['method_invocation']),
|
|
362
|
+
blockTypes: new Set(['block', 'constructor_body']),
|
|
363
|
+
isReturn: (s) => s.type === 'return_statement',
|
|
364
|
+
attachedTypes: new Set(['line_comment', 'block_comment']),
|
|
365
|
+
nameOf: (fn) => fn.childForFieldName('name')?.text ?? null,
|
|
366
|
+
bodyOf: (fn) => fn.childForFieldName('body'),
|
|
367
|
+
statementsOf: (block) => block.namedChildren.filter((n) => n !== null && !isComment(n)),
|
|
368
|
+
calleeOf: (call) => call.childForFieldName('name')?.text ?? null,
|
|
369
|
+
returnValueOf: (ret) => ret.namedChildren.find((n) => n !== null && !isComment(n)) ?? null,
|
|
370
|
+
identifierTypes: new Set(['identifier', 'type_identifier']),
|
|
371
|
+
declarationsIn: (container) => {
|
|
372
|
+
const out = [];
|
|
373
|
+
// An enum's methods and fields sit after its constants, in their own node.
|
|
374
|
+
const children = container.namedChildren.flatMap((c) => (c?.type === 'enum_body_declarations' ? c.namedChildren : [c]));
|
|
375
|
+
for (const child of children) {
|
|
376
|
+
if (!child || isComment(child))
|
|
377
|
+
continue;
|
|
378
|
+
if (child.type === 'import_declaration')
|
|
379
|
+
out.push(importDeclaration(child, withoutKeyword(child.text, 'import')));
|
|
380
|
+
else if (child.type === 'field_declaration' || child.type === 'constant_declaration') {
|
|
381
|
+
const name = firstDeclarator(child);
|
|
382
|
+
if (name)
|
|
383
|
+
out.push({ node: child, kind: 'field_declaration', name });
|
|
384
|
+
}
|
|
385
|
+
else if (child.type.endsWith('_declaration') && child.type !== 'package_declaration') {
|
|
386
|
+
const d = named(child);
|
|
387
|
+
if (d)
|
|
388
|
+
out.push(d);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return out;
|
|
392
|
+
},
|
|
393
|
+
contextAt: (root, line) => classContext(root, line, JAVA_CONTAINERS),
|
|
394
|
+
contextRuns: true,
|
|
395
|
+
};
|
|
396
|
+
const csharp = {
|
|
397
|
+
id: 'csharp',
|
|
398
|
+
wasm: 'tree-sitter-c-sharp/tree-sitter-c_sharp.wasm',
|
|
399
|
+
functionTypes: new Set(['method_declaration', 'constructor_declaration', 'local_function_statement']),
|
|
400
|
+
callTypes: new Set(['invocation_expression']),
|
|
401
|
+
blockTypes: new Set(['block']),
|
|
402
|
+
// An expression-bodied member (`int F(int x) => x + 1;`) returns its expression, like a
|
|
403
|
+
// one-line arrow in TypeScript: code pulled out into one is a single value moved.
|
|
404
|
+
isReturn: (s, last) => s.type === 'return_statement' || (!!last && s.type === 'arrow_expression_clause'),
|
|
405
|
+
attachedTypes: new Set(['comment']),
|
|
406
|
+
nameOf: (fn) => fn.childForFieldName('name')?.text ?? null,
|
|
407
|
+
bodyOf: (fn) => {
|
|
408
|
+
const b = fn.childForFieldName('body');
|
|
409
|
+
return b?.type === 'block' || b?.type === 'arrow_expression_clause' ? b : null;
|
|
410
|
+
},
|
|
411
|
+
statementsOf: (block) => (block.type === 'arrow_expression_clause' ? [block] : block.namedChildren.filter((n) => n !== null && !isComment(n))),
|
|
412
|
+
calleeOf: (call) => {
|
|
413
|
+
let f = call.childForFieldName('function');
|
|
414
|
+
if (f?.type === 'member_access_expression')
|
|
415
|
+
f = f.childForFieldName('name');
|
|
416
|
+
if (f?.type === 'generic_name')
|
|
417
|
+
f = f.namedChild(0);
|
|
418
|
+
return f?.text ?? null;
|
|
419
|
+
},
|
|
420
|
+
returnValueOf: (ret) => ret.namedChildren.find((n) => n !== null && !isComment(n)) ?? null, // `=> x` holds x too
|
|
421
|
+
identifierTypes: new Set(['identifier']),
|
|
422
|
+
declarationsIn: (container) => {
|
|
423
|
+
const out = [];
|
|
424
|
+
for (const child of container.namedChildren) {
|
|
425
|
+
if (!child || isComment(child))
|
|
426
|
+
continue;
|
|
427
|
+
if (child.type === 'using_directive')
|
|
428
|
+
out.push(importDeclaration(child, withoutKeyword(child.text, 'using')));
|
|
429
|
+
else if (child.type === 'field_declaration' || child.type === 'event_field_declaration') {
|
|
430
|
+
const name = firstDeclarator(child);
|
|
431
|
+
if (name)
|
|
432
|
+
out.push({ node: child, kind: 'field_declaration', name });
|
|
433
|
+
}
|
|
434
|
+
else if (child.type.endsWith('_declaration') && child.type !== 'file_scoped_namespace_declaration') {
|
|
435
|
+
const d = named(child);
|
|
436
|
+
if (d)
|
|
437
|
+
out.push(d);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return out;
|
|
441
|
+
},
|
|
442
|
+
contextAt: (root, line) => classContext(root, line, CSHARP_CONTAINERS),
|
|
443
|
+
contextRuns: true,
|
|
444
|
+
// Every class-like type gets one member scope by name, whether or not it is written `partial` on this side:
|
|
445
|
+
// a class that becomes partial (or stops being) keeps its members' pairing.
|
|
446
|
+
// `Foo` and `Foo<T>` are different types: the key counts type parameters.
|
|
447
|
+
typeOf: (d) => {
|
|
448
|
+
if (!CSHARP_TYPES.has(d.node.type))
|
|
449
|
+
return null;
|
|
450
|
+
const name = d.node.childForFieldName('name')?.text;
|
|
451
|
+
const params = d.node.namedChildren.find((c) => c?.type === 'type_parameter_list')?.namedChildren.filter((c) => c?.type === 'type_parameter').length ?? 0;
|
|
452
|
+
return name ? (params ? `${name}\`${params}` : name) : null;
|
|
453
|
+
},
|
|
454
|
+
};
|
|
455
|
+
/**
|
|
456
|
+
* PHP. The `php` grammar reads the HTML around `<?php ... ?>` as `text` (see stringInteriors in index.ts).
|
|
457
|
+
* Names are `name` nodes; a variable `$total` is `variable_name` holding the name `total`. Declarations after
|
|
458
|
+
* `namespace X;` are keyed `X\Name`, so two namespaces in one file never pair, and a move between them is an
|
|
459
|
+
* edit (contextAt). A trait use inside a class is a member keyed `use:A,B`; a group use `use A\{B, C}` is one
|
|
460
|
+
* import keyed `A\{}`, as Rust's `use a::{..}` is.
|
|
461
|
+
*/
|
|
462
|
+
const PHP_CALLS = new Set(['function_call_expression', 'member_call_expression', 'scoped_call_expression', 'nullsafe_member_call_expression']);
|
|
463
|
+
const PHP_TYPES = new Set(['function_definition', 'class_declaration', 'interface_declaration', 'trait_declaration', 'enum_declaration', 'method_declaration', 'enum_case']);
|
|
464
|
+
const phpName = (n) => n?.text.replace(/^\$/, '');
|
|
465
|
+
const phpNamespace = (n) => n.childForFieldName('name')?.text ?? '';
|
|
466
|
+
const phpUseKey = (n) => {
|
|
467
|
+
const kind = n.children.find((c) => c?.type === 'function' || c?.type === 'const')?.type;
|
|
468
|
+
const prefix = kind ? `${kind} ` : '';
|
|
469
|
+
const group = n.childForFieldName('body');
|
|
470
|
+
if (group)
|
|
471
|
+
return { key: `${n.namedChildren.find((c) => c?.type === 'namespace_name')?.text ?? ''}\\{}`, prefix };
|
|
472
|
+
return { key: n.namedChildren.filter((c) => c?.type === 'namespace_use_clause').map((c) => c.text).join(','), prefix };
|
|
473
|
+
};
|
|
474
|
+
const php = {
|
|
475
|
+
id: 'php',
|
|
476
|
+
wasm: 'tree-sitter-php/tree-sitter-php.wasm',
|
|
477
|
+
functionTypes: new Set(['function_definition', 'method_declaration']),
|
|
478
|
+
callTypes: PHP_CALLS,
|
|
479
|
+
blockTypes: new Set(['compound_statement']),
|
|
480
|
+
isReturn: (s) => s.type === 'return_statement',
|
|
481
|
+
attachedTypes: new Set(['comment']),
|
|
482
|
+
nameOf: (fn) => fn.childForFieldName('name')?.text ?? null,
|
|
483
|
+
bodyOf: (fn) => fn.childForFieldName('body'),
|
|
484
|
+
statementsOf: (block) => block.namedChildren.filter((n) => n !== null && !isComment(n)),
|
|
485
|
+
calleeOf: (call) => {
|
|
486
|
+
const f = call.type === 'function_call_expression' ? call.childForFieldName('function') : call.childForFieldName('name');
|
|
487
|
+
if (!f)
|
|
488
|
+
return null;
|
|
489
|
+
if (f.type === 'qualified_name')
|
|
490
|
+
return f.childForFieldName('name')?.text ?? f.text;
|
|
491
|
+
return f.type === 'name' ? f.text : null;
|
|
492
|
+
},
|
|
493
|
+
returnValueOf: (ret) => ret.namedChildren.find((n) => n !== null && !isComment(n)) ?? null,
|
|
494
|
+
identifierTypes: new Set(['name']),
|
|
495
|
+
declarationsIn: (container) => {
|
|
496
|
+
const out = [];
|
|
497
|
+
let ns = '';
|
|
498
|
+
const scoped = (name) => (ns && container.type === 'program' ? `${ns}\\${name}` : name);
|
|
499
|
+
for (const child of container.namedChildren) {
|
|
500
|
+
if (!child || isComment(child))
|
|
501
|
+
continue;
|
|
502
|
+
if (child.type === 'namespace_definition') {
|
|
503
|
+
const body = child.childForFieldName('body');
|
|
504
|
+
if (body)
|
|
505
|
+
out.push({ node: child, kind: 'namespace', name: phpNamespace(child), members: body });
|
|
506
|
+
else
|
|
507
|
+
ns = phpNamespace(child);
|
|
508
|
+
}
|
|
509
|
+
else if (child.type === 'namespace_use_declaration') {
|
|
510
|
+
const { key, prefix } = phpUseKey(child);
|
|
511
|
+
out.push(importDeclaration(child, key, prefix));
|
|
512
|
+
}
|
|
513
|
+
else if (child.type === 'use_declaration') {
|
|
514
|
+
out.push({ node: child, kind: 'use_declaration', name: `use:${child.namedChildren.filter((c) => c?.type === 'name' || c?.type === 'qualified_name').map((c) => c.text).join(',')}` });
|
|
515
|
+
}
|
|
516
|
+
else if (child.type === 'property_declaration') {
|
|
517
|
+
const name = phpName(child.namedChildren.find((c) => c?.type === 'property_element')?.childForFieldName('name'));
|
|
518
|
+
if (name)
|
|
519
|
+
out.push({ node: child, kind: child.type, name });
|
|
520
|
+
}
|
|
521
|
+
else if (child.type === 'const_declaration') {
|
|
522
|
+
const name = child.namedChildren.find((c) => c?.type === 'const_element')?.namedChildren.find((c) => c?.type === 'name')?.text;
|
|
523
|
+
if (name)
|
|
524
|
+
out.push({ node: child, kind: child.type, name: scoped(name) });
|
|
525
|
+
}
|
|
526
|
+
else if (PHP_TYPES.has(child.type)) {
|
|
527
|
+
const d = named(child, child.type, child.childForFieldName('name')?.text);
|
|
528
|
+
if (d)
|
|
529
|
+
out.push({ ...d, name: scoped(d.name) });
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return out;
|
|
533
|
+
},
|
|
534
|
+
contextAt: (root, line) => {
|
|
535
|
+
let ns = '';
|
|
536
|
+
for (const c of root.namedChildren) {
|
|
537
|
+
if (c?.type !== 'namespace_definition')
|
|
538
|
+
continue;
|
|
539
|
+
const start = c.startPosition.row + 1;
|
|
540
|
+
if (start > line)
|
|
541
|
+
break;
|
|
542
|
+
if (c.childForFieldName('body')) {
|
|
543
|
+
if (line <= c.endPosition.row + 1)
|
|
544
|
+
return phpNamespace(c);
|
|
545
|
+
}
|
|
546
|
+
else
|
|
547
|
+
ns = phpNamespace(c);
|
|
548
|
+
}
|
|
549
|
+
return ns;
|
|
550
|
+
},
|
|
551
|
+
};
|
|
552
|
+
/**
|
|
553
|
+
* Ruby (tree-sitter-ruby 0.23.1). Everything is an expression: a method returns its last one. A call
|
|
554
|
+
* with no receiver and no arguments (`total`) parses as a bare `identifier`; one naming no local
|
|
555
|
+
* variable is a call. A heredoc's body (`heredoc_body`) is a sibling after the statement that opens
|
|
556
|
+
* it, so a declaration owns the heredoc bodies that follow it (`tail`). A bare `private`, `protected`,
|
|
557
|
+
* `public` or `module_function` line changes every method below it: a move across one is an edit
|
|
558
|
+
* (contextAt). Reopened classes (`class Foo` twice) are keyed by occurrence and pair by similarity.
|
|
559
|
+
*/
|
|
560
|
+
const RUBY_BLOCKS = new Set(['body_statement', 'block_body', 'then', 'else', 'do', 'begin', 'ensure']);
|
|
561
|
+
/** Clauses of a body that are not its statements: they hold statements of their own. */
|
|
562
|
+
const RUBY_CLAUSES = new Set(['rescue', 'else', 'ensure']);
|
|
563
|
+
/** A last expression that is no value worth returning (a definition, a loop, a conditional one-liner). */
|
|
564
|
+
const RUBY_NOT_TAIL = new Set([
|
|
565
|
+
'method', 'singleton_method', 'class', 'module', 'singleton_class', 'heredoc_body', 'alias', 'undef',
|
|
566
|
+
'while', 'until', 'for', 'while_modifier', 'until_modifier', 'if_modifier', 'unless_modifier', 'return',
|
|
567
|
+
]);
|
|
568
|
+
const RUBY_VISIBILITY = /^(private|protected|public|module_function)$/;
|
|
569
|
+
const RUBY_SCOPES = new Set(['method', 'singleton_method', 'class', 'module', 'singleton_class', 'program']);
|
|
570
|
+
const RUBY_CONTAINERS = new Set(['class', 'module', 'singleton_class', 'program']);
|
|
571
|
+
const RUBY_PARAMETER_LISTS = new Set(['method_parameters', 'block_parameters', 'lambda_parameters', 'left_assignment_list', 'destructured_left_assignment', 'destructured_parameter', 'exception_variable']);
|
|
572
|
+
/** The identifier is a name being bound here: a parameter, an assignment target, a loop or rescue variable. */
|
|
573
|
+
function rubyBinds(id) {
|
|
574
|
+
const p = id.parent;
|
|
575
|
+
if (!p)
|
|
576
|
+
return false;
|
|
577
|
+
if (RUBY_PARAMETER_LISTS.has(p.type))
|
|
578
|
+
return true;
|
|
579
|
+
if (/parameter$/.test(p.type))
|
|
580
|
+
return p.childForFieldName('name')?.id === id.id;
|
|
581
|
+
if (p.type === 'assignment' || p.type === 'operator_assignment')
|
|
582
|
+
return p.childForFieldName('left')?.id === id.id;
|
|
583
|
+
if (p.type === 'for')
|
|
584
|
+
return p.childForFieldName('pattern')?.id === id.id;
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
587
|
+
/** A bare identifier names a local variable when its scope binds that name before it (lexically, as Ruby decides). */
|
|
588
|
+
function rubyLocal(id) {
|
|
589
|
+
let scope = id.parent;
|
|
590
|
+
while (scope && !RUBY_SCOPES.has(scope.type))
|
|
591
|
+
scope = scope.parent;
|
|
592
|
+
if (!scope)
|
|
593
|
+
return false;
|
|
594
|
+
const stack = [scope];
|
|
595
|
+
while (stack.length) {
|
|
596
|
+
const x = stack.pop();
|
|
597
|
+
if (x.startIndex >= id.startIndex)
|
|
598
|
+
continue;
|
|
599
|
+
if (x.id !== scope.id && RUBY_SCOPES.has(x.type) && x.type !== 'program')
|
|
600
|
+
continue;
|
|
601
|
+
if (x.type === 'identifier' && x.text === id.text && rubyBinds(x))
|
|
602
|
+
return true;
|
|
603
|
+
for (const c of x.namedChildren)
|
|
604
|
+
if (c)
|
|
605
|
+
stack.push(c);
|
|
606
|
+
}
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
/** The name a bare identifier calls, or null when it is not a call (a variable, a definition's name, a parameter). */
|
|
610
|
+
function rubyBareCall(id) {
|
|
611
|
+
const p = id.parent;
|
|
612
|
+
if (!p || rubyBinds(id))
|
|
613
|
+
return null;
|
|
614
|
+
if (p.type === 'call' && p.childForFieldName('method')?.id === id.id)
|
|
615
|
+
return null;
|
|
616
|
+
if ((p.type === 'method' || p.type === 'singleton_method') && p.childForFieldName('name')?.id === id.id)
|
|
617
|
+
return null;
|
|
618
|
+
if (/^(alias|undef|keyword_parameter|pair)$/.test(p.type))
|
|
619
|
+
return null;
|
|
620
|
+
if (RUBY_VISIBILITY.test(id.text))
|
|
621
|
+
return null;
|
|
622
|
+
return rubyLocal(id) ? null : id.text;
|
|
623
|
+
}
|
|
624
|
+
/** Heredoc bodies that follow a statement (after a comment on its last line) belong to it. */
|
|
625
|
+
function rubyTail(n) {
|
|
626
|
+
let tail;
|
|
627
|
+
for (let s = n.nextSibling; s; s = s.nextSibling) {
|
|
628
|
+
if (s.type === 'heredoc_body')
|
|
629
|
+
tail = s;
|
|
630
|
+
else if (!(isComment(s) && s.startPosition.row === (tail ?? n).endPosition.row))
|
|
631
|
+
break;
|
|
632
|
+
}
|
|
633
|
+
return tail;
|
|
634
|
+
}
|
|
635
|
+
const rubySymbols = (args) => (args?.namedChildren ?? []).filter((c) => !!c && !isComment(c)).map((c) => c.text.replace(/^:/, ''));
|
|
636
|
+
/** The visibility line a statement of a class body sits under (`public` when none). */
|
|
637
|
+
function rubyVisibility(body, before) {
|
|
638
|
+
let out = 'public';
|
|
639
|
+
for (const c of body.namedChildren) {
|
|
640
|
+
if (!c || c.startIndex >= before)
|
|
641
|
+
break;
|
|
642
|
+
if (c.type === 'identifier' && RUBY_VISIBILITY.test(c.text))
|
|
643
|
+
out = c.text;
|
|
644
|
+
}
|
|
645
|
+
return out;
|
|
646
|
+
}
|
|
647
|
+
const ruby = {
|
|
648
|
+
id: 'ruby',
|
|
649
|
+
wasm: 'tree-sitter-ruby/tree-sitter-ruby.wasm',
|
|
650
|
+
holdBack: new Set(['movedWithEdit']),
|
|
651
|
+
functionTypes: new Set(['method', 'singleton_method']),
|
|
652
|
+
callTypes: new Set(['call', 'identifier']),
|
|
653
|
+
blockTypes: RUBY_BLOCKS,
|
|
654
|
+
isReturn: (s, last) => s.type === 'return' || (last && !RUBY_NOT_TAIL.has(s.type) && !isComment(s)),
|
|
655
|
+
attachedTypes: new Set(['comment']),
|
|
656
|
+
nameOf: (fn) => fn.childForFieldName('name')?.text ?? null,
|
|
657
|
+
bodyOf: (fn) => fn.childForFieldName('body'),
|
|
658
|
+
// An endless method (`def area = w * h`) has one expression as its body.
|
|
659
|
+
statementsOf: (block) => RUBY_BLOCKS.has(block.type)
|
|
660
|
+
? block.namedChildren.filter((n) => n !== null && !isComment(n) && !(RUBY_CLAUSES.has(n.type) && (block.type === 'body_statement' || block.type === 'begin')))
|
|
661
|
+
: [block],
|
|
662
|
+
calleeOf: (call) => (call.type === 'identifier' ? rubyBareCall(call) : (call.childForFieldName('method')?.text ?? null)),
|
|
663
|
+
returnValueOf: (ret) => {
|
|
664
|
+
if (ret.type !== 'return')
|
|
665
|
+
return ret;
|
|
666
|
+
const args = ret.namedChildren.find((n) => n?.type === 'argument_list');
|
|
667
|
+
const values = args?.namedChildren.filter((n) => !!n && !isComment(n)) ?? [];
|
|
668
|
+
return values.length === 1 ? values[0] : (args ?? null);
|
|
669
|
+
},
|
|
670
|
+
identifierTypes: new Set(['identifier', 'constant', 'instance_variable', 'class_variable', 'global_variable']),
|
|
671
|
+
declarationsIn: (container) => {
|
|
672
|
+
const out = [];
|
|
673
|
+
const add = (d) => {
|
|
674
|
+
const tail = rubyTail(d.node);
|
|
675
|
+
out.push(tail ? { ...d, tail } : d);
|
|
676
|
+
};
|
|
677
|
+
for (const child of container.namedChildren) {
|
|
678
|
+
if (!child || isComment(child))
|
|
679
|
+
continue;
|
|
680
|
+
const t = child.type;
|
|
681
|
+
if (t === 'method') {
|
|
682
|
+
const name = child.childForFieldName('name')?.text;
|
|
683
|
+
if (name)
|
|
684
|
+
add({ node: child, kind: 'method', name });
|
|
685
|
+
}
|
|
686
|
+
else if (t === 'singleton_method') {
|
|
687
|
+
const name = child.childForFieldName('name')?.text;
|
|
688
|
+
if (name)
|
|
689
|
+
add({ node: child, kind: 'method', name: `${child.childForFieldName('object')?.text ?? 'self'}.${name}` });
|
|
690
|
+
}
|
|
691
|
+
else if (t === 'class' || t === 'module') {
|
|
692
|
+
const name = child.childForFieldName('name')?.text;
|
|
693
|
+
const body = child.childForFieldName('body');
|
|
694
|
+
if (name)
|
|
695
|
+
out.push({ node: child, kind: t, name, members: body ?? undefined });
|
|
696
|
+
}
|
|
697
|
+
else if (t === 'singleton_class') {
|
|
698
|
+
const body = child.childForFieldName('body');
|
|
699
|
+
out.push({ node: child, kind: t, name: child.childForFieldName('value')?.text ?? 'self', members: body ?? undefined });
|
|
700
|
+
}
|
|
701
|
+
else if (t === 'identifier' && RUBY_VISIBILITY.test(child.text)) {
|
|
702
|
+
// A bare `private` line holds its place among the methods: moving one across it is an edit.
|
|
703
|
+
out.push({ node: child, kind: child.text, name: child.text });
|
|
704
|
+
}
|
|
705
|
+
else if ((t === 'assignment' || t === 'operator_assignment') && child.childForFieldName('left')?.type === 'constant') {
|
|
706
|
+
add({ node: child, kind: 'constant', name: child.childForFieldName('left').text });
|
|
707
|
+
}
|
|
708
|
+
else if (t === 'call' && !child.childForFieldName('receiver')) {
|
|
709
|
+
const method = child.childForFieldName('method')?.text ?? '';
|
|
710
|
+
const args = child.childForFieldName('arguments');
|
|
711
|
+
const first = args?.namedChildren.find((c) => c && !isComment(c));
|
|
712
|
+
if (/^attr_(reader|writer|accessor)$/.test(method) || method === 'attr') {
|
|
713
|
+
add({ node: child, kind: method, name: rubySymbols(args).sort().join(',') });
|
|
714
|
+
}
|
|
715
|
+
else if (/^(include|extend|prepend)$/.test(method)) {
|
|
716
|
+
add(importDeclaration(child, rubySymbols(args).join(','), `${method} `));
|
|
717
|
+
}
|
|
718
|
+
else if ((method === 'require' || method === 'require_relative') && first) {
|
|
719
|
+
add(importDeclaration(child, first.text, `${method} `));
|
|
720
|
+
}
|
|
721
|
+
else if (method === 'define_method' && first && /^(simple_symbol|string)$/.test(first.type)) {
|
|
722
|
+
add({ node: child, kind: 'define_method', name: first.text.replace(/^:/, '').replace(/^(['"])(.*)\1$/, '$2') });
|
|
723
|
+
}
|
|
724
|
+
else if (RUBY_VISIBILITY.test(method) && (first?.type === 'method' || first?.type === 'singleton_method')) {
|
|
725
|
+
// `private def helper ... end`: the method, with its visibility written on its own line.
|
|
726
|
+
const name = first.childForFieldName('name')?.text;
|
|
727
|
+
if (name)
|
|
728
|
+
add({ node: child, kind: 'method', name: first.type === 'singleton_method' ? `self.${name}` : name });
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
return out;
|
|
733
|
+
},
|
|
734
|
+
contextAt: (root, line) => {
|
|
735
|
+
// The innermost class or module holding the line, and the visibility line in effect there.
|
|
736
|
+
const row = line - 1;
|
|
737
|
+
let container = root;
|
|
738
|
+
let body = root;
|
|
739
|
+
let statement = null;
|
|
740
|
+
let n = root;
|
|
741
|
+
const path = [];
|
|
742
|
+
while (n) {
|
|
743
|
+
let next = null;
|
|
744
|
+
for (const c of n.namedChildren)
|
|
745
|
+
if (c && c.startPosition.row <= row && row <= c.endPosition.row)
|
|
746
|
+
next = c;
|
|
747
|
+
if (!next)
|
|
748
|
+
break;
|
|
749
|
+
if (body && n.id === body.id)
|
|
750
|
+
statement = next;
|
|
751
|
+
if (RUBY_CONTAINERS.has(next.type)) {
|
|
752
|
+
container = next;
|
|
753
|
+
body = next.childForFieldName('body');
|
|
754
|
+
statement = null;
|
|
755
|
+
path.push(next.type === 'singleton_class' ? 'singleton' : 'class');
|
|
756
|
+
}
|
|
757
|
+
n = next;
|
|
758
|
+
}
|
|
759
|
+
if (!body || !statement)
|
|
760
|
+
return path.join('/');
|
|
761
|
+
// A visibility line means what it governs: the statements after it, up to the next one.
|
|
762
|
+
if (statement.type === 'identifier' && RUBY_VISIBILITY.test(statement.text)) {
|
|
763
|
+
const governed = [];
|
|
764
|
+
for (let s = statement.nextNamedSibling; s && !(s.type === 'identifier' && RUBY_VISIBILITY.test(s.text)); s = s.nextNamedSibling)
|
|
765
|
+
if (!isComment(s))
|
|
766
|
+
governed.push(s.text.split('\n')[0].trim());
|
|
767
|
+
return `${path.join('/')}:${rubyVisibility(body, statement.startIndex)}>${statement.text}:${governed.join('\u0000')}`;
|
|
768
|
+
}
|
|
769
|
+
return `${path.join('/')}:${container.type === 'singleton_class' ? 'singleton-' : ''}${rubyVisibility(body, statement.startIndex)}`;
|
|
770
|
+
},
|
|
771
|
+
};
|
|
772
|
+
/**
|
|
773
|
+
* Kotlin (@tree-sitter-grammars/tree-sitter-kotlin 1.1.0). Few fields: a function's name is the
|
|
774
|
+
* `name` field, everything else is found by node type. Identifiers are `identifier`, types too.
|
|
775
|
+
* - An extension's receiver type comes before its name: `fun String.shout()` is keyed `String.shout`,
|
|
776
|
+
* `val Int.isEven` is keyed `Int.isEven`, so `A.f` and `B.f` never pair.
|
|
777
|
+
* - Functions and secondary constructors carry their parameter types in their kind: overloads pair
|
|
778
|
+
* with their own partner, and a changed parameter type is a different function (never a rename).
|
|
779
|
+
* - A companion object is keyed `companion` (`companion Name` when named) and holds members.
|
|
780
|
+
* - contextAt: a move into or out of a class, object or companion changes how the code is called: an edit.
|
|
781
|
+
* - positionAt: primary-constructor parameters, function parameters and enum entries mean their position
|
|
782
|
+
* (`componentN()`, positional calls, `ordinal`): a move that changes it is an edit; re-wrapping is not.
|
|
783
|
+
* - One-line class bodies (`class A { val x = 1 }`) end in a missing `_class_member_semi` on a correct tree.
|
|
784
|
+
*/
|
|
785
|
+
const KT_TYPES = new Set(['user_type', 'nullable_type', 'function_type', 'parenthesized_type', 'not_nullable_type', 'dynamic']);
|
|
786
|
+
const KT_CONTAINERS = { class_declaration: 'class', object_declaration: 'object', companion_object: 'companion' };
|
|
787
|
+
const KT_ORDERED = ['class_parameter', 'parameter', 'enum_entry'];
|
|
788
|
+
const ktCompact = (s) => s.replace(/\s+/g, '');
|
|
789
|
+
/** The type written before a declaration's name (`String` in `fun String.shout()`), if any. */
|
|
790
|
+
export const kotlinReceiver = (n) => {
|
|
791
|
+
const name = n.type === 'property_declaration' ? n.namedChildren.find((c) => c?.type === 'variable_declaration' || c?.type === 'multi_variable_declaration') : n.childForFieldName('name');
|
|
792
|
+
let found = null;
|
|
793
|
+
for (const c of n.namedChildren) {
|
|
794
|
+
if (!c || (name && c.startIndex >= name.startIndex))
|
|
795
|
+
break;
|
|
796
|
+
if (KT_TYPES.has(c.type))
|
|
797
|
+
found = c;
|
|
798
|
+
}
|
|
799
|
+
return found;
|
|
800
|
+
};
|
|
801
|
+
const ktParameterTypes = (n) => {
|
|
802
|
+
const list = n.namedChildren.find((c) => c?.type === 'function_value_parameters');
|
|
803
|
+
if (!list)
|
|
804
|
+
return '';
|
|
805
|
+
const out = [];
|
|
806
|
+
let vararg = false;
|
|
807
|
+
for (const c of list.namedChildren) {
|
|
808
|
+
if (c?.type === 'parameter_modifiers')
|
|
809
|
+
vararg ||= /\bvararg\b/.test(c.text);
|
|
810
|
+
else if (c?.type === 'parameter') {
|
|
811
|
+
const type = c.namedChildren.find((t) => !!t && KT_TYPES.has(t.type));
|
|
812
|
+
out.push((vararg ? 'vararg ' : '') + ktCompact(type?.text ?? '?'));
|
|
813
|
+
vararg = false;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
return out.join(',');
|
|
817
|
+
};
|
|
818
|
+
const ktMembers = (n) => n.namedChildren.find((c) => c?.type === 'class_body' || c?.type === 'enum_class_body') ?? undefined;
|
|
819
|
+
const ktClassKind = (n) => {
|
|
820
|
+
if (n.children.some((c) => c?.type === 'interface'))
|
|
821
|
+
return 'interface';
|
|
822
|
+
const mods = n.namedChildren.find((c) => c?.type === 'modifiers');
|
|
823
|
+
return mods?.namedChildren.some((m) => m?.type === 'class_modifier' && m.text === 'enum') ? 'enum class' : 'class';
|
|
824
|
+
};
|
|
825
|
+
const ktImportKey = (n) => {
|
|
826
|
+
const path = n.namedChildren.find((c) => c?.type === 'qualified_identifier')?.text ?? '';
|
|
827
|
+
const alias = n.namedChildren.find((c) => c?.type === 'identifier')?.text;
|
|
828
|
+
const star = n.children.some((c) => c?.type === '*') ? '.*' : '';
|
|
829
|
+
return `${path}${star}${alias ? `:${alias}` : ''}`;
|
|
830
|
+
};
|
|
831
|
+
const ktContexts = new WeakMap();
|
|
832
|
+
const kotlinContexts = (root) => {
|
|
833
|
+
const cached = ktContexts.get(root.tree);
|
|
834
|
+
if (cached)
|
|
835
|
+
return cached;
|
|
836
|
+
const out = [];
|
|
837
|
+
for (const n of root.descendantsOfType([...Object.keys(KT_CONTAINERS), ...KT_ORDERED])) {
|
|
838
|
+
if (!n)
|
|
839
|
+
continue;
|
|
840
|
+
let label = KT_CONTAINERS[n.type];
|
|
841
|
+
if (!label) {
|
|
842
|
+
const siblings = n.parent?.namedChildren.filter((c) => c?.type === n.type) ?? [];
|
|
843
|
+
label = `${n.type}:${siblings.findIndex((c) => c?.id === n.id)}`;
|
|
844
|
+
}
|
|
845
|
+
out.push({ start: n.startPosition.row + 1, end: n.endPosition.row + 1, label, ordered: !KT_CONTAINERS[n.type] });
|
|
846
|
+
}
|
|
847
|
+
ktContexts.set(root.tree, out);
|
|
848
|
+
return out;
|
|
849
|
+
};
|
|
850
|
+
const kotlin = {
|
|
851
|
+
id: 'kotlin',
|
|
852
|
+
wasm: '@tree-sitter-grammars/tree-sitter-kotlin/tree-sitter-kotlin.wasm',
|
|
853
|
+
functionTypes: new Set(['function_declaration']),
|
|
854
|
+
callTypes: new Set(['call_expression']),
|
|
855
|
+
blockTypes: new Set(['block']),
|
|
856
|
+
// An expression body (`fun f() = x`) returns its expression, like C#'s `=> x`.
|
|
857
|
+
isReturn: (s, last) => s.type === 'return_expression' || (!!last && s.type === 'function_body'),
|
|
858
|
+
attachedTypes: new Set(['line_comment', 'block_comment']),
|
|
859
|
+
nameOf: (fn) => fn.childForFieldName('name')?.text ?? null,
|
|
860
|
+
bodyOf: (fn) => {
|
|
861
|
+
const body = fn.namedChildren.find((c) => c?.type === 'function_body');
|
|
862
|
+
if (!body)
|
|
863
|
+
return null;
|
|
864
|
+
return body.namedChildren.find((c) => c?.type === 'block') ?? body;
|
|
865
|
+
},
|
|
866
|
+
statementsOf: (block) => (block.type === 'function_body' ? [block] : block.namedChildren.filter((n) => n !== null && !isComment(n))),
|
|
867
|
+
calleeOf: (call) => {
|
|
868
|
+
const f = call.namedChild(0);
|
|
869
|
+
if (f?.type === 'identifier')
|
|
870
|
+
return f.text;
|
|
871
|
+
if (f?.type === 'navigation_expression') {
|
|
872
|
+
const last = f.namedChildren.at(-1);
|
|
873
|
+
return last?.type === 'identifier' ? last.text : null;
|
|
874
|
+
}
|
|
875
|
+
return null;
|
|
876
|
+
},
|
|
877
|
+
returnValueOf: (ret) => {
|
|
878
|
+
const label = ret.childForFieldName('label');
|
|
879
|
+
return ret.namedChildren.find((n) => n !== null && !isComment(n) && n.id !== label?.id) ?? null;
|
|
880
|
+
},
|
|
881
|
+
identifierTypes: new Set(['identifier']),
|
|
882
|
+
harmlessMissing: new Set(['_class_member_semi']),
|
|
883
|
+
contextRuns: true,
|
|
884
|
+
holdBack: new Set(['movedWithEdit']),
|
|
885
|
+
declarationsIn: (container) => {
|
|
886
|
+
const out = [];
|
|
887
|
+
for (const child of container.namedChildren) {
|
|
888
|
+
if (!child || isComment(child))
|
|
889
|
+
continue;
|
|
890
|
+
const receiver = kotlinReceiver(child);
|
|
891
|
+
const on = receiver ? `${ktCompact(receiver.text)}.` : '';
|
|
892
|
+
if (child.type === 'import')
|
|
893
|
+
out.push(importDeclaration(child, ktImportKey(child)));
|
|
894
|
+
else if (child.type === 'function_declaration') {
|
|
895
|
+
const name = child.childForFieldName('name')?.text;
|
|
896
|
+
if (name)
|
|
897
|
+
out.push({ node: child, kind: `function(${ktParameterTypes(child)})`, name: on + name });
|
|
898
|
+
}
|
|
899
|
+
else if (child.type === 'secondary_constructor')
|
|
900
|
+
out.push({ node: child, kind: `constructor(${ktParameterTypes(child)})`, name: 'constructor' });
|
|
901
|
+
else if (child.type === 'anonymous_initializer')
|
|
902
|
+
out.push({ node: child, kind: 'init', name: 'init' });
|
|
903
|
+
else if (child.type === 'property_declaration') {
|
|
904
|
+
const v = child.namedChildren.find((c) => c?.type === 'variable_declaration' || c?.type === 'multi_variable_declaration');
|
|
905
|
+
const names = v?.type === 'variable_declaration' ? v.namedChild(0)?.text : v && `(${v.namedChildren.map((d) => d?.namedChild(0)?.text ?? '').join(',')})`;
|
|
906
|
+
if (names)
|
|
907
|
+
out.push({ node: child, kind: 'property', name: on + names });
|
|
908
|
+
}
|
|
909
|
+
else if (child.type === 'class_declaration' || child.type === 'object_declaration') {
|
|
910
|
+
const name = child.childForFieldName('name')?.text;
|
|
911
|
+
if (name)
|
|
912
|
+
out.push({ node: child, kind: child.type === 'object_declaration' ? 'object' : ktClassKind(child), name, members: ktMembers(child) });
|
|
913
|
+
}
|
|
914
|
+
else if (child.type === 'companion_object') {
|
|
915
|
+
const name = child.childForFieldName('name')?.text;
|
|
916
|
+
out.push({ node: child, kind: 'companion', name: name ? `companion ${name}` : 'companion', members: ktMembers(child) });
|
|
917
|
+
}
|
|
918
|
+
else if (child.type === 'type_alias') {
|
|
919
|
+
const name = child.childForFieldName('type')?.text;
|
|
920
|
+
if (name)
|
|
921
|
+
out.push({ node: child, kind: 'type_alias', name });
|
|
922
|
+
}
|
|
923
|
+
else if (child.type === 'enum_entry') {
|
|
924
|
+
const name = child.namedChild(0)?.text;
|
|
925
|
+
if (name)
|
|
926
|
+
out.push({ node: child, kind: 'enum_entry', name });
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
return out;
|
|
930
|
+
},
|
|
931
|
+
contextAt: (root, line) => kotlinContexts(root)
|
|
932
|
+
.filter((c) => !c.ordered && c.start <= line && line <= c.end)
|
|
933
|
+
.map((c) => c.label)
|
|
934
|
+
.join('/'),
|
|
935
|
+
positionAt: (root, line) => kotlinContexts(root)
|
|
936
|
+
.filter((c) => c.start <= line && line <= c.end)
|
|
937
|
+
.map((c) => c.label)
|
|
938
|
+
.join('/'),
|
|
939
|
+
};
|
|
940
|
+
/**
|
|
941
|
+
* Swift (tree-sitter-swift 0.7.3, the GitHub release wasm; see vendor/tree-sitter-swift). One node type,
|
|
942
|
+
* `class_declaration`, holds class, struct, actor, enum and extension; `declaration_kind` says which.
|
|
943
|
+
* Functions are keyed by name, argument labels and parameter types (`scaled(by:Double)`), kept in the
|
|
944
|
+
* kind so overloads pair with their own partner and a changed parameter type is never a rename. The
|
|
945
|
+
* grammar reuses the `name` field for types (a parameter's type, a function's return type): only the
|
|
946
|
+
* first `name` child is the name. Extensions are keyed by what they extend, conform to and require.
|
|
947
|
+
* Names inside attributes (`@State`, `@AppStorage("k")`) are never names: swapping a wrapper is an edit.
|
|
948
|
+
* A move into or out of a constrained extension, or into another `#if` branch, is an edit (contextAt).
|
|
949
|
+
*/
|
|
950
|
+
const SWIFT_NOT_EXPRESSION = /declaration|statement|assignment|directive|^diagnostic$/;
|
|
951
|
+
const swiftParams = (fn) => fn.namedChildren
|
|
952
|
+
.filter((c) => c?.type === 'parameter')
|
|
953
|
+
.map((p) => {
|
|
954
|
+
const label = (p.childForFieldName('external_name') ?? p.childForFieldName('name'))?.text ?? '_';
|
|
955
|
+
const type = p.text.slice(p.text.indexOf(':') + 1).replace(/\s+/g, '');
|
|
956
|
+
return `${label}:${type}`;
|
|
957
|
+
})
|
|
958
|
+
.join(',');
|
|
959
|
+
const swiftBound = (n) => {
|
|
960
|
+
if (!n)
|
|
961
|
+
return undefined;
|
|
962
|
+
const direct = n.childForFieldName('bound_identifier');
|
|
963
|
+
if (direct)
|
|
964
|
+
return direct.text;
|
|
965
|
+
for (const c of n.namedChildren) {
|
|
966
|
+
const r = c && c.type !== 'value_binding_pattern' ? (c.type === 'simple_identifier' ? c.text : swiftBound(c)) : undefined;
|
|
967
|
+
if (r)
|
|
968
|
+
return r;
|
|
969
|
+
}
|
|
970
|
+
return undefined;
|
|
971
|
+
};
|
|
972
|
+
const swiftDeclKind = (n) => n.childForFieldName('declaration_kind')?.text ?? n.type;
|
|
973
|
+
const swiftExtended = (n) => {
|
|
974
|
+
const body = n.childForFieldName('body');
|
|
975
|
+
const name = n.childForFieldName('name');
|
|
976
|
+
if (!name)
|
|
977
|
+
return '';
|
|
978
|
+
const end = body ? body.startIndex : n.endIndex;
|
|
979
|
+
return n.text.slice(name.startIndex - n.startIndex, end - n.startIndex).replace(/\/\/[^\n]*|\/\*[\s\S]*?\*\//g, '').replace(/\s+/g, ' ').trim();
|
|
980
|
+
};
|
|
981
|
+
/** An extension that applies only under conditions: a `where` clause, or generic arguments (`extension Array<Circle>`). */
|
|
982
|
+
const swiftConstrained = (n) => n.type === 'class_declaration' && swiftDeclKind(n) === 'extension' && (n.namedChildren.some((c) => c?.type === 'type_constraints') || !!n.childForFieldName('name')?.descendantsOfType('type_arguments').length);
|
|
983
|
+
const SWIFT_TYPES = new Set(['class_declaration', 'protocol_declaration']);
|
|
984
|
+
const SWIFT_FUNCTIONS = new Set(['function_declaration', 'protocol_function_declaration', 'init_declaration', 'subscript_declaration']);
|
|
985
|
+
const SWIFT_NAMED = new Set(['typealias_declaration', 'associatedtype_declaration', 'precedence_group_declaration', 'macro_declaration']);
|
|
986
|
+
/** Per tree: each line's `#if` branch, as the conditions taken so far (`#if os(Linux) / #else`). */
|
|
987
|
+
const swiftBranches = new WeakMap();
|
|
988
|
+
function swiftBranchAt(root, line) {
|
|
989
|
+
const key = root.tree;
|
|
990
|
+
let at = swiftBranches.get(key);
|
|
991
|
+
if (!at) {
|
|
992
|
+
at = [];
|
|
993
|
+
const rows = root.endPosition.row + 2;
|
|
994
|
+
const marks = new Map();
|
|
995
|
+
for (const d of root.descendantsOfType('directive')) {
|
|
996
|
+
const m = d && /^#\s*(if|elseif|else|endif)\b(.*)$/s.exec(d.text.trim());
|
|
997
|
+
if (d && m)
|
|
998
|
+
marks.set(d.startPosition.row + 1, `${m[1]} ${m[2].replace(/\s+/g, ' ').trim()}`.trim());
|
|
999
|
+
}
|
|
1000
|
+
const stack = [];
|
|
1001
|
+
const show = () => stack.map((arms) => arms.join(' / ')).join(' > ');
|
|
1002
|
+
for (let l = 1; l <= rows; l++) {
|
|
1003
|
+
const mark = marks.get(l);
|
|
1004
|
+
if (mark?.startsWith('if'))
|
|
1005
|
+
stack.push([`#${mark}`]);
|
|
1006
|
+
else if (mark && mark !== 'endif' && stack.length)
|
|
1007
|
+
stack.at(-1).push(`#${mark}`);
|
|
1008
|
+
at[l] = show();
|
|
1009
|
+
if (mark === 'endif')
|
|
1010
|
+
stack.pop();
|
|
1011
|
+
}
|
|
1012
|
+
swiftBranches.set(key, at);
|
|
1013
|
+
}
|
|
1014
|
+
return at[line] ?? '';
|
|
1015
|
+
}
|
|
1016
|
+
const swift = {
|
|
1017
|
+
id: 'swift',
|
|
1018
|
+
wasm: 'tree-sitter-swift/tree-sitter-swift.wasm',
|
|
1019
|
+
functionTypes: new Set(['function_declaration', 'init_declaration', 'deinit_declaration', 'subscript_declaration', 'property_declaration']),
|
|
1020
|
+
callTypes: new Set(['call_expression']),
|
|
1021
|
+
blockTypes: new Set(['statements']),
|
|
1022
|
+
isReturn: (s, last) => {
|
|
1023
|
+
if (s.type === 'control_transfer_statement')
|
|
1024
|
+
return s.child(0)?.type === 'return';
|
|
1025
|
+
// A body of one expression returns it: `func total() -> Int { sum(prices) }`.
|
|
1026
|
+
if (!last || SWIFT_NOT_EXPRESSION.test(s.type) || s.parent?.type !== 'statements')
|
|
1027
|
+
return false;
|
|
1028
|
+
return s.parent.namedChildren.filter((c) => c && !isComment(c)).length === 1 && /^(function_body|computed_getter|computed_property)$/.test(s.parent.parent?.type ?? '');
|
|
1029
|
+
},
|
|
1030
|
+
attachedTypes: new Set(['comment', 'multiline_comment']),
|
|
1031
|
+
nameOf: (fn) => {
|
|
1032
|
+
if (fn.type === 'property_declaration')
|
|
1033
|
+
return swiftBound(fn.childForFieldName('name')) ?? null;
|
|
1034
|
+
if (fn.type === 'init_declaration')
|
|
1035
|
+
return 'init';
|
|
1036
|
+
if (fn.type === 'deinit_declaration')
|
|
1037
|
+
return 'deinit';
|
|
1038
|
+
if (fn.type === 'subscript_declaration')
|
|
1039
|
+
return 'subscript';
|
|
1040
|
+
return fn.childForFieldName('name')?.text ?? null;
|
|
1041
|
+
},
|
|
1042
|
+
bodyOf: (fn) => {
|
|
1043
|
+
if (fn.type === 'property_declaration' || fn.type === 'subscript_declaration') {
|
|
1044
|
+
// The getter of a computed property: `var area: Double { ... }` or `{ get { ... } set { ... } }`.
|
|
1045
|
+
const computed = fn.type === 'property_declaration' ? fn.childForFieldName('computed_value') : fn.namedChildren.find((c) => c?.type === 'computed_property');
|
|
1046
|
+
if (!computed)
|
|
1047
|
+
return null;
|
|
1048
|
+
return computed.namedChildren.find((c) => c?.type === 'computed_getter') ?? (computed.namedChildren.some((c) => c?.type === 'statements') ? computed : null);
|
|
1049
|
+
}
|
|
1050
|
+
return fn.childForFieldName('body');
|
|
1051
|
+
},
|
|
1052
|
+
statementsOf: (block) => {
|
|
1053
|
+
const holder = block.type === 'statements' ? block : block.namedChildren.find((c) => c?.type === 'statements');
|
|
1054
|
+
return holder ? holder.namedChildren.filter((n) => n !== null && !isComment(n)) : [];
|
|
1055
|
+
},
|
|
1056
|
+
calleeOf: (call) => {
|
|
1057
|
+
const f = call.namedChild(0);
|
|
1058
|
+
if (f?.type === 'simple_identifier')
|
|
1059
|
+
return f.text;
|
|
1060
|
+
if (f?.type === 'navigation_expression') {
|
|
1061
|
+
const s = f.childForFieldName('suffix')?.childForFieldName('suffix');
|
|
1062
|
+
return s?.type === 'simple_identifier' ? s.text : null;
|
|
1063
|
+
}
|
|
1064
|
+
return null;
|
|
1065
|
+
},
|
|
1066
|
+
returnValueOf: (ret) => (ret.type === 'control_transfer_statement' ? ret.childForFieldName('result') : ret),
|
|
1067
|
+
identifierTypes: new Set(['simple_identifier', 'type_identifier']),
|
|
1068
|
+
notAName: (id) => {
|
|
1069
|
+
for (let p = id.parent, depth = 0; p && depth < 8; p = p.parent, depth++) {
|
|
1070
|
+
if (p.type === 'attribute')
|
|
1071
|
+
return true;
|
|
1072
|
+
if (/declaration|statements|^source_file$|body$/.test(p.type))
|
|
1073
|
+
return false;
|
|
1074
|
+
}
|
|
1075
|
+
return false;
|
|
1076
|
+
},
|
|
1077
|
+
declarationsIn: (container) => {
|
|
1078
|
+
const out = [];
|
|
1079
|
+
for (const child of container.namedChildren) {
|
|
1080
|
+
if (!child || isComment(child))
|
|
1081
|
+
continue;
|
|
1082
|
+
if (child.type === 'import_declaration') {
|
|
1083
|
+
// `@testable import App` and `import App` are different imports; so are `import struct M.X` and `import M.X`.
|
|
1084
|
+
const id = child.namedChildren.find((c) => c?.type === 'identifier');
|
|
1085
|
+
if (!id)
|
|
1086
|
+
continue;
|
|
1087
|
+
const before = child.text.slice(0, id.startIndex - child.startIndex).replace(/\bimport\b/, '').replace(/\s+/g, ' ').trim();
|
|
1088
|
+
out.push(importDeclaration(child, id.text, before ? `${before} ` : ''));
|
|
1089
|
+
}
|
|
1090
|
+
else if (SWIFT_TYPES.has(child.type)) {
|
|
1091
|
+
const kind = swiftDeclKind(child);
|
|
1092
|
+
const name = kind === 'extension' ? swiftExtended(child) : child.childForFieldName('name')?.text;
|
|
1093
|
+
const body = child.childForFieldName('body');
|
|
1094
|
+
if (name)
|
|
1095
|
+
out.push({ node: child, kind, name, members: body ?? undefined });
|
|
1096
|
+
}
|
|
1097
|
+
else if (SWIFT_FUNCTIONS.has(child.type)) {
|
|
1098
|
+
const name = child.type === 'init_declaration' ? 'init' : child.type === 'subscript_declaration' ? 'subscript' : child.childForFieldName('name')?.text;
|
|
1099
|
+
if (name)
|
|
1100
|
+
out.push({ node: child, kind: `${child.type === 'protocol_function_declaration' ? 'requirement ' : ''}func(${swiftParams(child)})`, name });
|
|
1101
|
+
}
|
|
1102
|
+
else if (child.type === 'deinit_declaration') {
|
|
1103
|
+
out.push({ node: child, kind: 'deinit', name: 'deinit' });
|
|
1104
|
+
}
|
|
1105
|
+
else if (child.type === 'property_declaration' || child.type === 'protocol_property_declaration') {
|
|
1106
|
+
const name = swiftBound(child.childForFieldName('name'));
|
|
1107
|
+
if (name)
|
|
1108
|
+
out.push({ node: child, kind: child.type === 'property_declaration' ? 'property' : 'requirement property', name });
|
|
1109
|
+
}
|
|
1110
|
+
else if (child.type === 'enum_entry') {
|
|
1111
|
+
const names = child.childrenForFieldName('name').filter((c) => c?.type === 'simple_identifier').map((c) => c.text);
|
|
1112
|
+
if (names.length)
|
|
1113
|
+
out.push({ node: child, kind: 'case', name: names.join(',') });
|
|
1114
|
+
}
|
|
1115
|
+
else if (child.type === 'operator_declaration') {
|
|
1116
|
+
const op = child.namedChildren.find((c) => c?.type === 'custom_operator');
|
|
1117
|
+
const fixity = child.child(0)?.text ?? '';
|
|
1118
|
+
if (op)
|
|
1119
|
+
out.push({ node: child, kind: `${fixity} operator`, name: op.text });
|
|
1120
|
+
}
|
|
1121
|
+
else if (SWIFT_NAMED.has(child.type)) {
|
|
1122
|
+
const d = named(child, child.type, child.childForFieldName('name')?.text ?? child.namedChildren.find((c) => c?.type === 'type_identifier' || c?.type === 'simple_identifier')?.text);
|
|
1123
|
+
if (d)
|
|
1124
|
+
out.push(d);
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
return out;
|
|
1128
|
+
},
|
|
1129
|
+
// Moved-with-edits was wrong 2 times in 10 on the held-out sets: held back in v1, as in Ruby and Kotlin.
|
|
1130
|
+
holdBack: new Set(['movedWithEdit']),
|
|
1131
|
+
moveEditedFirst: true,
|
|
1132
|
+
// A whole `#if` block or extension moved with its directives and doc comment keeps its move.
|
|
1133
|
+
contextRuns: true,
|
|
1134
|
+
harmlessError: (n) => n.text.split('\n').every((l) => !l.trim() || /^\s*#\s*(if|elseif|else|endif)\b/.test(l)),
|
|
1135
|
+
marksOf: (decl) => decl.namedChildren.find((c) => c?.type === 'modifiers')?.namedChildren.filter((c) => c?.type === 'attribute').map((c) => c.text.replace(/\s+/g, '')).join(' ') ?? '',
|
|
1136
|
+
typeOf: (d) => {
|
|
1137
|
+
if (d.node.type !== 'class_declaration')
|
|
1138
|
+
return null;
|
|
1139
|
+
const name = d.node.childForFieldName('name');
|
|
1140
|
+
return name ? name.text.replace(/<[\s\S]*$/, '').replace(/\s+/g, '') : null;
|
|
1141
|
+
},
|
|
1142
|
+
contextAt: (root, line) => {
|
|
1143
|
+
// Inside a constrained extension, the code applies only where its conditions hold. The extension's
|
|
1144
|
+
// own first and last lines count as inside it, so a whole extension moved stays a move.
|
|
1145
|
+
const text = swiftLines(root)[line - 1] ?? '';
|
|
1146
|
+
const column = Math.max(0, text.search(/\S/));
|
|
1147
|
+
let ext = '';
|
|
1148
|
+
for (let p = root.descendantForPosition({ row: line - 1, column }, { row: line - 1, column: column + 1 }); p; p = p.parent) {
|
|
1149
|
+
if (swiftConstrained(p)) {
|
|
1150
|
+
ext = `extension ${swiftExtended(p)}`;
|
|
1151
|
+
break;
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
return `${swiftBranchAt(root, line)} | ${ext}`;
|
|
1155
|
+
},
|
|
1156
|
+
};
|
|
1157
|
+
const swiftLineCache = new WeakMap();
|
|
1158
|
+
function swiftLines(root) {
|
|
1159
|
+
const key = root.tree;
|
|
1160
|
+
let lines = swiftLineCache.get(key);
|
|
1161
|
+
if (!lines)
|
|
1162
|
+
swiftLineCache.set(key, (lines = root.text.split('\n')));
|
|
1163
|
+
return lines;
|
|
1164
|
+
}
|
|
1165
|
+
export const LANGUAGES = {
|
|
1166
|
+
go: {
|
|
1167
|
+
id: 'go',
|
|
1168
|
+
wasm: 'tree-sitter-go/tree-sitter-go.wasm',
|
|
1169
|
+
functionTypes: new Set(['function_declaration', 'method_declaration']),
|
|
1170
|
+
callTypes: new Set(['call_expression']),
|
|
1171
|
+
blockTypes: new Set(['block']),
|
|
1172
|
+
isReturn: (s) => s.type === 'return_statement',
|
|
1173
|
+
attachedTypes: new Set(['comment']),
|
|
1174
|
+
nameOf: (fn) => fn.childForFieldName('name')?.text ?? null,
|
|
1175
|
+
bodyOf: (fn) => fn.childForFieldName('body'),
|
|
1176
|
+
statementsOf: (block) => {
|
|
1177
|
+
const list = block.namedChildren.find((n) => n?.type === 'statement_list');
|
|
1178
|
+
const holder = list ?? block;
|
|
1179
|
+
return holder.namedChildren.filter((n) => n !== null && n.type !== 'comment');
|
|
1180
|
+
},
|
|
1181
|
+
calleeOf: (call) => {
|
|
1182
|
+
const f = call.childForFieldName('function');
|
|
1183
|
+
if (!f)
|
|
1184
|
+
return null;
|
|
1185
|
+
return f.type === 'selector_expression' ? (f.childForFieldName('field')?.text ?? null) : f.text;
|
|
1186
|
+
},
|
|
1187
|
+
returnValueOf: (ret) => {
|
|
1188
|
+
const list = ret.namedChildren.find((n) => n?.type === 'expression_list');
|
|
1189
|
+
// A single returned value: unwrap the expression list.
|
|
1190
|
+
if (list && list.namedChildCount === 1)
|
|
1191
|
+
return list.namedChild(0);
|
|
1192
|
+
return list ?? null;
|
|
1193
|
+
},
|
|
1194
|
+
identifierTypes: new Set(['identifier', 'field_identifier', 'type_identifier', 'package_identifier']),
|
|
1195
|
+
declarationsIn: (container) => {
|
|
1196
|
+
const out = [];
|
|
1197
|
+
for (const child of container.namedChildren) {
|
|
1198
|
+
if (!child || child.type === 'comment')
|
|
1199
|
+
continue;
|
|
1200
|
+
if (child.type === 'import_declaration') {
|
|
1201
|
+
// Each spec is its own import, whether grouped in `import ( ... )` or not.
|
|
1202
|
+
const list = child.namedChildren.find((n) => n?.type === 'import_spec_list') ?? child;
|
|
1203
|
+
for (const spec of list.namedChildren) {
|
|
1204
|
+
const path = spec?.type === 'import_spec' ? spec.childForFieldName('path') : null;
|
|
1205
|
+
if (spec && path)
|
|
1206
|
+
out.push(importDeclaration(spec, path.text));
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
else if (child.type === 'function_declaration') {
|
|
1210
|
+
const name = child.childForFieldName('name')?.text;
|
|
1211
|
+
if (name)
|
|
1212
|
+
out.push({ node: child, kind: child.type, name });
|
|
1213
|
+
}
|
|
1214
|
+
else if (child.type === 'method_declaration') {
|
|
1215
|
+
// Methods are keyed by receiver type too: (T).Name.
|
|
1216
|
+
const recv = child.childForFieldName('receiver')?.text ?? '';
|
|
1217
|
+
const name = child.childForFieldName('name')?.text;
|
|
1218
|
+
if (name)
|
|
1219
|
+
out.push({ node: child, kind: child.type, name: `${recv.replace(/^\(\s*\w+\s+/, '(')}.${name}` });
|
|
1220
|
+
}
|
|
1221
|
+
else if (['type_declaration', 'const_declaration', 'var_declaration'].includes(child.type)) {
|
|
1222
|
+
const spec = child.namedChildren.find((n) => n?.type.endsWith('_spec'));
|
|
1223
|
+
const name = spec?.childForFieldName('name')?.text ?? spec?.namedChild(0)?.text;
|
|
1224
|
+
if (name)
|
|
1225
|
+
out.push({ node: child, kind: child.type, name });
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
return out;
|
|
1229
|
+
},
|
|
1230
|
+
},
|
|
1231
|
+
typescript: tsLike('typescript', 'tree-sitter-typescript.wasm'),
|
|
1232
|
+
tsx: tsLike('tsx', 'tree-sitter-tsx.wasm'),
|
|
1233
|
+
rust,
|
|
1234
|
+
python,
|
|
1235
|
+
java,
|
|
1236
|
+
csharp,
|
|
1237
|
+
php,
|
|
1238
|
+
ruby,
|
|
1239
|
+
kotlin,
|
|
1240
|
+
swift,
|
|
1241
|
+
};
|
|
1242
|
+
let ready = null;
|
|
1243
|
+
const loaded = new Map();
|
|
1244
|
+
export async function getParser(id) {
|
|
1245
|
+
const setup = currentSetup();
|
|
1246
|
+
ready ??= Parser.init(setup.locateRuntime ? { locateFile: setup.locateRuntime } : undefined);
|
|
1247
|
+
await ready;
|
|
1248
|
+
let lang = loaded.get(id);
|
|
1249
|
+
if (!lang) {
|
|
1250
|
+
const file = LANGUAGES[id].wasm;
|
|
1251
|
+
const source = setup.locateGrammar ? await setup.locateGrammar(file, id) : await resolveInNode(file);
|
|
1252
|
+
lang = await Language.load(source instanceof URL ? source.href : source);
|
|
1253
|
+
loaded.set(id, lang);
|
|
1254
|
+
}
|
|
1255
|
+
const parser = new Parser();
|
|
1256
|
+
parser.setLanguage(lang);
|
|
1257
|
+
return parser;
|
|
1258
|
+
}
|
|
1259
|
+
/** Parse both versions, run `fn`, and always free the trees (they live in WASM memory). */
|
|
1260
|
+
export async function withTrees(id, oldText, newText, fn) {
|
|
1261
|
+
const parser = await getParser(id);
|
|
1262
|
+
const a = parser.parse(oldText);
|
|
1263
|
+
const b = parser.parse(newText);
|
|
1264
|
+
if (!a || !b)
|
|
1265
|
+
throw new Error('parse failed');
|
|
1266
|
+
try {
|
|
1267
|
+
return fn(a, b);
|
|
1268
|
+
}
|
|
1269
|
+
finally {
|
|
1270
|
+
a.delete();
|
|
1271
|
+
b.delete();
|
|
1272
|
+
parser.delete();
|
|
1273
|
+
}
|
|
1274
|
+
}
|