-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathswift.ts
More file actions
337 lines (313 loc) · 10.9 KB
/
Copy pathswift.ts
File metadata and controls
337 lines (313 loc) · 10.9 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import type {
Call,
ExtractorOutput,
SubDeclaration,
TreeSitterNode,
TreeSitterTree,
} from '../types.js';
import { extractModifierVisibility, findChild, nodeEndLine } from './helpers.js';
/**
* Extract symbols from Swift files.
*/
export function extractSwiftSymbols(tree: TreeSitterTree, _filePath: string): ExtractorOutput {
const ctx: ExtractorOutput = {
definitions: [],
calls: [],
imports: [],
classes: [],
exports: [],
typeMap: new Map(),
};
walkSwiftNode(tree.rootNode, ctx);
return ctx;
}
function walkSwiftNode(node: TreeSitterNode, ctx: ExtractorOutput): void {
switch (node.type) {
case 'class_declaration':
handleSwiftClassDecl(node, ctx);
break;
case 'protocol_declaration':
handleSwiftProtocolDecl(node, ctx);
break;
case 'function_declaration':
handleSwiftFunctionDecl(node, ctx);
break;
case 'import_declaration':
handleSwiftImportDecl(node, ctx);
break;
case 'call_expression':
handleSwiftCallExpression(node, ctx);
break;
case 'property_declaration':
seedSwiftPropertyTypeMap(node, ctx);
handleSwiftPropertyDecl(node, ctx);
break;
}
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i);
if (child) walkSwiftNode(child, ctx);
}
}
// ── Walk-path per-node-type handlers ────────────────────────────────────────
function hasKeywordChild(node: TreeSitterNode, keyword: string): boolean {
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i);
if (child && child.text === keyword) return true;
}
return false;
}
function handleSwiftClassDecl(node: TreeSitterNode, ctx: ExtractorOutput): void {
const isStruct = hasKeywordChild(node, 'struct');
const isEnum = hasKeywordChild(node, 'enum');
// Name is a type_identifier direct child
const nameNode = findChild(node, 'type_identifier');
if (!nameNode) return;
const name = nameNode.text;
const kind = isEnum ? 'enum' : isStruct ? 'struct' : 'class';
const children = isEnum ? collectSwiftEnumEntries(node) : collectSwiftProperties(node);
ctx.definitions.push({
name,
kind,
line: node.startPosition.row + 1,
endLine: nodeEndLine(node),
children: children.length > 0 ? children : undefined,
});
collectSwiftMethods(node, name, ctx);
collectSwiftInheritance(node, name, ctx);
}
/** Collect enum constant entries from an enum_class_body. */
function collectSwiftEnumEntries(node: TreeSitterNode): SubDeclaration[] {
const entries: SubDeclaration[] = [];
const body = findChild(node, 'enum_class_body');
if (!body) return entries;
for (let i = 0; i < body.childCount; i++) {
const child = body.child(i);
if (child?.type !== 'enum_entry') continue;
const entryName = findChild(child, 'simple_identifier');
if (entryName) {
entries.push({
name: entryName.text,
kind: 'constant',
line: child.startPosition.row + 1,
});
}
}
return entries;
}
/** Collect property declarations from a class_body. */
function collectSwiftProperties(node: TreeSitterNode): SubDeclaration[] {
const props: SubDeclaration[] = [];
const body = findChild(node, 'class_body');
if (!body) return props;
for (let i = 0; i < body.childCount; i++) {
const child = body.child(i);
if (child?.type !== 'property_declaration') continue;
const pattern = findChild(child, 'pattern');
if (!pattern) continue;
const propName = findChild(pattern, 'simple_identifier');
if (propName) {
props.push({
name: propName.text,
kind: 'property',
line: child.startPosition.row + 1,
visibility: extractModifierVisibility(child),
});
}
}
return props;
}
/** Collect method declarations from class_body or enum_class_body. */
function collectSwiftMethods(node: TreeSitterNode, className: string, ctx: ExtractorOutput): void {
const body = findChild(node, 'class_body') || findChild(node, 'enum_class_body');
if (!body) return;
for (let i = 0; i < body.childCount; i++) {
const child = body.child(i);
if (child?.type !== 'function_declaration') continue;
const methName = findChild(child, 'simple_identifier');
if (methName) {
ctx.definitions.push({
name: `${className}.${methName.text}`,
kind: 'method',
line: child.startPosition.row + 1,
endLine: child.endPosition.row + 1,
visibility: extractModifierVisibility(child),
});
}
}
}
/** Collect inheritance from inheritance_specifier children. First = extends, rest = implements. */
function collectSwiftInheritance(
node: TreeSitterNode,
className: string,
ctx: ExtractorOutput,
): void {
let first = true;
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i);
if (child?.type !== 'inheritance_specifier') continue;
const userType = findChild(child, 'user_type');
const typeId = userType ? findChild(userType, 'type_identifier') : null;
if (!typeId) continue;
if (first) {
ctx.classes.push({ name: className, extends: typeId.text, line: node.startPosition.row + 1 });
first = false;
} else {
ctx.classes.push({
name: className,
implements: typeId.text,
line: node.startPosition.row + 1,
});
}
}
}
function handleSwiftProtocolDecl(node: TreeSitterNode, ctx: ExtractorOutput): void {
const nameNode = findChild(node, 'type_identifier');
if (!nameNode) return;
const name = nameNode.text;
ctx.definitions.push({
name,
kind: 'interface',
line: node.startPosition.row + 1,
endLine: nodeEndLine(node),
});
// Methods inside protocol_body or class_body
const body = findChild(node, 'protocol_body') || findChild(node, 'class_body');
if (body) {
for (let i = 0; i < body.childCount; i++) {
const child = body.child(i);
if (child && child.type === 'function_declaration') {
const methName = findChild(child, 'simple_identifier');
if (methName) {
ctx.definitions.push({
name: `${name}.${methName.text}`,
kind: 'method',
line: child.startPosition.row + 1,
endLine: child.endPosition.row + 1,
});
}
}
}
}
}
function handleSwiftFunctionDecl(node: TreeSitterNode, ctx: ExtractorOutput): void {
// Skip methods already emitted by class/protocol handlers
if (
node.parent?.type === 'class_body' ||
node.parent?.type === 'protocol_body' ||
node.parent?.type === 'enum_class_body'
) {
if (
node.parent.parent?.type === 'class_declaration' ||
node.parent.parent?.type === 'protocol_declaration'
) {
return;
}
}
const nameNode = findChild(node, 'simple_identifier');
if (!nameNode) return;
ctx.definitions.push({
name: nameNode.text,
kind: 'function',
line: node.startPosition.row + 1,
endLine: nodeEndLine(node),
visibility: extractModifierVisibility(node),
});
}
function handleSwiftImportDecl(node: TreeSitterNode, ctx: ExtractorOutput): void {
const identNode = findChild(node, 'identifier');
if (!identNode) return;
const source = identNode.text;
ctx.imports.push({
source,
names: [source],
line: node.startPosition.row + 1,
swiftImport: true,
});
}
function handleSwiftCallExpression(node: TreeSitterNode, ctx: ExtractorOutput): void {
const funcNode = node.child(0);
if (!funcNode) return;
const call: Call = { name: '', line: node.startPosition.row + 1 };
if (funcNode.type === 'navigation_expression') {
// obj.method(...) — Swift's tree-sitter grammar wraps the suffix in a
// `navigation_suffix` node: navigation_expression > [simple_identifier, navigation_suffix].
// We must descend into navigation_suffix to get the bare method name.
// Mirrors Rust match_swift_node which reads node_text of the last child directly
// (which equals ".method") and the receiver from child(0).
const lastChild = funcNode.child(funcNode.childCount - 1);
const firstChild = funcNode.child(0);
if (lastChild && firstChild) {
// Resolve the method name: descend into navigation_suffix to find the
// simple_identifier, or fall back to stripping the leading dot from the text.
let methodName: string;
if (lastChild.type === 'simple_identifier') {
methodName = lastChild.text;
} else if (lastChild.type === 'navigation_suffix') {
const inner = findChild(lastChild, 'simple_identifier');
methodName = inner ? inner.text : lastChild.text.replace(/^\./, '');
} else {
methodName = lastChild.text;
}
call.name = methodName;
call.receiver = firstChild.text;
}
} else if (funcNode.type === 'simple_identifier') {
call.name = funcNode.text;
} else {
call.name = funcNode.text;
}
if (call.name) ctx.calls.push(call);
}
/**
* Seed the typeMap for a property_declaration with a type annotation.
* This runs for ALL property_declaration nodes (including class-body ones)
* so that `repo.method()` calls can be resolved to the correct class.
* Mirrors Rust match_swift_type_map which walks all nodes unconditionally.
*/
function seedSwiftPropertyTypeMap(node: TreeSitterNode, ctx: ExtractorOutput): void {
const typeAnn = findChild(node, 'type_annotation');
if (!typeAnn) return;
// type_annotation: ":" <user_type | simple_identifier | ...>
// The last child is the actual type node.
const lastChild = typeAnn.child(typeAnn.childCount - 1);
if (!lastChild) return;
// For "user_type > type_identifier", grab the inner identifier text;
// for a plain simple_identifier, use it directly.
const typeNode =
lastChild.type === 'user_type' ? findChild(lastChild, 'type_identifier') : lastChild;
if (!typeNode) return;
const typeName = typeNode.text;
if (!typeName) return;
const pattern = findChild(node, 'pattern');
if (!pattern) return;
const varName = findChild(pattern, 'simple_identifier')?.text ?? pattern.text;
if (!varName) return;
ctx.typeMap.set(varName, { type: typeName, confidence: 0.9 });
}
function handleSwiftPropertyDecl(node: TreeSitterNode, ctx: ExtractorOutput): void {
// Only handle top-level properties (class properties are handled inline)
if (
node.parent?.type === 'class_body' ||
node.parent?.type === 'protocol_body' ||
node.parent?.type === 'enum_class_body'
) {
return;
}
// Skip function-local let/var bindings
if (node.parent?.type === 'statements' || node.parent?.type === 'function_body') {
return;
}
const pattern = findChild(node, 'pattern');
if (!pattern) return;
const nameNode = findChild(pattern, 'simple_identifier');
if (!nameNode) return;
// let → constant, var → variable
const isLet = hasKeywordChild(node, 'let');
const kind = isLet ? 'constant' : 'variable';
ctx.definitions.push({
name: nameNode.text,
kind,
line: node.startPosition.row + 1,
endLine: nodeEndLine(node),
});
}