Skip to content

Commit aeb1164

Browse files
committed
Simplify context deletion messages and prevent race conditions during undo operations
1 parent 057d0bf commit aeb1164

4 files changed

Lines changed: 60 additions & 40 deletions

File tree

packages/shared/src/constants/dictionary.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ export const dictionary = {
7575
'No "Edit Context" configurations found. Please add one in the settings.',
7676
PRESET_PREVIEW_SENT_TO_BROWSER:
7777
'Preset preview sent to the connected browser.',
78-
DELETED_CONTEXT_FROM_ALL_ROOTS: (context_name: string) =>
79-
`Deleted context "${context_name}" from all workspace roots`,
78+
DELETED_CONTEXT_FROM_ALL_ROOTS: 'Deleted context.',
8079
NO_HISTORY_FOR_MODE: 'No history to show for the current mode.',
8180
NO_TEXT_SELECTED_FOR_SELECTION_PLACEHOLDER:
8281
'No text selected for #Selection placeholder.',
@@ -91,8 +90,7 @@ export const dictionary = {
9190
'No actionable unstaged files found (e.g. only deletions).',
9291
SELECTED_FILES: (count: number) =>
9392
`Selected ${count} file${count == 1 ? '' : 's'}.`,
94-
DELETED_CONTEXT_FROM_WORKSPACE_STATE: (context_name: string) =>
95-
`Deleted context "${context_name}" from workspace state`,
93+
DELETED_CONTEXT_FROM_WORKSPACE_STATE: 'Deleted context.',
9694
CONTEXT_SAVED_SUCCESSFULLY: 'Context saved successfully.',
9795
FILES_REMAIN_CHECKED: (count: number) =>
9896
`${count} file${count == 1 ? '' : 's'} remain${
@@ -174,8 +172,8 @@ export const dictionary = {
174172
`Are you sure you want to delete this ${item_type}?`,
175173
CONFIRM_DELETE_NAMED_ITEM: (item_type: string, name: string) =>
176174
`Are you sure you want to delete ${item_type} "${name}"?`,
177-
CONTEXT_WITH_IDENTICAL_PATHS_EXISTS: (context_name: string) =>
178-
`A context with identical paths already exists in workspace state: "${context_name}"`,
175+
CONTEXT_ALREADY_SAVED: (context_name: string) =>
176+
`Context already saved: "${context_name}"`,
179177
CONFIRM_OVERWRITE_CONTEXT: (context_name: string) =>
180178
`A context named "${context_name}" already exists. Overwrite?`,
181179
CONFIRM_OVERWRITE_CONTEXT_IN_WORKSPACE_STATE: (context_name: string) =>

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { Logger } from '@shared/utils/logger'
88
import { dictionary } from '@shared/constants/dictionary'
99
import { apply_saved_context } from '../utils'
1010

11+
let active_deletion_timestamp: number | undefined
12+
1113
async function save_contexts_to_file(
1214
contexts: SavedContext[],
1315
file_path: string
@@ -171,7 +173,7 @@ export async function handle_json_file_source(
171173
}
172174
}
173175

174-
let is_showing_dialog = false
176+
let active_dialog_count = 0
175177
let go_back_after_delete = false
176178
const quick_pick_promise = new Promise<
177179
| 'back'
@@ -209,7 +211,7 @@ export async function handle_json_file_source(
209211
}),
210212

211213
quick_pick.onDidHide(() => {
212-
if (is_showing_dialog) {
214+
if (active_dialog_count > 0) {
213215
return
214216
}
215217
if (go_back_after_delete) {
@@ -233,7 +235,7 @@ export async function handle_json_file_source(
233235
)
234236

235237
if (event.button === edit_button) {
236-
is_showing_dialog = true
238+
active_dialog_count++
237239

238240
const input_box = vscode.window.createInputBox()
239241
input_box.title = 'Rename Context'
@@ -284,7 +286,7 @@ export async function handle_json_file_source(
284286
input_box.show()
285287
}
286288
)
287-
is_showing_dialog = false
289+
active_dialog_count--
288290

289291
let name_to_highlight = item.context.name
290292

@@ -374,6 +376,8 @@ export async function handle_json_file_source(
374376
}
375377

376378
if (event.button === delete_button) {
379+
const current_timestamp = Date.now()
380+
active_deletion_timestamp = current_timestamp
377381
const deleted_context_name = item.context.name
378382

379383
const roots = context_to_roots.get(deleted_context_name) || []
@@ -424,14 +428,22 @@ export async function handle_json_file_source(
424428
context_to_roots = reloaded.context_to_roots
425429
quick_pick.items = create_quick_pick_items(file_contexts)
426430

427-
is_showing_dialog = true
431+
active_dialog_count++
428432
const choice = await vscode.window.showInformationMessage(
429-
dictionary.information_message.DELETED_CONTEXT_FROM_ALL_ROOTS(
430-
deleted_context_name
431-
),
433+
dictionary.information_message.DELETED_CONTEXT_FROM_ALL_ROOTS,
432434
'Undo'
433435
)
434-
is_showing_dialog = false
436+
active_dialog_count--
437+
438+
if (active_deletion_timestamp !== current_timestamp) {
439+
if (choice === 'Undo') {
440+
vscode.window.showInformationMessage(
441+
'Could not undo as another context was deleted.'
442+
)
443+
}
444+
quick_pick.show()
445+
return
446+
}
435447

436448
if (choice == 'Undo') {
437449
let success = true

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { Logger } from '@shared/utils/logger'
99
import { dictionary } from '@shared/constants/dictionary'
1010
import { apply_saved_context } from '../utils'
1111

12+
let active_deletion_timestamp: number | undefined
13+
1214
export async function handle_workspace_state_source(
1315
workspace_provider: WorkspaceProvider,
1416
extension_context: vscode.ExtensionContext,
@@ -66,7 +68,7 @@ export async function handle_workspace_state_source(
6668
}
6769
}
6870

69-
let is_showing_dialog = false
71+
let active_dialog_count = 0
7072
let go_back_after_delete = false
7173
const quick_pick_promise = new Promise<
7274
| 'back'
@@ -104,7 +106,7 @@ export async function handle_workspace_state_source(
104106
}),
105107

106108
quick_pick.onDidHide(() => {
107-
if (is_showing_dialog) {
109+
if (active_dialog_count > 0) {
108110
return
109111
}
110112
if (go_back_after_delete) {
@@ -128,7 +130,7 @@ export async function handle_workspace_state_source(
128130
)
129131

130132
if (event.button === edit_button) {
131-
is_showing_dialog = true
133+
active_dialog_count++
132134
const input_box = vscode.window.createInputBox()
133135
input_box.title = 'Rename Context'
134136
input_box.prompt = 'Enter new name for context'
@@ -178,7 +180,7 @@ export async function handle_workspace_state_source(
178180
input_box.show()
179181
}
180182
)
181-
is_showing_dialog = false
183+
active_dialog_count--
182184

183185
let name_to_highlight = item.context.name
184186

@@ -221,6 +223,8 @@ export async function handle_workspace_state_source(
221223
}
222224

223225
if (event.button === delete_button) {
226+
const current_timestamp = Date.now()
227+
active_deletion_timestamp = current_timestamp
224228
const deleted_context = item.context
225229
const deleted_context_name = item.context.name
226230
const deleted_index = item.index
@@ -235,14 +239,23 @@ export async function handle_workspace_state_source(
235239
internal_contexts = updated_contexts
236240
quick_pick.items = create_quick_pick_items(internal_contexts)
237241

238-
is_showing_dialog = true
242+
active_dialog_count++
239243
const choice = await vscode.window.showInformationMessage(
240-
dictionary.information_message.DELETED_CONTEXT_FROM_WORKSPACE_STATE(
241-
deleted_context_name
242-
),
244+
dictionary.information_message
245+
.DELETED_CONTEXT_FROM_WORKSPACE_STATE,
243246
'Undo'
244247
)
245-
is_showing_dialog = false
248+
active_dialog_count--
249+
250+
if (active_deletion_timestamp !== current_timestamp) {
251+
if (choice === 'Undo') {
252+
vscode.window.showInformationMessage(
253+
'Could not undo as another context was deleted.'
254+
)
255+
}
256+
quick_pick.show()
257+
return
258+
}
246259

247260
if (choice == 'Undo') {
248261
internal_contexts.splice(deleted_index, 0, deleted_context)

packages/vscode/src/commands/save-context-command.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ export function save_context_command(
533533
quick_pick_contexts.title = 'Save Context'
534534
quick_pick_contexts.buttons = [vscode.QuickInputButtons.Back]
535535

536-
let is_showing_dialog = false
536+
let active_dialogs_count = 0
537537
const selected_item = await new Promise<
538538
vscode.QuickPickItem | 'back' | undefined
539539
>((resolve) => {
@@ -611,15 +611,14 @@ export function save_context_command(
611611
quick_pick_contexts.items =
612612
create_quick_pick_items(all_contexts_map)
613613

614-
is_showing_dialog = true
614+
active_dialogs_count++
615615
const choice =
616616
await vscode.window.showInformationMessage(
617-
dictionary.information_message.DELETED_CONTEXT_FROM_ALL_ROOTS(
618-
deleted_context_name
619-
),
617+
dictionary.information_message
618+
.DELETED_CONTEXT_FROM_ALL_ROOTS,
620619
'Undo'
621620
)
622-
is_showing_dialog = false
621+
active_dialogs_count--
623622

624623
if (choice == 'Undo') {
625624
for (const [
@@ -654,7 +653,7 @@ export function save_context_command(
654653
quick_pick_contexts.hide()
655654
}),
656655
quick_pick_contexts.onDidHide(() => {
657-
if (is_showing_dialog) return
656+
if (active_dialogs_count > 0) return
658657
if (!is_accepted && !did_trigger_back) {
659658
resolve('back')
660659
}
@@ -782,11 +781,10 @@ export function save_context_command(
782781
) as SavedContext[]) {
783782
if (are_paths_equal(existingContext.paths, all_prefixed_paths)) {
784783
vscode.window.showWarningMessage(
785-
dictionary.warning_message.CONTEXT_WITH_IDENTICAL_PATHS_EXISTS(
784+
dictionary.warning_message.CONTEXT_ALREADY_SAVED(
786785
existingContext.name
787786
)
788787
)
789-
return
790788
}
791789
}
792790

@@ -839,7 +837,7 @@ export function save_context_command(
839837
PLACEHOLDER_SELECT_OR_CREATE_CONTEXT
840838
quick_pick_contexts.title = 'Save Context'
841839
quick_pick_contexts.buttons = [vscode.QuickInputButtons.Back]
842-
let is_showing_dialog = false
840+
let active_dialogs_count = 0
843841
const selected_item = await new Promise<
844842
vscode.QuickPickItem | 'back' | undefined
845843
>((resolve) => {
@@ -883,14 +881,13 @@ export function save_context_command(
883881
quick_pick_contexts.items =
884882
create_quick_pick_items(saved_contexts)
885883

886-
is_showing_dialog = true
884+
active_dialogs_count++
887885
const choice = await vscode.window.showInformationMessage(
888-
dictionary.information_message.DELETED_CONTEXT_FROM_WORKSPACE_STATE(
889-
deleted_context_name
890-
),
886+
dictionary.information_message
887+
.DELETED_CONTEXT_FROM_WORKSPACE_STATE,
891888
'Undo'
892889
)
893-
is_showing_dialog = false
890+
active_dialogs_count--
894891

895892
if (choice == 'Undo') {
896893
await extContext.workspaceState.update(
@@ -918,7 +915,7 @@ export function save_context_command(
918915
quick_pick_contexts.hide()
919916
}),
920917
quick_pick_contexts.onDidHide(() => {
921-
if (is_showing_dialog) return
918+
if (active_dialogs_count > 0) return
922919
if (!is_accepted && !did_trigger_back) {
923920
resolve('back')
924921
}

0 commit comments

Comments
 (0)