@@ -48,7 +48,6 @@ local approvals = require("codecompanion.interactions.chat.tools.approvals")
4848local codecompanion = require (" codecompanion" )
4949local config = require (" codecompanion.config" )
5050local constants = require (" codecompanion.interactions.chat.tools.builtin.insert_edit_into_file.constants" )
51- local diff = require (" codecompanion.interactions.chat.helpers.diff" )
5251local helpers = require (" codecompanion.interactions.chat.helpers" )
5352local match_selector = require (" codecompanion.interactions.chat.tools.builtin.insert_edit_into_file.match_selector" )
5453local strategies = require (" codecompanion.interactions.chat.tools.builtin.insert_edit_into_file.strategies" )
@@ -151,24 +150,19 @@ local function restore_file_content(path, content, file_info)
151150end
152151
153152--- Handle user decision for edits with diff approval
154- --- @param opts table Options containing : diff_id , chat_bufnr , display_name , should_diff , success_response , output_handler , bufnr (optional ), on_reject (optional for files )
153+ --- @param opts table Options containing : diff_id , chat_bufnr , display_name , diff_ui , success_response , output_handler , bufnr (optional ), on_reject (optional for files )
155154--- @return any Result of output_handler call
156155local function handle_user_decision (opts )
157- local accept = config .interactions .inline .keymaps .accept_change .modes .n
158- local reject = config .interactions .inline .keymaps .reject_change .modes .n
159- local is_buffer = opts .bufnr ~= nil
160-
161156 local wait_opts = {
162157 chat_bufnr = opts .chat_bufnr ,
163- notify = config .display .icons .warning
164- .. (is_buffer and " Waiting for diff approval ..." or " Waiting for decision ..." ),
165- sub_text = fmt (" `%s` - Accept edits / `%s` - Reject edits" , accept , reject ),
158+ notify = config .display .icons .warning .. " Waiting for diff approval ..." ,
159+ sub_text = " Review changes in the diff window" ,
166160 }
167161
168162 return wait .for_decision (opts .diff_id , { " CodeCompanionDiffAccepted" , " CodeCompanionDiffRejected" }, function (result )
169163 local response
170164 if result .accepted then
171- if is_buffer then
165+ if opts . bufnr then
172166 pcall (function ()
173167 api .nvim_buf_call (opts .bufnr , function ()
174168 vim .cmd (" silent! w" )
@@ -180,10 +174,7 @@ local function handle_user_decision(opts)
180174 return opts .output_handler (response )
181175 else
182176 get_rejection_reason (function (reason )
183- if is_buffer then
184- if result .timeout and opts .should_diff and opts .should_diff .reject then
185- opts .should_diff :reject ()
186- end
177+ if opts .bufnr then
187178 response = error_response (
188179 result .timeout and " User failed to accept the edits in time"
189180 or fmt (' User rejected the edits for `%s`, with the reason "%s"' , opts .display_name , reason )
@@ -201,9 +192,6 @@ local function handle_user_decision(opts)
201192 )
202193 )
203194 else
204- if result .timeout and opts .should_diff and opts .should_diff .reject then
205- opts .should_diff :reject ()
206- end
207195 response = error_response (
208196 result .timeout and " User failed to accept the edits in time"
209197 or fmt (' User rejected the edits for `%s`, with the reason "%s"' , opts .display_name , reason )
@@ -793,20 +781,31 @@ local function edit_file(action, chat_bufnr, output_handler, opts)
793781 end
794782
795783 local diff_id = math.random (10000000 )
796- local should_diff = diff .create (path , diff_id , {
784+ local from_lines = vim .split (current_content , " \n " , { plain = true })
785+ local to_lines = vim .split (dry_run_result .final_content , " \n " , { plain = true })
786+
787+ -- Detect filetype from path
788+ local ft = vim .filetype .match ({ filename = path }) or " text"
789+
790+ local diff_helpers = require (" codecompanion.helpers" )
791+ local diff_ui = diff_helpers .show_diff ({
792+ from_lines = from_lines ,
793+ to_lines = to_lines ,
794+ ft = ft ,
795+ title = action .filepath ,
796+ diff_id = diff_id ,
797797 chat_bufnr = chat_bufnr ,
798- original_content = vim .split (current_content , " \n " , { plain = true }),
799798 tool_name = " insert_edit_into_file" ,
800799 })
801800
802801 local final_success = success_response (fmt (" Edited `%s` file%s" , action .filepath , extract_explanation (action )))
803802
804- if should_diff and opts .require_confirmation_after then
803+ if opts .require_confirmation_after then
805804 return handle_user_decision ({
806805 diff_id = diff_id ,
807806 chat_bufnr = chat_bufnr ,
808807 display_name = action .filepath ,
809- should_diff = should_diff ,
808+ diff_ui = diff_ui ,
810809 success_response = final_success ,
811810 on_reject = function ()
812811 return restore_file_content (path , current_content , file_info )
@@ -900,28 +899,26 @@ local function edit_buffer(bufnr, chat_bufnr, action, output_handler, opts)
900899 return output_handler (success )
901900 end
902901
903- local should_diff = diff .create (bufnr , diff_id , {
902+ local ft = vim .bo [bufnr ].filetype or " text"
903+
904+ local diff_helpers = require (" codecompanion.helpers" )
905+ local diff_ui = diff_helpers .show_diff ({
904906 chat_bufnr = chat_bufnr ,
905- original_content = original_content ,
907+ from_lines = original_content ,
908+ to_lines = final_lines ,
909+ ft = ft ,
910+ title = display_name ,
911+ diff_id = diff_id ,
906912 tool_name = " insert_edit_into_file" ,
907913 })
908914
909- local start_line = nil
910- if dry_run_result .edit_results [1 ] and dry_run_result .edit_results [1 ].start_line then
911- start_line = dry_run_result .edit_results [1 ].start_line
912- end
913-
914- if start_line then
915- ui_utils .scroll_to_line (bufnr , start_line )
916- end
917-
918- if should_diff and opts .require_confirmation_after then
915+ if opts .require_confirmation_after then
919916 return handle_user_decision ({
920917 diff_id = diff_id ,
921918 chat_bufnr = chat_bufnr ,
922919 display_name = display_name ,
923920 bufnr = bufnr ,
924- should_diff = should_diff ,
921+ diff_ui = diff_ui ,
925922 success_response = success ,
926923 output_handler = output_handler ,
927924 })
0 commit comments