@@ -58,81 +58,104 @@ export const refactor_active_editor_command = (
5858 const original_content = document . getText ( )
5959 const file_path = document . uri . fsPath
6060
61- await vscode . window . withProgress (
62- {
63- location : vscode . ProgressLocation . Notification ,
64- title : `Refactoring ${ vscode . workspace . asRelativePath ( file_path ) } ...` ,
65- cancellable : true
66- } ,
67- async ( progress , token ) => {
68- const cancel_token_source = axios . CancelToken . source ( )
69- token . onCancellationRequested ( ( ) => {
70- cancel_token_source . cancel ( 'Cancelled by user.' )
71- } )
72-
73- let previous_progress = 0
74-
75- const updated_content = await process_file ( {
76- endpoint_url,
77- api_key : provider . api_key ,
78- model : intelligent_update_config . model ,
79- temperature : intelligent_update_config . temperature ,
80- reasoning_effort : intelligent_update_config . reasoning_effort ,
81- file_path : file_path ,
82- file_content : original_content ,
83- instruction : instructions ,
84- cancel_token : cancel_token_source . token ,
85- on_progress : ( receivedLength , totalLength ) => {
86- const current_progress = Math . min (
87- Math . round ( ( receivedLength / totalLength ) * 100 ) ,
88- 100
89- )
90- const increment = current_progress - previous_progress
91- if ( increment > 0 ) {
92- progress . report ( {
93- increment
94- } )
95- previous_progress = current_progress
61+ let result : any
62+ try {
63+ result = await vscode . window . withProgress (
64+ {
65+ location : vscode . ProgressLocation . Notification ,
66+ title : `Called Intelligent Update tool for file refactoring...` ,
67+ cancellable : true
68+ } ,
69+ async ( progress , token ) => {
70+ const cancel_token_source = axios . CancelToken . source ( )
71+ token . onCancellationRequested ( ( ) => {
72+ cancel_token_source . cancel ( 'Cancelled by user.' )
73+ } )
74+
75+ let previous_progress = 0
76+
77+ const content = await process_file ( {
78+ endpoint_url,
79+ api_key : provider . api_key ,
80+ model : intelligent_update_config . model ,
81+ temperature : intelligent_update_config . temperature ,
82+ reasoning_effort : intelligent_update_config . reasoning_effort ,
83+ file_path : file_path ,
84+ file_content : original_content ,
85+ instruction : instructions ,
86+ cancel_token : cancel_token_source . token ,
87+ on_progress : ( receivedLength , totalLength ) => {
88+ const current_progress = Math . min (
89+ Math . round ( ( receivedLength / totalLength ) * 100 ) ,
90+ 100
91+ )
92+ const increment = current_progress - previous_progress
93+ if ( increment > 0 ) {
94+ progress . report ( {
95+ increment
96+ } )
97+ previous_progress = current_progress
98+ }
9699 }
97- }
98- } )
100+ } )
99101
100- if ( token . isCancellationRequested ) {
101- return
102+ return {
103+ content,
104+ cancelled : token . isCancellationRequested
105+ }
102106 }
107+ )
108+ } catch ( error ) {
109+ if ( axios . isCancel ( error ) ) {
110+ return
111+ }
112+ throw error
113+ }
103114
104- if ( updated_content ) {
105- const original_state : OriginalFileState = {
106- file_path : vscode . workspace . asRelativePath ( document . uri ) ,
107- content : original_content ,
108- is_new : false ,
109- workspace_name : vscode . workspace . getWorkspaceFolder ( document . uri )
110- ?. name
111- }
115+ if ( result . cancelled ) {
116+ return
117+ }
112118
113- await editor . edit ( ( editBuilder ) => {
114- const fullRange = new vscode . Range (
115- document . positionAt ( 0 ) ,
116- document . positionAt ( original_content . length )
117- )
118- editBuilder . replace ( fullRange , updated_content )
119- } )
119+ const updated_content = result . content
120120
121- await vscode . commands . executeCommand ( 'editor.action.formatDocument' )
122- await document . save ( )
123-
124- context . workspaceState . update ( LAST_APPLIED_CHANGES_STATE_KEY , [
125- original_state
126- ] )
127- view_provider . set_revert_button_state ( true )
128- view_provider . set_apply_button_state ( false )
129- } else {
130- vscode . window . showErrorMessage (
131- 'Refactoring failed to produce any content.'
132- )
133- }
121+ if ( updated_content ) {
122+ const original_state : OriginalFileState = {
123+ file_path : vscode . workspace . asRelativePath ( document . uri ) ,
124+ content : original_content ,
125+ is_new : false ,
126+ workspace_name : vscode . workspace . getWorkspaceFolder ( document . uri )
127+ ?. name
134128 }
135- )
129+
130+ await editor . edit ( ( editBuilder ) => {
131+ const fullRange = new vscode . Range (
132+ document . positionAt ( 0 ) ,
133+ document . positionAt ( original_content . length )
134+ )
135+ editBuilder . replace ( fullRange , updated_content )
136+ } )
137+
138+ await vscode . commands . executeCommand ( 'editor.action.formatDocument' )
139+ await document . save ( )
140+
141+ context . workspaceState . update ( LAST_APPLIED_CHANGES_STATE_KEY , [
142+ original_state
143+ ] )
144+ view_provider . set_revert_button_state ( true )
145+ view_provider . set_apply_button_state ( false )
146+
147+ const response = await vscode . window . showInformationMessage (
148+ 'File refactored successfully.' ,
149+ 'Revert'
150+ )
151+ if ( response == 'Revert' ) {
152+ await vscode . commands . executeCommand ( 'codeWebChat.revert' )
153+ }
154+ } else {
155+ vscode . window . showErrorMessage (
156+ 'Refactoring failed to produce any content.'
157+ )
158+ }
136159 }
137160 )
138161}
0 commit comments