Skip to content

Commit

Permalink
Removing socat dependency for internal vim.uv
Browse files Browse the repository at this point in the history
  • Loading branch information
luxluth committed Jan 24, 2025
1 parent c008379 commit dcd981d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 49 deletions.
15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
93 changes: 59 additions & 34 deletions lua/oz/engine.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local LogBuf = require("oz.log")
local uv = vim.uv

---@class OzEngine
local M = {}
Expand All @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -112,17 +124,30 @@ 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
.. ":line:"
.. 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
Expand Down

0 comments on commit dcd981d

Please sign in to comment.