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
256 lines (214 loc) · 6.22 KB
/
client.lua
File metadata and controls
256 lines (214 loc) · 6.22 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
local api = require("copilot.api")
local config = require("copilot.config")
local util = require("copilot.util")
local is_disabled = false
local M = {
augroup = "copilot.client",
id = nil,
capabilities = nil,
config = nil,
node_version = nil,
startup_error = nil,
}
---@param id number
local function store_client_id(id)
if M.id and M.id ~= id then
if vim.lsp.get_client_by_id(M.id) then
error("unexpectedly started multiple copilot server")
end
end
M.id = id
end
local lsp_start = vim.lsp.start
if not lsp_start then
local function reuse_client(client, conf)
return client.config.root_dir == conf.root_dir and client.name == conf.name
end
-- shim for neovim < 0.8.2
lsp_start = function(lsp_config)
local bufnr = vim.api.nvim_get_current_buf()
local client = M.get()
if client and reuse_client(client, lsp_config) then
vim.lsp.buf_attach_client(bufnr, client.id)
return client.id
end
local client_id = vim.lsp.start_client(lsp_config) --[[@as number]]
vim.lsp.buf_attach_client(bufnr, client_id)
return client_id
end
end
---@return string
function M.get_node_version()
if not M.node_version then
local node = config.get("copilot_node_command")
local cmd = { node, "--version" }
local cmd_output_table = vim.fn.systemlist(cmd, nil, false)
local cmd_output = cmd_output_table[#cmd_output_table]
local cmd_exit_code = vim.v.shell_error
local node_version = string.match(cmd_output, "^v(%S+)") or ""
local node_version_major = tonumber(string.match(node_version, "^(%d+)%.")) or 0
if node_version_major == 0 then
local err = "[Copilot] Could not determine Node.js version"
vim.notify(err, vim.log.levels.WARN)
vim.api.nvim_echo({
{
table.concat({
err,
"-----------",
"(exit code) " .. tostring(cmd_exit_code),
" (output) " .. cmd_output,
"-----------",
}, "\n"),
"MoreMsg",
},
}, true, {})
elseif node_version_major < 16 then
local err = string.format("[Copilot] Node.js version 16.x or newer required but found %s", node_version)
vim.notify(err, vim.log.levels.WARN)
end
M.node_version = node_version or ""
end
return M.node_version
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 is_disabled then
print("[Copilot] Offline")
return
end
if not force and not util.should_attach() then
return
end
local client_id = lsp_start(M.config)
store_client_id(client_id)
end
function M.buf_detach()
if M.buf_is_attached(0) then
vim.lsp.buf_detach_client(0, M.id)
end
end
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
print("[Copilot] Offline")
return
end
local client = M.get() --[[@as table]]
if not client then
if not M.config then
error("copilot.setup is not called yet")
end
local client_id = vim.lsp.start_client(M.config)
store_client_id(client_id)
client = M.get()
end
if client.initialized then
callback(client)
return
end
local timer = vim.loop.new_timer()
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 prepare_client_config(overrides)
local node = config.get("copilot_node_command")
if vim.fn.executable(node) ~= 1 then
local err = string.format("copilot_node_command(%s) is not executable", node)
vim.notify("[Copilot] " .. err, vim.log.levels.ERROR)
M.startup_error = err
return
end
local agent_path = vim.api.nvim_get_runtime_file("copilot/index.js", false)[1]
if vim.fn.filereadable(agent_path) == 0 then
local err = string.format("Could not find agent.js (bad install?) : %s", agent_path)
vim.notify("[Copilot] " .. err, vim.log.levels.ERROR)
M.startup_error = err
return
end
M.startup_error = nil
return vim.tbl_deep_extend("force", {
cmd = {
node,
agent_path,
},
root_dir = vim.loop.cwd(),
name = "copilot",
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()
---@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()
set_editor_info_params.networkProxy = util.get_network_proxy()
api.set_editor_info(client, set_editor_info_params, function(err)
if err then
vim.notify(string.format("[copilot] setEditorInfo failure: %s", err), vim.log.levels.ERROR)
end
end)
end)
end,
on_exit = function(_code, _signal, client_id)
if M.id == client_id then
M.id = nil
M.capabilities = nil
end
end,
handlers = {
PanelSolution = api.handlers.PanelSolution,
PanelSolutionsDone = api.handlers.PanelSolutionsDone,
statusNotification = api.handlers.statusNotification,
},
}, overrides)
end
function M.setup()
M.config = prepare_client_config(config.get("server_opts_overrides"))
if not M.config then
is_disabled = true
return
end
is_disabled = false
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
return M