Skip to content

Commit 7e7b65a

Browse files
committed
Improve import parsing logic and add recursive index file resolution to enhance imported file discovery
1 parent 8290bf2 commit 7e7b65a

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,26 @@ export const select_imported_files_command = (
8686
].includes(ext)
8787
) {
8888
if (/\b(import|export|require)\b/.test(line)) {
89+
in_import_block = true
90+
}
91+
92+
if (in_import_block) {
8993
const matches = [...line.matchAll(/["']([^"']+)["']/g)]
90-
for (const match of matches) {
91-
positions_to_check.push(
92-
new vscode.Position(i, match.index! + 1)
93-
)
94+
if (matches.length > 0) {
95+
for (const match of matches) {
96+
positions_to_check.push(
97+
new vscode.Position(i, match.index! + 1)
98+
)
99+
}
100+
in_import_block = false
101+
} else if (
102+
!/\b(import|export|require)\b/.test(line) &&
103+
(/[;]/.test(line) ||
104+
/^\s*(const|let|var|class|function|interface|type|enum)\b/.test(
105+
line
106+
))
107+
) {
108+
in_import_block = false
94109
}
95110
}
96111
} else if (ext == '.py') {
@@ -306,15 +321,24 @@ export const select_imported_files_command = (
306321
},
307322
async () => {
308323
const queue: vscode.Uri[] = []
324+
const index_queue: vscode.Uri[] = [...starting_uris]
309325

310-
for (const start_uri of starting_uris) {
311-
const immediate = await get_imports_for_uri(start_uri)
312-
for (const uri_str of immediate) {
326+
while (index_queue.length > 0) {
327+
const current_uri = index_queue.shift()!
328+
const imports = await get_imports_for_uri(current_uri)
329+
for (const uri_str of imports) {
313330
if (!visited_uris.has(uri_str)) {
314331
visited_uris.add(uri_str)
315332
if (is_valid_uri(uri_str)) {
316333
immediate_uris.add(uri_str)
317-
queue.push(vscode.Uri.parse(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+
}
318342
}
319343
}
320344
}

0 commit comments

Comments
 (0)