From 1eec71f3b27dd9c8e0d2a81c9d363edf4ef21030 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 12 Apr 2023 00:31:59 +0200 Subject: [PATCH] fix: callbacks local to buffer, local bufnr + path --- src/client/nvim/codemp.lua | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/client/nvim/codemp.lua b/src/client/nvim/codemp.lua index d418bae..3388353 100644 --- a/src/client/nvim/codemp.lua +++ b/src/client/nvim/codemp.lua @@ -14,8 +14,6 @@ local M = {} M.create = function(path, content) return vim.rpcrequest(vim.g.codemp_jobid, "create", path, content) end M.insert = function(path, txt, pos) return vim.rpcrequest(vim.g.codemp_jobid, "insert", path, txt, pos) end M.delete = function(path, pos, count) return vim.rpcrequest(vim.g.codemp_jobid, "delete", path, pos, count) end -M.sync = function(path) return vim.rpcrequest(vim.g.codemp_jobid, "sync", path) end -M.dump = function() return vim.rpcrequest(vim.g.codemp_jobid, "dump") end M.attach = function(path) return vim.rpcrequest(vim.g.codemp_jobid, "attach", path) end local function cursor_offset() @@ -23,24 +21,27 @@ local function cursor_offset() return vim.fn.line2byte(cursor[1]) + cursor[2] - 1 end -local function hook_callbacks(path) +local function hook_callbacks(path, buffer) vim.api.nvim_create_autocmd( { "InsertCharPre" }, - { callback = function() M.insert(path, vim.v.char, cursor_offset()) end } + { + callback = function(_) M.insert(path, vim.v.char, cursor_offset()) end, + buffer = buffer, + } ) vim.keymap.set('i', '', function() local off = cursor_offset() - pcall(M.delete, path, off, 1) + M.delete(path, off, 1) return '' - end, {expr = true}) + end, {expr = true, buffer = buffer}) vim.keymap.set('i', '', function() - pcall(M.cancel, path, cursor_offset(), 1) + M.delete(path, cursor_offset(), 1) return '' - end, {expr = true}) + end, {expr = true, buffer = buffer}) vim.keymap.set('i', '', function() - pcall(M.insertpath, "\n", cursor_offset()) + M.insert(path, "\n", cursor_offset()) return '' - end, {expr = true}) + end, {expr = true, buffer = buffer}) end vim.api.nvim_create_user_command( @@ -50,7 +51,7 @@ vim.api.nvim_create_user_command( local bufnr = vim.api.nvim_get_current_buf() local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) M.create(path, vim.fn.join(lines, "\n")) - hook_callbacks(path) + hook_callbacks(path, bufnr) M.attach(path) end, {nargs=1} @@ -60,8 +61,8 @@ vim.api.nvim_create_user_command( 'Join', function(args) local path = args.fargs[1] - M.sync(path) - hook_callbacks(path) + local bufnr = vim.api.nvim_get_current_buf() + hook_callbacks(path, bufnr) M.attach(path) end, {nargs=1}