@@ -11,6 +11,9 @@ import {
11
11
import { intersection } from "core/util/ranges" ;
12
12
import * as vscode from "vscode" ;
13
13
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' ;
14
17
15
18
type GotoProviderName =
16
19
| "vscode.executeDefinitionProvider"
@@ -106,6 +109,40 @@ function findChildren(
106
109
return matchingNodes ;
107
110
}
108
111
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
+
109
146
function findTypeIdentifiers ( node : Parser . SyntaxNode ) : Parser . SyntaxNode [ ] {
110
147
return findChildren (
111
148
node ,
@@ -117,6 +154,42 @@ function findTypeIdentifiers(node: Parser.SyntaxNode): Parser.SyntaxNode[] {
117
154
) ;
118
155
}
119
156
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
+
120
193
async function crawlTypes (
121
194
rif : RangeInFile | RangeInFileWithContents ,
122
195
ide : IDE ,
0 commit comments