1
0
Fork 0
codesnap.nvim/lua/codesnap/client.lua

62 lines
1.1 KiB
Lua
Raw Normal View History

2024-02-19 14:27:17 +00:00
local logger = require("codesnap.utils.logger")
2024-02-21 10:33:44 +00:00
local static = require("codesnap.static")
2024-02-19 14:27:17 +00:00
local client = {
job_id = 0,
}
2024-02-21 10:33:44 +00:00
local cwd = static.cwd .. "/snap-server"
2024-02-19 14:27:17 +00:00
function client:connect()
return vim.fn.jobstart({
2024-02-21 10:33:44 +00:00
cwd .. "/target/release/snap-server",
2024-02-19 14:27:17 +00:00
}, {
2024-02-21 10:33:44 +00:00
cwd = cwd,
2024-02-19 14:27:17 +00:00
stderr_buffered = true,
rpc = true,
on_stderr = function(_, err)
vim.fn.jobstop(self.job_id)
logger.error(err)
end,
on_exit = function()
vim.fn.chanclose(self.job_id)
self.job_id = 0
end,
})
end
function client:init()
return self.job_id == 0 and client:connect() or self.job_id
end
function client:start()
self.job_id = client:init()
if self.job_id == 0 then
logger.error("cannot start rpc process")
return
end
if self.job_id == -1 then
logger.error("rpc process is not executable")
vim.fn.jobstop(self.job_id)
return
end
return self
end
function client:send(event, message)
vim.fn.rpcnotify(self.job_id, event, message)
end
function client:stop()
if self.job_id == 0 or self.job_id == -1 then
return
end
vim.fn.jobstop(self.job_id)
end
return client