11import * as vscode from 'vscode'
22import * as fs from 'fs'
3- import {
4- create_temporary_checkpoint ,
5- delete_checkpoint ,
6- promote_temporary_checkpoint
7- } from '../checkpoints-command/actions'
3+ import * as path from 'path'
4+ import { create_checkpoint } from '../checkpoints-command/actions'
85import { FileInReview } from '@shared/types/file-in-review'
96import { PanelProvider } from '@/views/panel/backend/panel-provider'
107import { dictionary } from '@shared/constants/dictionary'
@@ -17,6 +14,7 @@ import {
1714 ongoing_review_cleanup_promise
1815} from './utils/review-handler'
1916import { process_chat_response , CommandArgs } from './response-processor'
17+ import { undo_files } from './utils/file-operations'
2018
2119export const apply_chat_response_command = (
2220 context : vscode . ExtensionContext ,
@@ -67,119 +65,181 @@ export const apply_chat_response_command = (
6765 }
6866 }
6967
70- const temp_checkpoint = await create_temporary_checkpoint (
71- workspace_provider
72- )
73-
7468 const review_data = await process_chat_response (
7569 args ,
7670 chat_response ,
7771 context ,
7872 panel_provider
7973 )
8074
81- let checkpoint_promoted = false
75+ if ( review_data ) {
76+ if ( ! args ?. files_with_content ) {
77+ let total_lines_added = 0
78+ let total_lines_removed = 0
79+ const files_for_history : FileInReview [ ] = [ ]
8280
83- try {
84- if ( review_data ) {
85- if ( ! args ?. files_with_content ) {
86- let total_lines_added = 0
87- let total_lines_removed = 0
88- const files_for_history : FileInReview [ ] = [ ]
89-
90- const workspace_map = new Map < string , string > ( )
91- vscode . workspace . workspaceFolders ! . forEach ( ( folder ) => {
92- workspace_map . set ( folder . name , folder . uri . fsPath )
93- } )
94- const default_workspace =
95- vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath
96-
97- for ( const state of review_data . original_states ) {
98- let workspace_root = default_workspace
99- if (
100- state . workspace_name &&
101- workspace_map . has ( state . workspace_name )
102- ) {
103- workspace_root = workspace_map . get ( state . workspace_name ) !
104- }
81+ const workspace_map = new Map < string , string > ( )
82+ vscode . workspace . workspaceFolders ! . forEach ( ( folder ) => {
83+ workspace_map . set ( folder . name , folder . uri . fsPath )
84+ } )
85+ const default_workspace =
86+ vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath
87+
88+ for ( const state of review_data . original_states ) {
89+ let workspace_root = default_workspace
90+ if (
91+ state . workspace_name &&
92+ workspace_map . has ( state . workspace_name )
93+ ) {
94+ workspace_root = workspace_map . get ( state . workspace_name ) !
95+ }
10596
106- const sanitized_file_path = create_safe_path (
107- workspace_root ,
108- state . file_path
109- )
110- if ( ! sanitized_file_path ) {
111- continue
112- }
97+ const sanitized_file_path = create_safe_path (
98+ workspace_root ,
99+ state . file_path
100+ )
101+ if ( ! sanitized_file_path ) {
102+ continue
103+ }
113104
114- let current_content = ''
115- try {
116- if ( fs . existsSync ( sanitized_file_path ) ) {
117- current_content = fs . readFileSync ( sanitized_file_path , 'utf8' )
118- }
119- } catch ( error ) {
120- continue
105+ let current_content = ''
106+ try {
107+ if ( fs . existsSync ( sanitized_file_path ) ) {
108+ current_content = fs . readFileSync ( sanitized_file_path , 'utf8' )
121109 }
122-
123- const diff_stats = get_diff_stats ( {
124- original_content : state . content ,
125- new_content : current_content
126- } )
127-
128- total_lines_added += diff_stats . lines_added
129- total_lines_removed += diff_stats . lines_removed
130-
131- const is_deleted =
132- ! state . is_new && current_content === '' && state . content !== ''
133-
134- files_for_history . push ( {
135- file_path : state . file_path ,
136- workspace_name : state . workspace_name ,
137- is_new : state . is_new ,
138- is_deleted,
139- lines_added : diff_stats . lines_added ,
140- lines_removed : diff_stats . lines_removed ,
141- is_fallback : state . is_fallback ,
142- is_replaced : state . is_replaced ,
143- diff_fallback_method : state . diff_fallback_method ,
144- content : current_content
145- } )
110+ } catch ( error ) {
111+ continue
146112 }
147113
148- panel_provider . send_message ( {
149- command : 'NEW_RESPONSE_RECEIVED' ,
150- response : review_data . chat_response ,
151- raw_instructions : args ?. raw_instructions ,
152- lines_added : total_lines_added ,
153- lines_removed : total_lines_removed ,
154- files : files_for_history
114+ const diff_stats = get_diff_stats ( {
115+ original_content : state . content ,
116+ new_content : current_content
117+ } )
118+
119+ total_lines_added += diff_stats . lines_added
120+ total_lines_removed += diff_stats . lines_removed
121+
122+ const is_deleted =
123+ ! state . is_new && current_content === '' && state . content !== ''
124+
125+ files_for_history . push ( {
126+ file_path : state . file_path ,
127+ workspace_name : state . workspace_name ,
128+ is_new : state . is_new ,
129+ is_deleted,
130+ lines_added : diff_stats . lines_added ,
131+ lines_removed : diff_stats . lines_removed ,
132+ is_fallback : state . is_fallback ,
133+ is_replaced : state . is_replaced ,
134+ diff_fallback_method : state . diff_fallback_method ,
135+ content : current_content
155136 } )
156137 }
157138
158- const changes_accepted = await handle_code_review_and_cleanup ( {
159- original_states : review_data . original_states ,
160- chat_response : review_data . chat_response ,
161- panel_provider,
162- context,
163- original_editor_state : args ?. original_editor_state ,
164- raw_instructions : args ?. raw_instructions
139+ panel_provider . send_message ( {
140+ command : 'NEW_RESPONSE_RECEIVED' ,
141+ response : review_data . chat_response ,
142+ raw_instructions : args ?. raw_instructions ,
143+ lines_added : total_lines_added ,
144+ lines_removed : total_lines_removed ,
145+ files : files_for_history
146+ } )
147+ }
148+
149+ const changes_accepted = await handle_code_review_and_cleanup ( {
150+ original_states : review_data . original_states ,
151+ chat_response : review_data . chat_response ,
152+ panel_provider,
153+ context,
154+ original_editor_state : args ?. original_editor_state ,
155+ raw_instructions : args ?. raw_instructions
156+ } )
157+
158+ if ( changes_accepted ) {
159+ const workspace_map = new Map < string , string > ( )
160+ vscode . workspace . workspaceFolders ! . forEach ( ( folder ) => {
161+ workspace_map . set ( folder . name , folder . uri . fsPath )
165162 } )
163+ const default_workspace =
164+ vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath
165+
166+ const accepted_states : {
167+ file_path : string
168+ workspace_name ?: string
169+ content : string
170+ is_deleted : boolean
171+ } [ ] = [ ]
172+
173+ for ( const state of review_data . original_states ) {
174+ let workspace_root = default_workspace
175+ if (
176+ state . workspace_name &&
177+ workspace_map . has ( state . workspace_name )
178+ ) {
179+ workspace_root = workspace_map . get ( state . workspace_name ) !
180+ }
181+
182+ const sanitized_file_path = create_safe_path (
183+ workspace_root ,
184+ state . file_path
185+ )
186+ if ( ! sanitized_file_path ) {
187+ continue
188+ }
189+
190+ const exists = fs . existsSync ( sanitized_file_path )
191+ const content = exists
192+ ? fs . readFileSync ( sanitized_file_path , 'utf8' )
193+ : ''
166194
167- if ( changes_accepted ) {
168- await promote_temporary_checkpoint ( {
169- context,
170- temp_checkpoint,
171- title : 'Before changes approved' ,
172- description : args ?. raw_instructions
195+ accepted_states . push ( {
196+ file_path : state . file_path ,
197+ workspace_name : state . workspace_name ,
198+ content : content ,
199+ is_deleted : ! exists
173200 } )
174- checkpoint_promoted = true
175201 }
176- }
177- } finally {
178- if ( ! checkpoint_promoted ) {
179- await delete_checkpoint ( {
202+
203+ await undo_files ( { original_states : review_data . original_states } )
204+
205+ await create_checkpoint (
206+ workspace_provider ,
180207 context ,
181- checkpoint_to_delete : temp_checkpoint
182- } )
208+ 'Before changes approved' ,
209+ args ?. raw_instructions
210+ )
211+
212+ for ( const accepted_state of accepted_states ) {
213+ let workspace_root = default_workspace
214+ if (
215+ accepted_state . workspace_name &&
216+ workspace_map . has ( accepted_state . workspace_name )
217+ ) {
218+ workspace_root = workspace_map . get ( accepted_state . workspace_name ) !
219+ }
220+ const sanitized_file_path = create_safe_path (
221+ workspace_root ,
222+ accepted_state . file_path
223+ )
224+ if ( ! sanitized_file_path ) continue
225+
226+ if ( accepted_state . is_deleted ) {
227+ if ( fs . existsSync ( sanitized_file_path ) ) {
228+ await vscode . workspace . fs . delete (
229+ vscode . Uri . file ( sanitized_file_path )
230+ )
231+ }
232+ } else {
233+ const dir = path . dirname ( sanitized_file_path )
234+ if ( ! fs . existsSync ( dir ) ) {
235+ await vscode . workspace . fs . createDirectory ( vscode . Uri . file ( dir ) )
236+ }
237+ await vscode . workspace . fs . writeFile (
238+ vscode . Uri . file ( sanitized_file_path ) ,
239+ Buffer . from ( accepted_state . content , 'utf8' )
240+ )
241+ }
242+ }
183243 }
184244 }
185245 }
0 commit comments