Skip to content

Commit 7e0fedc

Browse files
committed
Refactor select referencing files command and update translation keys
- backspace at the end of 'pasted X tokens' doesn't remove the whole symbol
1 parent 8dd86aa commit 7e0fedc

2 files changed

Lines changed: 117 additions & 68 deletions

File tree

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

Lines changed: 91 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import * as vscode from 'vscode'
22
import * 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'
74
import { Logger } from '@shared/utils/logger'
85
import { display_token_count } from '../utils/display-token-count'
96
import { 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
)

‎apps/editor/src/i18n/translations/commands/select-referencing-files.ts‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const translations = {
2-
'command.context.select-references.searching': {
2+
'command.select-referencing-files.searching': {
33
en: 'Searching for references...',
44
pl: 'Wyszukiwanie odwołań...',
55
'zh-cn': '正在搜索引用...',
@@ -17,7 +17,7 @@ export const translations = {
1717
hu: 'Hivatkozások keresése...',
1818
bg: 'Търсене на референции...'
1919
},
20-
'command.context.select-references.no-files': {
20+
'command.select-referencing-files.no-files': {
2121
en: 'No references found.',
2222
pl: 'Nie znaleziono odwołań.',
2323
'zh-cn': '未找到引用。',
@@ -35,7 +35,7 @@ export const translations = {
3535
hu: 'Nem találhatók hivatkozások.',
3636
bg: 'Не са намерени референции.'
3737
},
38-
'command.context.select-references.select-files': {
38+
'command.select-referencing-files.select-files': {
3939
en: 'Select referencing files',
4040
pl: 'Wybierz pliki odwołujące się',
4141
'zh-cn': '选择引用文件',
@@ -53,7 +53,7 @@ export const translations = {
5353
hu: 'Hivatkozó fájlok kiválasztása',
5454
bg: 'Изберете рефериращите файлове'
5555
},
56-
'command.context.select-references.search-results': {
56+
'command.select-referencing-files.search-results': {
5757
en: 'Search Results',
5858
pl: 'Wyniki wyszukiwania',
5959
'zh-cn': '搜索结果',
@@ -71,7 +71,26 @@ export const translations = {
7171
hu: 'Keresési eredmények',
7272
bg: 'Резултати от търсенето'
7373
},
74-
'command.context.select-references.context-updated': {
74+
'command.select-referencing-files.search': {
75+
en: 'Search',
76+
pl: 'Szukaj',
77+
'zh-cn': '搜索',
78+
ja: '検索',
79+
'zh-tw': '搜尋',
80+
de: 'Suchen',
81+
es: 'Buscar',
82+
fr: 'Rechercher',
83+
'pt-br': 'Pesquisar',
84+
ru: 'Поиск',
85+
ko: '검색',
86+
it: 'Cerca',
87+
tr: 'Ara',
88+
cs: 'Hledat',
89+
hu: 'Keresés',
90+
bg: 'Търсене'
91+
},
92+
93+
'command.select-referencing-files.context-updated': {
7594
en: 'Context updated.',
7695
pl: 'Kontekst zaktualizowany.',
7796
'zh-cn': '上下文已更新。',
@@ -89,7 +108,7 @@ export const translations = {
89108
hu: 'Kontextus frissítve.',
90109
bg: 'Контекстът е актуализиран.'
91110
},
92-
'command.context.select-references.error-opening': {
111+
'command.select-referencing-files.error-opening': {
93112
en: 'Error opening file: {error}',
94113
pl: 'Błąd otwierania pliku: {error}',
95114
'zh-cn': '打开文件出错:{error}',
@@ -107,7 +126,7 @@ export const translations = {
107126
hu: 'Hiba a fájl megnyitásakor: {error}',
108127
bg: 'Грешка при отваряне на файл: {error}'
109128
},
110-
'command.context.select-references.failed': {
129+
'command.select-referencing-files.failed': {
111130
en: 'Reference check failed: {error}',
112131
pl: 'Sprawdzenie odwołań nie powiodło się: {error}',
113132
'zh-cn': '引用检查失败:{error}',

0 commit comments

Comments
 (0)