@@ -91,6 +91,61 @@ local function on_stderr(err, data)
9191 end
9292end
9393
94+ --- @class eca.ServerStartOpts : vim.SystemOpts
95+ --- @field cmd string[] The command to pass to vim.system
96+ --- @field on_exit fun ( out : vim.SystemCompleted ) callback to pass to vim.system
97+ --- @field initialize boolean Send the initialize message to ECA on startup , used
98+ --- in testing
99+ --- @param opts ? eca.ServerStartOpts
100+ function M :_run (server_path , opts )
101+ Logger .debug (" Starting ECA server: " .. server_path )
102+
103+ local args = { server_path , " server" }
104+
105+ if Config .server_args and Config .server_args ~= " " then
106+ vim .list_extend (args , vim .split (Config .server_args , " " ))
107+ end
108+
109+ opts = vim .tbl_deep_extend (" keep" , opts , {
110+ cmd = args ,
111+ text = true ,
112+ cwd = self .cwd ,
113+ stdin = true ,
114+ stdout = on_stdout (self ),
115+ stderr = on_stderr ,
116+ --- @param out vim.SystemCompleted
117+ on_exit = function (out )
118+ if out .code ~= 0 then
119+ require (" eca.logger" ).notify (string.format (" Server exited with status code %d" , out .code ), vim .log .levels
120+ .ERROR )
121+ end
122+ end ,
123+ })
124+
125+ local started , process_or_err = pcall (vim .system , opts .cmd , {
126+ cwd = opts .cwd ,
127+ text = opts .text ,
128+ stdin = opts .stdin ,
129+ stdout = opts .stdout ,
130+ stderr = opts .stderr ,
131+ }, opts .on_exit )
132+
133+ if not started then
134+ self .process = nil
135+ Logger .notify (vim .inspect (process_or_err ), vim .log .levels .ERROR )
136+ return
137+ end
138+
139+ self .process = process_or_err
140+ if self .on_start then
141+ self .on_start (process_or_err .pid )
142+ end
143+
144+ if opts .initialize then
145+ self :initialize ()
146+ end
147+ end
148+
94149--- @class eca.ServerStartOpts : vim.SystemOpts
95150--- @field cmd string[] The command to pass to vim.system
96151--- @field on_exit fun ( out : vim.SystemCompleted ) callback to pass to vim.system
100155function M :start (opts )
101156 opts = vim .tbl_deep_extend (" force" , { initialize = true }, opts or {})
102157
158+ -- Check for custom server path first
159+ local custom_path = Config .server_path
160+ if custom_path and custom_path :gsub (" %s+" , " " ) ~= " " then
161+ if not Utils .file_exists (custom_path ) then
162+ Logger .notify (" Custom server path does not exist: " .. custom_path , vim .log .levels .ERROR )
163+ return
164+ end
165+
166+ self :_run (custom_path , opts )
167+ return
168+ end
169+
103170 local this_file = debug.getinfo (1 , " S" ).source :sub (2 )
104171 local proj_root = vim .fn .fnamemodify (this_file , " :p:h:h:h" )
105172 local script_path = proj_root .. " /scripts/server_path.lua"
@@ -110,9 +177,7 @@ function M:start(opts)
110177 nvim_exe = " nvim"
111178 end
112179
113- local lua_cmd = string.format (" lua ServerPath.run(%s)" , Utils .lua_quote (Config .server_path or " " ))
114-
115- local cmd = { nvim_exe , " --headless" , " --noplugin" , (opts .clean and " --clean" or " " ), " -u" , script_path , " -c" , lua_cmd }
180+ local cmd = { nvim_exe , " --headless" , " -S" , script_path }
116181
117182 vim .system (cmd , { text = true }, function (out )
118183 if out .code ~= 0 then
@@ -123,52 +188,7 @@ function M:start(opts)
123188 local stdout_lines = Utils .split_lines (out .stdout )
124189 local server_path = stdout_lines [# stdout_lines ]
125190
126- Logger .debug (" Starting ECA server: " .. server_path )
127-
128- local args = { server_path , " server" }
129-
130- if Config .server_args and Config .server_args ~= " " then
131- vim .list_extend (args , vim .split (Config .server_args , " " ))
132- end
133-
134- opts = vim .tbl_deep_extend (" keep" , opts , {
135- cmd = args ,
136- text = true ,
137- cwd = self .cwd ,
138- stdin = true ,
139- stdout = on_stdout (self ),
140- stderr = on_stderr ,
141- --- @param output vim.SystemCompleted
142- on_exit = function (output )
143- if output .code ~= 0 then
144- require (" eca.logger" ).notify (string.format (" Server exited with status code %d" , output .code ), vim .log .levels
145- .ERROR )
146- end
147- end ,
148- })
149-
150- local started , process_or_err = pcall (vim .system , opts .cmd , {
151- cwd = opts .cwd ,
152- text = opts .text ,
153- stdin = opts .stdin ,
154- stdout = opts .stdout ,
155- stderr = opts .stderr ,
156- }, opts .on_exit )
157-
158- if not started then
159- self .process = nil
160- Logger .notify (vim .inspect (process_or_err ), vim .log .levels .ERROR )
161- return
162- end
163-
164- self .process = process_or_err
165- if self .on_start then
166- self .on_start (process_or_err .pid )
167- end
168-
169- if opts .initialize then
170- self :initialize ()
171- end
191+ self :_run (server_path , opts )
172192 end )
173193end
174194
0 commit comments