@@ -131,20 +131,7 @@ function! copilot#Clear() abort
131
131
return ' '
132
132
endfunction
133
133
134
- function ! s: Reject (bufnr )abort
135
- try
136
- let dict = getbufvar (a: bufnr ,' _copilot' )
137
- if type (dict )== v: t_dict&& ! empty (get (dict ,' shown_choices' , {}))
138
- call copilot#Request (' notifyRejected' , {' uuids' :keys (dict .shown_choices)})
139
- let dict .shown_choices= {}
140
- endif
141
- catch
142
- call copilot#logger#Exception ()
143
- endtry
144
- endfunction
145
-
146
134
function ! copilot#Dismiss ()abort
147
- call s: Reject (' %' )
148
135
call copilot#Clear ()
149
136
call s: UpdatePreview ()
150
137
return ' '
@@ -189,20 +176,30 @@ function! copilot#Enabled() abort
189
176
\ && empty (s: BufferDisabled ())
190
177
endfunction
191
178
179
+ let s: inline_invoked= 0
180
+ let s: inline_automatic= 1
181
+
192
182
function ! copilot#Complete (... )abort
193
183
if exists (' g:_copilot_timer' )
194
184
call timer_stop (remove (g: ,' _copilot_timer' ))
195
185
endif
196
- let params = copilot#doc#Params ()
197
- if ! exists (' b:_copilot.params ' )|| b: _copilot .params !=# params
186
+ let target = [ bufnr ( ' ' ), getbufvar ( ' ' , ' changedtick ' ), line ( ' . ' ), col ( ' . ' )]
187
+ if ! exists (' b:_copilot.target ' )|| b: _copilot .target !=# target
198
188
if exists (' b:_copilot.first' )
199
189
call copilot#agent#Cancel (b: _copilot .first )
200
190
endif
201
191
if exists (' b:_copilot.cycling' )
202
192
call copilot#agent#Cancel (b: _copilot .cycling)
203
193
endif
204
- let b: _copilot= {' params' : params,' first' :
205
- \ copilot#Request (' getCompletions' , params)}
194
+ let params= {
195
+ \ ' textDocument' : {' uri' :bufnr (' ' )},
196
+ \ ' position' :copilot#util#AppendPosition (),
197
+ \ ' formattingOptions' : {' insertSpaces' : &expandtab ?v: true :v: false ,' tabSize' :shiftwidth ()},
198
+ \ ' context' : {' triggerKind' :s: inline_automatic }}
199
+ let b: _copilot= {
200
+ \ ' target' : target,
201
+ \ ' params' : params,
202
+ \ ' first' :copilot#Request (' textDocument/inlineCompletion' , params)}
206
203
let g: _copilot_last= b: _copilot
207
204
endif
208
205
let completion= b: _copilot .first
@@ -221,37 +218,37 @@ function! s:HideDuringCompletion() abort
221
218
endfunction
222
219
223
220
function ! s: SuggestionTextWithAdjustments ()abort
221
+ let empty = [' ' ,0 ,0 , {}]
224
222
try
225
223
if mode ()!~# ' ^[iR]' || (s: HideDuringCompletion ()&& pumvisible ())|| ! exists (' b:_copilot.suggestions' )
226
- return [ ' ' , 0 , 0 , ' ' ]
224
+ return empty
227
225
endif
228
226
let choice= get (b: _copilot .suggestions,b: _copilot .choice, {})
229
- if ! has_key (choice,' range' )|| choice.range .start .line != line (' .' )- 1 || type (choice.text )!=# v: t_string
230
- return [ ' ' , 0 , 0 , ' ' ]
227
+ if ! has_key (choice,' range' )|| choice.range .start .line != line (' .' )- 1 || type (choice.insertText )!=# v: t_string
228
+ return empty
231
229
endif
232
230
let line = getline (' .' )
233
231
let offset= col (' .' )- 1
234
- let choice_text= strpart (line ,0 ,copilot#doc #UTF16ToByteIdx (line , choice.range .start .character )) .substitute (choice.text ," \n *$" ,' ' ,' ' )
232
+ let choice_text= strpart (line ,0 ,copilot#util #UTF16ToByteIdx (line , choice.range .start .character )) .substitute (choice.insertText ," \n *$" ,' ' ,' ' )
235
233
let typed= strpart (line ,0 , offset)
236
- let end_offset= copilot#doc #UTF16ToByteIdx (line , choice.range .end .character )
234
+ let end_offset= copilot#util #UTF16ToByteIdx (line , choice.range .end .character )
237
235
if end_offset < 0
238
236
let end_offset= len (line )
239
237
endif
240
238
let delete = strpart (line , offset, end_offset- offset)
241
- let uuid= get (choice,' uuid' ,' ' )
242
239
if typed= ~#' ^\s*$'
243
240
let leading= matchstr (choice_text,' ^\s\+' )
244
241
let unindented= strpart (choice_text,len (leading))
245
242
if strpart (typed,0 ,len (leading))== leading&& unindented!=# delete
246
- return [unindented,len (typed)- len (leading),strchars (delete ),uuid ]
243
+ return [unindented,len (typed)- len (leading),strchars (delete ),choice ]
247
244
endif
248
245
elseif typed== #strpart (choice_text,0 , offset)
249
- return [strpart (choice_text, offset),0 ,strchars (delete ),uuid ]
246
+ return [strpart (choice_text, offset),0 ,strchars (delete ),choice ]
250
247
endif
251
248
catch
252
249
call copilot#logger#Exception ()
253
250
endtry
254
- return [ ' ' , 0 , 0 , ' ' ]
251
+ return empty
255
252
endfunction
256
253
257
254
@@ -271,12 +268,12 @@ function! s:GetSuggestionsCyclingCallback(context, result) abort
271
268
let callbacks= remove (a: context ,' cycling_callbacks' )
272
269
let seen= {}
273
270
for suggestionin a: context .suggestions
274
- let seen[suggestion.text ]= 1
271
+ let seen[suggestion.insertText ]= 1
275
272
endfor
276
- for suggestionin get (a: result ,' completions ' , [])
277
- if ! has_key (seen, suggestion.text )
273
+ for suggestionin get (a: result ,' items ' , [])
274
+ if ! has_key (seen, suggestion.insertText )
278
275
call add (a: context .suggestions, suggestion)
279
- let seen[suggestion.text ]= 1
276
+ let seen[suggestion.insertText ]= 1
280
277
endif
281
278
endfor
282
279
for Callbackin callbacks
@@ -290,9 +287,11 @@ function! s:GetSuggestionsCycling(callback) abort
290
287
elseif exists (' b:_copilot.cycling' )
291
288
call a: callback (b: _copilot )
292
289
elseif exists (' b:_copilot.suggestions' )
290
+ let params= deepcopy (b: _copilot .first .params)
291
+ let params.context.triggerKind= s: inline_invoked
293
292
let b: _copilot .cycling_callbacks= [a: callback ]
294
- let b: _copilot .cycling= copilot#Request (' getCompletionsCycling ' ,
295
- \ b: _copilot . first . params,
293
+ let b: _copilot .cycling= copilot#Request (' textDocument/inlineCompletion ' ,
294
+ \ params,
296
295
\ function (' s:GetSuggestionsCyclingCallback' , [b: _copilot ]),
297
296
\ function (' s:GetSuggestionsCyclingCallback' , [b: _copilot ]),
298
297
\ )
@@ -310,10 +309,10 @@ function! copilot#Previous() abort
310
309
endfunction
311
310
312
311
function ! copilot#GetDisplayedSuggestion ()abort
313
- let [text, outdent,delete ,uuid ]= s: SuggestionTextWithAdjustments ()
312
+ let [text, outdent,delete ,item ]= s: SuggestionTextWithAdjustments ()
314
313
315
314
return {
316
- \ ' uuid ' :uuid ,
315
+ \ ' item ' :item ,
317
316
\ ' text' : text,
318
317
\ ' outdentSize' : outdent,
319
318
\ ' deleteSize' :delete }
@@ -330,7 +329,7 @@ endfunction
330
329
331
330
function ! s: UpdatePreview ()abort
332
331
try
333
- let [text, outdent,delete ,uuid ]= s: SuggestionTextWithAdjustments ()
332
+ let [text, outdent,delete ,item ]= s: SuggestionTextWithAdjustments ()
334
333
let text= split (text," \n " ,1 )
335
334
if empty (text[-1 ])
336
335
call remove (text,-1 )
@@ -370,10 +369,7 @@ function! s:UpdatePreview() abort
370
369
call prop_add (line (' .' ),col (' $' ), {' type' :s: annot_hlgroup ,' text' :' ' . annot})
371
370
endif
372
371
endif
373
- if ! has_key (b: _copilot .shown_choices, uuid)
374
- let b: _copilot .shown_choices[uuid]= v: true
375
- call copilot#Request (' notifyShown' , {' uuid' : uuid})
376
- endif
372
+ call copilot#Notify (' textDocument/didShowCompletion' , {' item' : item})
377
373
catch
378
374
return copilot#logger#Exception ()
379
375
endtry
@@ -383,9 +379,8 @@ function! s:HandleTriggerResult(result) abort
383
379
if ! exists (' b:_copilot' )
384
380
return
385
381
endif
386
- let b: _copilot .suggestions= get (a: result ,' completions ' , [])
382
+ let b: _copilot .suggestions= type ( a: result ) == type ([]) ? a: result : get (empty ( a: result) ? {} : a: result ,' items ' , [])
387
383
let b: _copilot .choice= 0
388
- let b: _copilot .shown_choices= {}
389
384
call s: UpdatePreview ()
390
385
endfunction
391
386
@@ -446,8 +441,9 @@ function! copilot#OnBufEnter() abort
446
441
call copilot#util#Defer (function (' s:Focus' ),bufnr )
447
442
endfunction
448
443
449
- function ! copilot#OnInsertLeave ()abort
450
- return copilot#Clear ()
444
+ function ! copilot#OnInsertLeavePre ()abort
445
+ call copilot#Clear ()
446
+ call s: ClearPreview ()
451
447
endfunction
452
448
453
449
function ! copilot#OnInsertEnter ()abort
@@ -467,7 +463,6 @@ function! copilot#OnCursorMovedI() abort
467
463
endfunction
468
464
469
465
function ! copilot#OnBufUnload ()abort
470
- call s: Reject (+ expand (' <abuf>' ))
471
466
endfunction
472
467
473
468
function ! copilot#OnVimLeavePre ()abort
@@ -492,11 +487,14 @@ function! copilot#Accept(...) abort
492
487
if empty (text)
493
488
let text= s .text
494
489
endif
495
- let acceptance= {' uuid' :s .uuid}
496
- if text!=# s .text
497
- let acceptance.acceptedLength= copilot#doc#UTF16Width (text)
490
+ if text== #s .text&& has_key (s .item,' command' )
491
+ call copilot#Request (' workspace/executeCommand' ,s .item.command )
492
+ else
493
+ let line_text= strpart (getline (' .' ),0 ,col (' .' )- 1 ) . text
494
+ call copilot#Notify (' textDocument/didPartiallyAcceptCompletion' , {
495
+ \ ' item' :s .item,
496
+ \ ' acceptedLength' :copilot#util#UTF16Width (line_text)- s .item.range .start .character })
498
497
endif
499
- call copilot#Request (' notifyAccepted' , acceptance)
500
498
call s: ClearPreview ()
501
499
let s: suggestion_text= text
502
500
return repeat (" \<Left>\<Del> " ,s .outdentSize) .repeat (" \<Del> " ,s .deleteSize) .