2023-04-11 22:35:27 +02:00
|
|
|
local BINARY = "/home/alemi/projects/codemp/target/debug/client-nvim --debug"
|
2023-04-11 06:20:40 +02:00
|
|
|
|
|
|
|
if vim.g.codemp_jobid == nil then
|
2023-04-11 14:02:45 +02:00
|
|
|
vim.g.codemp_jobid = vim.fn.jobstart(
|
|
|
|
BINARY,
|
|
|
|
{
|
|
|
|
rpc = true,
|
|
|
|
on_stderr = function(_, data, _) print(vim.fn.join(data, "\n")) end,
|
|
|
|
}
|
|
|
|
)
|
2023-04-11 06:20:40 +02:00
|
|
|
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
|
2023-04-11 14:24:40 +02:00
|
|
|
M.delete = function(path, pos, count) return vim.rpcrequest(vim.g.codemp_jobid, "delete", path, pos, count) end
|
2023-04-11 22:35:27 +02:00
|
|
|
M.sync = function(path) return vim.rpcrequest(vim.g.codemp_jobid, "sync", path) end
|
2023-04-11 06:20:40 +02:00
|
|
|
M.dump = function() return vim.rpcrequest(vim.g.codemp_jobid, "dump") end
|
2023-04-11 22:35:27 +02:00
|
|
|
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)
|
2023-04-11 06:20:40 +02:00
|
|
|
vim.api.nvim_create_autocmd(
|
|
|
|
{ "InsertCharPre" },
|
2023-04-11 22:35:27 +02:00
|
|
|
{ callback = function() M.insert(path, vim.v.char, cursor_offset()) end }
|
2023-04-11 06:20:40 +02:00
|
|
|
)
|
2023-04-11 14:24:40 +02:00
|
|
|
vim.keymap.set('i', '<BS>', function()
|
2023-04-11 22:35:27 +02:00
|
|
|
local off = cursor_offset()
|
|
|
|
pcall(M.delete, path, off, 1)
|
2023-04-11 14:24:40 +02:00
|
|
|
return '<BS>'
|
|
|
|
end, {expr = true})
|
2023-04-11 22:35:27 +02:00
|
|
|
vim.keymap.set('i', '<Del>', function()
|
|
|
|
pcall(M.cancel, path, cursor_offset(), 1)
|
|
|
|
return '<Del>'
|
|
|
|
end, {expr = true})
|
2023-04-11 14:24:40 +02:00
|
|
|
vim.keymap.set('i', '<CR>', function()
|
2023-04-11 22:35:27 +02:00
|
|
|
pcall(M.insertpath, "\n", cursor_offset())
|
2023-04-11 14:24:40 +02:00
|
|
|
return '<CR>'
|
|
|
|
end, {expr = true})
|
2023-04-11 06:20:40 +02:00
|
|
|
end
|
|
|
|
|
2023-04-11 22:35:27 +02:00
|
|
|
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}
|
|
|
|
)
|
|
|
|
|
2023-04-11 06:20:40 +02:00
|
|
|
return M
|