feat: added delete fn, handle CR and BS

This commit is contained in:
əlemi 2023-04-11 14:24:40 +02:00
parent 532de6639f
commit ca4f68c5ec
3 changed files with 52 additions and 1 deletions

View file

@ -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 if vim.g.codemp_jobid == nil then
vim.g.codemp_jobid = vim.fn.jobstart( vim.g.codemp_jobid = vim.fn.jobstart(
@ -13,6 +13,7 @@ end
local M = {} local M = {}
M.create = function(path, content) return vim.rpcrequest(vim.g.codemp_jobid, "create", path, content) end 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.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.dump = function() return vim.rpcrequest(vim.g.codemp_jobid, "dump") end
M.attach = function(path) M.attach = function(path)
vim.api.nvim_create_autocmd( vim.api.nvim_create_autocmd(
@ -24,6 +25,16 @@ M.attach = function(path)
end, end,
} }
) )
vim.keymap.set('i', '<BS>', function()
local cursor = vim.api.nvim_win_get_cursor(0)
M.delete(path, cursor[2], 1)
return '<BS>'
end, {expr = true})
vim.keymap.set('i', '<CR>', function()
local cursor = vim.api.nvim_win_get_cursor(0)
M.insert(path, "\n", cursor[2])
return '<CR>'
end, {expr = true})
return vim.rpcrequest(vim.g.codemp_jobid, "attach", path) return vim.rpcrequest(vim.g.codemp_jobid, "attach", path)
end end

View file

@ -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" => { "attach" => {
if args.len() < 1 { if args.len() < 1 {
return Err(Value::from("no path given")); return Err(Value::from("no path given"));

View file

@ -67,6 +67,28 @@ impl CodempClient {
} }
} }
pub async fn delete(&mut self, path: String, pos: u64, count: u64) -> Result<bool, Status> {
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<F : Fn(String) -> () + Send + 'static>(&mut self, path: String, callback: F) -> Result<(), Status> { pub async fn attach<F : Fn(String) -> () + Send + 'static>(&mut self, path: String, callback: F) -> Result<(), Status> {
let stream = self.client.attach( let stream = self.client.attach(
BufferPayload { BufferPayload {