-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
lsp.ts
354 lines (318 loc) · 9.75 KB
/
lsp.ts
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import type { IDE, RangeInFile } from "core";
import { getAst, getTreePathAtCursor } from "core/autocomplete/ast";
import { GetLspDefinitionsFunction } from "core/autocomplete/completionProvider";
import { AutocompleteLanguageInfo } from "core/autocomplete/languages";
import { AutocompleteSnippet } from "core/autocomplete/ranking";
import { RangeInFileWithContents } from "core/commands/util";
import {
FUNCTION_BLOCK_NODE_TYPES,
FUNCTION_DECLARATION_NODE_TYPEs,
} from "core/indexing/chunk/code";
import { intersection } from "core/util/ranges";
import * as vscode from "vscode";
import type Parser from "web-tree-sitter";
type GotoProviderName =
| "vscode.executeDefinitionProvider"
| "vscode.executeTypeDefinitionProvider"
| "vscode.executeDeclarationProvider"
| "vscode.executeImplementationProvider"
| "vscode.executeReferenceProvider";
interface GotoInput {
uri: string;
line: number;
character: number;
name: GotoProviderName;
}
function gotoInputKey(input: GotoInput) {
return `${input.name}${input.uri.toString}${input.line}${input.character}`;
}
const MAX_CACHE_SIZE = 50;
const gotoCache = new Map<string, RangeInFile[]>();
export async function executeGotoProvider(
input: GotoInput,
): Promise<RangeInFile[]> {
const cacheKey = gotoInputKey(input);
const cached = gotoCache.get(cacheKey);
if (cached) {
return cached;
}
try {
const definitions = (await vscode.commands.executeCommand(
input.name,
vscode.Uri.parse(input.uri),
new vscode.Position(input.line, input.character),
)) as any;
const results = definitions
.filter((d: any) => (d.targetUri || d.uri) && (d.targetRange || d.range))
.map((d: any) => ({
filepath: (d.targetUri || d.uri).fsPath,
range: d.targetRange || d.range,
}));
// Add to cache
if (gotoCache.size >= MAX_CACHE_SIZE) {
// Remove the oldest item from the cache
const oldestKey = gotoCache.keys().next().value;
gotoCache.delete(oldestKey);
}
gotoCache.set(cacheKey, results);
return results;
} catch (e) {
console.warn(`Error executing ${name}:`, e);
return [];
}
}
function isRifWithContents(
rif: RangeInFile | RangeInFileWithContents,
): rif is RangeInFileWithContents {
return typeof (rif as any).contents === "string";
}
function findChildren(
node: Parser.SyntaxNode,
predicate: (n: Parser.SyntaxNode) => boolean,
firstN?: number,
): Parser.SyntaxNode[] {
let matchingNodes: Parser.SyntaxNode[] = [];
if (firstN && firstN <= 0) {
return [];
}
// Check if the current node's type is in the list of types we're interested in
if (predicate(node)) {
matchingNodes.push(node);
}
// Recursively search for matching types in all children of the current node
for (const child of node.children) {
matchingNodes = matchingNodes.concat(
findChildren(
child,
predicate,
firstN ? firstN - matchingNodes.length : undefined,
),
);
}
return matchingNodes;
}
function findTypeIdentifiers(node: Parser.SyntaxNode): Parser.SyntaxNode[] {
return findChildren(
node,
(childNode) =>
childNode.type === "type_identifier" ||
(["ERROR"].includes(childNode.parent?.type ?? "") &&
childNode.type === "identifier" &&
childNode.text[0].toUpperCase() === childNode.text[0]),
);
}
async function crawlTypes(
rif: RangeInFile | RangeInFileWithContents,
ide: IDE,
depth: number = 1,
results: RangeInFileWithContents[] = [],
searchedLabels: Set<string> = new Set(),
): Promise<RangeInFileWithContents[]> {
// Get the file contents if not already attached
const contents = isRifWithContents(rif)
? rif.contents
: await ide.readFile(rif.filepath);
// Parse AST
const ast = await getAst(rif.filepath, contents);
if (!ast) return results;
const astLineCount = ast.rootNode.text.split("\n").length;
// Find type identifiers
const identifierNodes = findTypeIdentifiers(ast.rootNode).filter(
(node) => !searchedLabels.has(node.text),
);
// Don't search for the same type definition more than once
// We deduplicate below to be sure, but this saves calls to the LSP
identifierNodes.forEach((node) => searchedLabels.add(node.text));
// Use LSP to get the definitions of those types
const definitions = await Promise.all(
identifierNodes.map(async (node) => {
const [typeDef] = await executeGotoProvider({
uri: rif.filepath,
// TODO: tree-sitter is zero-indexed, but there seems to be an off-by-one
// error at least with the .ts parser sometimes
line:
rif.range.start.line +
Math.min(node.startPosition.row, astLineCount - 1),
character: rif.range.start.character + node.startPosition.column,
name: "vscode.executeDefinitionProvider",
});
if (!typeDef) {
return undefined;
}
return {
...typeDef,
contents: await ide.readRangeInFile(typeDef.filepath, typeDef.range),
};
}),
);
// TODO: Filter out if not in our code?
// Filter out duplicates
for (const definition of definitions) {
if (
!definition ||
results.some(
(result) =>
result.filepath === definition.filepath &&
intersection(result.range, definition.range) !== null,
)
) {
continue; // ;)
}
results.push(definition);
}
// Recurse
if (depth > 0) {
for (const result of [...results]) {
await crawlTypes(result, ide, depth - 1, results, searchedLabels);
}
}
return results;
}
export async function getDefinitionsForNode(
uri: string,
node: Parser.SyntaxNode,
ide: IDE,
lang: AutocompleteLanguageInfo,
): Promise<RangeInFileWithContents[]> {
const ranges: (RangeInFile | RangeInFileWithContents)[] = [];
switch (node.type) {
case "call_expression": {
// function call -> function definition
const [funDef] = await executeGotoProvider({
uri,
line: node.startPosition.row,
character: node.startPosition.column,
name: "vscode.executeDefinitionProvider",
});
if (!funDef) {
return [];
}
// Don't display a function of more than 15 lines
// We can of course do something smarter here eventually
let funcText = await ide.readRangeInFile(funDef.filepath, funDef.range);
if (funcText.split("\n").length > 15) {
let truncated = false;
const funRootAst = await getAst(funDef.filepath, funcText);
if (funRootAst) {
const [funNode] = findChildren(
funRootAst?.rootNode,
(node) => FUNCTION_DECLARATION_NODE_TYPEs.includes(node.type),
1,
);
if (funNode) {
const [statementBlockNode] = findChildren(
funNode,
(node) => FUNCTION_BLOCK_NODE_TYPES.includes(node.type),
1,
);
if (statementBlockNode) {
funcText = funRootAst.rootNode.text
.slice(0, statementBlockNode.startIndex)
.trim();
truncated = true;
}
}
}
if (!truncated) {
funcText = funcText.split("\n")[0];
}
}
ranges.push(funDef);
const typeDefs = await crawlTypes(
{
...funDef,
contents: funcText,
},
ide,
);
ranges.push(...typeDefs);
break;
}
case "variable_declarator":
// variable assignment -> variable definition/type
// usages of the var that appear after the declaration
break;
case "impl_item":
// impl of trait -> trait definition
break;
case "new_expression":
// In 'new MyClass(...)', "MyClass" is the classNameNode
const classNameNode = node.children.find(
(child) => child.type === "identifier",
);
const [classDef] = await executeGotoProvider({
uri,
line: (classNameNode ?? node).endPosition.row,
character: (classNameNode ?? node).endPosition.column,
name: "vscode.executeDefinitionProvider",
});
if (!classDef) {
break;
}
const contents = await ide.readRangeInFile(
classDef.filepath,
classDef.range,
);
ranges.push({
...classDef,
contents: `${
classNameNode?.text
? `${lang.singleLineComment} ${classNameNode.text}:\n`
: ""
}${contents.trim()}`,
});
const definitions = await crawlTypes({ ...classDef, contents }, ide);
ranges.push(...definitions.filter(Boolean));
break;
case "":
// function definition -> implementations?
break;
}
return await Promise.all(
ranges.map(async (rif) => {
if (!isRifWithContents(rif)) {
return {
...rif,
contents: await ide.readRangeInFile(rif.filepath, rif.range),
};
}
return rif;
}),
);
}
/**
* and other stuff not directly on the path:
* - variables defined on line above
* ...etc...
*/
export const getDefinitionsFromLsp: GetLspDefinitionsFunction = async (
filepath: string,
contents: string,
cursorIndex: number,
ide: IDE,
lang: AutocompleteLanguageInfo,
): Promise<AutocompleteSnippet[]> => {
try {
const ast = await getAst(filepath, contents);
if (!ast) return [];
const treePath = await getTreePathAtCursor(ast, cursorIndex);
if (!treePath) return [];
const results: RangeInFileWithContents[] = [];
for (const node of treePath.reverse()) {
const definitions = await getDefinitionsForNode(
filepath,
node,
ide,
lang,
);
results.push(...definitions);
}
return results.map((result) => ({
...result,
score: 0.8,
}));
} catch (e) {
console.warn("Error getting definitions from LSP: ", e);
return [];
}
};