From dcd981d3482d2d085c560912a4ec3e720175b782 Mon Sep 17 00:00:00 2001 From: luxluth Date: Fri, 24 Jan 2025 20:34:59 +0100 Subject: [PATCH] Removing `socat` dependency for internal `vim.uv` --- README.md | 15 -------- lua/oz/engine.lua | 93 ++++++++++++++++++++++++++++++----------------- 2 files changed, 59 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 3c26864..f47942e 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,6 @@ A neovim plugin for the oz programming language -> It's not a complete implementation but it can do the job for now - ## Configuration Default configuration options @@ -31,19 +29,6 @@ Some available commands: - `OzEngineRestart` to restart the engine - `OzOpenLog` to see the logs -## Dependencies - -The plugin depends on the `socat` command to send code to the ozengine. -It's available on all major distros see [here](https://pkgs.org/download/socat) - -On macOS, `socat` can be installed with [brew](https://formulae.brew.sh/formula/socat): - -```zsh -brew install socat -``` - -> The plugin is only tested on linux for the moment. - ## TODOs - [x] simple connection to the ozengine diff --git a/lua/oz/engine.lua b/lua/oz/engine.lua index d156c48..2459dbe 100644 --- a/lua/oz/engine.lua +++ b/lua/oz/engine.lua @@ -1,4 +1,5 @@ local LogBuf = require("oz.log") +local uv = vim.uv ---@class OzEngine local M = {} @@ -11,34 +12,48 @@ local EC = { active = false, }, compiler = { - ---@type number - pid = nil, + ---@type uv.uv_tcp_t|nil + client = nil, active = false, }, } ---@param port string function EC:spinsup_compiler(port) - local command = { "socat", "-", string.format("TCP:localhost:%s", port) } - - self.compiler.pid = vim.fn.jobstart(command, { - ---@param data string[] - on_stdout = function(_, data, _) - LogBuf:push(data) - end, - on_stderr = function(_, data, _) - vim.notify(table.concat(data, "\n"), vim.log.levels.WARN, { title = "oz.nvim" }) - end, - on_exit = function(_, _, _) - vim.notify(table.concat(command, " ") .. " " .. "has exited", vim.log.levels.WARN, { title = "oz.nvim" }) - end, - }) - - if self.compiler.pid > 0 then - self.compiler.active = true + uv.check_start(uv.new_check(), function() end) + + local client = uv.new_tcp() + local iport = tonumber(port) + + if client ~= nil and iport ~= nil then + self.compiler.client = client + client:connect("127.0.0.1", iport, function(err) + if err then + vim.notify( + "Unable to connect to the ozengine server\n[CAUSE] " .. err, + vim.log.levels.ERROR, + { title = "oz.nvim" } + ) + else + self.compiler.active = true + client:read_start(function(read_err, chunk) + if read_err then + vim.notify("ozengine server read error\n[CAUSE] " .. read_err, vim.log.levels.ERROR, { title = "oz.nvim" }) + elseif chunk then + LogBuf:push(vim.split(chunk, "\n", { trimempty = true })) + else + vim.notify("The ozengine server has disconnected", vim.log.levels.WARN, { title = "oz.nvim" }) + uv.close(client, function() + vim.notify("The listenner has been closed", vim.log.levels.WARN, { title = "oz.nvim" }) + self.compiler.active = false + self.compiler.client = nil + end) + end + end) + end + end) else - vim.notify("Unable to connect to the ozengine by TCP", vim.log.levels.WARN, { title = "oz.nvim" }) - self.compiler.active = false + vim.notify("Unable to connect to the ozengine througth TCP", vim.log.levels.WARN, { title = "oz.nvim" }) end end @@ -49,20 +64,17 @@ function EC:start(instance) if self.server.pid == nil then self.server.pid = vim.fn.jobstart(command, { ---@param data string[] - on_stdout = function(id, data, event) + on_stdout = function(_, data, _) -- check for socket if not connected yet if self.server.active == false then - local server_port, debug_port = string.match(table.concat(data, "\n"), "'oz%-socket (%d+) (%d+)'") - -- vim.notify(server_port, vim.log.levels.INFO, { title = "oz.nvim" }) + local server_port, _ = string.match(table.concat(data, "\n"), "'oz%-socket (%d+) (%d+)'") if self.compiler.active == false then - self.spinsup_compiler(self, server_port) + self:spinsup_compiler(server_port) end self.server.active = true end end, - on_stderr = function(id, data, event) - -- vim.notify(data, vim.log.levels.TRACE, { title = "oz.nvim" }) - end, + on_stderr = function(_, _, _) end, on_exit = function(_, _, _) vim.notify(table.concat(command, " ") .. " " .. "has exited", vim.log.levels.WARN, { title = "oz.nvim" }) end, @@ -90,8 +102,8 @@ function EC:shutdown() -- filename = "", -- line = 0, -- }) - vim.fn.jobstop(self.compiler.pid) - self.compiler.pid = nil + uv.close(self.compiler.client) + self.compiler.client = nil self.compiler.active = false LogBuf:reset() end @@ -112,9 +124,8 @@ end ---@param message CompilerMessage function EC:send(message) if self.compiler.active then - vim.notify(string.format("feed send to pid %d", self.compiler.pid), vim.log.levels.TRACE, { title = "oz.nvim" }) - vim.fn.chansend( - self.compiler.pid, + uv.write( + self.compiler.client, message.data .. "\n%%oz-nvim:linter:filename:" .. message.filename @@ -122,7 +133,21 @@ function EC:send(message) .. message.line .. ":char:" .. message.character - .. "\n\x04\n" + .. "\n\x04\n", + function(err) + if err then + vim.notify( + "An error occured while sending the feed to the client\n[CAUSE] " .. err, + vim.log.levels.WARN, + { title = "oz.nvim" } + ) + end + end + ) + vim.notify( + string.format("feed sended ... (localhost:%s)", self.compiler.client:getsockname().port), + vim.log.levels.TRACE, + { title = "oz.nvim" } ) end end