Skip to content

Commit 02cbb3c

Browse files
committed
Refactor imported files commands and enhance reference search accuracy using import validation
- in 'select referencing files command' verify each found reference by checking if the listed file in-fact imports the source file - newly used shared logic between commands should live in `apps/editor/src/utils` [Changes] - assess language support, e.g. dart. - place 'select imported files' commands into a single folder while moving `apps/editor/src/features/imported-files` there └── apps └── editor └── src ├── commands │ ├── index.ts │ ├── search-files-commands │ │ ├── index.ts │ │ ├── search-files-commands.ts │ │ └── utils │ │ ├── get-target-folder-path.ts │ │ └── prompt-for-provided-results.ts │ ├── select-imported-files-commands │ │ ├── index.ts │ │ ├── select-imported-files-commands.ts │ │ └── utils │ │ ├── is-valid-uri.ts │ │ └── prompt-for-imported-files.ts │ └── select-referencing-files-commands │ ├── index.ts │ ├── select-referencing-files-commands.ts │ └── utils │ ├── get-referencing-files-for-position.ts │ ├── get-referencing-files-for-uris.ts │ └── prompt-for-referencing-files.ts ├── extension.ts ├── features │ └── imported-files │ └── index.ts └── utils ├── get-imports-for-uri.ts └── is-binary.ts
1 parent b65fe45 commit 02cbb3c

12 files changed

Lines changed: 159 additions & 132 deletions

File tree

‎apps/editor/src/commands/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export * from './set-ranges-command'
2222
export * from './search-files-commands'
2323
export * from './select-referencing-files-commands'
2424
export * from './select-definition-file-command'
25-
export * from './select-imported-files-command'
25+
export * from './select-imported-files-commands'
2626
export * from './select-unstaged-files-command'
2727
export * from './select-files-of-commit-command'
2828
export * from './select-clipboard-paths-command'

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

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './select-imported-files-commands'
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import * as vscode from 'vscode'
2+
import { WorkspaceProvider } from '../../context/providers/workspace/workspace-provider'
3+
import { prompt_for_imported_files } from './utils/prompt-for-imported-files'
4+
import { t } from '@/i18n'
5+
import { WebSocketManager } from '@/services/websocket-manager'
6+
7+
export const select_imported_files_commands = (
8+
workspace_provider: WorkspaceProvider,
9+
extension_context: vscode.ExtensionContext,
10+
websocket_manager: WebSocketManager
11+
) => {
12+
return [
13+
vscode.commands.registerCommand(
14+
'codeWebChat.selectImportedFiles',
15+
async (item?: any) => {
16+
let target_uri: vscode.Uri | undefined
17+
18+
if (item?.resourceUri) {
19+
target_uri = item.resourceUri
20+
} else if (
21+
item instanceof vscode.Uri ||
22+
(item && item.fsPath && item.scheme)
23+
) {
24+
target_uri = item as vscode.Uri
25+
} else {
26+
const editor = vscode.window.activeTextEditor
27+
if (editor) {
28+
target_uri = editor.document.uri
29+
}
30+
}
31+
32+
if (!target_uri) return
33+
34+
const starting_uris =
35+
await workspace_provider.get_all_files_for_uri(target_uri)
36+
37+
if (starting_uris.length == 0) {
38+
vscode.window.showInformationMessage(
39+
t('command.select-imported-files.no-valid-files')
40+
)
41+
return
42+
}
43+
44+
const result = await prompt_for_imported_files({
45+
starting_uris,
46+
workspace_provider,
47+
extension_context,
48+
websocket_manager
49+
})
50+
51+
if (result) {
52+
const final_checked = workspace_provider.get_checked_files()
53+
const paths_to_apply = [
54+
...new Set([
55+
...final_checked.filter((p) => !result.shown_paths.includes(p)),
56+
...result.selected_paths
57+
])
58+
]
59+
await workspace_provider.set_checked_files(paths_to_apply)
60+
}
61+
}
62+
),
63+
vscode.commands.registerCommand(
64+
'codeWebChat.selectImportedFilesForSelected',
65+
async () => {
66+
const checked_files = workspace_provider.get_checked_files()
67+
68+
if (checked_files.length === 0) {
69+
vscode.window.showInformationMessage(
70+
t('command.select-imported-files.no-valid-files')
71+
)
72+
return
73+
}
74+
75+
const starting_uris = checked_files.map((file_path) =>
76+
vscode.Uri.file(file_path)
77+
)
78+
79+
const result = await prompt_for_imported_files({
80+
starting_uris,
81+
workspace_provider,
82+
extension_context,
83+
websocket_manager
84+
})
85+
86+
if (result) {
87+
const final_checked = workspace_provider.get_checked_files()
88+
const paths_to_apply = [
89+
...new Set([
90+
...final_checked.filter((p) => !result.shown_paths.includes(p)),
91+
...result.selected_paths
92+
])
93+
]
94+
await workspace_provider.set_checked_files(paths_to_apply)
95+
}
96+
}
97+
)
98+
]
99+
}

apps/editor/src/features/imported-files/utils/is-valid-uri.ts renamed to apps/editor/src/commands/select-imported-files-commands/utils/is-valid-uri.ts

File renamed without changes.

apps/editor/src/features/imported-files/utils/prompt-for-imported-files.ts renamed to apps/editor/src/commands/select-imported-files-commands/utils/prompt-for-imported-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path'
33
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
44
import { display_token_count } from '@/utils/display-token-count'
55
import { is_valid_uri } from './is-valid-uri'
6-
import { get_imports_for_uri } from './get-imports-for-uri'
6+
import { get_imports_for_uri } from '@/utils/get-imports-for-uri'
77
import { t } from '@/i18n'
88
import { search_files } from '@/features/search-files'
99
import { show_parent_folder_quick_pick } from '@/utils/show-parent-folder-quick-pick'

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

Lines changed: 0 additions & 47 deletions
This file was deleted.

‎apps/editor/src/commands/select-referencing-files-commands/utils/get-referencing-files-for-position.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode'
22
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
3+
import { get_imports_for_uri } from '@/utils/get-imports-for-uri'
34

45
export const get_referencing_files_for_position = async (params: {
56
uri: vscode.Uri
@@ -16,20 +17,36 @@ export const get_referencing_files_for_position = async (params: {
1617
if (!locations || locations.length === 0) return []
1718

1819
const file_map = new Map<string, vscode.Range>()
19-
locations.forEach((loc) => {
20+
const cancel_token_source = new vscode.CancellationTokenSource()
21+
const cached_imports = new Map<string, string[]>()
22+
23+
for (const loc of locations) {
2024
const file_path = loc.uri.fsPath
2125

22-
if (params.ignore_paths.includes(file_path)) return
26+
if (params.ignore_paths.includes(file_path)) continue
2327

2428
if (
2529
params.workspace_provider.get_workspace_root_for_file(file_path) &&
2630
!params.workspace_provider.is_ignored_by_patterns(file_path)
2731
) {
2832
if (!file_map.has(file_path)) {
29-
file_map.set(file_path, loc.range)
33+
let imports = cached_imports.get(file_path)
34+
if (!imports) {
35+
imports = await get_imports_for_uri(
36+
loc.uri,
37+
cancel_token_source.token
38+
)
39+
cached_imports.set(file_path, imports)
40+
}
41+
42+
if (imports.includes(params.uri.toString())) {
43+
file_map.set(file_path, loc.range)
44+
}
3045
}
3146
}
32-
})
47+
}
48+
49+
cancel_token_source.dispose()
3350

3451
return Array.from(file_map.entries()).map(([file_path, range]) => ({
3552
file_path,

‎apps/editor/src/commands/select-referencing-files-commands/utils/get-referencing-files-for-uris.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode'
22
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
33
import { Logger } from '@shared/utils/logger'
4+
import { get_imports_for_uri } from '@/utils/get-imports-for-uri'
45

56
export const get_referencing_files_for_uris = async (params: {
67
uris: vscode.Uri[]
@@ -10,6 +11,7 @@ export const get_referencing_files_for_uris = async (params: {
1011
token: vscode.CancellationToken
1112
}): Promise<{ file_path: string; range: vscode.Range }[]> => {
1213
const file_map = new Map<string, vscode.Range>()
14+
const cached_imports = new Map<string, string[]>()
1315

1416
for (const uri of params.uris) {
1517
if (params.token.isCancellationRequested) {
@@ -80,20 +82,28 @@ export const get_referencing_files_for_uris = async (params: {
8082
>('vscode.executeReferenceProvider', uri, position)
8183

8284
if (locations) {
83-
locations.forEach((loc) => {
85+
for (const loc of locations) {
8486
const file_path = loc.uri.fsPath
85-
if (params.ignore_paths.includes(file_path)) return
87+
if (params.ignore_paths.includes(file_path)) continue
8688
if (
8789
params.workspace_provider.get_workspace_root_for_file(
8890
file_path
8991
) &&
9092
!params.workspace_provider.is_ignored_by_patterns(file_path)
9193
) {
9294
if (!file_map.has(file_path)) {
93-
file_map.set(file_path, loc.range)
95+
let imports = cached_imports.get(file_path)
96+
if (!imports) {
97+
imports = await get_imports_for_uri(loc.uri, params.token)
98+
cached_imports.set(file_path, imports)
99+
}
100+
101+
if (imports.includes(uri.toString())) {
102+
file_map.set(file_path, loc.range)
103+
}
94104
}
95105
}
96-
})
106+
}
97107
}
98108
}
99109
} catch (err) {

‎apps/editor/src/extension.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ import {
4040
select_clipboard_paths_command,
4141
select_parent_folder_command,
4242
select_referencing_files_commands,
43+
select_imported_files_commands,
4344
copy_merge_commit_details_command,
4445
rate_extension_command
4546
} from './commands'
4647
import { setup_git_discard_file_watcher } from './services/git-discard-file-watcher'
47-
import { select_imported_files_command } from './commands/select-imported-files-command'
48-
import { select_imported_files_for_selected_command } from './commands/select-imported-files-for-selected-command'
4948
import { SettingsViewProvider } from './views/settings/backend/settings-view-provider'
5049
import { get_current_preview_url } from './views/prompt/backend/message-handlers/handle-open-website'
5150
import { t } from '@/i18n'
@@ -188,18 +187,13 @@ export const activate = async (extension_context: vscode.ExtensionContext) => {
188187
delete_command(),
189188
select_workspace_file_command(workspace_provider),
190189
set_ranges_command(workspace_provider, extension_context),
191-
select_imported_files_command(
190+
...select_imported_files_commands(
192191
workspace_provider,
193192
extension_context,
194193
websocket_server_instance
195194
),
196195
duplicate_workspace_command(workspace_provider, extension_context),
197196
copy_merge_commit_details_command(),
198-
select_imported_files_for_selected_command(
199-
workspace_provider,
200-
extension_context,
201-
websocket_server_instance
202-
),
203197
...select_referencing_files_commands(
204198
workspace_provider,
205199
extension_context,

0 commit comments

Comments
 (0)