fix: oil buffers remain unmodified after saving changes

This commit is contained in:
Steven Arcangeli 2023-03-23 21:18:46 -07:00
parent eea34329e8
commit 931453fc09
3 changed files with 63 additions and 2 deletions

View file

@ -120,6 +120,8 @@ M.rerender_all_oil_buffers = function(opts)
for _, bufnr in ipairs(buffers) do
if hidden_buffers[bufnr] then
vim.b[bufnr].oil_dirty = opts
-- We also need to mark this as nomodified so it doesn't interfere with quitting vim
vim.bo[bufnr].modified = false
else
M.render_buffer_async(bufnr, opts)
end

View file

@ -1,11 +1,22 @@
require("plenary.async").tests.add_to_env()
local oil = require("oil")
local test_util = require("tests.test_util")
local TmpDir = require("tests.tmpdir")
a.describe("regression tests", function()
after_each(function()
local tmpdir
a.before_each(function()
tmpdir = TmpDir.new()
end)
a.after_each(function()
if tmpdir then
tmpdir:dispose()
a.util.scheduler()
tmpdir = nil
end
test_util.reset_editor()
end)
-- see https://github.com/stevearc/oil.nvim/issues/25
a.it("can edit dirs that will be renamed to an existing buffer", function()
vim.cmd.edit({ args = { "README.md" } })
@ -71,4 +82,27 @@ a.describe("regression tests", function()
assert.not_equals("oil", vim.bo.filetype)
assert.equals("", vim.api.nvim_buf_get_name(0))
end)
a.it("All buffers set nomodified after save", function()
tmpdir:create({ "a.txt" })
a.util.scheduler()
vim.cmd.edit({ args = { "oil://" .. vim.fn.fnamemodify(tmpdir.path, ":p") } })
local first_dir = vim.api.nvim_get_current_buf()
test_util.wait_for_autocmd("BufReadPost")
test_util.feedkeys({ "dd", "itest/<esc>", "<CR>" }, 10)
vim.wait(1000, function()
return vim.bo.modifiable
end, 10)
test_util.feedkeys({ "p" }, 10)
a.util.scheduler()
oil.save({ confirm = false })
vim.wait(1000, function()
return vim.bo.modifiable
end, 10)
tmpdir:assert_fs({
["test/a.txt"] = "a.txt",
})
-- The first oil buffer should not be modified anymore
assert.falsy(vim.bo[first_dir].modified)
end)
end)

View file

@ -1,8 +1,15 @@
require("plenary.async").tests.add_to_env()
local cache = require("oil.cache")
local M = {}
M.reset_editor = function()
require("oil").setup({})
require("oil").setup({
columms = {},
adapters = {
["oil-test://"] = "test",
},
silence_disclaimer = true,
})
vim.cmd.tabonly({ mods = { silent = true } })
for i, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if i > 1 then
@ -13,6 +20,7 @@ M.reset_editor = function()
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
vim.api.nvim_buf_delete(bufnr, { force = true })
end
cache.clear_everything()
end
M.wait_for_autocmd = a.wrap(function(autocmd, cb)
@ -24,4 +32,21 @@ M.wait_for_autocmd = a.wrap(function(autocmd, cb)
})
end, 2)
---@param actions string[]
---@param timestep integer
M.feedkeys = function(actions, timestep)
timestep = timestep or 10
a.util.sleep(timestep)
for _, action in ipairs(actions) do
a.util.sleep(timestep)
local escaped = vim.api.nvim_replace_termcodes(action, true, false, true)
vim.api.nvim_feedkeys(escaped, "m", true)
end
a.util.sleep(timestep)
-- process pending keys until the queue is empty.
-- Note that this will exit insert mode.
vim.api.nvim_feedkeys("", "x", true)
a.util.sleep(timestep)
end
return M