Skip to content

Commit 6f44e99

Browse files
committed
Integrate file search capability into clipboard paths and referencing files commands
- implement searching files in `apps/editor/src/commands/select-clipboard-paths-command.ts` - in `apps/editor/src/commands/select-clipboard-paths-command.ts` [Pasted text] - simiplify notification shown during progress - don't show paths etc.
1 parent e4d435e commit 6f44e99

2 files changed

Lines changed: 122 additions & 80 deletions

File tree

‎apps/editor/src/commands/select-clipboard-paths-command.ts‎

Lines changed: 121 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { t } from '@/i18n'
99
import { Logger } from '@shared/utils/logger'
1010
import { dictionary } from '@shared/constants/dictionary'
1111
import { extract_paths_from_text } from '@/utils/extract-paths-from-text'
12+
import { search_files } from '@/features/search-files'
1213

1314
export const select_clipboard_paths_command = (
1415
workspace_provider: WorkspaceProvider,
@@ -66,52 +67,56 @@ export const select_clipboard_paths_command = (
6667
return
6768
}
6869

69-
while (true) {
70-
const currently_checked = workspace_provider.get_checked_files()
71-
const currently_checked_set = new Set(currently_checked)
72-
73-
const quick_pick_items = await Promise.all(
74-
absolute_paths.map(async (file_path) => {
75-
const token_count =
76-
await workspace_provider.calculate_file_tokens(file_path)
77-
78-
const formatted_token_count = display_token_count(
79-
token_count.total
80-
)
70+
const currently_checked = workspace_provider.get_checked_files()
71+
const currently_checked_set = new Set(currently_checked)
72+
73+
const quick_pick_items: (vscode.QuickPickItem & {
74+
file_path: string
75+
})[] = await Promise.all(
76+
absolute_paths.map(async (file_path) => {
77+
const token_count =
78+
await workspace_provider.calculate_file_tokens(file_path)
79+
80+
const formatted_token_count = display_token_count(token_count.total)
81+
82+
const root =
83+
workspace_provider.get_workspace_root_for_file(file_path)
84+
const relative_path = root
85+
? path.relative(root, file_path)
86+
: file_path
87+
const dir_name = path.dirname(relative_path)
88+
let display_dir = dir_name === '.' ? '' : dir_name
89+
90+
if (root && workspace_roots.length > 1) {
91+
const workspace_name = workspace_provider.get_workspace_name(root)
92+
display_dir = display_dir
93+
? `${workspace_name}/${display_dir}`
94+
: workspace_name
95+
}
8196

82-
const root =
83-
workspace_provider.get_workspace_root_for_file(file_path)
84-
const relative_path = root
85-
? path.relative(root, file_path)
86-
: file_path
87-
const dir_name = path.dirname(relative_path)
88-
let display_dir = dir_name === '.' ? '' : dir_name
89-
90-
if (root && workspace_roots.length > 1) {
91-
const workspace_name =
92-
workspace_provider.get_workspace_name(root)
93-
display_dir = display_dir
94-
? `${workspace_name}/${display_dir}`
95-
: workspace_name
96-
}
97+
return {
98+
label: path.basename(file_path),
99+
description: display_dir
100+
? `${formatted_token_count} · ${display_dir}`
101+
: formatted_token_count,
102+
file_path,
103+
buttons: [
104+
{
105+
iconPath: new vscode.ThemeIcon('go-to-file'),
106+
tooltip: t('common.go-to-file')
107+
}
108+
]
109+
}
110+
})
111+
)
97112

98-
return {
99-
label: path.basename(file_path),
100-
description: display_dir
101-
? `${formatted_token_count} · ${display_dir}`
102-
: formatted_token_count,
103-
picked: currently_checked_set.has(file_path),
104-
file_path,
105-
buttons: [
106-
{
107-
iconPath: new vscode.ThemeIcon('go-to-file'),
108-
tooltip: t('common.go-to-file')
109-
}
110-
]
111-
}
112-
})
113-
)
113+
let current_selected_items = quick_pick_items.filter((item) =>
114+
currently_checked_set.has(item.file_path)
115+
)
116+
let paths_to_apply: string[] = []
117+
let final_selected_paths: string[] = []
114118

119+
while (true) {
115120
const quick_pick = vscode.window.createQuickPick<
116121
vscode.QuickPickItem & { file_path: string }
117122
>()
@@ -120,21 +125,35 @@ export const select_clipboard_paths_command = (
120125
quick_pick.canSelectMany = true
121126
quick_pick.items = quick_pick_items
122127
quick_pick.ignoreFocusOut = true
123-
quick_pick.selectedItems = quick_pick_items.filter(
124-
(item) => item.picked
125-
)
126-
quick_pick.buttons = [
127-
{ iconPath: new vscode.ThemeIcon('close'), tooltip: 'Close' }
128-
]
128+
quick_pick.selectedItems = current_selected_items
129+
130+
const close_button = {
131+
iconPath: new vscode.ThemeIcon('close'),
132+
tooltip: t('common.close')
133+
}
134+
const search_button = {
135+
iconPath: new vscode.ThemeIcon('search'),
136+
tooltip: t('feature.search-files.search-in-results')
137+
}
138+
139+
quick_pick.buttons = [search_button, close_button]
129140

130141
const selected_items = await new Promise<
131142
| readonly (vscode.QuickPickItem & { file_path: string })[]
132143
| undefined
144+
| 'search'
133145
>((resolve) => {
134146
let is_accepted = false
135147

136-
quick_pick.onDidTriggerButton((_button) => {
137-
quick_pick.hide()
148+
quick_pick.onDidTriggerButton((button) => {
149+
if (button === close_button) {
150+
resolve(undefined)
151+
quick_pick.hide()
152+
} else if (button === search_button) {
153+
current_selected_items = [...quick_pick.selectedItems]
154+
resolve('search')
155+
quick_pick.hide()
156+
}
138157
})
139158

140159
quick_pick.onDidTriggerItemButton(async (e) => {
@@ -160,13 +179,43 @@ export const select_clipboard_paths_command = (
160179
quick_pick.show()
161180
})
162181

163-
if (!selected_items || selected_items.length === 0) {
182+
if (
183+
!selected_items ||
184+
(Array.isArray(selected_items) && selected_items.length === 0)
185+
) {
164186
return
165187
}
166188

167-
const selected_paths = selected_items.map((item) => item.file_path)
168-
let paths_to_apply = selected_paths
169-
let should_continue = false
189+
let selected_paths: string[] = []
190+
191+
if (selected_items === 'search') {
192+
const search_result = await search_files({
193+
get_files: async () => absolute_paths,
194+
workspace_provider,
195+
extension_context,
196+
show_back_button: true
197+
})
198+
199+
if (search_result === 'back') {
200+
continue
201+
}
202+
203+
if (!search_result) {
204+
return
205+
}
206+
207+
selected_paths = search_result.selected_paths
208+
} else {
209+
selected_paths = selected_items.map((item) => item.file_path)
210+
}
211+
212+
if (selected_paths.length === 0) {
213+
return
214+
}
215+
216+
paths_to_apply = selected_paths
217+
final_selected_paths = selected_paths
218+
let should_continue_main_loop = false
170219

171220
if (currently_checked.length > 0) {
172221
const selected_paths_set = new Set(selected_paths)
@@ -244,7 +293,7 @@ export const select_clipboard_paths_command = (
244293
})
245294

246295
if (choice === 'back') {
247-
should_continue = true
296+
should_continue_main_loop = true
248297
} else if (!choice) {
249298
return
250299
} else {
@@ -264,28 +313,29 @@ export const select_clipboard_paths_command = (
264313
}
265314
}
266315

267-
if (should_continue) {
316+
if (should_continue_main_loop) {
268317
continue
269318
}
270319

271-
Logger.info({
272-
message: `Selected ${selected_paths.length} clipboard paths.`,
273-
data: { paths: selected_paths }
274-
})
320+
break
321+
}
275322

276-
await workspace_provider.set_checked_files(paths_to_apply)
323+
Logger.info({
324+
message: `Selected ${final_selected_paths.length} clipboard paths.`,
325+
data: { paths: final_selected_paths }
326+
})
277327

278-
const newly_selected_count = selected_paths.filter(
279-
(p) => !currently_checked.includes(p)
280-
).length
328+
await workspace_provider.set_checked_files(paths_to_apply)
281329

282-
vscode.window.showInformationMessage(
283-
dictionary.information_message.ADDED_FILES_TO_CONTEXT(
284-
newly_selected_count
285-
)
330+
const newly_selected_count = final_selected_paths.filter(
331+
(p) => !currently_checked.includes(p)
332+
).length
333+
334+
vscode.window.showInformationMessage(
335+
dictionary.information_message.ADDED_FILES_TO_CONTEXT(
336+
newly_selected_count
286337
)
287-
return
288-
}
338+
)
289339
} catch (error) {
290340
vscode.window.showErrorMessage(
291341
`Failed to select clipboard paths: ${

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ export const select_referencing_files_command = (
119119
cancellable: true
120120
},
121121
async (progress, token) => {
122-
let processed_files = 0
123122
for (const uri of starting_uris) {
124123
if (token.isCancellationRequested) {
125124
is_cancelled = true
@@ -132,7 +131,6 @@ export const select_referencing_files_command = (
132131
>('vscode.executeDocumentSymbolProvider', uri)
133132

134133
if (!symbols) {
135-
processed_files++
136134
continue
137135
}
138136

@@ -183,10 +181,6 @@ export const select_referencing_files_command = (
183181
}
184182
const position = positions[i]
185183

186-
progress.report({
187-
message: `${path.basename(uri.fsPath)} (${i + 1}/${positions.length})`
188-
})
189-
190184
const locations = await vscode.commands.executeCommand<
191185
vscode.Location[]
192186
>('vscode.executeReferenceProvider', uri, position)
@@ -216,10 +210,8 @@ export const select_referencing_files_command = (
216210
})
217211
}
218212

219-
processed_files++
220213
progress.report({
221-
increment: (1 / starting_uris.length) * 100,
222-
message: `${processed_files}/${starting_uris.length}`
214+
increment: (1 / starting_uris.length) * 100
223215
})
224216
}
225217
}

0 commit comments

Comments
 (0)