forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
432 lines (359 loc) · 10.8 KB
/
client.lua
File metadata and controls
432 lines (359 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
local api = require("copilot.api")
local config = require("copilot.config")
local util = require("copilot.util")
local logger = require("copilot.logger")
local lsp_binary = require("copilot.lsp_binary")
local lsp_nodesj = require("copilot.lsp_nodejs")
local is_disabled = false
local M = {
augroup = "copilot.client",
id = nil,
--- @class copilot_capabilities:lsp.ClientCapabilities
--- @field copilot table<'openURL', boolean>
--- @field workspace table<'workspaceFolders', boolean>
capabilities = nil,
config = nil,
startup_error = nil,
initialized = false,
---@type copilot_should_attach
should_attach = nil,
---@type string<'nodejs', 'binary'>
---@class copilot_config_server
server = {
---@type string<'nodejs', 'binary'>
type = "nodejs",
---@type string|nil
custom_server_filepath = nil,
},
}
---@param id integer
local function store_client_id(id)
if M.id and M.id ~= id then
if vim.lsp.get_client_by_id(M.id) then
vim.lsp.stop_client(M.id)
end
end
M.id = id
end
function M.buf_is_attached(bufnr)
return M.id and vim.lsp.buf_is_attached(bufnr or 0, M.id)
end
---@param force? boolean
function M.buf_attach(force)
if lsp_binary.initialization_failed then
M.startup_error = "initialization of copilot-language-server failed"
return
end
if is_disabled then
logger.warn("copilot is disabled")
return
end
local bufnr = vim.api.nvim_get_current_buf()
local bufname = vim.api.nvim_buf_get_name(bufnr)
if not (force or (M.should_attach(bufnr, bufname) and util.should_attach())) then
logger.debug("not attaching to buffer based on force and should_attach criteria")
return
end
if not M.config then
logger.error("cannot attach: configuration not initialized")
return
end
-- In case it has changed, we update it
M.config.root_dir = config.get_root_dir()
local ok, client_id_or_err = pcall(vim.lsp.start, M.config)
if not ok then
logger.error(string.format("failed to start LSP client: %s", client_id_or_err))
return
end
if client_id_or_err then
store_client_id(client_id_or_err)
else
logger.error("LSP client failed to start (no client ID returned)")
end
end
function M.buf_detach()
if M.buf_is_attached(0) then
vim.lsp.buf_detach_client(0, M.id)
end
end
---@return nil|vim.lsp.Client
function M.get()
return vim.lsp.get_client_by_id(M.id)
end
function M.is_disabled()
return is_disabled
end
---@param callback fun(client:table):nil
function M.use_client(callback)
if is_disabled then
logger.warn("copilot is offline")
return
end
local client = M.get() --[[@as table]]
if not client then
if not M.config then
logger.error("copilot.setup is not called yet")
return
end
local client_id, err = vim.lsp.start(M.config)
if not client_id then
logger.error(string.format("error starting LSP client: %s", err))
return
end
store_client_id(client_id)
client = M.get() --[[@as table]]
end
if client.initialized then
callback(client)
return
end
local timer, err, _ = vim.uv.new_timer()
if not timer then
logger.error(string.format("error creating timer: %s", err))
return
end
timer:start(
0,
100,
vim.schedule_wrap(function()
if client.initialized and not timer:is_closing() then
timer:stop()
timer:close()
callback(client)
end
end)
)
end
local function get_handlers()
local handlers = {
PanelSolution = api.handlers.PanelSolution,
PanelSolutionsDone = api.handlers.PanelSolutionsDone,
statusNotification = api.handlers.statusNotification,
["window/showDocument"] = util.show_document,
}
-- optional handlers
local logger_conf = config.get("logger") --[[@as copilot_config_logging]]
if logger_conf.trace_lsp ~= "off" then
handlers = vim.tbl_extend("force", handlers, {
["$/logTrace"] = logger.handle_lsp_trace,
})
end
if logger_conf.trace_lsp_progress then
handlers = vim.tbl_extend("force", handlers, {
["$/progress"] = logger.handle_lsp_progress,
})
end
if logger_conf.log_lsp_messages then
handlers = vim.tbl_extend("force", handlers, {
["window/logMessage"] = logger.handle_log_lsp_messages,
})
end
return handlers
end
local function prepare_client_config(overrides)
if lsp_binary.initialization_failed then
M.startup_error = "initialization of copilot-language-server failed"
return
end
M.startup_error = nil
local server_path = nil
local cmd = nil
if M.server.custom_server_filepath and vim.fn.filereadable(M.server.custom_server_filepath) then
server_path = M.server.custom_server_filepath
end
if M.server.type == "nodejs" then
cmd = {
lsp_nodesj.node_command,
server_path or lsp_nodesj.get_server_path(),
"--stdio",
}
elseif M.server.type == "binary" then
cmd = {
server_path or lsp_binary.get_server_path(),
"--stdio",
}
end
if not cmd then
logger.error("copilot server type not supported")
return
end
local capabilities = vim.lsp.protocol.make_client_capabilities() --[[@as copilot_capabilities]]
capabilities.window.showDocument.support = true
capabilities.workspace = {
workspaceFolders = true,
}
local root_dir = config.get_root_dir()
local workspace_folders = {
--- @type workspace_folder
{
uri = vim.uri_from_fname(root_dir),
-- important to keep root_dir as-is for the name as lsp.lua uses this to check the workspace has not changed
name = root_dir,
},
}
local config_workspace_folders = config.get("workspace_folders") --[[@as table<string>]]
for _, config_workspace_folder in ipairs(config_workspace_folders) do
if config_workspace_folder ~= "" then
table.insert(
workspace_folders,
--- @type workspace_folder
{
uri = vim.uri_from_fname(config_workspace_folder),
name = config_workspace_folder,
}
)
end
end
local editor_info = util.get_editor_info()
local provider_url = config.get("auth_provider_url") --[[@as string|nil]]
local proxy_uri = vim.g.copilot_proxy
local settings = { ---@type copilot_settings
telemetry = { ---@type github_settings_telemetry
telemetryLevel = "all",
},
}
if proxy_uri then
vim.tbl_extend("force", settings, {
http = { ---@type copilot_settings_http
proxy = proxy_uri,
proxyStrictSSL = vim.g.copilot_proxy_strict_ssl or false,
proxyKerberosServicePrincipal = nil,
},
})
end
if provider_url then
vim.tbl_extend("force", settings, {
["github-enterprise"] = { ---@type copilot_settings_github-enterprise
uri = provider_url,
},
})
end
-- LSP config, not to be confused with config.lua
return vim.tbl_deep_extend("force", {
cmd = cmd,
root_dir = root_dir,
name = "copilot",
capabilities = capabilities,
get_language_id = function(_, filetype)
return util.language_for_file_type(filetype)
end,
on_init = function(client, initialize_result)
if M.id == client.id then
M.capabilities = initialize_result.capabilities
end
vim.schedule(function()
local configurations = util.get_workspace_configurations()
api.notify_change_configuration(client, configurations)
logger.trace("workspace configuration", configurations)
-- to activate tracing if we want it
local logger_conf = config.get("logger") --[[@as copilot_config_logging]]
local trace_params = { value = logger_conf.trace_lsp } --[[@as copilot_nofify_set_trace_params]]
api.notify_set_trace(client, trace_params)
-- prevent requests to copilot prior to being initialized
M.initialized = true
end)
end,
on_exit = function(code, _, client_id)
if M.id == client_id then
vim.schedule(function()
M.teardown()
M.id = nil
M.capabilities = nil
end)
end
if code > 0 then
vim.schedule(function()
require("copilot.command").status()
end)
end
end,
handlers = get_handlers(),
init_options = {
editorInfo = editor_info.editorInfo,
editorPluginInfo = editor_info.editorPluginInfo,
},
settings = settings,
workspace_folders = workspace_folders,
trace = config.get("trace") or "off",
}, overrides)
end
function M.setup()
M.should_attach = config.get("should_attach") --[[@as copilot_should_attach|nil]]
local server_config = config.get("server") --[[@as copilot_config_server]]
local node_command = config.get("copilot_node_command") --[[@as string|nil]]
M.server = vim.tbl_deep_extend("force", M.server, server_config)
if M.server.custom_server_filepath then
M.server.custom_server_filepath = vim.fs.normalize(M.server.custom_server_filepath)
end
if M.server.type == "nodejs" then
lsp_nodesj.setup(node_command, M.server.custom_server_filepath)
elseif M.server.type == "binary" then
lsp_binary.setup(M.server.custom_server_filepath)
end
M.config = prepare_client_config(config.get("server_opts_overrides"))
if not M.config then
is_disabled = true
return
end
is_disabled = false
M.id = nil
vim.api.nvim_create_augroup(M.augroup, { clear = true })
vim.api.nvim_create_autocmd("FileType", {
group = M.augroup,
callback = vim.schedule_wrap(function()
M.buf_attach()
end),
})
vim.schedule(function()
M.buf_attach()
end)
end
function M.teardown()
is_disabled = true
vim.api.nvim_clear_autocmds({ group = M.augroup })
if M.id then
vim.lsp.stop_client(M.id)
end
end
function M.add_workspace_folder(folder_path)
if type(folder_path) ~= "string" then
logger.error("workspace folder path must be a string")
return false
end
if vim.fn.isdirectory(folder_path) ~= 1 then
logger.error("invalid workspace folder: " .. folder_path)
return false
end
-- Normalize path
folder_path = vim.fn.fnamemodify(folder_path, ":p")
--- @type workspace_folder
local workspace_folder = {
uri = vim.uri_from_fname(folder_path),
name = folder_path,
}
local workspace_folders = config.get("workspace_folders") --[[@as table<string>]]
if not workspace_folders then
workspace_folders = {}
end
for _, existing_folder in ipairs(workspace_folders) do
if existing_folder == folder_path then
return
end
end
table.insert(workspace_folders, { folder_path })
config.set("workspace_folders", workspace_folders)
local client = M.get()
if client and client.initialized then
api.notify(client, "workspace/didChangeWorkspaceFolders", {
event = {
added = { workspace_folder },
removed = {},
},
})
logger.notify("added workspace folder: " .. folder_path)
else
logger.notify("workspace folder will be added on next session: " .. folder_path)
end
return true
end
return M