fix: quit after mutations when :wq or similar (#221)

This commit is contained in:
Steven Arcangeli 2023-11-13 10:36:28 -08:00
parent 3727410e48
commit af13ce333f
6 changed files with 65 additions and 18 deletions

View file

@ -749,10 +749,20 @@ end
---Save all changes
---@param opts nil|table
--- confirm nil|boolean Show confirmation when true, never when false, respect skip_confirm_for_simple_edits if nil
M.save = function(opts)
---@param cb? fun(err: nil|string) Called when mutations complete.
---@note
--- If you provide your own callback function, there will be no notification for errors.
M.save = function(opts, cb)
opts = opts or {}
if not cb then
cb = function(err)
if err and err ~= "Canceled" then
vim.notify(err, vim.log.levels.ERROR)
end
end
end
local mutator = require("oil.mutator")
mutator.try_write_changes(opts.confirm)
mutator.try_write_changes(opts.confirm, cb)
end
local function restore_alt_buf()
@ -944,10 +954,28 @@ M.setup = function(opts)
pattern = scheme_pattern,
nested = true,
callback = function(params)
local winid = vim.api.nvim_get_current_win()
local last_cmd = vim.fn.histget("cmd", -1)
local last_expr = vim.fn.histget("expr", -1)
-- If the user issued a :wq or similar, we should quit after saving
local quit_after_save = last_cmd == "wq" or last_cmd == "x" or last_expr == "ZZ"
local quit_all = last_cmd:match("^wqal*$")
local bufname = vim.api.nvim_buf_get_name(params.buf)
if vim.endswith(bufname, "/") then
vim.cmd.doautocmd({ args = { "BufWritePre", params.file }, mods = { silent = true } })
M.save()
M.save(nil, function(err)
if err then
if err ~= "Canceled" then
vim.notify(err, vim.log.levels.ERROR)
end
elseif winid == vim.api.nvim_get_current_win() then
if quit_after_save then
vim.cmd.quit()
elseif quit_all then
vim.cmd.quitall()
end
end
end)
vim.cmd.doautocmd({ args = { "BufWritePost", params.file }, mods = { silent = true } })
else
local adapter = config.get_adapter_by_scheme(bufname)

View file

@ -456,9 +456,13 @@ end
local mutation_in_progress = false
---@param confirm nil|boolean
M.try_write_changes = function(confirm)
---@param cb? fun(err: nil|string)
M.try_write_changes = function(confirm, cb)
if not cb then
cb = function(_err) end
end
if mutation_in_progress then
error("Cannot perform mutation when already in progress")
cb("Cannot perform mutation when already in progress")
return
end
local current_buf = vim.api.nvim_get_current_buf()
@ -495,7 +499,6 @@ M.try_write_changes = function(confirm)
local ns = vim.api.nvim_create_namespace("Oil")
vim.diagnostic.reset(ns)
if not vim.tbl_isempty(all_errors) then
vim.notify("Error parsing oil buffers", vim.log.levels.ERROR)
for bufnr, errors in pairs(all_errors) do
vim.diagnostic.set(ns, bufnr, errors)
end
@ -514,13 +517,17 @@ M.try_write_changes = function(confirm)
vim.api.nvim_win_set_buf(0, bufnr)
pcall(vim.api.nvim_win_set_cursor, 0, { errs[1].lnum + 1, errs[1].col })
end
return unlock()
unlock()
cb("Error parsing oil buffers")
return
end
local actions = M.create_actions_from_diffs(all_diffs)
preview.show(actions, confirm, function(proceed)
if not proceed then
return unlock()
unlock()
cb("Canceled")
return
end
M.process_actions(
@ -528,7 +535,7 @@ M.try_write_changes = function(confirm)
vim.schedule_wrap(function(err)
view.unlock_buffers()
if err then
vim.notify(string.format("[oil] Error applying actions: %s", err), vim.log.levels.ERROR)
err = string.format("[oil] Error applying actions: %s", err)
view.rerender_all_oil_buffers()
else
local current_entry = oil.get_cursor_entry()
@ -543,6 +550,7 @@ M.try_write_changes = function(confirm)
vim.api.nvim_exec_autocmds("User", { pattern = "OilMutationComplete", modeline = false })
end
mutation_in_progress = false
cb(err)
end)
)
end)

View file

@ -684,6 +684,7 @@ M.render_buffer_async = function(bufnr, opts, callback)
local seek_after_render_found = false
local first = true
vim.bo[bufnr].modifiable = false
vim.bo[bufnr].modified = false
loading.set_loading(bufnr, true)
local finish = vim.schedule_wrap(function()