Skip to content

Commit f803c6a

Browse files
committed
Refactor context and checkpoint management quick picks to use unified UI logic for editing and deleting items, enabling immediate optimistic deletion with revert options for both saved contexts and checkpoints
1 parent b0f6ac0 commit f803c6a

7 files changed

Lines changed: 281 additions & 165 deletions

File tree

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

Lines changed: 146 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,14 @@ export function apply_context_command(
364364
value: 'clipboard' | 'internal' | 'file'
365365
})[] = [
366366
{
367-
label: 'Find file paths in the clipboard text',
368-
description: 'Useful when asking AI for a list of relevant files',
367+
label: 'Paths from clipboard text',
368+
description: 'utility',
369369
value: 'clipboard'
370-
}
370+
},
371+
{
372+
label: 'Save location',
373+
kind: vscode.QuickPickItemKind.Separator
374+
} as any
371375
]
372376

373377
if (internal_contexts.length > 0) {
@@ -541,6 +545,7 @@ export function apply_context_command(
541545
}
542546

543547
let is_showing_dialog = false
548+
let go_back_after_delete = false
544549
const quick_pick_promise = new Promise<
545550
| (vscode.QuickPickItem & {
546551
context?: SavedContext
@@ -568,6 +573,10 @@ export function apply_context_command(
568573
if (is_showing_dialog) {
569574
return
570575
}
576+
if (go_back_after_delete) {
577+
resolve({ label: BACK_LABEL })
578+
return
579+
}
571580
if (!is_accepted) {
572581
resolve(undefined)
573582
}
@@ -703,9 +712,6 @@ export function apply_context_command(
703712
}
704713

705714
if (context_updated) {
706-
vscode.window.showInformationMessage(
707-
`Renamed context to "${trimmed_name}".`
708-
)
709715
await extension_context.workspaceState.update(
710716
last_selected_context_name_key,
711717
trimmed_name
@@ -730,107 +736,162 @@ export function apply_context_command(
730736
}
731737

732738
if (event.button === delete_button) {
733-
is_showing_dialog = true
734-
const confirm_delete = await vscode.window.showWarningMessage(
735-
dictionary.warning_message.CONFIRM_DELETE_CONTEXT(
736-
item.context.name
737-
),
738-
{ modal: true },
739-
'Delete'
740-
)
741-
is_showing_dialog = false
742-
743-
if (confirm_delete == 'Delete') {
744-
if (context_source == 'internal') {
745-
const updated_contexts = internal_contexts.filter(
746-
(c) => c.name != item.context.name
747-
)
748-
739+
const deleted_context = item.context
740+
const deleted_context_name = item.context.name
741+
const deleted_index = item.index
742+
743+
if (context_source == 'internal') {
744+
const updated_contexts = internal_contexts.filter(
745+
(c) => c.name != deleted_context_name
746+
)
747+
await extension_context.workspaceState.update(
748+
SAVED_CONTEXTS_STATE_KEY,
749+
updated_contexts
750+
)
751+
internal_contexts = updated_contexts
752+
quick_pick.items = create_quick_pick_items(internal_contexts)
753+
754+
is_showing_dialog = true
755+
const choice = await vscode.window.showInformationMessage(
756+
`Deleted context "${deleted_context_name}" from workspace state`,
757+
'Revert'
758+
)
759+
is_showing_dialog = false
760+
761+
if (choice === 'Revert') {
762+
internal_contexts.splice(deleted_index, 0, deleted_context)
749763
await extension_context.workspaceState.update(
750764
SAVED_CONTEXTS_STATE_KEY,
751-
updated_contexts
765+
internal_contexts
752766
)
753-
internal_contexts = updated_contexts
754-
755767
vscode.window.showInformationMessage(
756-
`Deleted context "${item.context.name}" from workspace state`
768+
`Restored context "${deleted_context_name}".`
757769
)
770+
quick_pick.items =
771+
create_quick_pick_items(internal_contexts)
772+
}
758773

759-
if (internal_contexts.length == 0) {
760-
quick_pick.hide()
761-
vscode.window.showInformationMessage(
762-
dictionary.information_message
763-
.NO_SAVED_CONTEXTS_IN_WORKSPACE_STATE
774+
if (internal_contexts.length === 0) {
775+
await vscode.window.showInformationMessage(
776+
dictionary.information_message
777+
.NO_SAVED_CONTEXTS_IN_WORKSPACE_STATE
778+
)
779+
go_back_after_delete = true
780+
quick_pick.hide()
781+
} else {
782+
quick_pick.show()
783+
}
784+
} else if (context_source == 'file') {
785+
const roots = context_to_roots.get(deleted_context_name) || []
786+
const original_file_contents = new Map<string, string>()
787+
788+
for (const root of roots) {
789+
const contexts_file_path = path.join(
790+
root,
791+
'.vscode',
792+
'contexts.json'
793+
)
794+
if (fs.existsSync(contexts_file_path)) {
795+
original_file_contents.set(
796+
contexts_file_path,
797+
fs.readFileSync(contexts_file_path, 'utf8')
764798
)
765-
} else {
766-
quick_pick.items =
767-
create_quick_pick_items(internal_contexts)
768-
quick_pick.show()
769799
}
770-
} else if (context_source == 'file') {
771-
const roots = context_to_roots.get(item.context.name) || []
772-
for (const root of roots) {
773-
const contexts_file_path = path.join(
774-
root,
775-
'.vscode',
776-
'contexts.json'
777-
)
778-
779-
try {
780-
if (fs.existsSync(contexts_file_path)) {
781-
const content = fs.readFileSync(
782-
contexts_file_path,
783-
'utf8'
784-
)
785-
let root_contexts = JSON.parse(content)
800+
}
786801

787-
if (!Array.isArray(root_contexts)) {
788-
root_contexts = []
789-
}
802+
for (const root of roots) {
803+
const contexts_file_path = path.join(
804+
root,
805+
'.vscode',
806+
'contexts.json'
807+
)
808+
try {
809+
if (fs.existsSync(contexts_file_path)) {
810+
const content = fs.readFileSync(
811+
contexts_file_path,
812+
'utf8'
813+
)
814+
let root_contexts = JSON.parse(content)
815+
if (!Array.isArray(root_contexts)) root_contexts = []
790816

791-
root_contexts = root_contexts.filter(
792-
(c: SavedContext) => c.name !== item.context.name
793-
)
817+
root_contexts = root_contexts.filter(
818+
(c: SavedContext) => c.name !== deleted_context_name
819+
)
820+
await save_contexts_to_file(
821+
root_contexts,
822+
contexts_file_path
823+
)
824+
}
825+
} catch (error: any) {
826+
vscode.window.showErrorMessage(
827+
`Error deleting context from ${path.basename(root)}: ${
828+
error.message
829+
}`
830+
)
831+
}
832+
}
794833

795-
await save_contexts_to_file(
796-
root_contexts,
797-
contexts_file_path
798-
)
799-
}
834+
let reloaded = await load_and_merge_file_contexts()
835+
file_contexts.length = 0
836+
file_contexts.push(...reloaded.merged)
837+
context_to_roots.clear()
838+
reloaded.context_to_roots.forEach((v, k) =>
839+
context_to_roots.set(k, v)
840+
)
841+
quick_pick.items = create_quick_pick_items(file_contexts)
842+
843+
is_showing_dialog = true
844+
const choice = await vscode.window.showInformationMessage(
845+
`Deleted context "${deleted_context_name}" from all workspace roots`,
846+
'Revert'
847+
)
848+
is_showing_dialog = false
849+
850+
if (choice === 'Revert') {
851+
let success = true
852+
for (const [
853+
file_path,
854+
content
855+
] of original_file_contents.entries()) {
856+
try {
857+
const original_contexts = JSON.parse(content)
858+
await save_contexts_to_file(
859+
original_contexts,
860+
file_path
861+
)
800862
} catch (error: any) {
801863
vscode.window.showErrorMessage(
802-
`Error deleting context from ${path.basename(
803-
root
804-
)}: ${error.message}`
864+
`Failed to revert context deletion in ${file_path}: ${error.message}`
805865
)
866+
success = false
806867
}
807868
}
808869

809-
const reloaded = await load_and_merge_file_contexts()
810-
file_contexts.length = 0
811-
file_contexts.push(...reloaded.merged)
812-
context_to_roots.clear()
813-
reloaded.context_to_roots.forEach((v, k) =>
814-
context_to_roots.set(k, v)
815-
)
816-
817-
vscode.window.showInformationMessage(
818-
`Deleted context "${item.context.name}" from all workspace roots`
819-
)
820-
821-
if (file_contexts.length == 0) {
822-
quick_pick.hide()
870+
if (success) {
871+
reloaded = await load_and_merge_file_contexts()
872+
file_contexts.length = 0
873+
file_contexts.push(...reloaded.merged)
874+
context_to_roots.clear()
875+
reloaded.context_to_roots.forEach((v, k) =>
876+
context_to_roots.set(k, v)
877+
)
823878
vscode.window.showInformationMessage(
824-
dictionary.information_message
825-
.NO_SAVED_CONTEXTS_IN_JSON_FILE
879+
`Restored context "${deleted_context_name}".`
826880
)
827-
} else {
828881
quick_pick.items = create_quick_pick_items(file_contexts)
829-
quick_pick.show()
830882
}
831883
}
832-
} else {
833-
quick_pick.show()
884+
885+
if (file_contexts.length === 0) {
886+
await vscode.window.showInformationMessage(
887+
dictionary.information_message
888+
.NO_SAVED_CONTEXTS_IN_JSON_FILE
889+
)
890+
go_back_after_delete = true
891+
quick_pick.hide()
892+
} else {
893+
quick_pick.show()
894+
}
834895
}
835896
return
836897
}
Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import * as vscode from 'vscode'
2-
import dayjs from 'dayjs'
32
import { CHECKPOINTS_STATE_KEY } from '../../../constants/state-keys'
43
import type { Checkpoint } from '../types'
54
import { get_checkpoint_path } from '../utils'
65

76
export const delete_checkpoint = async (params: {
87
context: vscode.ExtensionContext
98
checkpoint_to_delete: Checkpoint
10-
options?: { skip_undo_prompt?: boolean }
119
}) => {
12-
// First, remove checkpoint from state. This makes the deletion immediate from user's perspective.
10+
// Remove from state
1311
const checkpoints =
1412
params.context.workspaceState.get<Checkpoint[]>(
1513
CHECKPOINTS_STATE_KEY,
@@ -23,50 +21,15 @@ export const delete_checkpoint = async (params: {
2321
updated_checkpoints
2422
)
2523

26-
const actually_delete_files = async () => {
27-
try {
28-
const checkpoint_path = get_checkpoint_path(
29-
params.checkpoint_to_delete.timestamp
30-
)
31-
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
32-
recursive: true
33-
})
34-
} catch (error) {
35-
console.warn(`Could not delete checkpoint file: ${error}`)
36-
}
37-
}
38-
39-
if (!params.options?.skip_undo_prompt) {
40-
vscode.window
41-
.showInformationMessage(
42-
`Checkpoint from ${dayjs(
43-
params.checkpoint_to_delete.timestamp
44-
).fromNow()} deleted successfully.`,
45-
'Revert'
46-
)
47-
.then(async (action) => {
48-
if (action == 'Revert') {
49-
// User chose to revert. Add the checkpoint back to the state.
50-
const current_checkpoints =
51-
params.context.workspaceState.get<Checkpoint[]>(
52-
CHECKPOINTS_STATE_KEY,
53-
[]
54-
) ?? []
55-
current_checkpoints.push(params.checkpoint_to_delete)
56-
await params.context.workspaceState.update(
57-
CHECKPOINTS_STATE_KEY,
58-
current_checkpoints
59-
)
60-
vscode.window.showInformationMessage('Checkpoint deletion reverted.')
61-
} else {
62-
// User dismissed the notification or it timed out.
63-
// Proceed with deleting the checkpoint files.
64-
await actually_delete_files()
65-
}
66-
})
67-
} else {
68-
// This is a silent deletion (e.g. temporary checkpoint cleanup), no need
69-
// to ask for revert.
70-
await actually_delete_files()
24+
// Delete files
25+
try {
26+
const checkpoint_path = get_checkpoint_path(
27+
params.checkpoint_to_delete.timestamp
28+
)
29+
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
30+
recursive: true
31+
})
32+
} catch (error) {
33+
console.warn(`Could not delete checkpoint file: ${error}`)
7134
}
7235
}

‎packages/vscode/src/commands/checkpoints-command/actions/edit-checkpoint.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ export const edit_checkpoint = async (params: {
2828
CHECKPOINTS_STATE_KEY,
2929
checkpoints
3030
)
31-
vscode.window.showInformationMessage('Checkpoint description updated.')
3231
}
3332
}

‎packages/vscode/src/commands/checkpoints-command/actions/index.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export * from './copy-workspace-to-dir'
22
export * from './create-checkpoint'
33
export * from './create-temporary-checkpoint'
44
export * from './delete-checkpoint'
5-
export * from './edit-checkpoint'
65
export * from './get-checkpoints'
76
export * from './promote-temporary-checkpoint'
87
export * from './restore-checkpoint'

0 commit comments

Comments
 (0)