Skip to content

Commit 55bd6b0

Browse files
committed
Refactor template creation and editing into a unified, multi-step quick pick flow, replacing the previous sequential input boxes for a better user experience
1 parent 9f7fab5 commit 55bd6b0

1 file changed

Lines changed: 56 additions & 62 deletions

File tree

‎packages/vscode/src/views/panel/backend/message-handlers/handle-show-prompt-template-quick-pick.ts‎

Lines changed: 56 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -131,31 +131,37 @@ export const handle_show_prompt_template_quick_pick = async (
131131

132132
const edit_template = async (
133133
template: PromptTemplate,
134-
index: number
135-
): Promise<boolean> => {
134+
title: string
135+
): Promise<{
136+
updated_template: PromptTemplate | null
137+
cancelled_entirely: boolean
138+
}> => {
136139
const NAME_LABEL = 'Name'
137140
const TEMPLATE_LABEL = 'Template'
138141

139-
const create_edit_options = () => {
142+
const create_edit_options = (current_template: PromptTemplate) => {
143+
144+
140145
return [
141146
{
142147
label: NAME_LABEL,
143-
detail: template.name || 'Not set'
148+
detail: current_template.name
144149
},
145150
{
146151
label: TEMPLATE_LABEL,
147-
detail: template.template
152+
description: !current_template.template? 'required' : undefined,
153+
detail: current_template.template
148154
}
149155
]
150156
}
151157

152158
const edit_quick_pick = vscode.window.createQuickPick()
153-
edit_quick_pick.items = create_edit_options()
154-
edit_quick_pick.title = 'Edit Template'
159+
edit_quick_pick.items = create_edit_options(template)
160+
edit_quick_pick.title = title
155161
edit_quick_pick.placeholder = 'Select what to edit'
156162
edit_quick_pick.buttons = [vscode.QuickInputButtons.Back]
157163

158-
return new Promise<boolean>((resolve) => {
164+
return new Promise((resolve) => {
159165
let is_accepted = false
160166
let did_trigger_back = false
161167
const edit_disposables: vscode.Disposable[] = []
@@ -165,15 +171,15 @@ export const handle_show_prompt_template_quick_pick = async (
165171
if (button === vscode.QuickInputButtons.Back) {
166172
did_trigger_back = true
167173
edit_quick_pick.hide()
168-
resolve(false) // User clicked 'Back', do not cancel the main quick pick
174+
resolve({ updated_template: template, cancelled_entirely: false })
169175
}
170176
}),
171177
edit_quick_pick.onDidAccept(async () => {
172178
is_accepted = true
173179
const selected = edit_quick_pick.selectedItems[0]
174180
if (!selected) {
175181
edit_quick_pick.hide()
176-
resolve(false) // User cancelled, do not cancel the main quick pick
182+
resolve({ updated_template: template, cancelled_entirely: false })
177183
return
178184
}
179185

@@ -195,20 +201,11 @@ export const handle_show_prompt_template_quick_pick = async (
195201
delete updated_template.name
196202
}
197203

198-
prompt_templates[index] = updated_template
199-
await config.update(
200-
prompt_templates_key,
201-
prompt_templates,
202-
vscode.ConfigurationTarget.Global
203-
)
204204
next_template_state = updated_template
205205
}
206206

207-
const should_cancel_entirely = await edit_template(
208-
next_template_state,
209-
index
210-
)
211-
resolve(should_cancel_entirely)
207+
const result = await edit_template(next_template_state, title)
208+
resolve(result)
212209
} else if (selected.label == TEMPLATE_LABEL) {
213210
const new_template_text = await vscode.window.showInputBox({
214211
prompt: 'Enter the prompt template',
@@ -223,30 +220,20 @@ export const handle_show_prompt_template_quick_pick = async (
223220
...template,
224221
template: new_template_text.trim()
225222
}
226-
prompt_templates[index] = updated_template
227-
await config.update(
228-
prompt_templates_key,
229-
prompt_templates,
230-
vscode.ConfigurationTarget.Global
231-
)
232223
next_template_state = updated_template
233224
}
234225

235-
const should_cancel_entirely = await edit_template(
236-
next_template_state,
237-
index
238-
)
239-
resolve(should_cancel_entirely)
226+
const result = await edit_template(next_template_state, title)
227+
resolve(result)
240228
}
241229
}),
242230
edit_quick_pick.onDidHide(() => {
243231
if (!is_accepted && !did_trigger_back) {
244-
resolve(false) // If quick pick is hidden without accepting (e.g. Escape), treat as 'Back'
232+
resolve({ updated_template: template, cancelled_entirely: false })
245233
}
246234
edit_disposables.forEach((d) => d.dispose())
247235
})
248236
)
249-
250237
edit_quick_pick.show()
251238
})
252239
}
@@ -267,30 +254,30 @@ export const handle_show_prompt_template_quick_pick = async (
267254

268255
if (selected_template.label == ADD_NEW_TEMPLATE_LABEL) {
269256
is_editing_template = true
270-
const template_text = await vscode.window.showInputBox({
271-
prompt: 'Enter the prompt template',
272-
placeHolder:
273-
'E.g., Rewrite {function name} without redundant comments'
274-
})
275-
if (template_text !== undefined && template_text.trim()) {
276-
const new_template: PromptTemplate = {
277-
template: template_text.trim()
278-
}
279-
prompt_templates = [...prompt_templates, new_template]
257+
const { updated_template, cancelled_entirely } = await edit_template(
258+
{ template: '' },
259+
'New Template'
260+
)
280261

281-
await config.update(
282-
prompt_templates_key,
262+
if (cancelled_entirely) {
263+
templates_quick_pick.hide()
264+
is_disposed = true
265+
} else {
266+
if (updated_template?.template.trim()) {
267+
prompt_templates.unshift(updated_template)
268+
await config.update(
269+
prompt_templates_key,
270+
prompt_templates,
271+
vscode.ConfigurationTarget.Global
272+
)
273+
}
274+
templates_quick_pick.items = create_template_items(
283275
prompt_templates,
284-
vscode.ConfigurationTarget.Global
276+
templates_quick_pick.value
285277
)
286-
}
287-
templates_quick_pick.items = create_template_items(
288-
prompt_templates,
289-
templates_quick_pick.value
290-
)
291-
is_editing_template = false
292-
if (!is_disposed) {
293-
templates_quick_pick.show()
278+
if (!is_disposed) {
279+
templates_quick_pick.show()
280+
}
294281
}
295282
} else if (
296283
'template' in selected_template &&
@@ -374,16 +361,24 @@ export const handle_show_prompt_template_quick_pick = async (
374361

375362
if (event.button === edit_button) {
376363
is_editing_template = true
377-
const user_cancelled_entirely = await edit_template(
364+
const { updated_template, cancelled_entirely } = await edit_template(
378365
item.template,
379-
item.index
366+
'Edit Template'
380367
)
381368

382-
if (user_cancelled_entirely) {
383-
// User cancelled editing completely (e.g., pressed Escape on the edit quick pick or an input box)
369+
if (cancelled_entirely) {
384370
templates_quick_pick.hide()
385371
is_disposed = true
386372
} else {
373+
if (updated_template) {
374+
prompt_templates[item.index] = updated_template
375+
await config.update(
376+
prompt_templates_key,
377+
prompt_templates,
378+
vscode.ConfigurationTarget.Global
379+
)
380+
}
381+
387382
// User clicked 'Back' from the edit quick pick, or successfully edited and returned to it.
388383
// We need to re-show the main templates_quick_pick.
389384
templates_quick_pick.items = create_template_items(
@@ -427,16 +422,15 @@ export const handle_show_prompt_template_quick_pick = async (
427422
// Handle undo asynchronously without blocking the UI
428423
const undo_button_text = 'Undo'
429424
notification_count++
430-
const deletion_message = is_unnamed // NOSONAR
425+
const deletion_message = is_unnamed
431426
? dictionary.information_message.UNNAMED_TEMPLATE_DELETED
432427
: dictionary.information_message.NAMED_TEMPLATE_DELETED(
433428
template_name
434429
)
435430

436431
vscode.window
437432
.showInformationMessage(deletion_message, undo_button_text)
438-
.then(async (undo_result) => { // NOSONAR
439-
// Dialog is gone
433+
.then(async (undo_result) => {
440434
notification_count--
441435
if (undo_result === undo_button_text && deleted_template) {
442436
prompt_templates.splice(original_index, 0, deleted_template)

0 commit comments

Comments
 (0)