Fix: Undotree messing up with 'File deleted' autocmd, thatis required to rename deleted file buffers.

This commit is contained in:
psychhim
2025-10-12 14:25:03 +05:30
parent 72d555c9bf
commit d6c29b3d55
2 changed files with 42 additions and 22 deletions

View File

@@ -40,12 +40,16 @@ end
require('lazy').setup({ require('lazy').setup({
-- NOTE: First, some plugins that don't require any configuration -- NOTE: First, some plugins that don't require any configuration
-- Git related plugins -- Git related plugins
'tpope/vim-fugitive', {
'tpope/vim-rhubarb', 'tpope/vim-fugitive',
cmd = { 'Git', 'G', 'Gdiffsplit', 'Gread', 'Gwrite', 'Ggrep', 'GMove', 'GDelete', 'GBrowse' },
dependencies = { 'tpope/vim-rhubarb' },
},
{ -- NOTE: This is where your plugins related to LSP can be installed. { -- NOTE: This is where your plugins related to LSP can be installed.
-- LSP Configuration & Plugins -- LSP Configuration & Plugins
'neovim/nvim-lspconfig', 'neovim/nvim-lspconfig',
event = { 'BufReadPre', 'BufNewFile' },
dependencies = { dependencies = {
-- Automatically install LSPs to stdpath for neovim -- Automatically install LSPs to stdpath for neovim
'williamboman/mason.nvim', 'williamboman/mason.nvim',
@@ -505,24 +509,40 @@ vim.keymap.set('n', '<Esc>', '<Esc><Cmd>echo ""<CR>', { noremap = true, silent =
vim.keymap.set('v', '<Esc>', '<Esc><Cmd>echo ""<CR>', { noremap = true, silent = true }) vim.keymap.set('v', '<Esc>', '<Esc><Cmd>echo ""<CR>', { noremap = true, silent = true })
-- When a file is deleted externally, rename all its buffers to "[file]: file removed" -- When a file is deleted externally, rename all its buffers to "[file]: file removed"
-- List of buffer names or filetypes to skip (UndoTree, Neo-tree, etc.)
local skip_buffers = { 'undotree', 'neo-tree' }
-- Helper function to determine if a buffer should be skipped in future
local function should_skip(buf)
if not vim.api.nvim_buf_is_valid(buf) then
return true
end
local ft = vim.api.nvim_buf_get_option(buf, 'filetype')
local bufname = vim.api.nvim_buf_get_name(buf)
if bufname == '' then
return true
end
for _, v in ipairs(skip_buffers) do
if ft == v or bufname:match(v) then
return true
end
end
return false
end
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost' }, { vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost' }, {
callback = function() callback = function()
local current_buf = vim.api.nvim_get_current_buf() local current_buf = vim.api.nvim_get_current_buf()
local ft = vim.api.nvim_buf_get_option(current_buf, 'filetype') if should_skip(current_buf) then
local current_name = vim.api.nvim_buf_get_name(current_buf)
-- Skip Neo-tree, unnamed, or already-marked buffers
if ft == 'neo-tree' or current_name == '' or current_name:match 'file removed' then
return return
end end
local bufname = vim.api.nvim_buf_get_name(current_buf)
-- If this file no longer exists, mark all buffers showing it -- If this file no longer exists, mark all buffers showing it
if vim.fn.filereadable(current_name) == 0 then if vim.fn.filereadable(bufname) == 0 then
local filename = vim.fn.fnamemodify(current_name, ':t') local filename = vim.fn.fnamemodify(bufname, ':t')
local new_name = string.format('[%s]: file removed', filename) local new_name = string.format('[%s]: file removed', filename)
-- Iterate over all listed buffers
for _, buf in ipairs(vim.api.nvim_list_bufs()) do for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buf) then if vim.api.nvim_buf_is_valid(buf) then
local name = vim.api.nvim_buf_get_name(buf) local name = vim.api.nvim_buf_get_name(buf)
if name == current_name and not name:match 'file removed' then if name == bufname and not name:match 'file removed' then
-- Temporarily unlist so renaming works cleanly -- Temporarily unlist so renaming works cleanly
vim.api.nvim_buf_set_option(buf, 'buflisted', false) vim.api.nvim_buf_set_option(buf, 'buflisted', false)
vim.api.nvim_buf_set_name(buf, new_name) vim.api.nvim_buf_set_name(buf, new_name)
@@ -537,17 +557,18 @@ vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost' }, {
vim.api.nvim_create_autocmd('BufWinLeave', { vim.api.nvim_create_autocmd('BufWinLeave', {
callback = function(args) callback = function(args)
local buf = args.buf local buf = args.buf
if vim.api.nvim_buf_is_valid(buf) then if should_skip(buf) then
local name = vim.api.nvim_buf_get_name(buf) return
local marked = pcall(vim.api.nvim_buf_get_var, buf, 'marked_for_wipe') and vim.api.nvim_buf_get_var(buf, 'marked_for_wipe') end
-- If the buffer was marked for wipe or name indicates it's deleted local bufname = vim.api.nvim_buf_get_name(buf)
if marked or (name ~= '' and name:match 'file removed') then -- If the buffer was marked for wipe or name indicates it's deleted
vim.schedule(function() local marked = pcall(vim.api.nvim_buf_get_var, buf, 'marked_for_wipe') and vim.api.nvim_buf_get_var(buf, 'marked_for_wipe')
if vim.api.nvim_buf_is_valid(buf) then if marked or (bufname ~= '' and bufname:match 'file removed') then
vim.cmd('bwipeout! ' .. buf) vim.schedule(function()
end if vim.api.nvim_buf_is_valid(buf) then
end) vim.cmd('bwipeout! ' .. buf)
end end
end)
end end
end, end,
}) })

View File

@@ -1,4 +1,3 @@
-- lua/custom/plugins/undotree.lua
return { return {
'mbbill/undotree', 'mbbill/undotree',
keys = { keys = {