Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid attaching on_lines handler multiple times #99

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/jupynium/lua/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if Jupynium_syncing_bufs ~= nil then
end

Jupynium_syncing_bufs = {} -- key = bufnr, value = 1
Jupynium_bufs_attached = {} -- key = bufnr, value = 1

function Jupynium_reset_channel()
vim.g.jupynium_channel_id = -1
Expand Down Expand Up @@ -294,16 +295,25 @@ function Jupynium_start_sync(bufnr, ipynb_filename, ask)
group = augroup,
})

if Jupynium_bufs_attached[bufnr] ~= nil then
-- on_lines event handler already attached for this buffer
return
end

vim.api.nvim_buf_attach(bufnr, false, {
on_lines = function(_, _, _, start_row, old_end_row, new_end_row, _)
if Jupynium_syncing_bufs[bufnr] == nil then
return
-- Detach on_lines event handler
Jupynium_bufs_attached[bufnr] = nil
return true
end

local lines = vim.api.nvim_buf_get_lines(bufnr, start_row, new_end_row, false)
Jupynium_rpcnotify("on_lines", bufnr, true, lines, start_row, old_end_row, new_end_row)
end,
})

Jupynium_bufs_attached[bufnr] = 1
end

function Jupynium_stop_sync(bufnr)
Expand Down