Skip to content

Commit

Permalink
cleaning up autocommands on bufdelete
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Badragan committed May 11, 2024
1 parent 1c02df8 commit bb3cdb4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lua/grug-far.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ local function createContext(options)
options = options,
namespace = namespace,
locationsNamespace = vim.api.nvim_create_namespace(''),
augroup = vim.api.nvim_create_augroup('grug-far.nvim-augroup-' .. contextCount, {}),
extmarkIds = {},
state = {
inputs = {}
Expand All @@ -49,9 +50,11 @@ local function setupCleanup(buf, context)
local function onBufDelete()
vim.api.nvim_buf_clear_namespace(buf, context.locationsNamespace, 0, -1)
vim.api.nvim_buf_clear_namespace(buf, context.namespace, 0, -1)
vim.api.nvim_del_augroup_by_id(context.augroup)
end

vim.api.nvim_create_autocmd({ 'BufDelete' }, {
group = context.augroup,
buffer = buf,
callback = onBufDelete
})
Expand Down
7 changes: 5 additions & 2 deletions lua/grug-far/farBuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ local function updateBufName(buf, context)
context.count .. utils.strEllideAfter(context.state.inputs.search, context.options.maxSearchCharsInTitles, ': '))
end

local function setupGlobalOptOverrides(buf)
local function setupGlobalOptOverrides(buf, context)
local originalBackspaceOpt = vim.opt.backspace:get()
local function onBufEnter()
-- this prevents backspacing over eol when clearing an input line
Expand All @@ -65,10 +65,12 @@ local function setupGlobalOptOverrides(buf)
end

vim.api.nvim_create_autocmd({ 'BufEnter' }, {
group = context.augroup,
buffer = buf,
callback = onBufEnter
})
vim.api.nvim_create_autocmd({ 'BufLeave' }, {
group = context.augroup,
buffer = buf,
callback = onBufLeave
})
Expand All @@ -79,7 +81,7 @@ function M.createBuffer(win, context)
vim.api.nvim_buf_set_option(buf, 'filetype', 'grug-far')
vim.api.nvim_win_set_buf(win, buf)

setupGlobalOptOverrides(buf)
setupGlobalOptOverrides(buf, context)
setupKeymap(buf, context)

local debouncedSearch = utils.debounce(search, context.options.debounceMs)
Expand All @@ -102,6 +104,7 @@ function M.createBuffer(win, context)

-- set up re-render on change
vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI' }, {
group = context.augroup,
buffer = buf,
callback = handleBufferChange
})
Expand Down

0 comments on commit bb3cdb4

Please sign in to comment.