11local Utils = require (" eca.utils" )
22local Config = require (" eca.config" )
3- local PathFinder = require (" eca.path_finder" )
43local Logger = require (" eca.logger" )
54
65--- @class eca.Server
@@ -13,16 +12,17 @@ local Logger = require("eca.logger")
1312--- @field on_stop function Callback when the server stops
1413--- Called when a notification is received(message without an ID)
1514--- @field on_notification fun ( server : eca.Server , message : table )
16- --- @field private path_finder eca.PathFinder Server path finder
1715--- @field pending_requests { id : fun ( err , data )} -- outgoing requests with callbacks
16+ --- @field cwd string Current working directory for the server process
17+ --- @field workspace_folders { name : string , uri : string } [] Workspace folders to send on initialize
1818local M = {}
1919
2020--- @param opts ? table
2121--- @return eca.Server
2222function M .new (opts )
2323 opts = vim .tbl_extend (" keep" , opts or {}, {
2424 on_start = function (pid )
25- require (" eca.logger" ).notify (" Started server with pid " .. pid , vim . log . levels . INFO )
25+ require (" eca.logger" ).debug (" Started server with pid " .. pid )
2626 end ,
2727 on_initialize = function ()
2828 require (" eca.logger" ).notify (" Server ready to receive messages" , vim .log .levels .INFO )
@@ -37,7 +37,13 @@ function M.new(opts)
3737 require (" eca.observer" ).notify (message )
3838 end )
3939 end ,
40- path_finder = PathFinder :new (),
40+ cwd = vim .fn .getcwd (),
41+ workspace_folders = {
42+ {
43+ name = vim .fn .fnamemodify (Utils .get_project_root (), " :t" ),
44+ uri = " file://" .. Utils .get_project_root (),
45+ },
46+ },
4147 })
4248
4349 return setmetatable ({
@@ -46,11 +52,12 @@ function M.new(opts)
4652 on_initialize = opts .on_initialize ,
4753 on_stop = opts .on_stop ,
4854 on_notification = opts .on_notification ,
49- path_finder = opts .path_finder ,
5055 messages = {},
5156 pending_requests = {},
5257 initialized = false ,
5358 next_id = 0 ,
59+ cwd = opts .cwd ,
60+ workspace_folders = opts .workspace_folders ,
5461 }, { __index = M })
5562end
5663
93100function M :start (opts )
94101 opts = opts or { initialize = true }
95102
96- local server_path
97- local ok , path_finder_error = pcall (function ()
98- server_path = self .path_finder :find ()
99- end )
103+ local custom_path = Config .server_path or " "
100104
101- if not ok or not server_path then
102- Logger .notify (" Could not find or download ECA server" .. tostring (path_finder_error ), vim .log .levels .ERROR )
103- return
104- end
105+ local this_file = debug.getinfo (1 , " S" ).source :sub (2 )
106+ local proj_root = vim .fn .fnamemodify (this_file , " :p:h:h:h" )
107+ local script_path = proj_root .. " /scripts/server_path.lua"
105108
106- Logger . debug ( " Starting ECA server: " .. server_path )
109+ local nvim_exe = vim . fn . exepath ( " nvim " )
107110
108- local args = { server_path , " server" }
109- if Config .server_args and Config .server_args ~= " " then
110- vim .list_extend (args , vim .split (Config .server_args , " " ))
111+ if not nvim_exe or nvim_exe == " " then
112+ nvim_exe = " nvim"
111113 end
112114
113- opts = vim .tbl_deep_extend (" keep" , opts , {
114- cmd = args ,
115- text = true ,
116- cwd = vim .fn .getcwd (),
117- stdin = true ,
118- stdout = on_stdout (self ),
119- stderr = on_stderr ,
120- --- @param out vim.SystemCompleted
121- on_exit = function (out )
122- if out .code ~= 0 then
123- require (" eca.logger" ).notify (string.format (" Server exited with status code %d" , out .code ), vim .log .levels .ERROR )
124- end
125- end ,
126- })
115+ local cmd = { nvim_exe , " -l" , script_path , custom_path }
127116
128- local started , process_or_err = pcall (vim .system , opts .cmd , {
129- cwd = opts .cwd ,
130- text = opts .text ,
131- stdin = opts .stdin ,
132- stdout = opts .stdout ,
133- stderr = opts .stderr ,
134- }, opts .on_exit )
135-
136- if not started then
137- self .process = nil
138- Logger .notify (vim .inspect (process_or_err ), vim .log .levels .ERROR )
139- return
140- end
117+ vim .system (cmd , { text = true }, function (out )
118+ if out .code ~= 0 then
119+ Logger .notify (out .stderr , vim .log .levels .ERROR )
120+ return
121+ end
141122
142- self .process = process_or_err
143- if self .on_start then
144- self .on_start (process_or_err .pid )
145- end
123+ local stdout_lines = Utils .split_lines (out .stdout )
124+ local server_path = stdout_lines [# stdout_lines ]
146125
147- if opts .initialize then
148- self :initialize ()
149- end
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
172+ end )
150173end
151174
152175function M :initialize ()
153- local workspace_folders = {
154- {
155- name = vim .fn .fnamemodify (Utils .get_project_root (), " :t" ),
156- uri = " file://" .. Utils .get_project_root (),
157- },
158- }
159-
160176 self :send_request (" initialize" , {
161177 processId = vim .fn .getpid (),
162178 clientInfo = {
@@ -168,7 +184,7 @@ function M:initialize()
168184 chat = true ,
169185 },
170186 },
171- workspaceFolders = workspace_folders ,
187+ workspaceFolders = vim . deepcopy ( self . workspace_folders ) ,
172188 }, function (err , _ )
173189 if err then
174190 Logger .notify (" Could not initialize server: " .. err , vim .log .levels .ERROR )
0 commit comments