codemp-nvim/src/client/nvim/codemp.lua

47 lines
1.4 KiB
Lua
Raw Normal View History

local BINARY = "/home/alemi/source/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
M.delete = function(path, pos, count) return vim.rpcrequest(vim.g.codemp_jobid, "delete", path, pos, count) end
2023-04-11 06:20:40 +02:00
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)
2023-04-11 17:12:22 +02:00
local off = vim.fn.line2byte(cursor[1]) + cursor[2] - 1
M.insert(path, vim.v.char, off)
2023-04-11 06:20:40 +02:00
end,
}
)
vim.keymap.set('i', '<BS>', function()
local cursor = vim.api.nvim_win_get_cursor(0)
2023-04-11 17:12:22 +02:00
local off = vim.fn.line2byte(cursor[1]) + cursor[2] - 1
if off > 0 then
M.delete(path, off, 1)
end
return '<BS>'
end, {expr = true})
vim.keymap.set('i', '<CR>', function()
local cursor = vim.api.nvim_win_get_cursor(0)
2023-04-11 17:12:22 +02:00
local off = vim.fn.line2byte(cursor[1]) + cursor[2] - 1
M.insert(path, "\n", off)
return '<CR>'
end, {expr = true})
2023-04-11 06:20:40 +02:00
return vim.rpcrequest(vim.g.codemp_jobid, "attach", path)
end
return M