Skip to content

Commit b5d91d1

Browse files
committed
Refactor checkpoint actions and update history command to support pinned checkpoints
- place `apps/editor/src/commands/history-command.ts` in a folder of the same name - files of `apps/editor/src/features/checkpoints` not used by logic outside of `apps/editor/src/commands/history-command` should be moved to `apps/editor/src/commands/history-command` - some translation keys are invalid (don't match their placement in commands VS features) - implement pinning checkpoints - place pinned checkpoints (repeat them) in a section above ("pinned" separator), normal items prefix with "recent" separator. - it's been reported that when searching, users see repeated items. should continue to show repeated items when not searching though - remove 'checkpoints' from separators, effectively shortening them - don't place pin icon in labels - use snake case for identifiers └── apps └── editor └── src ├── commands │ ├── apply-response-command │ │ └── apply-response-command.ts │ ├── generate-commit-message-commands │ │ └── actions │ │ └── run-generate-action.ts │ ├── history-command │ │ ├── actions │ │ │ ├── clear-all-checkpoints.ts │ │ │ ├── create-temporary-checkpoint.ts │ │ │ ├── delete-checkpoint-with-undo.ts │ │ │ ├── restore-checkpoint.ts │ │ │ ├── sync-workspace-from-dir.ts │ │ │ ├── toggle-checkpoint-pin.ts │ │ │ └── update-checkpoint-description.ts │ │ ├── history-command.ts │ │ ├── index.ts │ │ └── utils │ │ └── sync-directory.ts │ ├── index.ts │ └── select-referencing-files-commands │ ├── index.ts │ ├── select-referencing-files-commands.ts │ └── utils │ ├── get-referencing-files-for-position.ts │ ├── get-referencing-files-for-uris.ts │ └── prompt-for-referencing-files.ts ├── features │ └── checkpoints │ ├── actions │ │ ├── create-checkpoint.ts │ │ ├── delete-checkpoint.ts │ │ ├── get-checkpoints.ts │ │ └── index.ts │ ├── types │ │ ├── checkpoint.ts │ │ └── index.ts │ └── utils │ ├── copy-optimised-recursively.ts │ ├── get-checkpoint-path.ts │ ├── get-incremented-description.ts │ ├── git-utils.ts │ └── index.ts └── i18n └── translations ├── commands │ └── history.ts └── features └── checkpoints.ts
1 parent 166712c commit b5d91d1

18 files changed

Lines changed: 622 additions & 536 deletions

apps/editor/src/features/checkpoints/actions/clear-all-checkpoints.ts renamed to apps/editor/src/commands/history-command/actions/clear-all-checkpoints.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ import {
33
CHECKPOINTS_STATE_KEY,
44
TEMPORARY_CHECKPOINT_STATE_KEY
55
} from '@/constants/state-keys'
6-
import { get_checkpoint_path } from '../utils'
7-
import { get_checkpoints } from './get-checkpoints'
6+
import { get_checkpoint_path } from '@/features/checkpoints/utils'
7+
import { get_checkpoints } from '@/features/checkpoints/actions'
88
import { Logger } from '@shared/utils/logger'
9-
import type { Checkpoint } from '../types'
9+
import type { Checkpoint } from '@/features/checkpoints/types'
1010
import { t } from '@/i18n'
1111

1212
export const clear_all_checkpoints = async (
1313
extension_context: vscode.ExtensionContext
1414
) => {
1515
const clear_task = async () => {
1616
const checkpoints = await get_checkpoints(extension_context)
17+
const checkpoints_to_keep: Checkpoint[] = []
18+
1719
for (const checkpoint of checkpoints) {
20+
if (checkpoint.is_pinned) {
21+
checkpoints_to_keep.push(checkpoint)
22+
continue
23+
}
1824
try {
1925
const checkpoint_path = get_checkpoint_path(checkpoint.timestamp)
2026
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
@@ -49,12 +55,15 @@ export const clear_all_checkpoints = async (
4955
TEMPORARY_CHECKPOINT_STATE_KEY,
5056
undefined
5157
)
52-
await extension_context.workspaceState.update(CHECKPOINTS_STATE_KEY, [])
58+
await extension_context.workspaceState.update(
59+
CHECKPOINTS_STATE_KEY,
60+
checkpoints_to_keep
61+
)
5362
}
5463
await vscode.window.withProgress(
5564
{
5665
location: vscode.ProgressLocation.Notification,
57-
title: t('feature.checkpoints.progress.clearing-all'),
66+
title: t('command.history.progress.clearing-all'),
5867
cancellable: false
5968
},
6069
clear_task

apps/editor/src/features/checkpoints/actions/create-temporary-checkpoint.ts renamed to apps/editor/src/commands/history-command/actions/create-temporary-checkpoint.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import * as vscode from 'vscode'
22
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
3-
import type { Checkpoint, CheckpointTab } from '../types'
4-
import { copy_optimised_recursively, get_checkpoint_path } from '../utils'
3+
import type { Checkpoint, CheckpointTab } from '@/features/checkpoints/types'
4+
import {
5+
copy_optimised_recursively,
6+
get_checkpoint_path
7+
} from '@/features/checkpoints/utils'
58
import {
69
get_git_diff,
710
get_git_info,
811
is_git_repository
9-
} from '../utils/git-utils'
12+
} from '@/features/checkpoints/utils/git-utils'
1013
import * as path from 'path'
1114
import { Logger } from '@shared/utils/logger'
1215

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import * as vscode from 'vscode'
2+
import { CHECKPOINTS_STATE_KEY } from '@/constants/state-keys'
3+
import type { Checkpoint } from '@/features/checkpoints/types'
4+
import { get_checkpoint_path } from '@/features/checkpoints/utils'
5+
import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
6+
import { t } from '@/i18n'
7+
8+
export type ActiveDeleteOperation = {
9+
finalize: () => Promise<void>
10+
timestamp: number
11+
}
12+
13+
export const delete_checkpoint_with_undo = async (params: {
14+
extension_context: vscode.ExtensionContext
15+
checkpoint: Checkpoint
16+
prompt_view_provider: PromptViewProvider
17+
get_active_operation: () => ActiveDeleteOperation | null
18+
set_active_operation: (op: ActiveDeleteOperation | null) => void
19+
on_did_update_checkpoints?: (checkpoints: Checkpoint[]) => void
20+
on_before_show_message?: () => void
21+
on_after_show_message?: () => void
22+
}): Promise<boolean> => {
23+
if (params.get_active_operation()) {
24+
await params.get_active_operation()!.finalize()
25+
params.set_active_operation(null)
26+
}
27+
28+
const checkpoints =
29+
params.extension_context.workspaceState.get<Checkpoint[]>(
30+
CHECKPOINTS_STATE_KEY,
31+
[]
32+
) ?? []
33+
34+
const updated_checkpoints = checkpoints.filter(
35+
(c) => c.timestamp !== params.checkpoint.timestamp
36+
)
37+
await params.extension_context.workspaceState.update(
38+
CHECKPOINTS_STATE_KEY,
39+
updated_checkpoints
40+
)
41+
params.on_did_update_checkpoints?.(updated_checkpoints)
42+
43+
const finalize = async () => {
44+
try {
45+
const checkpoint_path = get_checkpoint_path(params.checkpoint.timestamp)
46+
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
47+
recursive: true
48+
})
49+
} catch (error: any) {
50+
vscode.window.showWarningMessage(
51+
t('command.history.warning.could-not-delete', {
52+
error: error.message
53+
})
54+
)
55+
}
56+
}
57+
58+
const operation = { timestamp: params.checkpoint.timestamp, finalize }
59+
params.set_active_operation(operation)
60+
61+
params.on_before_show_message?.()
62+
const undo_action = t('command.history.action.undo')
63+
const choice = await vscode.window.showInformationMessage(
64+
t('command.history.success.deleted'),
65+
undo_action
66+
)
67+
params.on_after_show_message?.()
68+
69+
const current_active_op = params.get_active_operation()
70+
71+
if (
72+
current_active_op &&
73+
current_active_op.timestamp === operation.timestamp
74+
) {
75+
if (choice == undo_action) {
76+
const current_checkpoints =
77+
params.extension_context.workspaceState.get<Checkpoint[]>(
78+
CHECKPOINTS_STATE_KEY,
79+
[]
80+
) ?? []
81+
82+
current_checkpoints.push(params.checkpoint)
83+
current_checkpoints.sort((a, b) => b.timestamp - a.timestamp)
84+
85+
await params.extension_context.workspaceState.update(
86+
CHECKPOINTS_STATE_KEY,
87+
current_checkpoints
88+
)
89+
params.on_did_update_checkpoints?.(current_checkpoints)
90+
91+
params.on_before_show_message?.()
92+
await vscode.window.showInformationMessage(
93+
t('command.history.success.restored')
94+
)
95+
params.on_after_show_message?.()
96+
97+
params.set_active_operation(null)
98+
return true
99+
} else {
100+
await operation.finalize()
101+
params.set_active_operation(null)
102+
return false
103+
}
104+
} else if (choice == undo_action) {
105+
params.on_before_show_message?.()
106+
await vscode.window.showInformationMessage(
107+
t('command.history.error.could-not-undo')
108+
)
109+
params.on_after_show_message?.()
110+
return false
111+
}
112+
113+
return false
114+
}

apps/editor/src/features/checkpoints/actions/restore-checkpoint.ts renamed to apps/editor/src/commands/history-command/actions/restore-checkpoint.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ import {
88
CONTEXT_CHECKED_PATHS_STATE_KEY
99
} from '@/constants/state-keys'
1010
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
11-
import type { Checkpoint } from '../types'
12-
import { create_checkpoint } from './create-checkpoint'
11+
import type { Checkpoint } from '@/features/checkpoints/types'
12+
import {
13+
create_checkpoint,
14+
delete_checkpoint,
15+
get_checkpoints
16+
} from '@/features/checkpoints/actions'
1317
import { create_temporary_checkpoint } from './create-temporary-checkpoint'
14-
import { delete_checkpoint } from './delete-checkpoint'
15-
import { get_checkpoints } from './get-checkpoints'
1618
import { sync_workspace_from_dir } from './sync-workspace-from-dir'
17-
import { get_checkpoint_path, sync_directory } from '../utils'
19+
import { get_checkpoint_path } from '@/features/checkpoints/utils'
20+
import { sync_directory } from '../utils/sync-directory'
1821
import { Logger } from '@shared/utils/logger'
1922
import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
2023
import { response_preview_promise_resolve } from '@/commands/apply-response-command/utils/preview'
2124
import { ongoing_preview_cleanup_promise } from '@/commands/apply-response-command/utils/preview-handler'
22-
import { get_git_info } from '../utils/git-utils'
25+
import { get_git_info } from '@/features/checkpoints/utils/git-utils'
2326
import { CommitMessageDetails } from '@/utils/commit-message-details'
2427
import { t } from '@/i18n'
2528
export const restore_checkpoint = async (params: {
@@ -43,8 +46,8 @@ export const restore_checkpoint = async (params: {
4346
}
4447

4548
const title = params.options?.skip_confirmation
46-
? t('feature.checkpoints.progress.reverting')
47-
: t('feature.checkpoints.progress.restoring')
49+
? t('command.history.progress.reverting')
50+
: t('command.history.progress.restoring')
4851

4952
const main_task = async (
5053
progress: vscode.Progress<{ message?: string; increment?: number }>
@@ -140,7 +143,7 @@ export const restore_checkpoint = async (params: {
140143
}
141144
} catch (err: any) {
142145
vscode.window.showErrorMessage(
143-
t('feature.checkpoints.error.create-temp-failed', {
146+
t('command.history.error.create-temp-failed', {
144147
error: err.message
145148
})
146149
)
@@ -205,7 +208,7 @@ export const restore_checkpoint = async (params: {
205208
data: e
206209
})
207210
throw new Error(
208-
t('feature.checkpoints.error.reject-changes-failed', {
211+
t('command.history.error.reject-changes-failed', {
209212
folder: folder.name
210213
})
211214
)
@@ -255,18 +258,18 @@ export const restore_checkpoint = async (params: {
255258
}
256259
)
257260
vscode.window.showWarningMessage(
258-
t('feature.checkpoints.warning.diff-apply-issues', {
261+
t('command.history.warning.diff-apply-issues', {
259262
folder: folder.name
260263
})
261264
)
262-
} catch (rejectErr) {
265+
} catch (reject_error) {
263266
Logger.error({
264267
function_name: 'restore_checkpoint',
265268
message: `Failed to apply git diff even with --reject for ${folder.name}`,
266-
data: rejectErr
269+
data: reject_error
267270
})
268271
throw new Error(
269-
t('feature.checkpoints.error.diff-apply-failed', {
272+
t('command.history.error.diff-apply-failed', {
270273
folder: folder.name
271274
})
272275
)
@@ -345,18 +348,18 @@ export const restore_checkpoint = async (params: {
345348
}
346349
)
347350
vscode.window.showWarningMessage(
348-
t('feature.checkpoints.warning.diff-apply-issues', {
351+
t('command.history.warning.diff-apply-issues', {
349352
folder: folder.name
350353
})
351354
)
352-
} catch (rejectErr) {
355+
} catch (reject_error) {
353356
Logger.error({
354357
function_name: 'restore_checkpoint',
355358
message: `Failed to apply git diff even with --reject for ${folder.name}`,
356-
data: rejectErr
359+
data: reject_error
357360
})
358361
throw new Error(
359-
t('feature.checkpoints.error.diff-apply-failed', {
362+
t('command.history.error.diff-apply-failed', {
360363
folder: folder.name
361364
})
362365
)
@@ -502,7 +505,7 @@ export const restore_checkpoint = async (params: {
502505
undefined
503506
)
504507
vscode.window.showErrorMessage(
505-
t('feature.checkpoints.error.restore-failed', { error: err.message })
508+
t('command.history.error.restore-failed', { error: err.message })
506509
)
507510
if (temp_checkpoint) {
508511
await delete_checkpoint({
@@ -536,11 +539,11 @@ export const restore_checkpoint = async (params: {
536539
}
537540

538541
const message = params.options?.skip_confirmation
539-
? t('feature.checkpoints.success.reverted')
540-
: t('feature.checkpoints.success.restored')
542+
? t('command.history.success.reverted')
543+
: t('command.history.success.restored')
541544

542545
if (temp_check) {
543-
const action_label = t('feature.checkpoints.action.revert')
546+
const action_label = t('command.history.action.revert')
544547
const action = await vscode.window.showInformationMessage(
545548
message,
546549
action_label

apps/editor/src/features/checkpoints/actions/sync-workspace-from-dir.ts renamed to apps/editor/src/commands/history-command/actions/sync-workspace-from-dir.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode'
22
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
3-
import { sync_directory } from '../utils'
3+
import { sync_directory } from '../utils/sync-directory'
44

55
export const sync_workspace_from_dir = async (params: {
66
source_dir_uri: vscode.Uri

apps/editor/src/features/checkpoints/actions/toggle-checkpoint-star.ts renamed to apps/editor/src/commands/history-command/actions/toggle-checkpoint-pin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as vscode from 'vscode'
22
import { CHECKPOINTS_STATE_KEY } from '@/constants/state-keys'
3-
import type { Checkpoint } from '../types'
3+
import type { Checkpoint } from '@/features/checkpoints/types'
44
import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
55

6-
export const toggle_checkpoint_star = async (params: {
6+
export const toggle_checkpoint_pin = async (params: {
77
extension_context: vscode.ExtensionContext
88
timestamp: number
99
prompt_view_provider: PromptViewProvider
@@ -17,7 +17,7 @@ export const toggle_checkpoint_star = async (params: {
1717
(c) => c.timestamp == params.timestamp
1818
)
1919
if (checkpoint_to_update) {
20-
checkpoint_to_update.is_starred = !checkpoint_to_update.is_starred
20+
checkpoint_to_update.is_pinned = !checkpoint_to_update.is_pinned
2121
await params.extension_context.workspaceState.update(
2222
CHECKPOINTS_STATE_KEY,
2323
checkpoints

apps/editor/src/features/checkpoints/actions/update-checkpoint-description.ts renamed to apps/editor/src/commands/history-command/actions/update-checkpoint-description.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode'
22
import { CHECKPOINTS_STATE_KEY } from '@/constants/state-keys'
3-
import type { Checkpoint } from '../types'
3+
import type { Checkpoint } from '@/features/checkpoints/types'
44
import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
55

66
export const update_checkpoint_description = async (params: {

0 commit comments

Comments
 (0)