From 0a464296cd1a1308ced8baebf99f21edca1c8f3a Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 11 Apr 2023 22:35:27 +0200 Subject: [PATCH] feat: added Join/Share commands --- src/client/nvim/codemp.lua | 78 +++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/src/client/nvim/codemp.lua b/src/client/nvim/codemp.lua index 90fac65..d418bae 100644 --- a/src/client/nvim/codemp.lua +++ b/src/client/nvim/codemp.lua @@ -1,4 +1,4 @@ -local BINARY = "/home/alemi/source/codemp/target/debug/client-nvim --debug" +local BINARY = "/home/alemi/projects/codemp/target/debug/client-nvim --debug" if vim.g.codemp_jobid == nil then vim.g.codemp_jobid = vim.fn.jobstart( @@ -14,33 +14,57 @@ 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) - vim.api.nvim_create_autocmd( - { "InsertCharPre" }, - { - callback = function() - local cursor = vim.api.nvim_win_get_cursor(0) - local off = vim.fn.line2byte(cursor[1]) + cursor[2] - 1 - M.insert(path, vim.v.char, off) - end, - } - ) - vim.keymap.set('i', '', function() - local cursor = vim.api.nvim_win_get_cursor(0) - local off = vim.fn.line2byte(cursor[1]) + cursor[2] - 1 - if off > 0 then - M.delete(path, off, 1) - end - return '' - end, {expr = true}) - vim.keymap.set('i', '', function() - local cursor = vim.api.nvim_win_get_cursor(0) - local off = vim.fn.line2byte(cursor[1]) + cursor[2] - 1 - M.insert(path, "\n", off) - return '' - end, {expr = true}) - return vim.rpcrequest(vim.g.codemp_jobid, "attach", path) +M.attach = function(path) return vim.rpcrequest(vim.g.codemp_jobid, "attach", path) end + +local function cursor_offset() + local cursor = vim.api.nvim_win_get_cursor(0) + return vim.fn.line2byte(cursor[1]) + cursor[2] - 1 end +local function hook_callbacks(path) + vim.api.nvim_create_autocmd( + { "InsertCharPre" }, + { callback = function() M.insert(path, vim.v.char, cursor_offset()) end } + ) + vim.keymap.set('i', '', function() + local off = cursor_offset() + pcall(M.delete, path, off, 1) + return '' + end, {expr = true}) + vim.keymap.set('i', '', function() + pcall(M.cancel, path, cursor_offset(), 1) + return '' + end, {expr = true}) + vim.keymap.set('i', '', function() + pcall(M.insertpath, "\n", cursor_offset()) + return '' + end, {expr = true}) +end + +vim.api.nvim_create_user_command( + 'Share', + function(args) + local path = args.fargs[1] + 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) + M.attach(path) + end, + {nargs=1} +) + +vim.api.nvim_create_user_command( + 'Join', + function(args) + local path = args.fargs[1] + M.sync(path) + hook_callbacks(path) + M.attach(path) + end, + {nargs=1} +) + return M