Skip to content

Commit 1a8e3de

Browse files
committed
fix: make preview (q)uit cancel the whole process
1 parent 8909ac9 commit 1a8e3de

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

lua/parrot/preview.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function Preview:new(options)
2020
self.close_fn = nil
2121
self.apply_fn = nil
2222
self.reject_fn = nil
23+
self.quit_fn = nil
2324
self.auto_apply_timer = nil
2425
return self
2526
end
@@ -122,6 +123,7 @@ end
122123
---@param filename string|nil Optional filename to display in header
123124
---@param start_line number|nil Optional start line number
124125
---@param end_line number|nil Optional end line number
126+
---@param quit_callback function|nil Optional function to call when quitting (defaults to reject)
125127
function Preview:show_diff_preview(
126128
old_content,
127129
new_content,
@@ -130,7 +132,8 @@ function Preview:show_diff_preview(
130132
reject_callback,
131133
filename,
132134
start_line,
133-
end_line
135+
end_line,
136+
quit_callback
134137
)
135138
if not self.options.enable_preview_mode then
136139
-- Preview disabled, apply changes directly
@@ -206,6 +209,7 @@ function Preview:show_diff_preview(
206209
-- Store callbacks
207210
self.apply_fn = apply_callback
208211
self.reject_fn = reject_callback
212+
self.quit_fn = quit_callback
209213

210214
-- Set up keymaps
211215
self:setup_preview_keymaps()
@@ -308,7 +312,12 @@ end
308312

309313
--- Closes the preview without applying changes
310314
function Preview:close_preview()
311-
self:reject_changes()
315+
if self.quit_fn then
316+
self.quit_fn()
317+
else
318+
self:reject_changes()
319+
end
320+
self:cleanup()
312321
end
313322

314323
--- Cleans up timers and closes preview window
@@ -331,6 +340,7 @@ function Preview:cleanup()
331340
self.close_fn = nil
332341
self.apply_fn = nil
333342
self.reject_fn = nil
343+
self.quit_fn = nil
334344
end
335345

336346
return Preview

lua/parrot/preview_response_handler.lua

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,23 @@ function PreviewResponseHandler:show_preview()
124124
end
125125
end
126126

127-
self.preview:show_diff_preview(self.original_content, new_content, self.target_type, function()
128-
self:apply_changes()
129-
end, function()
130-
self:reject_changes()
131-
end, filename, self.start_line, self.end_line)
127+
self.preview:show_diff_preview(
128+
self.original_content,
129+
new_content,
130+
self.target_type,
131+
function()
132+
self:apply_changes()
133+
end,
134+
function()
135+
self:reject_changes()
136+
end,
137+
filename,
138+
self.start_line,
139+
self.end_line,
140+
function()
141+
self:quit_changes()
142+
end
143+
)
132144
end
133145

134146
--- Prepares the new content based on target type
@@ -240,6 +252,12 @@ function PreviewResponseHandler:reject_changes()
240252
vim.cmd("doautocmd User PrtPreviewRejected")
241253
end
242254

255+
--- Quits the preview without any prompts or actions
256+
function PreviewResponseHandler:quit_changes()
257+
logger.debug("PreviewResponseHandler: Preview quit")
258+
vim.cmd("doautocmd User PrtPreviewQuit")
259+
end
260+
243261
--- Creates a handler function for the response processing
244262
---@return function
245263
function PreviewResponseHandler:create_handler()

0 commit comments

Comments
 (0)