@@ -112,6 +112,29 @@ local function is_buffer_dirty(file_path)
112112 return is_dirty , nil
113113end
114114
115+ --- Discard unsaved changes in a buffer by reloading from disk.
116+ --- @param file_path string The file path whose buffer changes should be discarded
117+ --- @return boolean success True if changes were discarded successfully
118+ --- @return string ? error Error message if discard failed
119+ local function discard_buffer_changes (file_path )
120+ local bufnr = vim .fn .bufnr (file_path )
121+ if bufnr == - 1 then
122+ return false , " Buffer for " .. file_path .. " is not available"
123+ end
124+
125+ local discard_success , discard_error = pcall (function ()
126+ vim .api .nvim_buf_call (bufnr , function ()
127+ vim .cmd (" edit!" ) -- Force reload from disk, discarding changes
128+ end )
129+ end )
130+
131+ if not discard_success then
132+ return false , " Discard error: " .. tostring (discard_error )
133+ end
134+
135+ return true , nil
136+ end
137+
115138--- Setup the diff module
116139--- @param user_config ClaudeCodeConfig ? The configuration passed from init.lua
117140function M .setup (user_config )
@@ -712,15 +735,29 @@ function M._setup_blocking_diff(params, resolution_callback)
712735 local old_file_exists = vim .fn .filereadable (params .old_file_path ) == 1
713736 local is_new_file = not old_file_exists
714737
715- -- Step 1.5: Check if the file buffer has unsaved changes
738+ -- Step 1.5: Handle unsaved changes based on configuration
716739 if old_file_exists then
717740 local is_dirty = is_buffer_dirty (params .old_file_path )
718741 if is_dirty then
719- error ({
720- code = - 32000 ,
721- message = " Cannot create diff: file has unsaved changes" ,
722- data = " Please save (:w) or discard (:e!) changes to " .. params .old_file_path .. " before creating diff" ,
723- })
742+ local behavior = config and config .diff_opts and config .diff_opts .on_unsaved_changes or " error"
743+
744+ if behavior == " error" then
745+ error ({
746+ code = - 32000 ,
747+ message = " Cannot create diff: file has unsaved changes" ,
748+ data = " Please save (:w) or discard (:e!) changes to " .. params .old_file_path .. " before creating diff" ,
749+ })
750+ elseif behavior == " discard" then
751+ -- Discard unsaved changes using the extracted function
752+ local discard_success , discard_err = discard_buffer_changes (params .old_file_path )
753+ if not discard_success then
754+ error ({
755+ code = - 32000 ,
756+ message = " Failed to discard unsaved changes before creating diff" ,
757+ data = discard_err ,
758+ })
759+ end
760+ end
724761 end
725762 end
726763
0 commit comments