From b0d46bde3810125475912b01b46198550bc85510 Mon Sep 17 00:00:00 2001 From: vyfor Date: Tue, 3 Dec 2024 09:00:31 +0500 Subject: [PATCH] refactor: separate process spawning logic --- lua/cord/core/ipc.lua | 63 +++------------------------------------ lua/cord/core/spawn.lua | 66 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 59 deletions(-) create mode 100644 lua/cord/core/spawn.lua diff --git a/lua/cord/core/ipc.lua b/lua/cord/core/ipc.lua index 7d1aac69..62e9b0a2 100644 --- a/lua/cord/core/ipc.lua +++ b/lua/cord/core/ipc.lua @@ -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 } @@ -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) diff --git a/lua/cord/core/spawn.lua b/lua/cord/core/spawn.lua new file mode 100644 index 00000000..7f710f25 --- /dev/null +++ b/lua/cord/core/spawn.lua @@ -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