Skip to content

Commit 5052917

Browse files
author
Calvinn Ng
committed
add missing function to add imported files into context
1 parent 2c26aef commit 5052917

File tree

1 file changed

+73
-0
lines changed
  • extensions/vscode/src/autocomplete

1 file changed

+73
-0
lines changed

extensions/vscode/src/autocomplete/lsp.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
import { intersection } from "core/util/ranges";
1212
import * as vscode from "vscode";
1313
import type Parser from "web-tree-sitter";
14+
import * as path from 'path';
15+
import * as fs from 'fs';
16+
import * as levenshtein from 'fast-levenshtein';
1417

1518
type GotoProviderName =
1619
| "vscode.executeDefinitionProvider"
@@ -106,6 +109,40 @@ function findChildren(
106109
return matchingNodes;
107110
}
108111

112+
function isAbsolutePath(inputPath: string): boolean {
113+
return path.isAbsolute(inputPath);
114+
}
115+
116+
function findFunctionDefinitions(node: Parser.SyntaxNode, functionName: string): Array<Parser.SyntaxNode> {
117+
const functions = [];
118+
const queue = [node];
119+
120+
while (queue.length > 0) {
121+
const currentNode = queue.shift();
122+
123+
if (currentNode) {
124+
if (currentNode.type === 'function_declaration' || currentNode.type === 'method_definition') {
125+
const nameNode = currentNode.childForFieldName('name');
126+
if (nameNode && nameNode.text === functionName) {
127+
functions.push(currentNode);
128+
}
129+
else if (functionName === "*") {
130+
functions.push(currentNode);
131+
}
132+
}
133+
134+
for (let i = 0; i < currentNode.childCount; i++) {
135+
const childNode = currentNode.child(i);
136+
if (childNode) {
137+
queue.push(childNode);
138+
}
139+
}
140+
}
141+
}
142+
143+
return functions;
144+
}
145+
109146
function findTypeIdentifiers(node: Parser.SyntaxNode): Parser.SyntaxNode[] {
110147
return findChildren(
111148
node,
@@ -117,6 +154,42 @@ function findTypeIdentifiers(node: Parser.SyntaxNode): Parser.SyntaxNode[] {
117154
);
118155
}
119156

157+
function findClosestMatchingFile(filepath: string): Promise<string | undefined> {
158+
return new Promise((resolve, reject) => {
159+
const directory = path.dirname(filepath);
160+
const targetFileName = path.basename(filepath);
161+
162+
// Read the directory contents
163+
fs.readdir(directory, (err, files) => {
164+
if (err) {
165+
console.error('Error reading directory:', err);
166+
reject(err);
167+
return;
168+
}
169+
170+
// Initialize variables to keep track of the closest match
171+
let closestFile: string | null = null;
172+
let closestDistance = Infinity;
173+
174+
// Iterate over each file in the directory
175+
files.forEach(file => {
176+
const distance = levenshtein.get(targetFileName, file);
177+
178+
if (distance < closestDistance) {
179+
closestDistance = distance;
180+
closestFile = file;
181+
}
182+
});
183+
184+
if (closestFile) {
185+
resolve(path.resolve(directory, closestFile));
186+
} else {
187+
resolve(undefined);
188+
}
189+
});
190+
});
191+
}
192+
120193
async function crawlTypes(
121194
rif: RangeInFile | RangeInFileWithContents,
122195
ide: IDE,

0 commit comments

Comments
 (0)