Skip to content

Commit 63b6534

Browse files
committed
Refactor the undo logic into a dedicated message handler file
1 parent 9af2acf commit 63b6534

4 files changed

Lines changed: 86 additions & 75 deletions

File tree

‎packages/vscode/src/constants/state-keys.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ export const get_last_selected_preset_key = (web_mode: string) =>
7272
export const get_last_selected_group_state_key = (web_mode: string) =>
7373
`last-selected-group-${web_mode}`
7474

75-
export interface DuplicateWorkspaceContext {
75+
export type DuplicateWorkspaceContext = {
7676
checked_files: string[]
7777
checked_websites: string[]
7878
timestamp: number
7979
workspace_root_folders: string[]
8080
}
8181

82-
export interface HistoryEntry {
82+
export type HistoryEntry = {
8383
text: string
8484
createdAt: number
8585
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import * as vscode from 'vscode'
2+
import {
3+
LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY,
4+
LAST_APPLIED_CHANGES_STATE_KEY,
5+
LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY
6+
} from '@/constants/state-keys'
7+
import { dictionary } from '@shared/constants/dictionary'
8+
import { PanelProvider } from '@/views/panel/backend/panel-provider'
9+
import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
10+
import { undo_files } from '@/commands/apply-chat-response-command/utils/file-operations'
11+
12+
export const handle_undo = async (
13+
panel_provider: PanelProvider
14+
): Promise<void> => {
15+
const context = panel_provider.context
16+
const original_states = context.workspaceState.get<OriginalFileState[]>(
17+
LAST_APPLIED_CHANGES_STATE_KEY
18+
)
19+
const editor_state = context.workspaceState.get<
20+
| {
21+
file_path: string
22+
position: { line: number; character: number }
23+
}
24+
| undefined
25+
>(LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY)
26+
27+
if (!original_states || original_states.length == 0) {
28+
vscode.window.showInformationMessage(
29+
dictionary.information_message.NO_RECENT_CHANGES_TO_UNDO
30+
)
31+
return
32+
}
33+
34+
try {
35+
const success = await undo_files({ original_states })
36+
37+
if (!success) {
38+
return
39+
}
40+
41+
if (editor_state) {
42+
try {
43+
const uri = vscode.Uri.file(editor_state.file_path)
44+
const document = await vscode.workspace.openTextDocument(uri)
45+
const editor = await vscode.window.showTextDocument(document, {
46+
preview: false
47+
})
48+
const position = new vscode.Position(
49+
editor_state.position.line,
50+
editor_state.position.character
51+
)
52+
editor.selection = new vscode.Selection(position, position)
53+
editor.revealRange(
54+
new vscode.Range(position, position),
55+
vscode.TextEditorRevealType.InCenter
56+
)
57+
} catch (error) {
58+
console.error('Error restoring editor state:', error)
59+
}
60+
}
61+
62+
context.workspaceState.update(LAST_APPLIED_CHANGES_STATE_KEY, null)
63+
context.workspaceState.update(
64+
LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY,
65+
null
66+
)
67+
context.workspaceState.update(
68+
LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY,
69+
null
70+
)
71+
panel_provider.set_undo_button_state(false)
72+
} catch (error: any) {
73+
console.error('Error during undo:', error)
74+
vscode.window.showErrorMessage(
75+
dictionary.error_message.FAILED_TO_UNDO_CHANGES(
76+
error.message || 'Unknown error'
77+
)
78+
)
79+
}
80+
}

‎packages/vscode/src/views/panel/backend/message-handlers/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ export * from './handle-cancel-commit-message'
4646
export * from './handle-get-collapsed-states'
4747
export * from './handle-manage-configurations'
4848
export * from './handle-save-component-collapsed-state'
49+
export * from './handle-undo'

‎packages/vscode/src/views/panel/backend/panel-provider.ts‎

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ import {
5656
handle_proceed_with_commit,
5757
handle_get_collapsed_states,
5858
handle_manage_configurations,
59-
handle_save_component_collapsed_state
59+
handle_save_component_collapsed_state,
60+
handle_undo
6061
} from './message-handlers'
6162
import {
6263
API_EDIT_FORMAT_STATE_KEY,
63-
LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY,
6464
API_MODE_STATE_KEY,
6565
CHAT_EDIT_FORMAT_STATE_KEY,
6666
INSTRUCTIONS_ASK_STATE_KEY,
@@ -72,7 +72,6 @@ import {
7272
get_last_selected_group_state_key,
7373
get_last_selected_preset_key,
7474
LAST_APPLIED_CHANGES_STATE_KEY,
75-
LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY,
7675
LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY,
7776
LAST_SELECTED_EDIT_CONTEXT_CONFIG_ID_STATE_KEY,
7877
get_presets_collapsed_state_key,
@@ -91,7 +90,6 @@ import { response_preview_promise_resolve } from '@/commands/apply-chat-response
9190
import { Logger } from '@shared/utils/logger'
9291
import { CancelTokenSource } from 'axios'
9392
import { update_last_used_preset_or_group } from './message-handlers/update-last-used-preset-or-group'
94-
import { undo_files } from '@/commands/apply-chat-response-command/utils/file-operations'
9593
import { dictionary } from '@shared/constants/dictionary'
9694

9795
export class PanelProvider implements vscode.WebviewViewProvider {
@@ -449,7 +447,7 @@ export class PanelProvider implements vscode.WebviewViewProvider {
449447
create_on_index: message.create_on_index
450448
})
451449
} else if (message.command == 'UNDO') {
452-
await this._handle_undo()
450+
await handle_undo(this)
453451
} else if (message.command == 'APPLY_RESPONSE_FROM_HISTORY') {
454452
await vscode.commands.executeCommand(
455453
'codeWebChat.applyChatResponse',
@@ -851,74 +849,6 @@ export class PanelProvider implements vscode.WebviewViewProvider {
851849
return !!original_states && original_states.length > 0
852850
}
853851

854-
private async _handle_undo(): Promise<void> {
855-
const context = this.context
856-
const original_states = context.workspaceState.get<OriginalFileState[]>(
857-
LAST_APPLIED_CHANGES_STATE_KEY
858-
)
859-
const editor_state = context.workspaceState.get<
860-
| {
861-
file_path: string
862-
position: { line: number; character: number }
863-
}
864-
| undefined
865-
>(LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY)
866-
867-
if (!original_states || original_states.length == 0) {
868-
vscode.window.showInformationMessage(
869-
dictionary.information_message.NO_RECENT_CHANGES_TO_UNDO
870-
)
871-
return
872-
}
873-
874-
try {
875-
const success = await undo_files({ original_states })
876-
877-
if (!success) {
878-
return
879-
}
880-
881-
if (editor_state) {
882-
try {
883-
const uri = vscode.Uri.file(editor_state.file_path)
884-
const document = await vscode.workspace.openTextDocument(uri)
885-
const editor = await vscode.window.showTextDocument(document, {
886-
preview: false
887-
})
888-
const position = new vscode.Position(
889-
editor_state.position.line,
890-
editor_state.position.character
891-
)
892-
editor.selection = new vscode.Selection(position, position)
893-
editor.revealRange(
894-
new vscode.Range(position, position),
895-
vscode.TextEditorRevealType.InCenter
896-
)
897-
} catch (error) {
898-
console.error('Error restoring editor state:', error)
899-
}
900-
}
901-
902-
context.workspaceState.update(LAST_APPLIED_CHANGES_STATE_KEY, null)
903-
context.workspaceState.update(
904-
LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY,
905-
null
906-
)
907-
context.workspaceState.update(
908-
LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY,
909-
null
910-
)
911-
this.set_undo_button_state(false)
912-
} catch (error: any) {
913-
console.error('Error during undo:', error)
914-
vscode.window.showErrorMessage(
915-
dictionary.error_message.FAILED_TO_UNDO_CHANGES(
916-
error.message || 'Unknown error'
917-
)
918-
)
919-
}
920-
}
921-
922852
private _get_html_for_webview(webview: vscode.Webview) {
923853
const resources_uri = webview.asWebviewUri(
924854
vscode.Uri.joinPath(this.extension_uri, 'resources')

0 commit comments

Comments
 (0)