-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from hat0uma/fix_duplicate_parsing
Fix: Proper detachment of `nvim_buf_attach` on toggling
- Loading branch information
Showing
6 changed files
with
176 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- Buffer utilities | ||
local M = {} | ||
|
||
--- Resolve bufnr | ||
---@param bufnr integer| nil | ||
---@return integer | ||
function M.resolve_bufnr(bufnr) | ||
if not bufnr or bufnr == 0 then | ||
return vim.api.nvim_get_current_buf() | ||
else | ||
return bufnr | ||
end | ||
end | ||
|
||
--- Watch buffer-update events | ||
---@param bufnr integer | ||
---@param callbacks vim.api.keyset.buf_attach | ||
---@return fun() detach_bufevent | ||
function M.attach(bufnr, callbacks) | ||
local detached = false | ||
local function wrap_buf_attach_handler(cb) | ||
if not cb then | ||
return nil | ||
end | ||
|
||
return function(...) | ||
if detached then | ||
return true -- detach | ||
end | ||
|
||
return cb(...) | ||
end | ||
end | ||
|
||
local function attach_events() | ||
vim.api.nvim_buf_attach(bufnr, false, { | ||
on_lines = wrap_buf_attach_handler(callbacks.on_lines), | ||
on_bytes = wrap_buf_attach_handler(callbacks.on_bytes), | ||
on_changedtick = wrap_buf_attach_handler(callbacks.on_changedtick), | ||
on_reload = wrap_buf_attach_handler(callbacks.on_reload), | ||
on_detach = wrap_buf_attach_handler(callbacks.on_detach), | ||
}) | ||
end | ||
|
||
-- Attach to buffer | ||
attach_events() | ||
|
||
-- Re-register events on `:e` | ||
local buf_event_auid = vim.api.nvim_create_autocmd({ "BufReadPost" }, { | ||
callback = function() | ||
attach_events() | ||
if callbacks.on_reload then | ||
callbacks.on_reload("reload", bufnr) | ||
end | ||
end, | ||
buffer = bufnr, | ||
}) | ||
|
||
-- detach | ||
return function() | ||
if detached then | ||
return | ||
end | ||
|
||
vim.api.nvim_del_autocmd(buf_event_auid) | ||
detached = true | ||
end | ||
end | ||
|
||
return M |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.