Skip to content

Commit b27a4ca

Browse files
committed
Extract common search results quick pick logic into a shared utility function
- Update the file search results display to show a unified list of files across all workspaces instead of splitting them by workspace - the issue is that prompt-for-search-results.ts, prompt-for-intelligent-search-results.ts, and prompt-for-provided-results.ts share roughly 80–85% identical boilerplate └── apps └── editor └── src ├── commands │ ├── apply-response-command │ │ ├── apply-response-command.ts │ │ └── response-processor.ts │ └── search-files-commands │ ├── index.ts │ ├── search-files-commands.ts │ └── utils │ ├── get-target-folder-path.ts │ └── prompt-for-provided-results.ts ├── features │ └── search-files │ ├── index.ts │ ├── search-files.ts │ ├── search-modes │ │ ├── perform-intelligent-search-mode.ts │ │ ├── perform-keywords-search-mode.ts │ │ └── perform-phrase-search-mode.ts │ └── utils │ ├── analyze-files.ts │ ├── check-matches-condition.ts │ ├── create-search-regex.ts │ ├── group-quick-pick-items.ts │ ├── handle-parent-folder-result.ts │ ├── map-files-to-quick-pick-items.ts │ ├── prompt-for-config.ts │ ├── prompt-for-intelligent-search-results.ts │ ├── prompt-for-keywords-match-mode.ts │ ├── prompt-for-keywords-target.ts │ ├── prompt-for-search-mode.ts │ ├── prompt-for-search-results.ts │ ├── prompt-for-search-term.ts │ ├── prompt-for-shrink-mode.ts │ ├── search-files-by-intelligent.ts │ ├── search-files-by-keywords.ts │ ├── search-files-by-phrase.ts │ └── show-search-results-quick-pick.ts ├── i18n │ └── translations │ └── features │ └── search-files.ts └── utils └── natural-sort.ts
1 parent 3444beb commit b27a4ca

6 files changed

Lines changed: 292 additions & 707 deletions

File tree

Lines changed: 9 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import * as vscode from 'vscode'
21
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
32
import { t } from '@/i18n'
4-
import { show_parent_folder_quick_pick } from '@/utils/show-parent-folder-quick-pick'
5-
import { group_quick_pick_items } from '@/features/search-files/utils/group-quick-pick-items'
6-
import { map_files_to_quick_pick_items } from '@/features/search-files/utils/map-files-to-quick-pick-items'
7-
import { handle_parent_folder_result } from '@/features/search-files/utils/handle-parent-folder-result'
8-
import { display_token_count } from '@/utils/display-token-count'
3+
import { show_search_results_quick_pick } from '@/features/search-files/utils/show-search-results-quick-pick'
94

105
export const prompt_for_provided_results = async (params: {
116
files: { path: string; checked: boolean }[]
@@ -24,26 +19,7 @@ export const prompt_for_provided_results = async (params: {
2419
}
2520
| undefined
2621
> => {
27-
const open_file_button = {
28-
iconPath: new vscode.ThemeIcon('go-to-file'),
29-
tooltip: t('common.go-to-file')
30-
}
31-
const add_parent_folder_button = {
32-
iconPath: new vscode.ThemeIcon('folder'),
33-
tooltip: t('common.select-parent-folder')
34-
}
35-
const close_button = {
36-
iconPath: new vscode.ThemeIcon('close'),
37-
tooltip: t('common.close')
38-
}
39-
const search_in_results_button = {
40-
iconPath: new vscode.ThemeIcon('search'),
41-
tooltip: t('common.search-in-selected-results')
42-
}
43-
4422
const currently_checked = params.workspace_provider.get_checked_files()
45-
const is_multi_root =
46-
params.workspace_provider.get_workspace_roots().length > 1
4723

4824
const matched_paths = params.files.map((f) => f.path)
4925

@@ -55,184 +31,14 @@ export const prompt_for_provided_results = async (params: {
5531
)
5632
: [])
5733

58-
const mapped_items = await map_files_to_quick_pick_items({
59-
files: params.files,
60-
is_multi_root,
61-
workspace_provider: params.workspace_provider,
62-
open_file_button,
63-
add_parent_folder_button
64-
})
65-
66-
const quick_pick_items = group_quick_pick_items({
67-
mapped_items,
68-
is_multi_root,
69-
workspace_provider: params.workspace_provider
70-
}) as (vscode.QuickPickItem & {
71-
file_path?: string
72-
checked?: boolean
73-
})[]
74-
75-
if (unmatched_checked_files.length > 0) {
76-
const mapped_unmatched_items = await map_files_to_quick_pick_items({
77-
files: unmatched_checked_files.map((path) => ({ path })),
78-
is_multi_root,
79-
workspace_provider: params.workspace_provider,
80-
open_file_button,
81-
add_parent_folder_button
82-
})
83-
84-
const unmatched_quick_pick_items = group_quick_pick_items({
85-
mapped_items: mapped_unmatched_items,
86-
is_multi_root: false,
87-
workspace_provider: params.workspace_provider,
88-
label_prefix: t('feature.search-files.results.unmatched')
89-
}) as (vscode.QuickPickItem & { file_path?: string })[]
90-
91-
quick_pick_items.push(...unmatched_quick_pick_items)
92-
}
93-
94-
const quick_pick = vscode.window.createQuickPick<
95-
vscode.QuickPickItem & { file_path?: string; checked?: boolean }
96-
>()
97-
quick_pick.items = quick_pick_items
98-
quick_pick.selectedItems = quick_pick_items.filter((item) => {
99-
if (!item.file_path) return false
100-
if (params.restored_selected_paths) {
101-
return params.restored_selected_paths.includes(item.file_path)
102-
}
103-
if (unmatched_checked_files.includes(item.file_path)) {
104-
return false
105-
}
106-
return item.checked !== undefined ? item.checked : false
107-
})
108-
quick_pick.canSelectMany = true
109-
quick_pick.matchOnDescription = true
11034
const base_title = t('feature.search-files.results.intelligent')
111-
quick_pick.title = base_title
112-
113-
const base_placeholder = t('feature.search-files.select-files')
114-
115-
const update_title = () => {
116-
const total = quick_pick.selectedItems.reduce(
117-
(sum, item) => sum + ((item as any).token_count || 0),
118-
0
119-
)
120-
const total_text =
121-
total > 0
122-
? ` (${t('common.totalling-tokens', {
123-
tokens: display_token_count(total)
124-
})})`
125-
: ''
126-
quick_pick.placeholder = `${base_placeholder}${total_text}`
127-
}
128-
129-
update_title()
130-
quick_pick.onDidChangeSelection(update_title)
131-
132-
quick_pick.ignoreFocusOut = true
133-
quick_pick.buttons = [search_in_results_button, close_button]
13435

135-
let is_showing_folder_quick_pick = false
136-
137-
return new Promise<
138-
| { selected_paths: string[]; matched_paths: string[]; title: string }
139-
| {
140-
action: 'search-in-results'
141-
matched_paths: string[]
142-
selected_paths: string[]
143-
unmatched_paths: string[]
144-
}
145-
| undefined
146-
>((resolve) => {
147-
let is_accepted = false
148-
149-
quick_pick.onDidTriggerButton((button) => {
150-
if (button === search_in_results_button) {
151-
const selected = quick_pick.selectedItems
152-
.map((item) => item.file_path)
153-
.filter((p): p is string => p !== undefined)
154-
155-
if (selected.length == 0) {
156-
vscode.window.showInformationMessage(
157-
t('common.info.select-files-to-search')
158-
)
159-
return
160-
}
161-
162-
is_accepted = true
163-
resolve({
164-
action: 'search-in-results',
165-
matched_paths: selected,
166-
selected_paths: selected,
167-
unmatched_paths: unmatched_checked_files
168-
})
169-
quick_pick.hide()
170-
} else if (button === close_button) {
171-
resolve(undefined)
172-
quick_pick.hide()
173-
}
174-
})
175-
176-
quick_pick.onDidAccept(() => {
177-
is_accepted = true
178-
resolve({
179-
selected_paths: quick_pick.selectedItems
180-
.map((item) => item.file_path)
181-
.filter((p): p is string => p !== undefined),
182-
matched_paths: [
183-
...params.files.map((f) => f.path),
184-
...unmatched_checked_files
185-
],
186-
title: base_title
187-
})
188-
quick_pick.hide()
189-
})
190-
191-
quick_pick.onDidHide(() => {
192-
if (is_showing_folder_quick_pick) return
193-
if (!is_accepted) {
194-
resolve(undefined)
195-
}
196-
quick_pick.dispose()
197-
})
198-
199-
quick_pick.onDidTriggerItemButton(async (e) => {
200-
if (!e.item.file_path) return
201-
if (e.button === open_file_button) {
202-
try {
203-
const doc = await vscode.workspace.openTextDocument(e.item.file_path)
204-
await vscode.window.showTextDocument(doc, { preview: true })
205-
} catch (error) {
206-
vscode.window.showErrorMessage(
207-
t('feature.search-files.error.opening-file', {
208-
error: String(error)
209-
})
210-
)
211-
}
212-
} else if (e.button === add_parent_folder_button) {
213-
is_showing_folder_quick_pick = true
214-
quick_pick.hide()
215-
216-
const result = await show_parent_folder_quick_pick({
217-
file_path: e.item.file_path,
218-
workspace_provider: params.workspace_provider
219-
})
220-
221-
is_showing_folder_quick_pick = false
222-
223-
handle_parent_folder_result({
224-
result,
225-
quick_pick,
226-
workspace_provider: params.workspace_provider,
227-
item: e.item,
228-
on_cancel: () => {
229-
is_accepted = true
230-
resolve(undefined)
231-
}
232-
})
233-
}
234-
})
235-
236-
quick_pick.show()
237-
})
36+
return (await show_search_results_quick_pick({
37+
matched_items: params.files,
38+
unmatched_checked_paths: unmatched_checked_files,
39+
workspace_provider: params.workspace_provider,
40+
title: base_title,
41+
show_back_button: false,
42+
restored_selected_paths: params.restored_selected_paths
43+
})) as any
23844
}
Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as vscode from 'vscode'
22
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
3-
import { t } from '@/i18n'
43

54
export const group_quick_pick_items = <
65
T extends vscode.QuickPickItem & { workspace_name?: string }
@@ -12,54 +11,14 @@ export const group_quick_pick_items = <
1211
}): T[] => {
1312
const quick_pick_items: T[] = []
1413

15-
if (params.is_multi_root) {
16-
const grouped = new Map<string, T[]>()
17-
for (const item of params.mapped_items) {
18-
const ws = item.workspace_name
19-
if (!ws) continue
20-
if (!grouped.has(ws)) grouped.set(ws, [])
21-
grouped.get(ws)!.push(item)
22-
}
23-
24-
const ordered_workspaces = params.workspace_provider
25-
.get_workspace_roots()
26-
.map((root) => params.workspace_provider.get_workspace_name(root))
27-
28-
const unique_ordered_workspaces = Array.from(new Set(ordered_workspaces))
29-
30-
for (const ws of unique_ordered_workspaces) {
31-
if (grouped.has(ws)) {
32-
quick_pick_items.push({
33-
label: params.label_prefix
34-
? `${params.label_prefix} · ${ws}`
35-
: t('feature.search-files.results.in-workspace', { workspace: ws }),
36-
kind: vscode.QuickPickItemKind.Separator
37-
} as T)
38-
quick_pick_items.push(...grouped.get(ws)!)
39-
}
40-
}
41-
} else {
42-
if (params.mapped_items.length > 0) {
43-
if (params.label_prefix) {
44-
quick_pick_items.push({
45-
label: params.label_prefix,
46-
kind: vscode.QuickPickItemKind.Separator
47-
} as T)
48-
} else {
49-
const root = params.workspace_provider.get_workspace_roots()[0]
50-
const ws_name = root
51-
? params.workspace_provider.get_workspace_name(root)
52-
: ''
53-
quick_pick_items.push({
54-
label: t('feature.search-files.results.in-workspace', {
55-
workspace: ws_name
56-
}),
57-
kind: vscode.QuickPickItemKind.Separator
58-
} as T)
59-
}
60-
}
61-
quick_pick_items.push(...params.mapped_items)
14+
if (params.mapped_items.length > 0 && params.label_prefix) {
15+
quick_pick_items.push({
16+
label: params.label_prefix,
17+
kind: vscode.QuickPickItemKind.Separator
18+
} as T)
6219
}
6320

21+
quick_pick_items.push(...params.mapped_items)
22+
6423
return quick_pick_items
6524
}

0 commit comments

Comments
 (0)