Skip to content

Commit 05f489d

Browse files
committed
Refactor checkpoint and prompt template deletion logic to use asynchronous undo notifications instead of modal confirmation dialogs, update the Discord icon SVG and color, and optimize checkpoint creation by pre-checking Git status
1 parent ecd456a commit 05f489d

5 files changed

Lines changed: 172 additions & 161 deletions

File tree

Lines changed: 2 additions & 11 deletions
Loading

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

Lines changed: 85 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -23,93 +23,105 @@ export const create_checkpoint = async (
2323
): Promise<boolean> => {
2424
try {
2525
const timestamp = Date.now()
26-
await vscode.window.withProgress(
27-
{
28-
location: vscode.ProgressLocation.Notification,
29-
title: 'Creating checkpoint...',
30-
cancellable: false
31-
},
32-
async () => {
33-
await vscode.workspace.saveAll()
34-
const checkpoint_dir_path = get_checkpoint_path(timestamp)
35-
const checkpoint_dir_uri = vscode.Uri.file(checkpoint_dir_path)
36-
await vscode.workspace.fs.createDirectory(checkpoint_dir_uri)
26+
const workspace_folders = vscode.workspace.workspaceFolders!
3727

38-
const workspace_folders = vscode.workspace.workspaceFolders!
39-
const git_data: Record<string, any> = {}
40-
let uses_git = false
28+
const folder_git_statuses = await Promise.all(
29+
workspace_folders.map((folder) => is_git_repository(folder))
30+
)
31+
const all_folders_use_git = folder_git_statuses.every((status) => status)
32+
33+
const create_checkpoint_task = async () => {
34+
await vscode.workspace.saveAll()
35+
const checkpoint_dir_path = get_checkpoint_path(timestamp)
36+
const checkpoint_dir_uri = vscode.Uri.file(checkpoint_dir_path)
37+
await vscode.workspace.fs.createDirectory(checkpoint_dir_uri)
4138

42-
for (const folder of workspace_folders) {
43-
const has_git = await is_git_repository(folder)
39+
const git_data: Record<string, any> = {}
40+
let uses_git = false
4441

45-
if (has_git) {
46-
uses_git = true
47-
const git_info = await get_git_info(folder)
48-
const diff = await get_git_diff(folder)
42+
for (const [index, folder] of workspace_folders.entries()) {
43+
const has_git = folder_git_statuses[index]
4944

50-
if (git_info && diff !== null) {
51-
const folder_name = folder.name
52-
git_data[folder_name] = {
53-
branch: git_info.branch,
54-
commit_hash: git_info.commit_hash,
55-
folder_name
56-
}
45+
if (has_git) {
46+
uses_git = true
47+
const git_info = await get_git_info(folder)
48+
const diff = await get_git_diff(folder)
5749

58-
const diff_file_path = path.join(
59-
checkpoint_dir_path,
60-
`${folder_name}.diff`
61-
)
62-
await vscode.workspace.fs.writeFile(
63-
vscode.Uri.file(diff_file_path),
64-
Buffer.from(diff, 'utf8')
65-
)
66-
} else {
67-
throw new Error(
68-
`Failed to get git information for repository in ${folder.name}.`
69-
)
50+
if (git_info && diff !== null) {
51+
const folder_name = folder.name
52+
git_data[folder_name] = {
53+
branch: git_info.branch,
54+
commit_hash: git_info.commit_hash,
55+
folder_name
7056
}
57+
58+
const diff_file_path = path.join(
59+
checkpoint_dir_path,
60+
`${folder_name}.diff`
61+
)
62+
await vscode.workspace.fs.writeFile(
63+
vscode.Uri.file(diff_file_path),
64+
Buffer.from(diff, 'utf8')
65+
)
7166
} else {
72-
const dest_folder_path =
73-
workspace_folders.length > 1
74-
? path.join(checkpoint_dir_path, folder.name)
75-
: checkpoint_dir_path
76-
const dest_folder_uri = vscode.Uri.file(dest_folder_path)
77-
if (workspace_folders.length > 1) {
78-
await vscode.workspace.fs.createDirectory(dest_folder_uri)
79-
}
67+
throw new Error(
68+
`Failed to get git information for repository in ${folder.name}.`
69+
)
70+
}
71+
} else {
72+
const dest_folder_path =
73+
workspace_folders.length > 1
74+
? path.join(checkpoint_dir_path, folder.name)
75+
: checkpoint_dir_path
76+
const dest_folder_uri = vscode.Uri.file(dest_folder_path)
77+
if (workspace_folders.length > 1) {
78+
await vscode.workspace.fs.createDirectory(dest_folder_uri)
79+
}
8080

81-
const entries = await vscode.workspace.fs.readDirectory(folder.uri)
82-
for (const [name] of entries) {
83-
await copy_optimised_recursively(
84-
vscode.Uri.joinPath(folder.uri, name),
85-
vscode.Uri.joinPath(dest_folder_uri, name),
86-
folder.uri.fsPath,
87-
workspace_provider
88-
)
89-
}
81+
const entries = await vscode.workspace.fs.readDirectory(folder.uri)
82+
for (const [name] of entries) {
83+
await copy_optimised_recursively(
84+
vscode.Uri.joinPath(folder.uri, name),
85+
vscode.Uri.joinPath(dest_folder_uri, name),
86+
folder.uri.fsPath,
87+
workspace_provider
88+
)
9089
}
9190
}
91+
}
9292

93-
const checkpoints = await get_checkpoints(context)
94-
95-
const final_description = get_incremented_description(
96-
title,
97-
description,
98-
checkpoints
99-
)
93+
const checkpoints = await get_checkpoints(context)
10094

101-
const new_checkpoint: Checkpoint = {
102-
title,
103-
timestamp,
104-
description: final_description,
105-
uses_git,
106-
git_data: Object.keys(git_data).length > 0 ? git_data : undefined
107-
}
95+
const final_description = get_incremented_description(
96+
title,
97+
description,
98+
checkpoints
99+
)
108100

109-
checkpoints.unshift(new_checkpoint)
110-
await context.workspaceState.update(CHECKPOINTS_STATE_KEY, checkpoints)
101+
const new_checkpoint: Checkpoint = {
102+
title,
103+
timestamp,
104+
description: final_description,
105+
uses_git,
106+
git_data: Object.keys(git_data).length > 0 ? git_data : undefined
111107
}
112-
)
108+
109+
checkpoints.unshift(new_checkpoint)
110+
await context.workspaceState.update(CHECKPOINTS_STATE_KEY, checkpoints)
111+
}
112+
113+
if (all_folders_use_git) {
114+
await create_checkpoint_task()
115+
} else {
116+
await vscode.window.withProgress(
117+
{
118+
location: vscode.ProgressLocation.Notification,
119+
title: 'Creating checkpoint...',
120+
cancellable: false
121+
},
122+
create_checkpoint_task
123+
)
124+
}
113125
return true
114126
} catch (err: any) {
115127
vscode.window.showErrorMessage(

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

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ export const checkpoints_command = (
2323
workspace_provider: WorkspaceProvider,
2424
context: vscode.ExtensionContext
2525
): vscode.Disposable[] => {
26+
let activeDeleteOperation: {
27+
finalize: () => Promise<void>
28+
timestamp: number
29+
} | null = null
30+
2631
const create_new_checkpoint_command = vscode.commands.registerCommand(
2732
'codeWebChat.createNewCheckpoint',
2833
async () => {
@@ -62,7 +67,7 @@ export const checkpoints_command = (
6267
quick_pick.placeholder =
6368
'Select a checkpoint to restore or add a new one'
6469

65-
let is_showing_dialog = false
70+
let notification_count = 0
6671
let checkpoints: Checkpoint[] = []
6772

6873
const refresh_items = async () => {
@@ -230,13 +235,13 @@ export const checkpoints_command = (
230235
}
231236

232237
if (e.button.tooltip == 'Edit Description') {
233-
is_showing_dialog = true
238+
notification_count++
234239
const new_description = await vscode.window.showInputBox({
235240
prompt: 'Enter a description for the checkpoint',
236241
value: item.checkpoint.description || '',
237242
placeHolder: 'e.g. Before refactoring the main component'
238243
})
239-
is_showing_dialog = false
244+
notification_count--
240245

241246
if (new_description !== undefined) {
242247
const checkpoint_to_update = checkpoints.find(
@@ -266,6 +271,10 @@ export const checkpoints_command = (
266271
}
267272

268273
if (e.button.tooltip == 'Delete') {
274+
if (activeDeleteOperation) {
275+
await activeDeleteOperation.finalize()
276+
}
277+
269278
const deleted_checkpoint = item.checkpoint
270279
const real_index_in_state = checkpoints.findIndex(
271280
(c) => c.timestamp == deleted_checkpoint.timestamp
@@ -286,50 +295,79 @@ export const checkpoints_command = (
286295
checkpoints = updated_checkpoints
287296
await refresh_items()
288297

289-
is_showing_dialog = true
298+
const currentOperation = {
299+
timestamp: deleted_checkpoint.timestamp,
300+
finalize: async () => {
301+
try {
302+
const checkpoint_path = get_checkpoint_path(
303+
deleted_checkpoint.timestamp
304+
)
305+
await vscode.workspace.fs.delete(
306+
vscode.Uri.file(checkpoint_path),
307+
{ recursive: true }
308+
)
309+
} catch (error: any) {
310+
vscode.window.showWarningMessage(
311+
`Could not delete checkpoint files: ${error.message}`
312+
)
313+
}
314+
}
315+
}
316+
activeDeleteOperation = currentOperation
317+
318+
notification_count++
290319
const choice = await vscode.window.showInformationMessage(
291320
`Checkpoint from ${dayjs(
292321
deleted_checkpoint.timestamp
293322
).fromNow()} deleted.`,
294323
'Undo'
295324
)
296-
is_showing_dialog = false
325+
notification_count--
297326

298-
if (choice == 'Undo') {
299-
// Restore to state and UI
300-
checkpoints.splice(
301-
real_index_in_state,
302-
0,
303-
original_checkpoint_from_state
304-
)
305-
await context.workspaceState.update(
306-
CHECKPOINTS_STATE_KEY,
307-
checkpoints
308-
)
309-
vscode.window.showInformationMessage('Checkpoint restored.')
310-
await refresh_items()
311-
} else {
312-
try {
313-
const checkpoint_path = get_checkpoint_path(
314-
deleted_checkpoint.timestamp
315-
)
316-
await vscode.workspace.fs.delete(
317-
vscode.Uri.file(checkpoint_path),
318-
{ recursive: true }
327+
if (
328+
activeDeleteOperation &&
329+
activeDeleteOperation.timestamp === currentOperation.timestamp
330+
) {
331+
if (choice == 'Undo') {
332+
// Restore to state and UI
333+
checkpoints.splice(
334+
real_index_in_state,
335+
0,
336+
original_checkpoint_from_state
319337
)
320-
} catch (error: any) {
321-
vscode.window.showWarningMessage(
322-
`Could not delete checkpoint files: ${error.message}`
338+
await context.workspaceState.update(
339+
CHECKPOINTS_STATE_KEY,
340+
checkpoints
323341
)
342+
notification_count++
343+
vscode.window
344+
.showInformationMessage('Checkpoint restored.')
345+
.then(() => {
346+
notification_count--
347+
})
348+
await refresh_items()
349+
} else {
350+
await currentOperation.finalize()
324351
}
352+
activeDeleteOperation = null
353+
} else if (choice === 'Undo') {
354+
notification_count++
355+
vscode.window
356+
.showInformationMessage(
357+
'Could not undo. Another checkpoint was deleted.'
358+
)
359+
.then(() => {
360+
notification_count--
361+
})
325362
}
363+
326364
quick_pick.show()
327365
return
328366
}
329367
})
330368

331369
quick_pick.onDidHide(() => {
332-
if (is_showing_dialog) {
370+
if (notification_count > 0) {
333371
return
334372
}
335373
quick_pick.dispose()

0 commit comments

Comments
 (0)