From 55c4ddb93ab44e434246ac4a69c4b1d17f06a89f Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 12 Apr 2023 01:38:38 +0200 Subject: [PATCH] feat: improved Lua API for plugin added commands to connect/disconnect, jobid is kept internally, commands to hook/unhook callbacks, refactored stuff a little --- src/client/nvim/codemp.lua | 100 ++++++++++++++++++++++++------------- src/client/nvim/main.rs | 10 ++++ src/lib/client.rs | 8 ++- 3 files changed, 81 insertions(+), 37 deletions(-) diff --git a/src/client/nvim/codemp.lua b/src/client/nvim/codemp.lua index 3388353..63fb064 100644 --- a/src/client/nvim/codemp.lua +++ b/src/client/nvim/codemp.lua @@ -1,51 +1,74 @@ -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( - BINARY, - { - rpc = true, - on_stderr = function(_, data, _) print(vim.fn.join(data, "\n")) end, - } - ) -end +local BINARY = vim.g.codemp_binary or "/home/alemi/projects/codemp/target/debug/client-nvim" 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.attach = function(path) return vim.rpcrequest(vim.g.codemp_jobid, "attach", path) end +M.jobid = nil +M.create = function(path, content) return vim.rpcrequest(M.jobid, "create", path, content) end +M.insert = function(path, txt, pos) return vim.rpcrequest(M.jobid, "insert", path, txt, pos) end +M.delete = function(path, pos, count) return vim.rpcrequest(M.jobid, "delete", path, pos, count) end +M.attach = function(path) return vim.rpcrequest(M.jobid, "attach", path) end +M.detach = function(path) return vim.rpcrequest(M.jobid, "detach", 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 codemp_autocmds = vim.api.nvim_create_augroup("CodempAuGroup", { clear = true }) + local function hook_callbacks(path, buffer) vim.api.nvim_create_autocmd( { "InsertCharPre" }, { callback = function(_) M.insert(path, vim.v.char, cursor_offset()) end, buffer = buffer, + group = codemp_autocmds, } ) - vim.keymap.set('i', '', function() - local off = cursor_offset() - M.delete(path, off, 1) - return '' - end, {expr = true, buffer = buffer}) - vim.keymap.set('i', '', function() - M.delete(path, cursor_offset(), 1) - return '' - end, {expr = true, buffer = buffer}) - vim.keymap.set('i', '', function() - M.insert(path, "\n", cursor_offset()) - return '' - end, {expr = true, buffer = buffer}) + vim.keymap.set('i', '', function() M.delete(path, cursor_offset(), 1) return '' end, {expr = true, buffer = buffer}) + vim.keymap.set('i', '', function() M.delete(path, cursor_offset() + 1, 1) return '' end, {expr = true, buffer = buffer}) + vim.keymap.set('i', '', function() M.insert(path, "\n", cursor_offset()) return ''end, {expr = true, buffer = buffer}) end -vim.api.nvim_create_user_command( - 'Share', +local function unhook_callbacks(buffer) + vim.api.nvim_clear_autocmds({ group = codemp_autocmds, buffer = buffer }) + vim.keymap.del('i', '', { buffer = buffer }) + vim.keymap.del('i', '', { buffer = buffer }) + vim.keymap.del('i', '', { buffer = buffer }) +end + + +vim.api.nvim_create_user_command('Connect', + function(args) + if M.jobid ~= nil then + print("already connected, disconnect first") + return + end + local bin_args = { BINARY } + if #args.args > 0 then + table.insert(bin_args, "--host") + table.insert(bin_args, args.args[1]) + end + if args.bang then + table.insert(bin_args, "--debug") + end + M.jobid = vim.fn.jobstart( + bin_args, + { + rpc = true, + on_stderr = function(_, data, _) print(vim.fn.join(data, "\n")) end, + } + ) + end, +{ nargs='?', bang=true }) + +vim.api.nvim_create_user_command('Stop', + function(_) + vim.fn.jobstop(M.jobid) + M.jobid = nil + end, +{ bang=true }) + +vim.api.nvim_create_user_command('Share', function(args) local path = args.fargs[1] local bufnr = vim.api.nvim_get_current_buf() @@ -54,18 +77,23 @@ vim.api.nvim_create_user_command( hook_callbacks(path, bufnr) M.attach(path) end, - {nargs=1} -) +{ nargs=1 }) -vim.api.nvim_create_user_command( - 'Join', +vim.api.nvim_create_user_command('Join', function(args) local path = args.fargs[1] local bufnr = vim.api.nvim_get_current_buf() hook_callbacks(path, bufnr) M.attach(path) end, - {nargs=1} -) +{ nargs=1 }) + +vim.api.nvim_create_user_command('Detach', + function(args) + local bufnr = vim.api.nvim_get_current_buf() + unhook_callbacks(bufnr) + M.detach(args.fargs[1]) + end, +{ nargs=1 }) return M diff --git a/src/client/nvim/main.rs b/src/client/nvim/main.rs index 3e16d56..4f59aeb 100644 --- a/src/client/nvim/main.rs +++ b/src/client/nvim/main.rs @@ -137,6 +137,16 @@ impl Handler for NeovimHandler { } }, + "detach" => { + if args.len() < 1 { + return Err(Value::from("no path given")); + } + let path = default_empty_str(&args, 0); + let mut c = self.client.clone(); + c.detach(path); + Ok(Value::Nil) + }, + _ => Err(Value::from("unimplemented")), } } diff --git a/src/lib/client.rs b/src/lib/client.rs index 436fe89..fd1b308 100644 --- a/src/lib/client.rs +++ b/src/lib/client.rs @@ -1,7 +1,7 @@ /// TODO better name for this file use std::{sync::{Arc, RwLock}, collections::BTreeMap}; -use tracing::{error, warn}; +use tracing::{error, warn, info}; use uuid::Uuid; use crate::{ @@ -104,6 +104,10 @@ impl CodempClient { Ok(content) } + pub fn detach(&mut self, path: String) { + self.factories.write().unwrap().remove(&path); + } + async fn sync(&mut self, path: String) -> Result { let res = self.client.sync( BufferPayload { @@ -115,6 +119,7 @@ impl CodempClient { async fn worker(mut stream: Streaming, factory: Arc, callback: F) where F : Fn(String) -> () { + info!("|> buffer worker started"); loop { match stream.message().await { Err(e) => break error!("error receiving change: {}", e), @@ -130,5 +135,6 @@ impl CodempClient { }, } } + info!("[] buffer worker stopped"); } }