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
79 lines (69 loc) · 2.27 KB
/
client.lua
File metadata and controls
79 lines (69 loc) · 2.27 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
local api = require("copilot.api")
local config = require("copilot.config")
local util = require("copilot.util")
local M = {}
local copilot_node_version = nil
function M.get_node_version()
if not copilot_node_version then
copilot_node_version = string.match(table.concat(vim.fn.systemlist(config.get("copilot_node_command") .. " --version", nil, false)), "v(%S+)")
end
return copilot_node_version
end
local register_autocmd = function()
vim.api.nvim_create_autocmd({ "BufEnter" }, {
callback = vim.schedule_wrap(M.buf_attach_copilot),
})
end
---@param force? boolean
function M.buf_attach(client, force)
if not force and not util.should_attach() then
return
end
client = client or util.get_copilot_client()
if client and not util.is_attached(client) then
vim.lsp.buf_attach_client(0, client.id)
end
end
function M.buf_detach(client)
client = client or util.get_copilot_client()
if client and util.is_attached(client) then
vim.lsp.buf_detach_client(0, client.id)
end
end
M.buf_attach_copilot = function()
M.buf_attach()
end
M.merge_server_opts = function(params)
return vim.tbl_deep_extend("force", {
cmd = {
params.copilot_node_command,
require("copilot.util").get_copilot_path(),
},
cmd_cwd = vim.fn.expand("~"),
root_dir = vim.loop.cwd(),
name = "copilot",
autostart = true,
single_file_support = true,
on_init = function(client)
vim.schedule(function ()
---@type copilot_set_editor_info_params
local set_editor_info_params = util.get_editor_info()
set_editor_info_params.editorInfo.version = set_editor_info_params.editorInfo.version .. ' + Node.js ' .. M.get_node_version()
set_editor_info_params.editorConfiguration = util.get_editor_configuration()
api.set_editor_info(client, set_editor_info_params)
end)
vim.schedule(M.buf_attach_copilot)
vim.schedule(register_autocmd)
end,
handlers = {
PanelSolution = api.handlers.PanelSolution,
PanelSolutionsDone = api.handlers.PanelSolutionsDone,
statusNotification = api.handlers.statusNotification,
},
}, params.server_opts_overrides or {})
end
M.start = function(params)
local client_config = M.merge_server_opts(params)
vim.lsp.start_client(client_config)
end
return M