Skip to content

Commit

Permalink
fix(attach): detach on when the buffer name changes
Browse files Browse the repository at this point in the history
Fixes #1021
  • Loading branch information
lewis6991 committed May 28, 2024
1 parent af3fdad commit 01acafb
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lua/gitsigns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ local function setup_attach()

local attach_autocmd_disabled = false

api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'BufWritePost' }, {
-- Need to attach in 'BufFilePost' since we always detach in 'BufFilePre'
api.nvim_create_autocmd({ 'BufFilePost', 'BufRead', 'BufNewFile', 'BufWritePost' }, {
group = 'gitsigns',
desc = 'Gitsigns: attach',
callback = function(args)
local bufnr = args.buf --[[@as integer]]
if attach_autocmd_disabled then
Expand All @@ -142,11 +144,23 @@ local function setup_attach()
end,
})

-- If the buffer name is about to change, then detach
api.nvim_create_autocmd('BufFilePre', {
group = 'gitsigns',
desc = 'Gitsigns: detach when changing buffer names',
callback = function(args)
-- TODO(lewis6991): Should we instead implicitly run `git mv` if the file
-- is tracked?
require('gitsigns.attach').detach(args.buf)
end,
})

--- vimpgrep creates and deletes lots of buffers so attaching to each one will
--- waste lots of resource and even slow down vimgrep.
--- waste lots of resource and slow down vimgrep.
api.nvim_create_autocmd({ 'QuickFixCmdPre', 'QuickFixCmdPost' }, {
group = 'gitsigns',
pattern = '*vimgrep*',
desc = 'Gitsigns: disable attach during vimgrep',
callback = function(args)
attach_autocmd_disabled = args.event == 'QuickFixCmdPre'
end,
Expand Down

0 comments on commit 01acafb

Please sign in to comment.