@@ -22,7 +22,7 @@ pub trait CodingService: Send + Sync {
2222 & self ,
2323 workspace_manager : & mut WorkspaceManager ,
2424 request : FormatCodeRequest ,
25- ) -> Result < Vec < TextEdit > > ;
25+ ) -> Result < usize > ;
2626}
2727
2828/// LSP-based implementation of CodingService
@@ -79,29 +79,35 @@ impl CodingService for LspCodingService {
7979 let params = RenameParams {
8080 text_document_position : TextDocumentPositionParams {
8181 text_document : TextDocumentIdentifier { uri } ,
82- position : crate :: utils:: to_lsp_position ( request. row , request. column ) ,
82+ position : Position {
83+ line : request. row ,
84+ character : request. column ,
85+ } ,
8386 } ,
8487 new_name : request. new_name . clone ( ) ,
8588 work_done_progress_params : Default :: default ( ) ,
8689 } ;
8790
88- let workspace_edit = client. rename ( params) . await ? ;
91+ let result = client. rename ( params) . await ;
8992
90- // Apply workspace edit using LSP batch operations if not dry-run
91- if let Some ( ref edit) = workspace_edit {
92- if !request. dry_run {
93- client. apply_workspace_edit ( edit) . await ?;
93+ // Apply edits if not dry_run
94+ if !request. dry_run {
95+ if let Ok ( Some ( ref workspace_edit) ) = result {
96+ use crate :: utils:: apply_workspace_edit;
97+ if let Err ( e) = apply_workspace_edit ( workspace_edit) {
98+ tracing:: trace!( "Failed to apply workspace edit: {}" , e) ;
99+ }
94100 }
95101 }
96102
97- Ok ( workspace_edit )
103+ result
98104 }
99105
100106 async fn format_code (
101107 & self ,
102108 workspace_manager : & mut WorkspaceManager ,
103109 request : FormatCodeRequest ,
104- ) -> Result < Vec < TextEdit > > {
110+ ) -> Result < usize > {
105111 // Ensure initialized
106112 if !workspace_manager. is_initialized ( ) {
107113 workspace_manager. initialize ( ) . await ?;
@@ -125,12 +131,12 @@ impl CodingService for LspCodingService {
125131 )
126132 } ) ?;
127133
128- let uri = Url :: from_file_path ( & canonical_path) . map_err ( |_| {
129- anyhow:: anyhow!( "Invalid file path: {}" , canonical_path. display( ) )
130- } ) ?;
131-
132134 let params = DocumentFormattingParams {
133- text_document : TextDocumentIdentifier { uri : uri. clone ( ) } ,
135+ text_document : TextDocumentIdentifier {
136+ uri : Url :: from_file_path ( & canonical_path) . map_err ( |_| {
137+ anyhow:: anyhow!( "Invalid file path: {}" , canonical_path. display( ) )
138+ } ) ?,
139+ } ,
134140 options : FormattingOptions {
135141 tab_size : request. tab_size ,
136142 insert_spaces : request. insert_spaces ,
@@ -147,25 +153,18 @@ impl CodingService for LspCodingService {
147153 . await ?
148154 . unwrap_or_default ( ) ;
149155
150- // Apply formatting edits using LSP batch operations
156+ let edit_count = edits. len ( ) ;
157+
158+ // Apply formatting edits to the actual file
151159 if !edits. is_empty ( ) {
152- let mut changes = std:: collections:: HashMap :: new ( ) ;
153- changes. insert ( uri, edits. clone ( ) ) ;
154-
155- let workspace_edit = WorkspaceEdit {
156- changes : Some ( changes) ,
157- document_changes : None ,
158- change_annotations : None ,
159- } ;
160-
161- client. apply_workspace_edit ( & workspace_edit) . await ?;
160+ use crate :: utils:: apply_text_edits;
161+ apply_text_edits ( & canonical_path, & edits) ?;
162162 }
163163
164- Ok ( edits )
164+ Ok ( edit_count )
165165 } else {
166166 // Format workspace - not commonly supported by LSPs
167- // Return empty edits for now
168- Ok ( Vec :: new ( ) )
167+ Ok ( 0 )
169168 }
170169 }
171170}
0 commit comments