@@ -64,68 +64,231 @@ export const select_imported_files_command = (
6464 const document = await vscode . workspace . openTextDocument ( document_uri )
6565 const text = document . getText ( )
6666 const lines = text . split ( '\n' )
67+ const ext = path . extname ( document_uri . fsPath ) . toLowerCase ( )
68+
69+ let in_import_block = false
70+ const positions_to_check : vscode . Position [ ] = [ ]
6771
6872 for ( let i = 0 ; i < lines . length ; i ++ ) {
6973 const line = lines [ i ]
70- if ( / ^ \s * ( i m p o r t | f r o m | r e q u i r e | i n c l u d e | u s i n g | u s e ) \b / . test ( line ) ) {
71- const matches = [ ...line . matchAll ( / [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g) ]
72- if ( matches . length > 0 ) {
74+
75+ if (
76+ [
77+ '.ts' ,
78+ '.js' ,
79+ '.tsx' ,
80+ '.jsx' ,
81+ '.mjs' ,
82+ '.cjs' ,
83+ '.vue' ,
84+ '.svelte' ,
85+ '.astro'
86+ ] . includes ( ext )
87+ ) {
88+ if ( / \b ( i m p o r t | e x p o r t | r e q u i r e ) \b / . test ( line ) ) {
89+ const matches = [ ...line . matchAll ( / [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g) ]
7390 for ( const match of matches ) {
74- const position = new vscode . Position ( i , match . index ! + 1 )
75- try {
76- const definitions = await vscode . commands . executeCommand <
77- vscode . Location [ ] | vscode . LocationLink [ ]
78- > (
79- 'vscode.executeDefinitionProvider' ,
80- document . uri ,
81- position
82- )
83- if ( definitions ) {
84- for ( const def of definitions ) {
85- const uri = 'uri' in def ? def . uri : def . targetUri
86- if ( uri . scheme == 'file' ) found_uris . add ( uri . toString ( ) )
87- }
88- }
89- } catch { }
91+ positions_to_check . push (
92+ new vscode . Position ( i , match . index ! + 1 )
93+ )
9094 }
91- } else {
92- const words = [ ...line . matchAll ( / [ \w . / \\ ] + / g) ]
95+ }
96+ } else if ( ext == '.py' ) {
97+ const from_match = line . match ( / ^ \s * f r o m \s + ( [ a - z A - Z 0 - 9 _ . ] + ) / )
98+ if ( from_match ) {
99+ const offset = line . indexOf ( from_match [ 1 ] , from_match . index )
100+ positions_to_check . push (
101+ new vscode . Position (
102+ i ,
103+ offset + Math . floor ( from_match [ 1 ] . length / 2 )
104+ )
105+ )
106+ }
107+ const import_match = line . match ( / ^ \s * i m p o r t \s + ( [ a - z A - Z 0 - 9 _ . , ] + ) / )
108+ if ( import_match ) {
109+ const group_offset = line . indexOf (
110+ import_match [ 1 ] ,
111+ import_match . index
112+ )
113+ const words = [ ...import_match [ 1 ] . matchAll ( / [ a - z A - Z 0 - 9 _ . ] + / g) ]
93114 for ( const word of words ) {
94- if (
95- [
96- 'import' ,
97- 'from' ,
98- 'require' ,
99- 'include' ,
100- 'using' ,
101- 'use' ,
102- 'as'
103- ] . includes ( word [ 0 ] )
115+ const offset = group_offset + word . index !
116+ positions_to_check . push (
117+ new vscode . Position (
118+ i ,
119+ offset + Math . floor ( word [ 0 ] . length / 2 )
120+ )
104121 )
105- continue
106- const position = new vscode . Position (
122+ }
123+ }
124+ } else if (
125+ [ '.c' , '.cpp' , '.h' , '.hpp' , '.cc' , '.cxx' ] . includes ( ext )
126+ ) {
127+ const inc_match = line . match ( / ^ \s * # \s * i n c l u d e \s * [ < " ] ( [ ^ > " ] + ) [ > " ] / )
128+ if ( inc_match ) {
129+ const offset = line . indexOf ( inc_match [ 1 ] , inc_match . index )
130+ positions_to_check . push (
131+ new vscode . Position (
107132 i ,
108- word . index ! + Math . floor ( word [ 0 ] . length / 2 )
133+ offset + Math . floor ( inc_match [ 1 ] . length / 2 )
134+ )
135+ )
136+ }
137+ } else if ( [ '.cs' , '.java' ] . includes ( ext ) ) {
138+ const regex =
139+ ext == '.cs'
140+ ? / ^ \s * u s i n g \s + ( [ a - z A - Z 0 - 9 _ . ] + ) \s * ; /
141+ : / ^ \s * i m p o r t \s + ( [ a - z A - Z 0 - 9 _ . * ] + ) \s * ; /
142+ const match = line . match ( regex )
143+ if ( match ) {
144+ const offset = line . indexOf ( match [ 1 ] , match . index )
145+ positions_to_check . push (
146+ new vscode . Position (
147+ i ,
148+ offset + Math . floor ( match [ 1 ] . length / 2 )
149+ )
150+ )
151+ }
152+ } else if ( ext == '.rs' ) {
153+ const match = line . match ( / ^ \s * (?: u s e | m o d ) \s + ( [ a - z A - Z 0 - 9 _ : ] + ) / )
154+ if ( match ) {
155+ const offset = line . indexOf ( match [ 1 ] , match . index )
156+ positions_to_check . push (
157+ new vscode . Position (
158+ i ,
159+ offset + Math . floor ( match [ 1 ] . length / 2 )
160+ )
161+ )
162+ }
163+ } else if ( ext == '.go' ) {
164+ if ( / ^ \s * i m p o r t \s + \( / . test ( line ) ) {
165+ in_import_block = true
166+ } else if ( in_import_block && / \) / . test ( line ) ) {
167+ in_import_block = false
168+ } else if ( in_import_block || / ^ \s * i m p o r t \s + / . test ( line ) ) {
169+ const match = line . match ( / " ( [ ^ " ] + ) " / )
170+ if ( match ) {
171+ const offset = line . indexOf ( match [ 1 ] , match . index )
172+ positions_to_check . push (
173+ new vscode . Position (
174+ i ,
175+ offset + Math . floor ( match [ 1 ] . length / 2 )
176+ )
177+ )
178+ }
179+ }
180+ } else if ( ext == '.rb' ) {
181+ if ( / ^ \s * r e q u i r e (?: _ r e l a t i v e ) ? \b / . test ( line ) ) {
182+ const match = line . match ( / [ ' " ] ( [ ^ ' " ] + ) [ ' " ] / )
183+ if ( match ) {
184+ const offset = line . indexOf ( match [ 1 ] , match . index )
185+ positions_to_check . push (
186+ new vscode . Position (
187+ i ,
188+ offset + Math . floor ( match [ 1 ] . length / 2 )
189+ )
109190 )
110- try {
111- const definitions = await vscode . commands . executeCommand <
112- vscode . Location [ ] | vscode . LocationLink [ ]
113- > (
114- 'vscode.executeDefinitionProvider' ,
115- document . uri ,
116- position
191+ }
192+ }
193+ } else if ( ext == '.php' ) {
194+ if (
195+ / ^ \s * (?: i n c l u d e | r e q u i r e | i n c l u d e _ o n c e | r e q u i r e _ o n c e | u s e ) \b / . test (
196+ line
197+ )
198+ ) {
199+ const str_match = line . match ( / [ ' " ] ( [ ^ ' " ] + ) [ ' " ] / )
200+ if ( str_match ) {
201+ const offset = line . indexOf ( str_match [ 1 ] , str_match . index )
202+ positions_to_check . push (
203+ new vscode . Position (
204+ i ,
205+ offset + Math . floor ( str_match [ 1 ] . length / 2 )
117206 )
118- if ( definitions ) {
119- for ( const def of definitions ) {
120- const uri = 'uri' in def ? def . uri : def . targetUri
121- if ( uri . scheme == 'file' ) found_uris . add ( uri . toString ( ) )
122- }
123- }
124- } catch { }
207+ )
208+ } else {
209+ const use_match = line . match ( / ^ \s * u s e \s + ( [ a - z A - Z 0 - 9 _ \\ ] + ) / )
210+ if ( use_match ) {
211+ const offset = line . indexOf ( use_match [ 1 ] , use_match . index )
212+ positions_to_check . push (
213+ new vscode . Position (
214+ i ,
215+ offset + Math . floor ( use_match [ 1 ] . length / 2 )
216+ )
217+ )
218+ }
219+ }
220+ }
221+ } else {
222+ let is_import_line = false
223+
224+ if (
225+ / ^ \s * (?: # \s * ) ? ( i m p o r t | f r o m | r e q u i r e | i n c l u d e | u s i n g | u s e ) \b / . test (
226+ line
227+ )
228+ ) {
229+ is_import_line = true
230+ if ( / [ { ( ] / . test ( line ) && ! / [ } ) ] / . test ( line ) ) {
231+ in_import_block = true
232+ }
233+ } else if ( in_import_block ) {
234+ is_import_line = true
235+ if ( / [ } ) ] / . test ( line ) ) {
236+ in_import_block = false
237+ }
238+ } else if (
239+ / \b ( f r o m | r e q u i r e | i m p o r t | i n c l u d e ) \s * \( ? [ " ' ] / . test ( line )
240+ ) {
241+ is_import_line = true
242+ }
243+
244+ if ( is_import_line ) {
245+ const matches = [ ...line . matchAll ( / [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g) ]
246+ if ( matches . length > 0 ) {
247+ for ( const match of matches ) {
248+ positions_to_check . push (
249+ new vscode . Position ( i , match . index ! + 1 )
250+ )
251+ }
252+ } else {
253+ const words = [ ...line . matchAll ( / [ \w . / \\ ] + / g) ]
254+ for ( const word of words ) {
255+ if (
256+ [
257+ 'import' ,
258+ 'from' ,
259+ 'require' ,
260+ 'include' ,
261+ 'using' ,
262+ 'use' ,
263+ 'as'
264+ ] . includes ( word [ 0 ] )
265+ )
266+ continue
267+ positions_to_check . push (
268+ new vscode . Position (
269+ i ,
270+ word . index ! + Math . floor ( word [ 0 ] . length / 2 )
271+ )
272+ )
273+ }
125274 }
126275 }
127276 }
128277 }
278+
279+ for ( const position of positions_to_check ) {
280+ try {
281+ const definitions = await vscode . commands . executeCommand <
282+ vscode . Location [ ] | vscode . LocationLink [ ]
283+ > ( 'vscode.executeDefinitionProvider' , document . uri , position )
284+ if ( definitions ) {
285+ for ( const def of definitions ) {
286+ const uri = 'uri' in def ? def . uri : def . targetUri
287+ if ( uri . scheme == 'file' ) found_uris . add ( uri . toString ( ) )
288+ }
289+ }
290+ } catch { }
291+ }
129292 } catch { }
130293 return Array . from ( found_uris )
131294 }
0 commit comments