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
2121 let lines = [' Error: ' . state .status]
2222 else
2323 let 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)' ]
2626 endif
2727 if len (sorted)
2828 call add (lines , ' Press <CR> on a solution to accept' )
@@ -44,7 +44,11 @@ function! copilot#panel#Solution(params, ...) abort
4444 if ! bufloaded (a: params .panelId) || type (state ) != v: t_dict
4545 return
4646 endif
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
4852 call s: Render (a: params .panelId)
4953endfunction
5054
@@ -78,12 +82,12 @@ function! copilot#panel#Accept(...) abort
7882 let solution = solutions[solution_index - 1 ]
7983 let lnum = solution.range .start .line + 1
8084 if getbufline (state .bufnr , lnum) !=# [state .line ]
81- return ' echoerr "Buffer has changed since synthesizing solution "'
85+ return ' echoerr "Buffer has changed since synthesizing completion "'
8286 endif
8387 let 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 ]
8589 let 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 ]
8791 let lines [-1 ] .= strpart (old_last, copilot#doc#UTF16ToByteIdx (old_last, solution.range .start .character ))
8892 call setbufline (state .bufnr , solution.range .start .line + 1 , lines [0 ])
8993 call appendbufline (state .bufnr , solution.range .start .line + 1 , lines [1 :-1 ])
@@ -126,16 +130,18 @@ endfunction
126130
127131function ! copilot#panel#Open (opts) abort
128132 let 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)
130135 let bufname = ' copilot:///panel/' . s: panel_id
131136 let params = copilot#doc#Params ({' panelId' : bufname })
132- let state .was_insert = mode () = ~# ' ^[iR]'
133137 if state .was_insert
134138 stopinsert
135139 else
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 )
138142 endif
143+ let params.doc.position = params.position
144+ let state .count_received = 0
139145 let response = copilot#Request (' getPanelCompletions' , params).Wait ()
140146 if response.status == # ' error'
141147 return ' echoerr ' . string (response.error .message)
0 commit comments