Skip to content

Commit

Permalink
refactor: separate process spawning logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vyfor committed Dec 6, 2024
1 parent 19d2ef9 commit b0d46bd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 59 deletions.
63 changes: 4 additions & 59 deletions lua/cord/core/ipc.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local uv = vim.loop
local logger = require 'cord.util.logger'
local utils = require 'cord.util'
local spawn = require 'cord.core.spawn'

local IPC = {}
local mt = { __index = IPC }
Expand All @@ -27,65 +28,9 @@ function IPC:connect(callback)
vim.schedule_wrap(function(err)
if err then
if err == 'ENOENT' then
if self.config.advanced.server.executable_path then
self.executable = self.config.advanced.server.executable_path
else
self.executable = utils.os_name == 'Windows'
and 'target/release/cord.exe'
or 'target/release/cord'
end

if not utils.file_exists(self.executable) then
logger.error(
'Server executable not found at \'' .. self.executable .. '\''
)
return
end

local stdout = uv.new_pipe()
local stderr = uv.new_pipe()
uv.spawn(self.executable, {
args = {
'-p',
self.path,
'-c',
self.config.editor.client,
'-t',
tostring(self.config.advanced.server.timeout),
},
stdio = { nil, stdout, stderr },
detached = true,
hide = true,
})

stderr:read_start(vim.schedule_wrap(function(err, chunk)
if err then
logger.error('Failed to read stderr: ' .. err)
return
end
if chunk then
if chunk:match 'kind: AlreadyExists' then
self:connect(callback)
stderr:close()
stdout:close()
return
end
logger.error('Server error: ' .. chunk)
end
end))

stdout:read_start(vim.schedule_wrap(function(err, chunk)
if err then
logger.error('Failed to read pipe: ' .. err)
return
end

if chunk and chunk:match 'Ready' then
self:connect(callback)
return
end
end))

spawn.spawn_server(self.config, self.path, function()
self:connect(callback)
end)
return
else
logger.error('Failed to connect to pipe: ' .. err)
Expand Down
66 changes: 66 additions & 0 deletions lua/cord/core/spawn.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
local uv = vim.loop
local logger = require 'cord.util.logger'
local utils = require 'cord.util'

local M = {}

function M.spawn_server(config, path, callback)
local executable
if config.advanced.server.executable_path then
executable = config.advanced.server.executable_path
else
executable = utils.os_name == 'Windows' and 'target/release/cord.exe'
or 'target/release/cord'
end

if not utils.file_exists(executable) then
logger.error('Server executable not found at \'' .. executable .. '\'')
return
end

local stdout = uv.new_pipe()
local stderr = uv.new_pipe()
uv.spawn(executable, {
args = {
'-p',
path,
'-c',
config.editor.client,
'-t',
tostring(config.advanced.server.timeout),
},
stdio = { nil, stdout, stderr },
detached = true,
hide = true,
})

stderr:read_start(vim.schedule_wrap(function(err, chunk)
if err then
logger.error('Failed to read stderr: ' .. err)
return
end
if chunk then
if chunk:match 'kind: AlreadyExists' then
callback()
stderr:close()
stdout:close()
return
end
logger.error('Server error: ' .. chunk)
end
end))

stdout:read_start(vim.schedule_wrap(function(err, chunk)
if err then
logger.error('Failed to read pipe: ' .. err)
return
end

if chunk and chunk:match 'Ready' then
callback()
return
end
end))
end

return M

0 comments on commit b0d46bd

Please sign in to comment.