Skip to content

Commit

Permalink
fix: oil buffers remain unmodified after saving changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Mar 24, 2023
1 parent eea3432 commit 931453f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 35 additions & 1 deletion tests/regression_spec.lua
Original file line number Diff line number Diff line change
@@ -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" } })
Expand Down Expand Up @@ -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)
27 changes: 26 additions & 1 deletion tests/test_util.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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

0 comments on commit 931453f

Please sign in to comment.