@@ -55,6 +55,7 @@ import {
5555} from './message-handlers'
5656import {
5757 API_EDIT_FORMAT_STATE_KEY ,
58+ LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY ,
5859 API_MODE_STATE_KEY ,
5960 CHAT_EDIT_FORMAT_STATE_KEY ,
6061 INSTRUCTIONS_ASK_STATE_KEY ,
@@ -64,23 +65,26 @@ import {
6465 get_last_group_or_preset_choice_state_key ,
6566 get_last_selected_group_state_key ,
6667 get_last_selected_preset_key ,
68+ LAST_APPLIED_CHANGES_STATE_KEY ,
69+ LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY ,
6770 LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY ,
6871 LAST_SELECTED_EDIT_CONTEXT_CONFIG_ID_STATE_KEY ,
6972 RECENT_DONATIONS_VISIBLE_STATE_KEY ,
7073 WEB_MODE_STATE_KEY
7174} from '@/constants/state-keys'
72- import { can_undo } from '@/commands/undo-command'
7375import {
7476 config_preset_to_ui_format ,
7577 ConfigPresetFormat
7678} from '@/views/panel/backend/utils/preset-format-converters'
79+ import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
7780import { CHATBOTS } from '@shared/constants/chatbots'
7881import { HOME_VIEW_TYPES , HomeViewType } from '../types/home-view-type'
7982import { ApiMode , WebMode } from '@shared/types/modes'
8083import { code_review_promise_resolve } from '@/commands/apply-chat-response-command/utils/review'
8184import { Logger } from '@shared/utils/logger'
8285import { CancelTokenSource } from 'axios'
8386import { update_last_used_preset_or_group } from './message-handlers/update-last-used-preset-or-group'
87+ import { undo_files } from '@/commands/apply-chat-response-command/utils/file-operations'
8488import { dictionary } from '@shared/constants/dictionary'
8589
8690export class ViewProvider implements vscode . WebviewViewProvider {
@@ -363,6 +367,8 @@ export class ViewProvider implements vscode.WebviewViewProvider {
363367 await handle_duplicate_preset ( this , message , webview_view )
364368 } else if ( message . command == 'CREATE_PRESET' ) {
365369 await handle_create_preset ( this )
370+ } else if ( message . command == 'UNDO' ) {
371+ await this . _handle_undo ( )
366372 } else if ( message . command == 'EXECUTE_COMMAND' ) {
367373 await vscode . commands . executeCommand ( message . command_id )
368374 } else if ( message . command == 'EDIT_CONTEXT' ) {
@@ -479,7 +485,7 @@ export class ViewProvider implements vscode.WebviewViewProvider {
479485 setTimeout ( ( ) => {
480486 this . send_message ( {
481487 command : 'CAN_UNDO_CHANGED' ,
482- can_undo : can_undo ( this . context )
488+ can_undo : this . _can_undo ( )
483489 } )
484490 } , 1000 )
485491 }
@@ -736,6 +742,81 @@ export class ViewProvider implements vscode.WebviewViewProvider {
736742 } )
737743 }
738744
745+ private _can_undo ( ) : boolean {
746+ const original_states = this . context . workspaceState . get <
747+ OriginalFileState [ ]
748+ > ( LAST_APPLIED_CHANGES_STATE_KEY )
749+ return ! ! original_states && original_states . length > 0
750+ }
751+
752+ private async _handle_undo ( ) : Promise < void > {
753+ const context = this . context
754+ const original_states = context . workspaceState . get < OriginalFileState [ ] > (
755+ LAST_APPLIED_CHANGES_STATE_KEY
756+ )
757+ const editor_state = context . workspaceState . get <
758+ | {
759+ file_path : string
760+ position : { line : number ; character : number }
761+ }
762+ | undefined
763+ > ( LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY )
764+
765+ if ( ! original_states || original_states . length == 0 ) {
766+ vscode . window . showInformationMessage (
767+ dictionary . information_message . NO_RECENT_CHANGES_TO_UNDO
768+ )
769+ return
770+ }
771+
772+ try {
773+ const success = await undo_files ( { original_states } )
774+
775+ if ( ! success ) {
776+ return
777+ }
778+
779+ if ( editor_state ) {
780+ try {
781+ const uri = vscode . Uri . file ( editor_state . file_path )
782+ const document = await vscode . workspace . openTextDocument ( uri )
783+ const editor = await vscode . window . showTextDocument ( document , {
784+ preview : false
785+ } )
786+ const position = new vscode . Position (
787+ editor_state . position . line ,
788+ editor_state . position . character
789+ )
790+ editor . selection = new vscode . Selection ( position , position )
791+ editor . revealRange (
792+ new vscode . Range ( position , position ) ,
793+ vscode . TextEditorRevealType . InCenter
794+ )
795+ } catch ( error ) {
796+ console . error ( 'Error restoring editor state:' , error )
797+ }
798+ }
799+
800+ context . workspaceState . update ( LAST_APPLIED_CHANGES_STATE_KEY , null )
801+ context . workspaceState . update (
802+ LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY ,
803+ null
804+ )
805+ context . workspaceState . update (
806+ LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY ,
807+ null
808+ )
809+ this . set_undo_button_state ( false )
810+ } catch ( error : any ) {
811+ console . error ( 'Error during undo:' , error )
812+ vscode . window . showErrorMessage (
813+ dictionary . error_message . FAILED_TO_UNDO_CHANGES (
814+ error . message || 'Unknown error'
815+ )
816+ )
817+ }
818+ }
819+
739820 private _get_html_for_webview ( webview : vscode . Webview ) {
740821 const resources_uri = webview . asWebviewUri (
741822 vscode . Uri . joinPath ( this . extension_uri , 'resources' )
0 commit comments