forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuggestion.lua
More file actions
459 lines (375 loc) · 10.5 KB
/
suggestion.lua
File metadata and controls
459 lines (375 loc) · 10.5 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
local api = require("copilot.api")
local hl_group = require("copilot.highlight").group
local util = require("copilot.util")
local mod = {}
local copilot = {
setup_done = false,
augroup = "copilot.suggestion",
ns_id = vim.api.nvim_create_namespace("copilot.suggestion"),
extmark_id = 1,
uuid = nil,
_copilot_timer = nil,
_copilot = {
first = nil,
cycling = nil,
cycling_callbacks = nil,
params = nil,
suggestions = nil,
choice = nil,
},
auto_trigger = false,
debounce = 75,
}
local function get_client()
if not copilot.client then
copilot.client = util.get_copilot_client()
end
return copilot.client
end
local function with_client(fn)
local client = get_client()
if client then
fn(client)
end
end
local function is_enabled()
local client = get_client()
return client and vim.lsp.buf_is_attached(0, client.id) or false
end
local function should_auto_trigger()
if vim.b.copilot_suggestion_auto_trigger == nil then
return copilot.auto_trigger
end
return vim.b.copilot_suggestion_auto_trigger
end
local function reset_state()
copilot._copilot = {
first = nil,
cycling = nil,
cycling_callbacks = nil,
params = nil,
suggestions = nil,
choice = nil,
}
end
local function set_keymap(keymap)
if keymap.accept then
vim.keymap.set("i", keymap.accept, mod.accept, {
desc = "[copilot] accept suggestion",
silent = true,
})
end
if keymap.next then
vim.keymap.set("i", keymap.next, mod.next, {
desc = "[copilot] next suggestion",
silent = true,
})
end
if keymap.prev then
vim.keymap.set("i", keymap.prev, mod.prev, {
desc = "[copilot] prev suggestion",
silent = true,
})
end
if keymap.dismiss then
vim.keymap.set("i", keymap.dismiss, mod.dismiss, {
desc = "[copilot] dismiss suggestion",
silent = true,
})
end
end
local function stop_timer()
if copilot._copilot_timer then
vim.fn.timer_stop(copilot._copilot_timer)
copilot._copilot_timer = nil
end
end
local function reject_current()
if copilot.uuid then
with_client(function(client)
api.notify_rejected(client, { uuids = { copilot.uuid } }, function() end)
end)
copilot.uuid = nil
end
end
local function cancel_inflight_requests()
with_client(function(client)
if copilot._copilot.first then
client.cancel_request(copilot._copilot.first)
copilot._copilot.first = nil
end
if copilot._copilot.cycling then
client.cancel_request(copilot._copilot.cycling)
copilot._copilot.cycling = nil
end
end)
end
local function clear_preview()
vim.api.nvim_buf_del_extmark(0, copilot.ns_id, copilot.extmark_id)
end
local function get_current_suggestion()
local ok, choice = pcall(function()
if
not vim.fn.mode():match("^[iR]")
or vim.fn.pumvisible() == 1
or vim.b.copilot_suggestion_hidden
or not copilot._copilot.suggestions
or #copilot._copilot.suggestions == 0
then
return nil
end
local choice = copilot._copilot.suggestions[copilot._copilot.choice]
if not choice or not choice.range or choice.range.start.line ~= vim.fn.line(".") - 1 then
return nil
end
if choice.range.start.character ~= 0 then
-- unexpected range
return nil
end
return choice
end)
if ok then
return choice
end
return nil
end
local function update_preview()
local suggestion = get_current_suggestion()
local displayLines = suggestion and vim.split(suggestion.displayText, "\n", { plain = true }) or {}
clear_preview()
if not suggestion or #displayLines == 0 then
return
end
---@todo support popup preview
local annot = ""
if copilot._copilot.cycling_callbacks then
annot = "(1/…)"
elseif copilot._copilot.cycling then
annot = "(" .. copilot._copilot.choice .. "/" .. #copilot._copilot.suggestions .. ")"
end
local extmark = {
id = copilot.extmark_id,
virt_text_win_col = vim.fn.virtcol(".") - 1,
virt_text = { { displayLines[1], hl_group.CopilotSuggestion } },
}
if #displayLines > 1 then
extmark.virt_lines = {}
for i = 2, #displayLines do
extmark.virt_lines[i - 1] = { { displayLines[i], hl_group.CopilotSuggestion } }
end
if #annot > 0 then
extmark.virt_lines[#displayLines] = { { " " }, { annot, hl_group.CopilotAnnotation } }
end
elseif #annot > 0 then
extmark.virt_text[2] = { " " }
extmark.virt_text[3] = { annot, hl_group.CopilotAnnotation }
end
extmark.hl_mode = "combine"
vim.api.nvim_buf_set_extmark(0, copilot.ns_id, vim.fn.line(".") - 1, vim.fn.col(".") - 1, extmark)
if suggestion.uuid ~= copilot.uuid then
copilot.uuid = suggestion.uuid
with_client(function(client)
api.notify_shown(client, { uuid = suggestion.uuid }, function() end)
end)
end
end
local function clear()
stop_timer()
reject_current()
cancel_inflight_requests()
update_preview()
reset_state()
end
---@param callback fun(err: any|nil, data: copilot_get_completions_data): nil
local function complete(callback)
stop_timer()
local params = util.get_doc_params()
if not vim.deep_equal(copilot._copilot.params, params) then
with_client(function(client)
local _, id = api.get_completions(client, params, callback)
copilot._copilot = { params = params, first = id }
end)
end
end
---@param data copilot_get_completions_data
local function handle_trigger_request(err, data)
if err then
print(err)
end
copilot._copilot.suggestions = data and data.completions or {}
copilot._copilot.choice = 1
update_preview()
end
local function trigger(bufnr, timer)
local _timer = copilot._copilot_timer
copilot._copilot_timer = nil
if bufnr ~= vim.api.nvim_get_current_buf() or timer ~= _timer or vim.fn.mode() ~= "i" then
return
end
complete(handle_trigger_request)
end
local function get_suggestions_cycling_callback(state, err, data)
local callbacks = state.cycling_callbacks
state.cycling_callbacks = nil
if err then
print(err)
end
local seen = {}
for _, suggestion in ipairs(state.suggestions or {}) do
seen[suggestion.text] = true
end
for _, suggestion in ipairs(data.completions or {}) do
if not seen[suggestion.text] then
table.insert(state.suggestions, suggestion)
seen[suggestion.text] = true
end
end
for _, callback in ipairs(callbacks) do
callback(state)
end
end
local function get_suggestions_cycling(callback)
if copilot._copilot.cycling_callbacks then
table.insert(copilot._copilot.cycling_callbacks, callback)
return
end
if copilot._copilot.cycling then
callback(copilot._copilot)
return
end
if copilot._copilot.suggestions then
copilot._copilot.cycling_callbacks = { callback }
with_client(function(client)
local _, id = api.get_completions_cycling(client, copilot._copilot.params, function(err, data)
get_suggestions_cycling_callback(copilot._copilot, err, data)
end)
copilot._copilot.cycling = id
update_preview()
end)
end
end
local function advance(count, state)
if state ~= copilot._copilot then
return
end
state.choice = (state.choice + count) % #state.suggestions
if state.choice < 1 then
state.choice = #state.suggestions
end
update_preview()
end
local function schedule()
clear()
if not is_enabled() then
return
end
local bufnr = vim.api.nvim_get_current_buf()
copilot._copilot_timer = vim.fn.timer_start(copilot.debounce, function(timer)
trigger(bufnr, timer)
end)
end
function mod.next()
if not copilot._copilot.params and not should_auto_trigger() then
schedule()
return
end
get_suggestions_cycling(function(state)
advance(1, state)
end)
end
function mod.prev()
if not copilot._copilot.params and not should_auto_trigger() then
schedule()
return
end
get_suggestions_cycling(function(state)
advance(-1, state)
end)
end
function mod.accept()
local suggestion = get_current_suggestion()
if suggestion and vim.fn.empty(suggestion.text) == 0 then
reset_state()
with_client(function(client)
api.notify_accepted(client, { uuid = suggestion.uuid }, function() end)
end)
copilot.uuid = nil
clear_preview()
-- Hack for 'autoindent', makes the indent persist. Check `:help 'autoindent'`.
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Space><Left><Del>", true, false, true), "n", false)
vim.lsp.util.apply_text_edits({ { range = suggestion.range, newText = suggestion.text } }, 0, "utf-16")
-- Put cursor at the end of current line.
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<End>", true, false, true), "n", false)
end
end
function mod.dismiss()
clear()
update_preview()
end
function mod.is_visible()
return not not vim.api.nvim_buf_get_extmark_by_id(0, copilot.ns_id, copilot.extmark_id, { details = false })[1]
end
-- toggles auto trigger for the current buffer
function mod.toggle_auto_trigger()
vim.b.copilot_suggestion_auto_trigger = not should_auto_trigger()
end
local function on_insert_leave()
clear()
end
local function on_buf_leave()
if vim.fn.mode():match("^[iR]") then
on_insert_leave()
end
end
local function on_insert_enter()
if should_auto_trigger() then
schedule()
end
end
local function on_buf_enter()
if vim.fn.mode():match("^[iR]") then
on_insert_enter()
end
end
local function on_cursor_moved_i()
if copilot._copilot_timer or copilot._copilot.params or should_auto_trigger() then
schedule()
end
end
local function on_complete_changed()
clear()
end
function mod.setup(config)
if copilot.setup_done then
return
end
set_keymap(config.keymap or {})
copilot.auto_trigger = config.auto_trigger
vim.api.nvim_create_autocmd("InsertLeave", {
callback = on_insert_leave,
desc = "[copilot] (suggestion) insert leave",
})
vim.api.nvim_create_autocmd("BufLeave", {
callback = on_buf_leave,
desc = "[copilot] (suggestion) buf leave",
})
vim.api.nvim_create_autocmd("InsertEnter", {
callback = on_insert_enter,
desc = "[copilot] (suggestion) insert enter",
})
vim.api.nvim_create_autocmd("BufEnter", {
callback = on_buf_enter,
desc = "[copilot] (suggestion) buf enter",
})
vim.api.nvim_create_autocmd("CursorMovedI", {
callback = on_cursor_moved_i,
desc = "[copilot] (suggestion) cursor moved insert",
})
vim.api.nvim_create_autocmd("CompleteChanged", {
callback = on_complete_changed,
desc = "[copilot] (suggestion) complete changed",
})
copilot.debounce = config.debounce
copilot.setup_done = true
end
return mod