Skip to content

Commit 25ae8ed

Browse files
committed
Add commit files source option to apply context command menu
1 parent 68eb84d commit 25ae8ed

3 files changed

Lines changed: 338 additions & 63 deletions

File tree

‎packages/vscode/src/commands/apply-context-command/apply-context-command.ts‎

Lines changed: 101 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { dictionary } from '@shared/constants/dictionary'
1212
import {
1313
handle_unstaged_files_source,
1414
handle_json_file_source,
15-
handle_workspace_state_source
15+
handle_workspace_state_source,
16+
handle_commit_files_source
1617
} from './sources'
1718
import { load_all_contexts, get_contexts_file_path } from './helpers/saving'
1819
import { handle_quick_save } from './helpers/saving/handle-quick-save'
@@ -254,74 +255,111 @@ export const apply_context_command = (
254255
show_main_menu = true
255256
}
256257
} else if (main_selection.value == 'other') {
257-
const other_quick_pick_options: (vscode.QuickPickItem & {
258-
value?: 'clipboard' | 'unstaged'
259-
})[] = [
260-
{
261-
label: 'Scan the clipboard for valid paths',
262-
value: 'clipboard'
263-
},
264-
{
265-
label: 'Select unstaged files',
266-
value: 'unstaged'
267-
}
268-
]
258+
let show_other_menu = true
259+
let last_other_selection_value: string | undefined
260+
261+
while (show_other_menu) {
262+
show_other_menu = false
263+
264+
const other_quick_pick_options: (vscode.QuickPickItem & {
265+
value?: 'clipboard' | 'unstaged' | 'commit_files'
266+
})[] = [
267+
{
268+
label: 'Scan the clipboard for valid paths',
269+
value: 'clipboard'
270+
},
271+
{
272+
label: 'Unstaged files',
273+
value: 'unstaged'
274+
},
275+
{
276+
label: 'Files of a commit...',
277+
value: 'commit_files'
278+
}
279+
]
280+
281+
const other_quick_pick = vscode.window.createQuickPick<
282+
vscode.QuickPickItem & {
283+
value?: 'clipboard' | 'unstaged' | 'commit_files'
284+
}
285+
>()
286+
other_quick_pick.title = 'Context Sources'
287+
other_quick_pick.items = other_quick_pick_options
288+
other_quick_pick.placeholder = 'Select other option'
289+
other_quick_pick.buttons = [vscode.QuickInputButtons.Back]
269290

270-
const other_quick_pick = vscode.window.createQuickPick<
271-
vscode.QuickPickItem & { value?: 'clipboard' | 'unstaged' }
272-
>()
273-
other_quick_pick.title = 'Context Sources'
274-
other_quick_pick.items = other_quick_pick_options
275-
other_quick_pick.placeholder = 'Select other option'
276-
other_quick_pick.buttons = [vscode.QuickInputButtons.Back]
291+
if (last_other_selection_value) {
292+
const active_item = other_quick_pick_options.find(
293+
(opt) => opt.value === last_other_selection_value
294+
)
295+
if (active_item) {
296+
other_quick_pick.activeItems = [active_item]
297+
}
298+
}
277299

278-
const other_selection = await new Promise<
279-
| 'back'
280-
| (vscode.QuickPickItem & { value?: 'clipboard' | 'unstaged' })
281-
| undefined
282-
>((resolve) => {
283-
let is_accepted = false
284-
let did_trigger_back = false
285-
const disposables: vscode.Disposable[] = []
300+
const other_selection = await new Promise<
301+
| 'back'
302+
| (vscode.QuickPickItem & {
303+
value?: 'clipboard' | 'unstaged' | 'commit_files'
304+
})
305+
| undefined
306+
>((resolve) => {
307+
let is_accepted = false
308+
let did_trigger_back = false
309+
const disposables: vscode.Disposable[] = []
286310

287-
disposables.push(
288-
other_quick_pick.onDidTriggerButton((button) => {
289-
if (button === vscode.QuickInputButtons.Back) {
290-
did_trigger_back = true
311+
disposables.push(
312+
other_quick_pick.onDidTriggerButton((button) => {
313+
if (button === vscode.QuickInputButtons.Back) {
314+
did_trigger_back = true
315+
other_quick_pick.hide()
316+
resolve('back')
317+
}
318+
}),
319+
other_quick_pick.onDidAccept(() => {
320+
is_accepted = true
321+
resolve(other_quick_pick.selectedItems[0])
291322
other_quick_pick.hide()
292-
resolve('back')
293-
}
294-
}),
295-
other_quick_pick.onDidAccept(() => {
296-
is_accepted = true
297-
resolve(other_quick_pick.selectedItems[0])
298-
other_quick_pick.hide()
299-
}),
300-
other_quick_pick.onDidHide(() => {
301-
if (!is_accepted && !did_trigger_back) {
302-
resolve('back')
303-
}
304-
disposables.forEach((d) => d.dispose())
305-
other_quick_pick.dispose()
306-
})
307-
)
308-
other_quick_pick.show()
309-
})
323+
}),
324+
other_quick_pick.onDidHide(() => {
325+
if (!is_accepted && !did_trigger_back) {
326+
resolve('back')
327+
}
328+
disposables.forEach((d) => d.dispose())
329+
other_quick_pick.dispose()
330+
})
331+
)
332+
other_quick_pick.show()
333+
})
310334

311-
if (!other_selection || other_selection == 'back') {
312-
show_main_menu = true
313-
continue
314-
}
335+
if (!other_selection || other_selection == 'back') {
336+
show_main_menu = true
337+
break
338+
}
315339

316-
if (other_selection.value == 'clipboard') {
317-
await vscode.commands.executeCommand(
318-
'codeWebChat.findPathsInClipboard'
319-
)
320-
return
321-
}
322-
if (other_selection.value == 'unstaged') {
323-
await handle_unstaged_files_source(workspace_provider)
324-
return
340+
last_other_selection_value = other_selection.value
341+
342+
if (other_selection.value == 'clipboard') {
343+
await vscode.commands.executeCommand(
344+
'codeWebChat.findPathsInClipboard'
345+
)
346+
return
347+
}
348+
if (other_selection.value == 'unstaged') {
349+
await handle_unstaged_files_source(workspace_provider)
350+
return
351+
}
352+
if (other_selection.value == 'commit_files') {
353+
const result = await handle_commit_files_source(
354+
workspace_provider,
355+
extension_context
356+
)
357+
if (result === 'back') {
358+
show_other_menu = true
359+
continue
360+
}
361+
return
362+
}
325363
}
326364
} else if (main_selection.value.startsWith('quick_save_')) {
327365
const slot = parseInt(main_selection.value.split('_')[2])

0 commit comments

Comments
 (0)