feat: add cleanup_buffers_on_delete option

Problem: When files are deleted via canola, any open Neovim buffers
for those files remain alive, polluting the jumplist with stale
entries.

Solution: Add an opt-in `cleanup_buffers_on_delete` config option
(default `false`). When enabled, `finish()` in `mutator/init.lua`
iterates completed delete actions and wipes matching buffers via
`nvim_buf_delete` before `CanolaActionsPost` fires. Only local
filesystem deletes are handled (guarded by the `files` adapter
check).
This commit is contained in:
Barrett Ruth 2026-03-06 15:16:25 -05:00
parent 69d85b8de1
commit 047338a6dd
Signed by: barrett
GPG key ID: A6C96C9349D2FC81
5 changed files with 176 additions and 108 deletions

View file

@ -311,6 +311,12 @@ prompt_save_on_select_new_entry *canola.prompt_save_on_select_new_e
When this option is `true`, Canola will prompt you to save before entering a file or When this option is `true`, Canola will prompt you to save before entering a file or
directory that is pending within canola, but does not exist on disk. directory that is pending within canola, but does not exist on disk.
cleanup_buffers_on_delete *canola.cleanup_buffers_on_delete*
type: `boolean` default: `false`
When `true`, canola will wipe any open buffer whose path matches a file that
was successfully deleted via canola. This prevents stale buffers from
appearing in the jumplist after a deletion.
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
API *canola-api* API *canola-api*

View file

@ -56,7 +56,7 @@ cherry-picked PR, `not actionable` = can't/won't fix, `tracking` = known/not yet
addressed, `open` = not yet triaged. addressed, `open` = not yet triaged.
| Issue | Status | Notes | | Issue | Status | Notes |
| ------------------------------------------------------- | -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ------------------------------------------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [#85](https://github.com/stevearc/oil.nvim/issues/85) | open | Git status column (P2) | | [#85](https://github.com/stevearc/oil.nvim/issues/85) | open | Git status column (P2) |
| [#95](https://github.com/stevearc/oil.nvim/issues/95) | open | Undo after renaming files (P1) | | [#95](https://github.com/stevearc/oil.nvim/issues/95) | open | Undo after renaming files (P1) |
| [#117](https://github.com/stevearc/oil.nvim/issues/117) | open | Move file into new dir via slash in name (P1, related to [#708](https://github.com/stevearc/oil.nvim/pull/708)) | | [#117](https://github.com/stevearc/oil.nvim/issues/117) | open | Move file into new dir via slash in name (P1, related to [#708](https://github.com/stevearc/oil.nvim/pull/708)) |
@ -124,7 +124,7 @@ addressed, `open` = not yet triaged.
| [#599](https://github.com/stevearc/oil.nvim/issues/599) | open | user:group display and manipulation (P2) | | [#599](https://github.com/stevearc/oil.nvim/issues/599) | open | user:group display and manipulation (P2) |
| [#607](https://github.com/stevearc/oil.nvim/issues/607) | open | Per-host SCP args (P2) | | [#607](https://github.com/stevearc/oil.nvim/issues/607) | open | Per-host SCP args (P2) |
| [#609](https://github.com/stevearc/oil.nvim/issues/609) | open | Cursor placement via Snacks picker | | [#609](https://github.com/stevearc/oil.nvim/issues/609) | open | Cursor placement via Snacks picker |
| [#612](https://github.com/stevearc/oil.nvim/issues/612) | open | Delete buffers on file delete (P2) | | [#612](https://github.com/stevearc/oil.nvim/issues/612) | fixed | Delete buffers on file delete — `cleanup_buffers_on_delete` option |
| [#615](https://github.com/stevearc/oil.nvim/issues/615) | open | Cursor at name column on o/O (P2) | | [#615](https://github.com/stevearc/oil.nvim/issues/615) | open | Cursor at name column on o/O (P2) |
| [#617](https://github.com/stevearc/oil.nvim/issues/617) | open | Filetype by actual filetype (P2) | | [#617](https://github.com/stevearc/oil.nvim/issues/617) | open | Filetype by actual filetype (P2) |
| [#621](https://github.com/stevearc/oil.nvim/issues/621) | open | Toggle function for regular windows (P2) | | [#621](https://github.com/stevearc/oil.nvim/issues/621) | open | Toggle function for regular windows (P2) |

View file

@ -28,6 +28,8 @@ local default_config = {
}, },
-- Send deleted files to the trash instead of permanently deleting them (:help canola-trash) -- Send deleted files to the trash instead of permanently deleting them (:help canola-trash)
delete_to_trash = false, delete_to_trash = false,
-- Wipe open buffers for files deleted via canola (:help canola.cleanup_buffers_on_delete)
cleanup_buffers_on_delete = false,
-- Skip the confirmation popup for simple operations (:help canola.skip_confirm_for_simple_edits) -- Skip the confirmation popup for simple operations (:help canola.skip_confirm_for_simple_edits)
skip_confirm_for_simple_edits = false, skip_confirm_for_simple_edits = false,
-- Selecting a new/moved/renamed file or directory will prompt you to save changes first -- Selecting a new/moved/renamed file or directory will prompt you to save changes first
@ -232,6 +234,7 @@ default_config.view_options.highlight_filename = nil
---@field buf_options table<string, any> ---@field buf_options table<string, any>
---@field win_options table<string, any> ---@field win_options table<string, any>
---@field delete_to_trash boolean ---@field delete_to_trash boolean
---@field cleanup_buffers_on_delete boolean
---@field skip_confirm_for_simple_edits boolean ---@field skip_confirm_for_simple_edits boolean
---@field prompt_save_on_select_new_entry boolean ---@field prompt_save_on_select_new_entry boolean
---@field cleanup_delay_ms integer ---@field cleanup_delay_ms integer
@ -263,6 +266,7 @@ local M = {}
---@field buf_options? table<string, any> Buffer-local options to use for canola buffers ---@field buf_options? table<string, any> Buffer-local options to use for canola buffers
---@field win_options? table<string, any> Window-local options to use for canola buffers ---@field win_options? table<string, any> Window-local options to use for canola buffers
---@field delete_to_trash? boolean Send deleted files to the trash instead of permanently deleting them (:help canola-trash). ---@field delete_to_trash? boolean Send deleted files to the trash instead of permanently deleting them (:help canola-trash).
---@field cleanup_buffers_on_delete? boolean Wipe open buffers for files deleted via canola (:help canola.cleanup_buffers_on_delete).
---@field skip_confirm_for_simple_edits? boolean Skip the confirmation popup for simple operations (:help canola.skip_confirm_for_simple_edits). ---@field skip_confirm_for_simple_edits? boolean Skip the confirmation popup for simple operations (:help canola.skip_confirm_for_simple_edits).
---@field prompt_save_on_select_new_entry? boolean Selecting a new/moved/renamed file or directory will prompt you to save changes first (:help prompt_save_on_select_new_entry). ---@field prompt_save_on_select_new_entry? boolean Selecting a new/moved/renamed file or directory will prompt you to save changes first (:help prompt_save_on_select_new_entry).
---@field cleanup_delay_ms? integer Canola will automatically delete hidden buffers after this delay. You can set the delay to false to disable cleanup entirely. Note that the cleanup process only starts when none of the canola buffers are currently displayed. ---@field cleanup_delay_ms? integer Canola will automatically delete hidden buffers after this delay. You can set the delay to false to disable cleanup entirely. Note that the cleanup process only starts when none of the canola buffers are currently displayed.

View file

@ -420,6 +420,21 @@ M.process_actions = function(actions, cb)
finished = true finished = true
progress:close() progress:close()
progress = nil progress = nil
if config.cleanup_buffers_on_delete and not err then
for _, action in ipairs(actions) do
if action.type == 'delete' then
local scheme, path = util.parse_url(action.url)
if config.adapters[scheme] == 'files' then
assert(path)
local os_path = fs.posix_to_os_path(path)
local bufnr = vim.fn.bufnr(os_path)
if bufnr ~= -1 then
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
end
end
end
vim.api.nvim_exec_autocmds( vim.api.nvim_exec_autocmds(
'User', 'User',
{ pattern = 'CanolaActionsPost', modeline = false, data = { err = err, actions = actions } } { pattern = 'CanolaActionsPost', modeline = false, data = { err = err, actions = actions } }

View file

@ -163,4 +163,47 @@ describe('files adapter', function()
assert.equals(vim.fn.fnamemodify(tmpdir.path, ':p') .. 'file.rb', vim.api.nvim_buf_get_name(0)) assert.equals(vim.fn.fnamemodify(tmpdir.path, ':p') .. 'file.rb', vim.api.nvim_buf_get_name(0))
assert.equals(tmpdir.path .. '/file.rb', vim.fn.bufname()) assert.equals(tmpdir.path .. '/file.rb', vim.fn.bufname())
end) end)
describe('cleanup_buffers_on_delete', function()
local cache = require('canola.cache')
local config = require('canola.config')
local mutator = require('canola.mutator')
before_each(function()
config.cleanup_buffers_on_delete = true
end)
after_each(function()
config.cleanup_buffers_on_delete = false
end)
it('wipes the buffer for a deleted file', function()
tmpdir:create({ 'a.txt' })
local dirurl = 'canola://' .. vim.fn.fnamemodify(tmpdir.path, ':p')
local filepath = vim.fn.fnamemodify(tmpdir.path, ':p') .. 'a.txt'
cache.create_and_store_entry(dirurl, 'a.txt', 'file')
vim.cmd.edit({ args = { filepath } })
local bufnr = vim.api.nvim_get_current_buf()
local url = 'canola://' .. filepath
test_util.await(mutator.process_actions, 2, {
{ type = 'delete', url = url, entry_type = 'file' },
})
assert.is_false(vim.api.nvim_buf_is_valid(bufnr))
end)
it('does not wipe the buffer when disabled', function()
config.cleanup_buffers_on_delete = false
tmpdir:create({ 'b.txt' })
local dirurl = 'canola://' .. vim.fn.fnamemodify(tmpdir.path, ':p')
local filepath = vim.fn.fnamemodify(tmpdir.path, ':p') .. 'b.txt'
cache.create_and_store_entry(dirurl, 'b.txt', 'file')
vim.cmd.edit({ args = { filepath } })
local bufnr = vim.api.nvim_get_current_buf()
local url = 'canola://' .. filepath
test_util.await(mutator.process_actions, 2, {
{ type = 'delete', url = url, entry_type = 'file' },
})
assert.is_true(vim.api.nvim_buf_is_valid(bufnr))
end)
end)
end) end)