Skip to content

Commit

Permalink
fix: quit after mutations when :wq or similar (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Nov 13, 2023
1 parent 3727410 commit af13ce3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Note that at the moment the ssh adapter does not support Windows machines, and i
- [open(dir)](doc/api.md#opendir)
- [close()](doc/api.md#close)
- [select(opts, callback)](doc/api.md#selectopts-callback)
- [save(opts)](doc/api.md#saveopts)
- [save(opts, cb)](doc/api.md#saveopts-cb)
- [setup(opts)](doc/api.md#setupopts)

<!-- /API -->
Expand Down
20 changes: 13 additions & 7 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- [open(dir)](#opendir)
- [close()](#close)
- [select(opts, callback)](#selectopts-callback)
- [save(opts)](#saveopts)
- [save(opts, cb)](#saveopts-cb)
- [setup(opts)](#setupopts)

<!-- /TOC -->
Expand Down Expand Up @@ -132,15 +132,21 @@ Select the entry under the cursor
| | close | `boolean` | Close the original oil buffer once selection is made |
| callback | `nil\|fun(err: nil\|string)` | Called once all entries have been opened | |

## save(opts)
## save(opts, cb)

`save(opts)` \
`save(opts, cb)` \
Save all changes

| Param | Type | Desc | |
| ----- | ------------ | -------------- | ------------------------------------------------------------------------------------------- |
| opts | `nil\|table` | | |
| | confirm | `nil\|boolean` | Show confirmation when true, never when false, respect skip_confirm_for_simple_edits if nil |
| Param | Type | Desc | |
| ----- | ---------------------------- | ------------------------------- | ------------------------------------------------------------------------------------------- |
| opts | `nil\|table` | | |
| | confirm | `nil\|boolean` | Show confirmation when true, never when false, respect skip_confirm_for_simple_edits if nil |
| cb | `nil\|fun(err: nil\|string)` | Called when mutations complete. | |

**Note:**
<pre>
If you provide your own callback function, there will be no notification for errors.
</pre>

## setup(opts)

Expand Down
6 changes: 5 additions & 1 deletion doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,17 @@ select({opts}, {callback}) *oil.selec
{callback} `nil|fun(err: nil|string)` Called once all entries have been
opened

save({opts}) *oil.save*
save({opts}, {cb}) *oil.save*
Save all changes

Parameters:
{opts} `nil|table`
{confirm} `nil|boolean` Show confirmation when true, never when false,
respect skip_confirm_for_simple_edits if nil
{cb} `nil|fun(err: nil|string)` Called when mutations complete.

Note:
If you provide your own callback function, there will be no notification for errors.

setup({opts}) *oil.setup*
Initialize oil
Expand Down
34 changes: 31 additions & 3 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 14 additions & 6 deletions lua/oil/mutator/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -514,21 +517,25 @@ 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(
actions,
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()
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit af13ce3

Please # to comment.