Skip to content

Commit 80d17b5

Browse files
committed
Implement the ability to clear all checkpoints and their associated files via a new quick pick option
1 parent f99015e commit 80d17b5

4 files changed

Lines changed: 76 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as vscode from 'vscode'
2+
import { CHECKPOINTS_STATE_KEY } from '../../../constants/state-keys'
3+
import { get_checkpoint_path } from '../utils'
4+
import { get_checkpoints } from './get-checkpoints'
5+
6+
export const clear_all_checkpoints = async (
7+
context: vscode.ExtensionContext
8+
) => {
9+
const checkpoints = await get_checkpoints(context)
10+
11+
for (const checkpoint of checkpoints) {
12+
try {
13+
const checkpoint_path = get_checkpoint_path(checkpoint.timestamp)
14+
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
15+
recursive: true
16+
})
17+
} catch (error) {
18+
console.warn(`Could not delete checkpoint file: ${error}`)
19+
}
20+
}
21+
22+
await context.workspaceState.update(CHECKPOINTS_STATE_KEY, [])
23+
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import * as vscode from 'vscode'
2-
import { CHECKPOINTS_STATE_KEY } from '../../../constants/state-keys'
2+
import {
3+
CHECKPOINTS_STATE_KEY,
4+
TEMPORARY_CHECKPOINT_STATE_KEY
5+
} from '../../../constants/state-keys'
36
import { WorkspaceProvider } from '../../../context/providers/workspace-provider'
47
import type { Checkpoint } from '../types'
58
import { get_checkpoints } from './get-checkpoints'
@@ -22,6 +25,25 @@ export const create_checkpoint = async (
2225
description?: string
2326
): Promise<Checkpoint | undefined> => {
2427
try {
28+
const old_temp_checkpoint = context.workspaceState.get<Checkpoint>(
29+
TEMPORARY_CHECKPOINT_STATE_KEY
30+
)
31+
if (old_temp_checkpoint) {
32+
try {
33+
const checkpoint_path = get_checkpoint_path(
34+
old_temp_checkpoint.timestamp
35+
)
36+
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
37+
recursive: true
38+
})
39+
} catch (error) {
40+
console.warn(`Could not delete old temporary checkpoint file: ${error}`)
41+
}
42+
await context.workspaceState.update(
43+
TEMPORARY_CHECKPOINT_STATE_KEY,
44+
undefined
45+
)
46+
}
2547
const timestamp = Date.now()
2648
const workspace_folders = vscode.workspace.workspaceFolders!
2749

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './create-checkpoint'
22
export * from './create-temporary-checkpoint'
33
export * from './delete-checkpoint'
4+
export * from './clear-all-checkpoints'
45
export * from './get-checkpoints'
56
export * from './restore-checkpoint'
67
export * from './sync-workspace-from-dir'

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import relativeTime from 'dayjs/plugin/relativeTime'
99
import type { Checkpoint } from './types'
1010
import {
1111
create_checkpoint,
12+
clear_all_checkpoints,
1213
delete_checkpoint,
1314
get_checkpoints,
1415
restore_checkpoint
@@ -116,6 +117,15 @@ export const checkpoints_command = (
116117
label: '$(add) New checkpoint',
117118
alwaysShow: true
118119
},
120+
...(visible_checkpoints.length > 0
121+
? [
122+
{
123+
id: 'clear-all',
124+
label: '$(trash) Clear all checkpoints',
125+
alwaysShow: true
126+
}
127+
]
128+
: []),
119129
...(revert_item ? [revert_item] : []),
120130
...(visible_checkpoints.length > 0
121131
? [
@@ -194,6 +204,25 @@ export const checkpoints_command = (
194204
TEMPORARY_CHECKPOINT_STATE_KEY,
195205
undefined
196206
)
207+
} else if (selected.id == 'clear-all') {
208+
quick_pick.hide()
209+
const confirmation = await vscode.window.showWarningMessage(
210+
'Are you sure you want to clear all checkpoints? This action is irreversible.',
211+
{ modal: true },
212+
'Clear All'
213+
)
214+
215+
if (confirmation == 'Clear All') {
216+
await clear_all_checkpoints(context)
217+
vscode.window.showInformationMessage(
218+
'All checkpoints have been cleared.'
219+
)
220+
}
221+
await context.workspaceState.update(
222+
TEMPORARY_CHECKPOINT_STATE_KEY,
223+
undefined
224+
)
225+
await show_quick_pick()
197226
} else if (selected.checkpoint) {
198227
quick_pick.hide()
199228
await restore_checkpoint({

0 commit comments

Comments
 (0)