Skip to content

Commit 2f5b184

Browse files
committed
Improve import detection by adding identifier matching for non-keyword variables in import blocks
1 parent 8db58ce commit 2f5b184

1 file changed

Lines changed: 34 additions & 12 deletions

File tree

apps/editor/src/commands/select-imported-files-command.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,37 @@ export const select_imported_files_command = (
9090
}
9191

9292
if (in_import_block) {
93+
const import_keywords = new Set([
94+
'import',
95+
'export',
96+
'from',
97+
'as',
98+
'require',
99+
'type',
100+
'typeof',
101+
'default',
102+
'const',
103+
'let',
104+
'var',
105+
'function',
106+
'class',
107+
'interface',
108+
'enum'
109+
])
110+
const identifier_matches = [
111+
...line.matchAll(/\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g)
112+
]
113+
for (const id_match of identifier_matches) {
114+
if (!import_keywords.has(id_match[1])) {
115+
positions_to_check.push(
116+
new vscode.Position(
117+
i,
118+
id_match.index! + Math.floor(id_match[1].length / 2)
119+
)
120+
)
121+
}
122+
}
123+
93124
const matches = [...line.matchAll(/["']([^"']+)["']/g)]
94125
if (matches.length > 0) {
95126
for (const match of matches) {
@@ -321,24 +352,15 @@ export const select_imported_files_command = (
321352
},
322353
async () => {
323354
const queue: vscode.Uri[] = []
324-
const index_queue: vscode.Uri[] = [...starting_uris]
325355

326-
while (index_queue.length > 0) {
327-
const current_uri = index_queue.shift()!
328-
const imports = await get_imports_for_uri(current_uri)
356+
for (const starting_uri of starting_uris) {
357+
const imports = await get_imports_for_uri(starting_uri)
329358
for (const uri_str of imports) {
330359
if (!visited_uris.has(uri_str)) {
331360
visited_uris.add(uri_str)
332361
if (is_valid_uri(uri_str)) {
333362
immediate_uris.add(uri_str)
334-
const parsed_uri = vscode.Uri.parse(uri_str)
335-
queue.push(parsed_uri)
336-
337-
if (
338-
path.parse(parsed_uri.fsPath).name.toLowerCase() == 'index'
339-
) {
340-
index_queue.push(parsed_uri)
341-
}
363+
queue.push(vscode.Uri.parse(uri_str))
342364
}
343365
}
344366
}

0 commit comments

Comments
 (0)