@@ -2,31 +2,20 @@ local api = require("copilot.api")
2
2
local config = require (" copilot.config" )
3
3
local util = require (" copilot.util" )
4
4
local logger = require (" copilot.logger" )
5
- local lsp_binary = require (" copilot.lsp_binary " )
6
- local lsp_nodesj = require (" copilot.lsp_nodejs " )
5
+ local lsp = require (" copilot.lsp " )
6
+ local utils = require (" copilot.client.utils " )
7
7
8
8
local is_disabled = false
9
9
10
10
local M = {
11
11
augroup = " copilot.client" ,
12
12
id = nil ,
13
13
--- @class copilot_capabilities : lsp.ClientCapabilities
14
- --- @field copilot table< ' openURL' , boolean>
15
14
--- @field workspace table< ' workspaceFolders' , boolean>
16
15
capabilities = nil ,
17
16
config = nil ,
18
17
startup_error = nil ,
19
18
initialized = false ,
20
- --- @type copilot_should_attach
21
- should_attach = nil ,
22
- --- @type string< ' nodejs' , ' binary' >
23
- --- @class copilot_config_server
24
- server = {
25
- --- @type string< ' nodejs' , ' binary' >
26
- type = " nodejs" ,
27
- --- @type string | nil
28
- custom_server_filepath = nil ,
29
- },
30
19
}
31
20
32
21
--- @param id integer
46
35
47
36
--- @param force ? boolean
48
37
function M .buf_attach (force )
49
- if lsp_binary .initialization_failed then
38
+ if lsp . binary .initialization_failed then
50
39
M .startup_error = " initialization of copilot-language-server failed"
51
40
return
52
41
end
@@ -59,7 +48,7 @@ function M.buf_attach(force)
59
48
local bufnr = vim .api .nvim_get_current_buf ()
60
49
local bufname = vim .api .nvim_buf_get_name (bufnr )
61
50
62
- if not (force or (M .should_attach (bufnr , bufname ) and util .should_attach ())) then
51
+ if not (force or (config .should_attach (bufnr , bufname ) and util .should_attach ())) then
63
52
logger .debug (" not attaching to buffer based on force and should_attach criteria" )
64
53
return
65
54
end
@@ -70,7 +59,7 @@ function M.buf_attach(force)
70
59
end
71
60
72
61
-- In case it has changed, we update it
73
- M .config .root_dir = config .get_root_dir ()
62
+ M .config .root_dir = utils .get_root_dir (config . root_dir )
74
63
75
64
local ok , client_id_or_err = pcall (vim .lsp .start , M .config )
76
65
if not ok then
@@ -161,7 +150,7 @@ local function get_handlers()
161
150
}
162
151
163
152
-- optional handlers
164
- local logger_conf = config .config . logger
153
+ local logger_conf = config .logger
165
154
if logger_conf .trace_lsp ~= " off" then
166
155
handlers = vim .tbl_extend (" force" , handlers , {
167
156
[" $/logTrace" ] = logger .handle_lsp_trace ,
@@ -184,7 +173,7 @@ local function get_handlers()
184
173
end
185
174
186
175
local function prepare_client_config (overrides )
187
- if lsp_binary .initialization_failed then
176
+ if lsp . binary .initialization_failed then
188
177
M .startup_error = " initialization of copilot-language-server failed"
189
178
return
190
179
end
@@ -194,19 +183,19 @@ local function prepare_client_config(overrides)
194
183
local server_path = nil
195
184
local cmd = nil
196
185
197
- if M .server .custom_server_filepath and vim .fn .filereadable (M .server .custom_server_filepath ) then
198
- server_path = M .server .custom_server_filepath
186
+ if config .server .custom_server_filepath and vim .fn .filereadable (config .server .custom_server_filepath ) then
187
+ server_path = config .server .custom_server_filepath
199
188
end
200
189
201
- if M .server .type == " nodejs" then
190
+ if config .server .type == " nodejs" then
202
191
cmd = {
203
- lsp_nodesj .node_command ,
204
- server_path or lsp_nodesj .get_server_path (),
192
+ lsp . nodejs .node_command ,
193
+ server_path or lsp . nodejs .get_server_path (),
205
194
" --stdio" ,
206
195
}
207
- elseif M .server .type == " binary" then
196
+ elseif config .server .type == " binary" then
208
197
cmd = {
209
- server_path or lsp_binary .get_server_path (),
198
+ server_path or lsp . binary .get_server_path (),
210
199
" --stdio" ,
211
200
}
212
201
end
@@ -223,7 +212,7 @@ local function prepare_client_config(overrides)
223
212
workspaceFolders = true ,
224
213
}
225
214
226
- local root_dir = config .get_root_dir ()
215
+ local root_dir = utils .get_root_dir (config . root_dir )
227
216
local workspace_folders = {
228
217
--- @type workspace_folder
229
218
{
@@ -233,7 +222,7 @@ local function prepare_client_config(overrides)
233
222
},
234
223
}
235
224
236
- local config_workspace_folders = config .config . workspace_folders
225
+ local config_workspace_folders = config .workspace_folders
237
226
238
227
for _ , config_workspace_folder in ipairs (config_workspace_folders ) do
239
228
if config_workspace_folder ~= " " then
@@ -249,7 +238,7 @@ local function prepare_client_config(overrides)
249
238
end
250
239
251
240
local editor_info = util .get_editor_info ()
252
- local provider_url = config .config . auth_provider_url
241
+ local provider_url = config .auth_provider_url
253
242
local proxy_uri = vim .g .copilot_proxy
254
243
255
244
local settings = { --- @type copilot_settings
@@ -296,7 +285,7 @@ local function prepare_client_config(overrides)
296
285
logger .trace (" workspace configuration" , configurations )
297
286
298
287
-- to activate tracing if we want it
299
- local logger_conf = config .config . logger
288
+ local logger_conf = config .logger
300
289
local trace_params = { value = logger_conf .trace_lsp } --[[ @as copilot_nofify_set_trace_params]]
301
290
api .notify_set_trace (client , trace_params )
302
291
@@ -329,22 +318,16 @@ local function prepare_client_config(overrides)
329
318
end
330
319
331
320
function M .setup ()
332
- M .should_attach = config .config .should_attach
333
- local server_config = config .config .server
334
- local node_command = config .config .copilot_node_command
335
- M .server = vim .tbl_deep_extend (" force" , M .server , server_config )
321
+ local node_command = config .copilot_node_command
336
322
337
- if M .server .custom_server_filepath then
338
- M .server .custom_server_filepath = vim .fs .normalize (M .server .custom_server_filepath )
323
+ -- TODO: merge the two types into an indirection
324
+ if config .server .type == " nodejs" then
325
+ lsp .nodejs .setup (node_command , config .server .custom_server_filepath )
326
+ elseif config .server .type == " binary" then
327
+ lsp .binary .setup (config .server .custom_server_filepath )
339
328
end
340
329
341
- if M .server .type == " nodejs" then
342
- lsp_nodesj .setup (node_command , M .server .custom_server_filepath )
343
- elseif M .server .type == " binary" then
344
- lsp_binary .setup (M .server .custom_server_filepath )
345
- end
346
-
347
- M .config = prepare_client_config (config .config .server_opts_overrides )
330
+ M .config = prepare_client_config (config .server_opts_overrides )
348
331
349
332
if not M .config then
350
333
is_disabled = true
@@ -378,54 +361,4 @@ function M.teardown()
378
361
end
379
362
end
380
363
381
- function M .add_workspace_folder (folder_path )
382
- if type (folder_path ) ~= " string" then
383
- logger .error (" workspace folder path must be a string" )
384
- return false
385
- end
386
-
387
- if vim .fn .isdirectory (folder_path ) ~= 1 then
388
- logger .error (" invalid workspace folder: " .. folder_path )
389
- return false
390
- end
391
-
392
- -- Normalize path
393
- folder_path = vim .fn .fnamemodify (folder_path , " :p" )
394
-
395
- --- @type workspace_folder
396
- local workspace_folder = {
397
- uri = vim .uri_from_fname (folder_path ),
398
- name = folder_path ,
399
- }
400
-
401
- local workspace_folders = config .config .workspace_folders
402
- if not workspace_folders then
403
- workspace_folders = {}
404
- end
405
-
406
- for _ , existing_folder in ipairs (workspace_folders ) do
407
- if existing_folder == folder_path then
408
- return
409
- end
410
- end
411
-
412
- table.insert (workspace_folders , { folder_path })
413
- config .set (" workspace_folders" , workspace_folders )
414
-
415
- local client = M .get ()
416
- if client and client .initialized then
417
- api .notify (client , " workspace/didChangeWorkspaceFolders" , {
418
- event = {
419
- added = { workspace_folder },
420
- removed = {},
421
- },
422
- })
423
- logger .notify (" added workspace folder: " .. folder_path )
424
- else
425
- logger .notify (" workspace folder will be added on next session: " .. folder_path )
426
- end
427
-
428
- return true
429
- end
430
-
431
364
return M
0 commit comments