Skip to content

Commit 788e948

Browse files
committed
Add search-in-results support to context path selection and propagate websocket context through restore commands
- allow searching in file sets in 'restore context', just like you allow searching in 'referencing files', etc. [Accuracy boost] - 'intelligent search' in 'restore context' doesn't work correctly - we get warning "No files selected." - when doing 'intellligent search' with 'autofill', it not always behaves like when making 'request', e.g. in 'restore context' autofill would during applicaition list other files in the workspace as 'unmatched selected files' section. The expectance is to leave other files selected. └── apps └── editor └── src ├── commands │ ├── apply-response-command │ │ ├── apply-response-command.ts │ │ ├── response-processor │ │ │ └── response-processor.ts │ │ └── utils │ │ └── response-parser │ │ ├── parsers │ │ │ ├── index.ts │ │ │ └── intelligent-file-search-results-parser │ │ │ └── intelligent-file-search-results-parser.ts │ │ └── response-parser.ts │ ├── index.ts │ ├── restore-context-command │ │ ├── actions │ │ │ ├── restore-from-json-file.ts │ │ │ └── restore-from-workspace-state.ts │ │ ├── index.ts │ │ └── restore-context-command.ts │ ├── search-files-commands │ │ ├── index.ts │ │ ├── search-files-commands.ts │ │ └── utils │ │ └── prompt-for-provided-results.ts │ ├── select-imported-files-commands │ │ ├── index.ts │ │ ├── select-imported-files-commands.ts │ │ └── utils │ │ └── prompt-for-imported-files.ts │ └── select-referencing-files-commands │ ├── index.ts │ ├── select-referencing-files-commands.ts │ └── utils │ └── prompt-for-referencing-files.ts ├── context │ └── providers │ └── workspace │ └── workspace-provider.ts ├── extension.ts ├── features │ ├── context-restoration │ │ ├── index.ts │ │ └── utils │ │ ├── resolve-context-paths.ts │ │ ├── resolve-glob-patterns.ts │ │ └── select-context-paths.ts │ └── search-files │ ├── index.ts │ ├── search-files.ts │ ├── search-modes │ │ └── perform-intelligent-search-mode.ts │ └── utils │ ├── analyze-files.ts │ ├── prompt-for-intelligent-search-results.ts │ ├── prompt-for-search-mode.ts │ ├── search-files-by-intelligent.ts │ └── show-search-results-quick-pick.ts ├── i18n │ └── translations │ ├── commands │ │ ├── index.ts │ │ ├── restore-context-command.ts │ │ ├── search-files-command.ts │ │ ├── select-imported-files-command.ts │ │ └── select-referencing-files-command.ts │ ├── common.ts │ └── features │ └── search-files.ts ├── types │ └── context.ts └── utils ├── group-quick-pick-items.ts ├── handle-parent-folder-result.ts └── map-files-to-quick-pick-items.ts
1 parent 0009ecb commit 788e948

10 files changed

Lines changed: 98 additions & 32 deletions

File tree

‎apps/editor/src/commands/restore-context-command/actions/restore-from-json-file.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import {
1616
} from '@/features/context-restoration'
1717
import { t } from '@/i18n'
1818
import { normalize_path } from '@/utils/normalize-path'
19+
import { WebSocketManager } from '@/services/websocket-manager'
1920

2021
export const restore_from_json_file = async (params: {
2122
workspace_provider: WorkspaceProvider
2223
extension_context: vscode.ExtensionContext
24+
websocket_manager: WebSocketManager
2325
on_context_selected: () => void
2426
show_back_button?: boolean
2527
}): Promise<'back' | void> => {
@@ -235,6 +237,8 @@ export const restore_from_json_file = async (params: {
235237
const result = await select_context_paths({
236238
context: selection.context,
237239
workspace_provider: params.workspace_provider,
240+
extension_context: params.extension_context,
241+
websocket_manager: params.websocket_manager,
238242
update_context_paths: async (remaining_files: string[]) => {
239243
const files_by_workspace = group_files_by_workspace(remaining_files)
240244
const current_roots =

‎apps/editor/src/commands/restore-context-command/actions/restore-from-workspace-state.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import {
1313
} from '@/features/context-restoration'
1414
import { t } from '@/i18n'
1515
import { normalize_path } from '@/utils/normalize-path'
16+
import { WebSocketManager } from '@/services/websocket-manager'
1617

1718
let active_deletion_timestamp: number | undefined
1819

1920
export const restore_from_workspace_state = async (params: {
2021
workspace_provider: WorkspaceProvider
2122
extension_context: vscode.ExtensionContext
23+
websocket_manager: WebSocketManager
2224
on_context_selected: () => void
2325
show_back_button?: boolean
2426
}): Promise<'back' | void> => {
@@ -337,6 +339,8 @@ export const restore_from_workspace_state = async (params: {
337339
const result = await select_context_paths({
338340
context: context_to_apply,
339341
workspace_provider: params.workspace_provider,
342+
extension_context: params.extension_context,
343+
websocket_manager: params.websocket_manager,
340344
update_context_paths: async (remaining_files: string[]) => {
341345
const files_by_workspace = group_files_by_workspace(remaining_files)
342346
const current_roots =

‎apps/editor/src/commands/restore-context-command/restore-context-command.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { select_context_source } from '@/features/context-restoration'
44
import { restore_from_workspace_state } from './actions/restore-from-workspace-state'
55
import { restore_from_json_file } from './actions/restore-from-json-file'
66
import { t } from '@/i18n'
7+
import { WebSocketManager } from '@/services/websocket-manager'
78

89
export const restore_context_command = (params: {
910
workspace_provider: WorkspaceProvider
1011
on_context_selected: () => void
1112
extension_context: vscode.ExtensionContext
13+
websocket_manager: WebSocketManager
1214
}): vscode.Disposable => {
1315
return vscode.commands.registerCommand(
1416
'codeWebChat.restoreContext',
@@ -32,13 +34,15 @@ export const restore_context_command = (params: {
3234
action_result = await restore_from_workspace_state({
3335
workspace_provider: params.workspace_provider,
3436
extension_context: params.extension_context,
37+
websocket_manager: params.websocket_manager,
3538
on_context_selected: params.on_context_selected,
3639
show_back_button: !skipped_menu
3740
})
3841
} else if (source === 'file') {
3942
action_result = await restore_from_json_file({
4043
workspace_provider: params.workspace_provider,
4144
extension_context: params.extension_context,
45+
websocket_manager: params.websocket_manager,
4246
on_context_selected: params.on_context_selected,
4347
show_back_button: !skipped_menu
4448
})

‎apps/editor/src/commands/search-files-commands/search-files-commands.ts‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ export const search_files_commands = (
5252
}
5353

5454
let paths_to_apply: string[]
55-
const all_listed_were_selected = params.result.matched_paths.every((file) =>
56-
currently_selected.includes(file)
57-
)
5855

5956
if (params.is_search_in_selected) {
6057
const searched_but_not_selected = params.resolved_all_files.filter(
@@ -69,25 +66,6 @@ export const search_files_commands = (
6966
...params.result.selected_paths
7067
])
7168
]
72-
} else if (params.is_provided_files) {
73-
if (currently_selected.length > 0) {
74-
const action = all_listed_were_selected ? 'replace' : 'merge'
75-
76-
if (action === 'merge') {
77-
paths_to_apply = [
78-
...new Set([
79-
...currently_selected.filter(
80-
(p) => !params.result.matched_paths.includes(p)
81-
),
82-
...params.result.selected_paths
83-
])
84-
]
85-
} else {
86-
paths_to_apply = [...params.result.selected_paths]
87-
}
88-
} else {
89-
paths_to_apply = [...params.result.selected_paths]
90-
}
9169
} else {
9270
paths_to_apply = [
9371
...new Set([
@@ -170,7 +148,7 @@ export const search_files_commands = (
170148
searched_files: resolved_all_files
171149
})
172150

173-
if (!result) return
151+
if (!result || result === 'back' || result === 'cancel') return
174152

175153
if ('action' in result) {
176154
const sub_search_result = await search_files({

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export const prompt_for_provided_results = async (params: {
1717
selected_paths: string[]
1818
unmatched_paths: string[]
1919
}
20+
| 'back'
21+
| 'cancel'
2022
| undefined
2123
> => {
2224
const currently_selected = params.workspace_provider.get_selected_files()
@@ -33,12 +35,12 @@ export const prompt_for_provided_results = async (params: {
3335

3436
const base_title = t('feature.search-files.results.intelligent')
3537

36-
return (await show_search_results_quick_pick({
38+
return await show_search_results_quick_pick({
3739
matched_items: params.files,
3840
unmatched_selected_paths: unmatched_selected_files,
3941
workspace_provider: params.workspace_provider,
4042
title: base_title,
4143
show_back_button: false,
4244
restored_selected_paths: params.restored_selected_paths
43-
})) as any
45+
})
4446
}

‎apps/editor/src/commands/select-imported-files-commands/utils/prompt-for-imported-files.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ export const prompt_for_imported_files = async (params: {
455455
workspace_provider,
456456
extension_context,
457457
websocket_manager,
458-
show_back_button: true
458+
show_back_button: true,
459+
is_sub_search: true
459460
})
460461

461462
if (search_result === 'back') {

‎apps/editor/src/commands/select-referencing-files-commands/utils/prompt-for-referencing-files.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ export const prompt_for_referencing_files = async (params: {
231231
workspace_provider: params.workspace_provider,
232232
extension_context: params.extension_context,
233233
websocket_manager: params.websocket_manager,
234-
show_back_button: true
234+
show_back_button: true,
235+
is_sub_search: true
235236
})
236237

237238
if (search_result === 'back') {

‎apps/editor/src/extension.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export const activate = async (extension_context: vscode.ExtensionContext) => {
177177
restore_context_command({
178178
workspace_provider,
179179
extension_context,
180+
websocket_manager: websocket_server_instance,
180181
on_context_selected: () => {}
181182
}),
182183
select_modified_files_command(

‎apps/editor/src/features/context-restoration/utils/select-context-paths.ts‎

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import { WorkspaceProvider } from '../../../context/providers/workspace/workspac
55
import { resolve_context_paths } from './resolve-context-paths'
66
import { t } from '@/i18n'
77
import { display_token_count } from '@shared/utils/display-token-count'
8+
import { search_files } from '@/features/search-files'
9+
import { WebSocketManager } from '@/services/websocket-manager'
810

911
let active_deletion_timestamp: number | undefined
1012

1113
export const select_context_paths = async (params: {
1214
context: SavedContext
1315
workspace_provider: WorkspaceProvider
16+
extension_context: vscode.ExtensionContext
17+
websocket_manager: WebSocketManager
1418
update_context_paths?: (remaining_files: string[]) => Promise<void>
1519
}): Promise<'back' | void> => {
1620
const primary_workspace_root =
@@ -26,6 +30,13 @@ export const select_context_paths = async (params: {
2630
tooltip: t('common.delete')
2731
}
2832

33+
const search_button = {
34+
iconPath: new vscode.ThemeIcon('search'),
35+
tooltip: t('common.search-in-selected-results')
36+
}
37+
38+
let restored_selected_paths: Set<string> | undefined
39+
2940
while (true) {
3041
const create_quick_pick_items = async (local_params: {
3142
paths: string[]
@@ -51,7 +62,7 @@ export const select_context_paths = async (params: {
5162
const buttons: vscode.QuickInputButton[] = [
5263
{
5364
iconPath: new vscode.ThemeIcon('go-to-file'),
54-
tooltip: 'Go to file'
65+
tooltip: t('common.go-to-file')
5566
}
5667
]
5768

@@ -74,9 +85,12 @@ export const select_context_paths = async (params: {
7485
}
7586

7687
const quick_pick_items = await create_quick_pick_items({
77-
paths: resolved_paths
88+
paths: resolved_paths,
89+
selected_paths_override: restored_selected_paths
7890
})
7991

92+
restored_selected_paths = undefined
93+
8094
const list_quick_pick = vscode.window.createQuickPick<
8195
vscode.QuickPickItem & { file_path: string; token_count: number }
8296
>()
@@ -92,19 +106,24 @@ export const select_context_paths = async (params: {
92106
0
93107
)
94108
const total_text =
95-
total > 0 ? ` (totalling ${display_token_count(total)} tokens)` : ''
109+
total > 0
110+
? ` (${t('common.totalling-tokens', {
111+
tokens: display_token_count(total)
112+
})})`
113+
: ''
96114
list_quick_pick.placeholder = `Select files to apply to context${total_text}`
97115
}
98116
update_placeholder()
99117
list_quick_pick.onDidChangeSelection(update_placeholder)
100-
list_quick_pick.buttons = [vscode.QuickInputButtons.Back]
118+
list_quick_pick.buttons = [vscode.QuickInputButtons.Back, search_button]
101119

102120
const list_selection = await new Promise<
103121
| readonly (vscode.QuickPickItem & {
104122
file_path: string
105123
token_count: number
106124
})[]
107125
| 'back'
126+
| { search: string[] }
108127
| undefined
109128
>((resolve) => {
110129
let is_resolved = false
@@ -119,11 +138,26 @@ export const select_context_paths = async (params: {
119138
if (button === vscode.QuickInputButtons.Back) {
120139
list_quick_pick.hide()
121140
resolve_once('back')
141+
} else if (button === search_button) {
142+
const selected_paths = list_quick_pick.selectedItems.map(
143+
(i) => i.file_path
144+
)
145+
if (selected_paths.length === 0) {
146+
vscode.window.showInformationMessage(
147+
t('common.info.select-files-to-search')
148+
)
149+
return
150+
}
151+
list_quick_pick.hide()
152+
resolve_once({ search: selected_paths })
122153
}
123154
})
124155

125156
list_quick_pick.onDidTriggerItemButton(async (e) => {
126-
if (e.button.tooltip === 'Go to file') {
157+
if (
158+
e.button.tooltip === 'Go to file' ||
159+
e.button.tooltip === t('common.go-to-file')
160+
) {
127161
const uri = vscode.Uri.file(e.item.file_path)
128162
vscode.window.showTextDocument(uri, {
129163
preview: true,
@@ -204,6 +238,37 @@ export const select_context_paths = async (params: {
204238
if (!list_selection) return undefined
205239
if (list_selection === 'back') return 'back'
206240

241+
if (!Array.isArray(list_selection) && 'search' in list_selection) {
242+
const search_result = await search_files({
243+
get_files: async () => list_selection.search,
244+
workspace_provider: params.workspace_provider,
245+
extension_context: params.extension_context,
246+
websocket_manager: params.websocket_manager,
247+
show_back_button: true,
248+
is_sub_search: true
249+
})
250+
251+
if (search_result === 'back') {
252+
restored_selected_paths = new Set(list_selection.search)
253+
continue
254+
}
255+
256+
if (!search_result) {
257+
return undefined
258+
}
259+
260+
const select_paths_set = new Set(resolved_paths)
261+
const current_selected_files =
262+
params.workspace_provider.get_selected_files()
263+
const final_selected = current_selected_files
264+
.filter((p) => !select_paths_set.has(p))
265+
.concat(search_result.selected_paths)
266+
await params.workspace_provider.set_selected_files([
267+
...new Set(final_selected)
268+
])
269+
return undefined
270+
}
271+
207272
if (Array.isArray(list_selection)) {
208273
const new_selected = list_selection.map((i) => i.file_path)
209274
const select_paths_set = new Set(resolved_paths)

‎apps/editor/src/features/search-files/search-modes/perform-intelligent-search-mode.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ export const perform_intelligent_search_mode = async (params: {
285285
} else {
286286
metadata = ` for selected files`
287287
}
288+
} else if (params.is_sub_search) {
289+
if (display_folder_path) {
290+
metadata = ` for subset of files in folder \`${display_folder_path}\``
291+
} else {
292+
metadata = ` for subset of files`
293+
}
288294
} else if (display_folder_path) {
289295
metadata = ` for folder \`${display_folder_path}\``
290296
}

0 commit comments

Comments
 (0)