Skip to content

Commit e78fe3e

Browse files
committed
Add search in results functionality to file search flows
- In search results user should be able to search using all available methods, not just 'intelligent'. - [Pasted text] - going 'back' while using 'intelligent' re-triggers API call - should re-use previously found results - because we have nested searches, search query should be set for each nesting - in phrase/keywords, should not show progress notification when searching in less than 1000 files
1 parent 4268462 commit e78fe3e

5 files changed

Lines changed: 168 additions & 65 deletions

File tree

‎apps/editor/src/features/search-files/actions/search-files.ts‎

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const search_files = async (params: {
4141
return _resolved_files
4242
}
4343

44+
const local_queries: Record<string, string> = {}
45+
4446
while (true) {
4547
try {
4648
const mode_result = await prompt_for_search_mode(
@@ -124,7 +126,15 @@ export const search_files = async (params: {
124126
const intelligent_result = await perform_intelligent_search_flow({
125127
files,
126128
workspace_provider: params.workspace_provider,
127-
extension_context: params.extension_context
129+
extension_context: params.extension_context,
130+
show_back_button: params.show_back_button,
131+
search_in_results: (matched_paths) =>
132+
search_files({
133+
get_files: async () => matched_paths,
134+
workspace_provider: params.workspace_provider,
135+
extension_context: params.extension_context,
136+
show_back_button: true
137+
})
128138
})
129139

130140
if (intelligent_result == 'back') {
@@ -149,15 +159,26 @@ export const search_files = async (params: {
149159
: LAST_SEARCH_FILES_SEMANTIC_QUERY_STATE_KEY
150160

151161
const initial_search_term =
152-
params.extension_context.workspaceState.get<string>(state_key) ||
153-
''
162+
local_queries[state_key] !== undefined
163+
? local_queries[state_key]
164+
: params.show_back_button
165+
? ''
166+
: params.extension_context.workspaceState.get<string>(
167+
state_key
168+
) || ''
154169

155170
const result = await prompt_for_search_term(
156171
initial_search_term,
157172
search_mode,
158173
keywords_target,
159174
(value) => {
160-
params.extension_context.workspaceState.update(state_key, value)
175+
local_queries[state_key] = value
176+
if (!params.show_back_button) {
177+
params.extension_context.workspaceState.update(
178+
state_key,
179+
value
180+
)
181+
}
161182
}
162183
)
163184
if (result.back) {
@@ -169,12 +190,16 @@ export const search_files = async (params: {
169190
break
170191
}
171192
if (!result.value) return undefined
193+
172194
const search_term_input = result.value
195+
local_queries[state_key] = search_term_input
173196

174-
await params.extension_context.workspaceState.update(
175-
state_key,
176-
search_term_input
177-
)
197+
if (!params.show_back_button) {
198+
await params.extension_context.workspaceState.update(
199+
state_key,
200+
search_term_input
201+
)
202+
}
178203

179204
const search_term = search_term_input.trim()
180205
if (search_term.length == 0) return undefined
@@ -184,7 +209,13 @@ export const search_files = async (params: {
184209
let is_cancelled = false
185210
let matched_files: string[] = []
186211

187-
if (search_mode == 'keywords' && keywords_target == 'filenames') {
212+
const is_filename_search =
213+
search_mode == 'keywords' && keywords_target == 'filenames'
214+
const is_small_search =
215+
(search_mode == 'phrase' || search_mode == 'keywords') &&
216+
files.length < 1000
217+
218+
if (is_filename_search || is_small_search) {
188219
matched_files = await search_files_by_term({
189220
files,
190221
search_term,
@@ -247,29 +278,24 @@ export const search_files = async (params: {
247278
return undefined
248279
}
249280

250-
if (selected_items == 'intelligent') {
251-
const intelligent_result =
252-
await perform_intelligent_search_flow({
253-
files: matched_files,
254-
workspace_provider: params.workspace_provider,
255-
extension_context: params.extension_context
256-
})
281+
if ('action' in selected_items) {
282+
const sub_search_result = await search_files({
283+
get_files: async () => selected_items.matched_paths,
284+
workspace_provider: params.workspace_provider,
285+
extension_context: params.extension_context,
286+
show_back_button: true
287+
})
257288

258-
if (intelligent_result == 'back') {
289+
if (sub_search_result === 'back') {
259290
continue
260291
}
261-
if (!intelligent_result) return undefined
262-
263-
final_result = intelligent_result
264-
break_outer = true
265-
break_match_mode = true
266-
break
267-
} else {
268-
final_result = selected_items
269-
break_outer = true
270-
break_match_mode = true
271-
break
292+
return sub_search_result
272293
}
294+
295+
final_result = selected_items
296+
break_outer = true
297+
break_match_mode = true
298+
break
273299
}
274300

275301
if (break_outer) break

‎apps/editor/src/features/search-files/utils/perform-intelligent-search-flow.ts‎

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,39 @@ export const perform_intelligent_search_flow = async (params: {
1717
files: string[]
1818
workspace_provider: WorkspaceProvider
1919
extension_context: vscode.ExtensionContext
20+
search_in_results: (
21+
matched_paths: string[]
22+
) => Promise<
23+
{ selected_paths: string[]; matched_paths: string[] } | undefined | 'back'
24+
>
25+
show_back_button?: boolean
2026
}): Promise<
2127
{ selected_paths: string[]; matched_paths: string[] } | undefined | 'back'
2228
> => {
29+
const local_queries: Record<string, string> = {}
30+
2331
while (true) {
2432
const initial_search_term =
25-
params.extension_context.workspaceState.get<string>(
26-
LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY
27-
) || ''
33+
local_queries[LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY] !== undefined
34+
? local_queries[LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY]
35+
: params.show_back_button
36+
? ''
37+
: params.extension_context.workspaceState.get<string>(
38+
LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY
39+
) || ''
2840

2941
const result = await prompt_for_search_term(
3042
initial_search_term,
3143
'intelligent',
3244
undefined,
3345
(value) => {
34-
params.extension_context.workspaceState.update(
35-
LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY,
36-
value
37-
)
46+
local_queries[LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY] = value
47+
if (!params.show_back_button) {
48+
params.extension_context.workspaceState.update(
49+
LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY,
50+
value
51+
)
52+
}
3853
}
3954
)
4055
if (result.back) return 'back'
@@ -43,10 +58,13 @@ export const perform_intelligent_search_flow = async (params: {
4358
const search_term = result.value.trim()
4459
if (search_term.length == 0) return undefined
4560

46-
await params.extension_context.workspaceState.update(
47-
LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY,
48-
search_term
49-
)
61+
local_queries[LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY] = search_term
62+
if (!params.show_back_button) {
63+
await params.extension_context.workspaceState.update(
64+
LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY,
65+
search_term
66+
)
67+
}
5068

5169
const analysis = await analyze_files({
5270
workspace_provider: params.workspace_provider,
@@ -143,23 +161,42 @@ export const perform_intelligent_search_flow = async (params: {
143161
break
144162
}
145163

146-
const apply_result = await prompt_for_intelligent_search_results({
147-
extracted_files: api_result,
148-
analysis,
149-
workspace_provider: params.workspace_provider
150-
})
164+
let go_back_to_term_from_results = false
165+
while (true) {
166+
const apply_result = await prompt_for_intelligent_search_results({
167+
extracted_files: api_result,
168+
analysis,
169+
workspace_provider: params.workspace_provider
170+
})
171+
172+
if (apply_result == 'back') {
173+
go_back_to_term_from_results = true
174+
break
175+
}
176+
if (apply_result == 'cancel') {
177+
return undefined
178+
}
179+
180+
if ('action' in apply_result) {
181+
const sub_result = await params.search_in_results(
182+
apply_result.matched_paths
183+
)
184+
if (sub_result === 'back') {
185+
continue
186+
}
187+
return sub_result
188+
}
189+
190+
final_result = apply_result
191+
break_outer = true
192+
break
193+
}
151194

152-
if (apply_result == 'back') {
195+
if (break_outer) break
196+
if (go_back_to_term_from_results) {
153197
go_back_to_term = true
154198
break
155199
}
156-
if (apply_result == 'cancel') {
157-
return undefined
158-
}
159-
160-
final_result = apply_result
161-
break_outer = true
162-
break
163200
}
164201

165202
if (break_outer) return final_result

‎apps/editor/src/features/search-files/utils/prompt-for-intelligent-search-results.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export const prompt_for_intelligent_search_results = async (params: {
1010
analysis: FileAnalysisResult
1111
workspace_provider: WorkspaceProvider
1212
}): Promise<
13-
{ selected_paths: string[]; matched_paths: string[] } | 'back' | 'cancel'
13+
| { selected_paths: string[]; matched_paths: string[] }
14+
| { action: 'search_in_results'; matched_paths: string[] }
15+
| 'back'
16+
| 'cancel'
1417
> => {
1518
const absolute_paths: string[] = []
1619

@@ -44,6 +47,10 @@ export const prompt_for_intelligent_search_results = async (params: {
4447
iconPath: new vscode.ThemeIcon('go-to-file'),
4548
tooltip: t('common.go-to-file')
4649
}
50+
const search_in_results_button = {
51+
iconPath: new vscode.ThemeIcon('search'),
52+
tooltip: t('feature.search-files.search-in-results')
53+
}
4754

4855
const quick_pick_items = await Promise.all(
4956
unique_paths.map(async (file_path) => {
@@ -96,10 +103,17 @@ export const prompt_for_intelligent_search_results = async (params: {
96103

97104
quick_pick.placeholder = t('feature.search-files.select-files')
98105
quick_pick.ignoreFocusOut = true
99-
quick_pick.buttons = [vscode.QuickInputButtons.Back, close_button]
106+
quick_pick.buttons = [
107+
vscode.QuickInputButtons.Back,
108+
search_in_results_button,
109+
close_button
110+
]
100111

101112
const list_selection = await new Promise<
102-
{ selected_paths: string[]; matched_paths: string[] } | 'back' | 'cancel'
113+
| { selected_paths: string[]; matched_paths: string[] }
114+
| { action: 'search_in_results'; matched_paths: string[] }
115+
| 'back'
116+
| 'cancel'
103117
>((resolve) => {
104118
let is_resolved = false
105119

@@ -108,6 +122,10 @@ export const prompt_for_intelligent_search_results = async (params: {
108122
is_resolved = true
109123
resolve('back')
110124
quick_pick.hide()
125+
} else if (button === search_in_results_button) {
126+
is_resolved = true
127+
resolve({ action: 'search_in_results', matched_paths: unique_paths })
128+
quick_pick.hide()
111129
} else if (button === close_button) {
112130
is_resolved = true
113131
resolve('cancel')

‎apps/editor/src/features/search-files/utils/prompt-for-search-results.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ export const prompt_for_search_results = async (params: {
1313
workspace_provider: WorkspaceProvider
1414
}): Promise<
1515
| { selected_paths: string[]; matched_paths: string[] }
16+
| { action: 'search_in_results'; matched_paths: string[] }
1617
| undefined
1718
| 'back'
18-
| 'intelligent'
1919
> => {
2020
const open_file_button = {
2121
iconPath: new vscode.ThemeIcon('go-to-file'),
2222
tooltip: t('common.go-to-file')
2323
}
24-
const intelligent_search_button = {
25-
iconPath: new vscode.ThemeIcon('search-sparkle'),
26-
tooltip: t('feature.search-files.mode.intelligent')
27-
}
2824
const close_button = {
2925
iconPath: new vscode.ThemeIcon('close'),
3026
tooltip: t('common.close')
3127
}
28+
const search_in_results_button = {
29+
iconPath: new vscode.ThemeIcon('search'),
30+
tooltip: t('feature.search-files.search-in-results')
31+
}
3232

3333
const currently_checked = params.workspace_provider.get_checked_files()
3434

@@ -94,28 +94,32 @@ export const prompt_for_search_results = async (params: {
9494
quick_pick.ignoreFocusOut = true
9595
quick_pick.buttons = [
9696
vscode.QuickInputButtons.Back,
97-
intelligent_search_button,
97+
search_in_results_button,
9898
close_button
9999
]
100100

101101
return new Promise<
102102
| { selected_paths: string[]; matched_paths: string[] }
103+
| { action: 'search_in_results'; matched_paths: string[] }
103104
| undefined
104105
| 'back'
105-
| 'intelligent'
106106
>((resolve) => {
107107
let is_accepted = false
108108

109109
quick_pick.onDidTriggerButton((button) => {
110110
if (button === vscode.QuickInputButtons.Back) {
111111
resolve('back')
112112
quick_pick.hide()
113+
} else if (button === search_in_results_button) {
114+
is_accepted = true
115+
resolve({
116+
action: 'search_in_results',
117+
matched_paths: params.matched_files
118+
})
119+
quick_pick.hide()
113120
} else if (button === close_button) {
114121
resolve(undefined)
115122
quick_pick.hide()
116-
} else if (button === intelligent_search_button) {
117-
resolve('intelligent')
118-
quick_pick.hide()
119123
}
120124
})
121125

0 commit comments

Comments
 (0)