|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as path from 'path' |
| 3 | +import { WorkspaceProvider } from '../context/providers/workspace/workspace-provider' |
| 4 | +import { Logger } from '@shared/utils/logger' |
| 5 | + |
| 6 | +export const check_references_command = ( |
| 7 | + workspace_provider: WorkspaceProvider |
| 8 | +) => { |
| 9 | + return vscode.commands.registerCommand( |
| 10 | + 'codeWebChat.checkReferences', |
| 11 | + async () => { |
| 12 | + const editor = vscode.window.activeTextEditor |
| 13 | + if (!editor) { |
| 14 | + return |
| 15 | + } |
| 16 | + |
| 17 | + try { |
| 18 | + const document = editor.document |
| 19 | + const position = editor.selection.active |
| 20 | + |
| 21 | + const matched_files = await vscode.window.withProgress( |
| 22 | + { |
| 23 | + location: vscode.ProgressLocation.Window, |
| 24 | + title: 'Searching for references...' |
| 25 | + }, |
| 26 | + async () => { |
| 27 | + const locations = await vscode.commands.executeCommand< |
| 28 | + vscode.Location[] |
| 29 | + >('vscode.executeReferenceProvider', document.uri, position) |
| 30 | + |
| 31 | + if (!locations) return [] |
| 32 | + |
| 33 | + const unique_paths = new Set<string>() |
| 34 | + locations.forEach((loc) => { |
| 35 | + const file_path = loc.uri.fsPath |
| 36 | + if ( |
| 37 | + workspace_provider.get_workspace_root_for_file(file_path) && |
| 38 | + !workspace_provider.is_ignored_by_patterns(file_path) |
| 39 | + ) { |
| 40 | + unique_paths.add(file_path) |
| 41 | + } |
| 42 | + }) |
| 43 | + return Array.from(unique_paths) |
| 44 | + } |
| 45 | + ) |
| 46 | + |
| 47 | + if (matched_files.length === 0) { |
| 48 | + vscode.window.showInformationMessage( |
| 49 | + 'No files found referencing the current symbol.' |
| 50 | + ) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + const open_file_button = { |
| 55 | + iconPath: new vscode.ThemeIcon('go-to-file'), |
| 56 | + tooltip: 'Open file' |
| 57 | + } |
| 58 | + |
| 59 | + const quick_pick_items = matched_files.map((file_path) => { |
| 60 | + const workspace_root = |
| 61 | + workspace_provider.get_workspace_root_for_file(file_path) |
| 62 | + const relative_path = workspace_root |
| 63 | + ? path.relative(workspace_root, file_path) |
| 64 | + : file_path |
| 65 | + |
| 66 | + const dir_name = path.dirname(relative_path) |
| 67 | + return { |
| 68 | + label: path.basename(file_path), |
| 69 | + description: dir_name == '.' ? '' : dir_name, |
| 70 | + file_path: file_path, |
| 71 | + buttons: [open_file_button] |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + const quick_pick = vscode.window.createQuickPick< |
| 76 | + vscode.QuickPickItem & { file_path: string } |
| 77 | + >() |
| 78 | + quick_pick.items = quick_pick_items |
| 79 | + quick_pick.selectedItems = quick_pick_items |
| 80 | + quick_pick.canSelectMany = true |
| 81 | + quick_pick.placeholder = 'Select files to check for context' |
| 82 | + quick_pick.title = `Found ${matched_files.length} file${ |
| 83 | + matched_files.length == 1 ? '' : 's' |
| 84 | + }` |
| 85 | + quick_pick.ignoreFocusOut = true |
| 86 | + |
| 87 | + const close_button = { |
| 88 | + iconPath: new vscode.ThemeIcon('close'), |
| 89 | + tooltip: 'Close' |
| 90 | + } |
| 91 | + quick_pick.buttons = [close_button] |
| 92 | + |
| 93 | + const selected_items = await new Promise< |
| 94 | + readonly (vscode.QuickPickItem & { file_path: string })[] | undefined |
| 95 | + >((resolve) => { |
| 96 | + let is_accepted = false |
| 97 | + |
| 98 | + quick_pick.onDidTriggerButton((button) => { |
| 99 | + if (button === close_button) { |
| 100 | + resolve(undefined) |
| 101 | + quick_pick.hide() |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + quick_pick.onDidAccept(() => { |
| 106 | + is_accepted = true |
| 107 | + resolve(quick_pick.selectedItems) |
| 108 | + quick_pick.hide() |
| 109 | + }) |
| 110 | + |
| 111 | + quick_pick.onDidHide(() => { |
| 112 | + if (!is_accepted) { |
| 113 | + resolve(undefined) |
| 114 | + } |
| 115 | + quick_pick.dispose() |
| 116 | + }) |
| 117 | + |
| 118 | + quick_pick.onDidTriggerItemButton(async (e) => { |
| 119 | + if (e.button === open_file_button) { |
| 120 | + try { |
| 121 | + const doc = await vscode.workspace.openTextDocument( |
| 122 | + e.item.file_path |
| 123 | + ) |
| 124 | + await vscode.window.showTextDocument(doc, { |
| 125 | + preview: true |
| 126 | + }) |
| 127 | + } catch (error) { |
| 128 | + vscode.window.showErrorMessage(`Error opening file: ${error}`) |
| 129 | + } |
| 130 | + } |
| 131 | + }) |
| 132 | + |
| 133 | + quick_pick.show() |
| 134 | + }) |
| 135 | + |
| 136 | + if (!selected_items || selected_items.length == 0) { |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + const selected_paths = selected_items.map((item) => item.file_path) |
| 141 | + const currently_checked = workspace_provider.get_checked_files() |
| 142 | + |
| 143 | + const selected_paths_set = new Set(selected_paths) |
| 144 | + const unselected_files_set = new Set( |
| 145 | + matched_files.filter((file) => !selected_paths_set.has(file)) |
| 146 | + ) |
| 147 | + const currently_checked_filtered = currently_checked.filter( |
| 148 | + (file) => !unselected_files_set.has(file) |
| 149 | + ) |
| 150 | + const paths_to_apply = [ |
| 151 | + ...new Set([...currently_checked_filtered, ...selected_paths]) |
| 152 | + ] |
| 153 | + |
| 154 | + Logger.info({ |
| 155 | + message: `Selected ${selected_paths.length} files from reference search.`, |
| 156 | + data: { paths: selected_paths } |
| 157 | + }) |
| 158 | + |
| 159 | + await workspace_provider.set_checked_files(paths_to_apply) |
| 160 | + vscode.window.showInformationMessage(`Context updated.`) |
| 161 | + } catch (error) { |
| 162 | + vscode.window.showErrorMessage( |
| 163 | + `Reference check failed: ${ |
| 164 | + error instanceof Error ? error.message : String(error) |
| 165 | + }` |
| 166 | + ) |
| 167 | + Logger.error({ |
| 168 | + function_name: 'check_references_command', |
| 169 | + message: 'Error searching references', |
| 170 | + data: error |
| 171 | + }) |
| 172 | + } |
| 173 | + } |
| 174 | + ) |
| 175 | +} |
0 commit comments