11import * as vscode from 'vscode'
22import * as path from 'path'
3- import {
4- WorkspaceProvider ,
5- FileItem
6- } from '../context/providers/workspace/workspace-provider'
3+ import { WorkspaceProvider } from '../context/providers/workspace/workspace-provider'
74import { Logger } from '@shared/utils/logger'
85import { display_token_count } from '../utils/display-token-count'
96import { t } from '../i18n'
@@ -16,20 +13,98 @@ export const select_referencing_files_command = (
1613) => {
1714 return vscode . commands . registerCommand (
1815 'codeWebChat.selectReferencingFiles' ,
19- async ( item ?: FileItem ) => {
16+ async ( item ?: any ) => {
2017 try {
2118 let matched_files : { file_path : string ; range : vscode . Range } [ ] = [ ]
2219
20+ let target_uri : vscode . Uri | undefined
21+
22+ let should_check_position = false
23+ let target_position : vscode . Position | undefined
24+
2325 if ( item ?. resourceUri ) {
24- const target_uri = item . resourceUri
26+ target_uri = item . resourceUri
27+ } else if (
28+ item instanceof vscode . Uri ||
29+ ( item && item . fsPath && item . scheme )
30+ ) {
31+ target_uri = item as vscode . Uri
32+ const editor = vscode . window . activeTextEditor
33+ if ( editor && editor . document . uri . fsPath === target_uri . fsPath ) {
34+ should_check_position = true
35+ target_position = editor . selection . active
36+ }
37+ } else {
38+ const editor = vscode . window . activeTextEditor
39+ if ( editor ) {
40+ target_uri = editor . document . uri
41+ should_check_position = true
42+ target_position = editor . selection . active
43+ }
44+ }
45+
46+ if ( ! target_uri ) {
47+ return
48+ }
49+
50+ let do_whole_file_search = true
51+
52+ if ( should_check_position && target_position ) {
53+ matched_files = await vscode . window . withProgress (
54+ {
55+ location : vscode . ProgressLocation . Window ,
56+ title : t ( 'command.select-referencing-files.searching' )
57+ } ,
58+ async ( ) => {
59+ const locations = await vscode . commands . executeCommand <
60+ vscode . Location [ ]
61+ > (
62+ 'vscode.executeReferenceProvider' ,
63+ target_uri ! ,
64+ target_position !
65+ )
66+
67+ if ( ! locations || locations . length === 0 ) return [ ]
68+
69+ const current_file_path = target_uri ! . fsPath
70+ const file_map = new Map < string , vscode . Range > ( )
71+ locations . forEach ( ( loc ) => {
72+ const file_path = loc . uri . fsPath
73+
74+ if ( file_path == current_file_path ) return
75+
76+ if (
77+ workspace_provider . get_workspace_root_for_file ( file_path ) &&
78+ ! workspace_provider . is_ignored_by_patterns ( file_path )
79+ ) {
80+ if ( ! file_map . has ( file_path ) ) {
81+ file_map . set ( file_path , loc . range )
82+ }
83+ }
84+ } )
85+ return Array . from ( file_map . entries ( ) ) . map (
86+ ( [ file_path , range ] ) => ( {
87+ file_path,
88+ range
89+ } )
90+ )
91+ }
92+ )
93+
94+ if ( matched_files . length > 0 ) {
95+ do_whole_file_search = false
96+ }
97+ }
98+
99+ if ( do_whole_file_search ) {
25100 const starting_uris = await get_all_files (
26- target_uri ,
101+ target_uri ! ,
27102 workspace_provider
28103 )
29104
30105 if ( starting_uris . length == 0 ) {
31106 vscode . window . showInformationMessage (
32- t ( 'command.context. select-references .no-files' )
107+ t ( 'command.select-referencing-files .no-files' )
33108 )
34109 return
35110 }
@@ -40,7 +115,7 @@ export const select_referencing_files_command = (
40115 await vscode . window . withProgress (
41116 {
42117 location : vscode . ProgressLocation . Notification ,
43- title : t ( 'command.context. select-references .searching' ) ,
118+ title : t ( 'command.select-referencing-files .searching' ) ,
44119 cancellable : true
45120 } ,
46121 async ( progress , token ) => {
@@ -160,56 +235,11 @@ export const select_referencing_files_command = (
160235 range
161236 } )
162237 )
163- } else {
164- const editor = vscode . window . activeTextEditor
165- if ( ! editor ) {
166- return
167- }
168-
169- const document = editor . document
170- const position = editor . selection . active
171-
172- matched_files = await vscode . window . withProgress (
173- {
174- location : vscode . ProgressLocation . Window ,
175- title : t ( 'command.context.select-references.searching' )
176- } ,
177- async ( ) => {
178- const locations = await vscode . commands . executeCommand <
179- vscode . Location [ ]
180- > ( 'vscode.executeReferenceProvider' , document . uri , position )
181-
182- if ( ! locations ) return [ ]
183-
184- const current_file_path = document . uri . fsPath
185- const file_map = new Map < string , vscode . Range > ( )
186- locations . forEach ( ( loc ) => {
187- const file_path = loc . uri . fsPath
188-
189- if ( file_path == current_file_path ) return
190-
191- if (
192- workspace_provider . get_workspace_root_for_file ( file_path ) &&
193- ! workspace_provider . is_ignored_by_patterns ( file_path )
194- ) {
195- if ( ! file_map . has ( file_path ) ) {
196- file_map . set ( file_path , loc . range )
197- }
198- }
199- } )
200- return Array . from ( file_map . entries ( ) ) . map (
201- ( [ file_path , range ] ) => ( {
202- file_path,
203- range
204- } )
205- )
206- }
207- )
208238 }
209239
210240 if ( matched_files . length == 0 ) {
211241 vscode . window . showInformationMessage (
212- t ( 'command.context. select-references .no-files' )
242+ t ( 'command.select-referencing-files .no-files' )
213243 )
214244 return
215245 }
@@ -264,10 +294,10 @@ export const select_referencing_files_command = (
264294 quick_pick . selectedItems = current_selected_items
265295 quick_pick . canSelectMany = true
266296 quick_pick . placeholder = t (
267- 'command.context. select-references .select-files'
297+ 'command.select-referencing-files .select-files'
268298 )
269299 quick_pick . title = t (
270- 'command.context. select-references .search-results'
300+ 'command.select-referencing-files .search-results'
271301 )
272302 quick_pick . ignoreFocusOut = true
273303
@@ -277,7 +307,7 @@ export const select_referencing_files_command = (
277307 }
278308 const search_button = {
279309 iconPath : new vscode . ThemeIcon ( 'search' ) ,
280- tooltip : t ( 'command.search.title ' )
310+ tooltip : t ( 'command.select-referencing-files.search ' )
281311 }
282312 quick_pick . buttons = [ search_button , close_button ]
283313
@@ -327,7 +357,7 @@ export const select_referencing_files_command = (
327357 } )
328358 } catch ( error ) {
329359 vscode . window . showErrorMessage (
330- t ( 'command.context. select-references .error-opening' , {
360+ t ( 'command.select-referencing-files .error-opening' , {
331361 error : String ( error )
332362 } )
333363 )
@@ -387,11 +417,11 @@ export const select_referencing_files_command = (
387417
388418 await workspace_provider . set_checked_files ( paths_to_apply )
389419 vscode . window . showInformationMessage (
390- t ( 'command.context. select-references .context-updated' )
420+ t ( 'command.select-referencing-files .context-updated' )
391421 )
392422 } catch ( error ) {
393423 vscode . window . showErrorMessage (
394- t ( 'command.context. select-references .failed' , {
424+ t ( 'command.select-referencing-files .failed' , {
395425 error : error instanceof Error ? error . message : String ( error )
396426 } )
397427 )
0 commit comments