Skip to content

Commit 0d7f464

Browse files
authored
fix: fix #204 via Cleanup (#205)
Allow the cleanup required with write_to_file = true to occur under more circumstances that may prevent clean exit otherwise. chore: silence automatic writes
1 parent 3ff6c15 commit 0d7f464

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

lua/otter/init.lua

+25-10
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,31 @@ M.activate = function(languages, completion, diagnostics, tsquery)
123123
table.insert(keeper.rafts[main_nr].languages, lang)
124124

125125
if config.cfg.buffers.write_to_disk then
126+
-- closure to clean up this otter buffer and file
127+
local cleanup = function(ev)
128+
if api.nvim_buf_is_loaded(otter_nr) then
129+
api.nvim_buf_delete(otter_nr, { force = true })
130+
vim.fn.delete(otter_path)
131+
end
132+
end
126133
-- remove otter buffer when main buffer is closed
127-
api.nvim_create_autocmd({ "QuitPre", "BufDelete" }, {
134+
api.nvim_create_autocmd({ "BufDelete" }, {
128135
buffer = main_nr,
129-
group = api.nvim_create_augroup("OtterAutoclose" .. otter_nr, {}),
130-
callback = function(_, _)
131-
if api.nvim_buf_is_loaded(otter_nr) then
132-
api.nvim_buf_delete(otter_nr, { force = true })
133-
vim.fn.delete(otter_path)
134-
end
135-
end,
136+
group = api.nvim_create_augroup("OtterAutocloseOnMainDelete" .. otter_nr, {}),
137+
callback = cleanup
138+
})
139+
-- Remove otter buffer before exiting, preventing unsaved otter
140+
-- buffers from triggering a 'No write since last change' message.
141+
-- Must be a separate autocmd that is not attached to buffer = main_nr
142+
-- because the active buffer may be different from the main buffer when
143+
-- exiting.
144+
-- Must be ExitPre, not QuitPre, because QuitPre also triggers when a
145+
-- window with the main buffer is closed, even though the
146+
-- buffer may still be loaded in another window.
147+
api.nvim_create_autocmd({ "ExitPre" }, {
148+
pattern = "*",
149+
group = api.nvim_create_augroup("OtterAutocloseOnQuit" .. otter_nr, {}),
150+
callback = cleanup
136151
})
137152
-- write to disk when main buffer is written
138153
api.nvim_create_autocmd("BufWritePost", {
@@ -141,7 +156,7 @@ M.activate = function(languages, completion, diagnostics, tsquery)
141156
callback = function(_, _)
142157
if api.nvim_buf_is_loaded(otter_nr) then
143158
api.nvim_buf_call(otter_nr, function()
144-
vim.cmd("write! " .. otter_path)
159+
vim.cmd("silent write! " .. otter_path)
145160
end)
146161
end
147162
end,
@@ -170,7 +185,7 @@ M.activate = function(languages, completion, diagnostics, tsquery)
170185
-- and also write out once before lsps can complain
171186
local otter_path = keeper.rafts[main_nr].paths[lang]
172187
api.nvim_buf_call(otter_nr, function()
173-
vim.cmd("write! " .. otter_path)
188+
vim.cmd("silent write! " .. otter_path)
174189
end)
175190
end
176191

0 commit comments

Comments
 (0)