From ca4f68c5ec8662bc46198bb3bb87577ad8b9a9ce Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 11 Apr 2023 14:24:40 +0200 Subject: [PATCH] feat: added delete fn, handle CR and BS --- src/client/nvim/codemp.lua | 13 ++++++++++++- src/client/nvim/main.rs | 18 ++++++++++++++++++ src/lib/client.rs | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/client/nvim/codemp.lua b/src/client/nvim/codemp.lua index c6c34e5..111a151 100644 --- a/src/client/nvim/codemp.lua +++ b/src/client/nvim/codemp.lua @@ -1,4 +1,4 @@ -local BINARY = "/home/alemi/projects/codemp/target/debug/client-nvim --debug" +local BINARY = "/home/alemi/source/codemp/target/debug/client-nvim --debug" if vim.g.codemp_jobid == nil then vim.g.codemp_jobid = vim.fn.jobstart( @@ -13,6 +13,7 @@ end 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.dump = function() return vim.rpcrequest(vim.g.codemp_jobid, "dump") end M.attach = function(path) vim.api.nvim_create_autocmd( @@ -24,6 +25,16 @@ M.attach = function(path) end, } ) + vim.keymap.set('i', '', function() + local cursor = vim.api.nvim_win_get_cursor(0) + M.delete(path, cursor[2], 1) + return '' + end, {expr = true}) + vim.keymap.set('i', '', function() + local cursor = vim.api.nvim_win_get_cursor(0) + M.insert(path, "\n", cursor[2]) + return '' + end, {expr = true}) return vim.rpcrequest(vim.g.codemp_jobid, "attach", path) end diff --git a/src/client/nvim/main.rs b/src/client/nvim/main.rs index 715d9ea..138c808 100644 --- a/src/client/nvim/main.rs +++ b/src/client/nvim/main.rs @@ -68,6 +68,24 @@ impl Handler for NeovimHandler { } }, + "delete" => { + if args.len() < 3 { + return Err(Value::from("not enough arguments")); + } + let path = args.get(0).unwrap().as_str().unwrap().into(); + let pos = args.get(1).unwrap().as_u64().unwrap(); + let count = args.get(2).unwrap().as_u64().unwrap(); + + let mut c = self.client.clone(); + match c.delete(path, pos, count).await { + Ok(res) => match res { + true => Ok(Value::from("accepted")), + false => Err(Value::from("rejected")), + }, + Err(e) => Err(Value::from(format!("could not send insert: {}", e))), + } + }, + "attach" => { if args.len() < 1 { return Err(Value::from("no path given")); diff --git a/src/lib/client.rs b/src/lib/client.rs index 071f178..5dae970 100644 --- a/src/lib/client.rs +++ b/src/lib/client.rs @@ -67,6 +67,28 @@ impl CodempClient { } } + pub async fn delete(&mut self, path: String, pos: u64, count: u64) -> Result { + let res = { self.factory.lock().unwrap().delete(pos, count) }; + match res { + Ok(op) => { + Ok( + self.client.edit( + OperationRequest { + path, + hash: "".into(), + opseq: serde_json::to_string(&op).unwrap(), + user: self.id.to_string(), + } + ) + .await? + .into_inner() + .accepted + ) + }, + Err(e) => Err(Status::internal(format!("invalid operation: {}", e))), + } + } + pub async fn attach () + Send + 'static>(&mut self, path: String, callback: F) -> Result<(), Status> { let stream = self.client.attach( BufferPayload {