Skip to content

Commit 63c9ce8

Browse files
committed
Refactor context saving and application flows to use consistent QuickPick menus for renaming and deleting saved contexts, replacing simple input boxes and consolidating success messages
1 parent d19eace commit 63c9ce8

5 files changed

Lines changed: 679 additions & 288 deletions

File tree

‎packages/shared/src/constants/dictionary.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ export const dictionary = {
2121
CLIPBOARD_CONTENT_APPLIED_SUCCESSFULLY:
2222
'Clipboard content applied successfully.',
2323
COPIED_TO_CLIPBOARD: 'Message copied to the clipboard.',
24-
CHECKPOINTS_COPIED_TO_CLIPBOARD:
25-
'Checkpoints copied to clipboard as logs.',
24+
CHECKPOINTS_COPIED_TO_CLIPBOARD: 'Checkpoints copied to clipboard as logs.',
2625
CHECKPOINT_DELETED: (from_now: string) =>
2726
`Checkpoint from ${from_now} deleted.`,
2827
CHECKPOINT_RESTORED: 'Checkpoint restored.',
@@ -94,10 +93,7 @@ export const dictionary = {
9493
`Selected ${count} file${count == 1 ? '' : 's'}.`,
9594
DELETED_CONTEXT_FROM_WORKSPACE_STATE: (context_name: string) =>
9695
`Deleted context "${context_name}" from workspace state`,
97-
CONTEXT_SAVED_TO_JSON: (context_name: string, workspaces: string) =>
98-
`Context "${context_name}" saved to ${workspaces}.`,
99-
CONTEXT_SAVED_TO_WORKSPACE_STATE: (context_name: string) =>
100-
`Context "${context_name}" saved to the workspace state successfully.`,
96+
CONTEXT_SAVED_SUCCESSFULLY: 'Context saved successfully.',
10197
FILES_REMAIN_CHECKED: (count: number) =>
10298
`${count} file${count == 1 ? '' : 's'} remain${
10399
count == 1 ? 's' : ''

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function apply_context_command(
6969
})
7070

7171
main_quick_pick_options.push({
72-
label: 'Other',
72+
label: 'Other...',
7373
value: 'other'
7474
})
7575

@@ -78,7 +78,7 @@ export function apply_context_command(
7878
const main_quick_pick = vscode.window.createQuickPick<
7979
vscode.QuickPickItem & { value: 'internal' | 'file' | 'other' }
8080
>()
81-
main_quick_pick.title = 'Select Source'
81+
main_quick_pick.title = 'Select Context Source'
8282
main_quick_pick.items = final_quick_pick_options
8383
main_quick_pick.placeholder = 'Select option'
8484
if (last_main_selection_value) {
@@ -125,7 +125,7 @@ export function apply_context_command(
125125
extension_context,
126126
on_context_selected
127127
)
128-
if (result === 'back') {
128+
if (result == 'back') {
129129
show_main_menu = true
130130
}
131131
} else if (main_selection.value == 'file') {
@@ -134,15 +134,15 @@ export function apply_context_command(
134134
extension_context,
135135
on_context_selected
136136
)
137-
if (result === 'back') {
137+
if (result == 'back') {
138138
show_main_menu = true
139139
}
140140
} else if (main_selection.value == 'other') {
141141
const other_quick_pick_options: (vscode.QuickPickItem & {
142142
value?: 'clipboard' | 'unstaged'
143143
})[] = [
144144
{
145-
label: 'Select files based on paths found in the clipboard text',
145+
label: 'Scan the clipboard for valid paths',
146146
value: 'clipboard'
147147
},
148148
{
@@ -154,7 +154,7 @@ export function apply_context_command(
154154
const other_quick_pick = vscode.window.createQuickPick<
155155
vscode.QuickPickItem & { value?: 'clipboard' | 'unstaged' }
156156
>()
157-
other_quick_pick.title = 'Select Source'
157+
other_quick_pick.title = 'Select Context Source'
158158
other_quick_pick.items = other_quick_pick_options
159159
other_quick_pick.placeholder = 'Select other option'
160160
other_quick_pick.buttons = [vscode.QuickInputButtons.Back]

‎packages/vscode/src/commands/apply-context-command/sources/json-file-source.ts‎

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,31 +234,71 @@ export async function handle_json_file_source(
234234

235235
if (event.button === edit_button) {
236236
is_showing_dialog = true
237-
const new_name = await vscode.window.showInputBox({
238-
prompt: 'Enter new name for context',
239-
value: item.context.name,
240-
validateInput: (value) => {
241-
if (!value.trim()) {
242-
return 'Name cannot be empty'
243-
}
244237

245-
const duplicate = file_contexts.find(
246-
(c) => c.name == value.trim() && c.name != item.context.name
247-
)
238+
const input_box = vscode.window.createInputBox()
239+
input_box.title = 'Rename Context'
240+
input_box.prompt = 'Enter new name for context'
241+
input_box.value = item.context.name
242+
input_box.buttons = [vscode.QuickInputButtons.Back]
243+
244+
const new_name = await new Promise<string | undefined | 'back'>(
245+
(resolve) => {
246+
let accepted = false
247+
let back_triggered = false
248+
const disposables: vscode.Disposable[] = []
249+
250+
const validate = (value: string): boolean => {
251+
const trimmed_value = value.trim()
252+
if (!trimmed_value) {
253+
input_box.validationMessage = 'Name cannot be empty'
254+
return false
255+
}
248256

249-
if (duplicate) {
250-
return 'A context with this name already exists'
257+
const duplicate = file_contexts.find(
258+
(c) =>
259+
c.name === trimmed_value && c.name !== item.context.name
260+
)
261+
if (duplicate) {
262+
input_box.validationMessage =
263+
'A context with this name already exists'
264+
return false
265+
}
266+
input_box.validationMessage = ''
267+
return true
251268
}
252269

253-
return null
270+
disposables.push(
271+
input_box.onDidChangeValue(validate),
272+
input_box.onDidAccept(() => {
273+
if (!validate(input_box.value)) return
274+
accepted = true
275+
resolve(input_box.value.trim())
276+
input_box.hide()
277+
}),
278+
input_box.onDidTriggerButton((button) => {
279+
if (button === vscode.QuickInputButtons.Back) {
280+
back_triggered = true
281+
resolve('back')
282+
input_box.hide()
283+
}
284+
}),
285+
input_box.onDidHide(() => {
286+
if (!accepted && !back_triggered) {
287+
resolve(undefined)
288+
}
289+
disposables.forEach((d) => d.dispose())
290+
input_box.dispose()
291+
})
292+
)
293+
input_box.show()
254294
}
255-
})
295+
)
256296
is_showing_dialog = false
257297

258298
let name_to_highlight = item.context.name
259299

260-
if (new_name?.trim()) {
261-
const trimmed_name = new_name.trim()
300+
if (new_name && new_name !== 'back') {
301+
const trimmed_name = new_name
262302
let context_updated = false
263303

264304
if (trimmed_name != item.context.name) {

‎packages/vscode/src/commands/apply-context-command/sources/workspace-state-source.ts‎

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,70 @@ export async function handle_workspace_state_source(
129129

130130
if (event.button === edit_button) {
131131
is_showing_dialog = true
132-
const new_name = await vscode.window.showInputBox({
133-
prompt: 'Enter new name for context',
134-
value: item.context.name,
135-
validateInput: (value) => {
136-
if (!value.trim()) {
137-
return 'Name cannot be empty'
132+
const input_box = vscode.window.createInputBox()
133+
input_box.title = 'Rename Context'
134+
input_box.prompt = 'Enter new name for context'
135+
input_box.value = item.context.name
136+
input_box.buttons = [vscode.QuickInputButtons.Back]
137+
138+
const new_name = await new Promise<string | undefined | 'back'>(
139+
(resolve) => {
140+
let accepted = false
141+
let back_triggered = false
142+
const disposables: vscode.Disposable[] = []
143+
144+
const validate = (value: string): boolean => {
145+
const trimmed_value = value.trim()
146+
if (!trimmed_value) {
147+
input_box.validationMessage = 'Name cannot be empty'
148+
return false
149+
}
150+
151+
const duplicate = internal_contexts.find(
152+
(c) =>
153+
c.name === trimmed_value && c.name !== item.context.name
154+
)
155+
if (duplicate) {
156+
input_box.validationMessage =
157+
'A context with this name already exists'
158+
return false
159+
}
160+
input_box.validationMessage = ''
161+
return true
138162
}
139163

140-
const duplicate = internal_contexts.find(
141-
(c) => c.name == value.trim() && c.name != item.context.name
164+
disposables.push(
165+
input_box.onDidChangeValue(validate),
166+
input_box.onDidAccept(() => {
167+
if (!validate(input_box.value)) return
168+
accepted = true
169+
resolve(input_box.value.trim())
170+
input_box.hide()
171+
}),
172+
input_box.onDidTriggerButton((button) => {
173+
if (button === vscode.QuickInputButtons.Back) {
174+
back_triggered = true
175+
resolve('back')
176+
input_box.hide()
177+
}
178+
}),
179+
input_box.onDidHide(() => {
180+
if (!accepted && !back_triggered) {
181+
resolve(undefined)
182+
}
183+
disposables.forEach((d) => d.dispose())
184+
input_box.dispose()
185+
})
142186
)
143-
144-
if (duplicate) {
145-
return 'A context with this name already exists'
146-
}
147-
148-
return null
187+
input_box.show()
149188
}
150-
})
189+
)
151190
is_showing_dialog = false
152191

153192
let name_to_highlight = item.context.name
154193

155-
if (new_name?.trim()) {
156-
const trimmed_name = new_name.trim()
194+
if (new_name && new_name !== 'back') {
195+
const trimmed_name = new_name
157196
let context_updated = false
158197

159198
if (trimmed_name != item.context.name) {

0 commit comments

Comments
 (0)