77let s: separator= repeat (' ─' , 72 )
88
99function ! s: Solutions (state ) abort
10- return sort (values (get (a: state , ' solutions' ,{})),{a , b - > a .score< b .score })
10+ return sort (values (get (a: state , ' solutions' ,{})),{a , b - > a .score != b .score ? b .score - a .score : a ._index - b ._index })
1111endfunction
1212
1313function ! s: Render (panel_id) abort
@@ -21,8 +21,8 @@ function! s:Render(panel_id) abort
2121let lines = [' Error: ' . state .status]
2222else
2323let target = get (state , ' count_target' , ' ?' )
24- let received = has_key ( state , ' status ' ) ? target : len (sorted)
25- let lines = [' Synthesiz' . (has_key (state , ' status' ) ? ' ed ' : ' ing ' ) . received . ' /' . target . ' solutions (Duplicates hidden)' ]
24+ let received = state .count_received
25+ let lines = [' Synthesiz' . (has_key (state , ' status' ) ? ' ed ' : ' ing ' ) . received . ' /' . target . ' completions (Duplicates hidden)' ]
2626endif
2727if len (sorted)
2828call add (lines , ' Press <CR> on a solution to accept' )
@@ -44,7 +44,11 @@ function! copilot#panel#Solution(params, ...) abort
4444if ! bufloaded (a: params .panelId) || type (state ) != v: t_dict
4545return
4646endif
47- let state .solutions[a: params .solutionId] = a: params
47+ let state .count_received += 1
48+ if ! has_key (state .solutions, a: params .solutionId) || state .solutions[a: params .solutionId].score < a: params .score
49+ let a: params ._index = state .count_received
50+ let state .solutions[a: params .solutionId] = a: params
51+ endif
4852call s: Render (a: params .panelId)
4953endfunction
5054
@@ -78,12 +82,12 @@ function! copilot#panel#Accept(...) abort
7882let solution = solutions[solution_index - 1 ]
7983let lnum = solution.range .start .line + 1
8084if getbufline (state .bufnr , lnum) !=# [state .line ]
81- return ' echoerr "Buffer has changed since synthesizing solution "'
85+ return ' echoerr "Buffer has changed since synthesizing completion "'
8286endif
8387let lines = split (solution.completionText, " \n " , 1 )
84- let old_first = getline ( solution.range .start .line + 1 )
88+ let old_first = getbufline ( state . bufnr , solution.range .start .line + 1 )[ 0 ]
8589let lines [0 ] = strpart (old_first, 0 , copilot#doc#UTF16ToByteIdx (old_first, solution.range .start .character )) . lines [0 ]
86- let old_last = getline ( solution.range .end .line + 1 )
90+ let old_last = getbufline ( state . bufnr , solution.range .end .line + 1 )[ 0 ]
8791let lines [-1 ] .= strpart (old_last, copilot#doc#UTF16ToByteIdx (old_last, solution.range .start .character ))
8892call setbufline (state .bufnr , solution.range .start .line + 1 , lines [0 ])
8993call appendbufline (state .bufnr , solution.range .start .line + 1 , lines [1 :-1 ])
@@ -126,16 +130,18 @@ endfunction
126130
127131function ! copilot#panel#Open (opts) abort
128132let s: panel_id+= 1
129- let state = {' solutions' :{}, ' filetype' : &filetype , ' line' : getline (' .' ), ' bufnr' : bufnr (' ' ), ' tabstop' : &tabstop }
133+ let state = {' solutions' :{}, ' filetype' : &filetype , ' was_insert' : mode () = ~# ' ^[iR]' , ' bufnr' : bufnr (' ' ), ' tabstop' : &tabstop }
134+ let state .line = getline (state .was_insert ? ' .' : a: opts .line1)
130135let bufname = ' copilot:///panel/' . s: panel_id
131136let params = copilot#doc#Params ({' panelId' : bufname })
132- let state .was_insert = mode () = ~# ' ^[iR]'
133137if state .was_insert
134138stopinsert
135139else
136- let params.doc. position.character = copilot#doc#UTF16Width ( state . line )
137- let params.position.character = params. doc.position. character
140+ let params.position.line = a: opts .line1 > 0 ? a: opts .line1 - 1 : 0
141+ let params.position.character = copilot# doc#UTF16Width ( state . line )
138142endif
143+ let params.doc.position = params.position
144+ let state .count_received = 0
139145let response = copilot#Request (' getPanelCompletions' , params).Wait ()
140146if response.status == # ' error'
141147return ' echoerr ' . string (response.error .message)
0 commit comments