-
Notifications
You must be signed in to change notification settings - Fork 734
Expand file tree
/
Copy pathpanel.vim
More file actions
173 lines (161 loc) · 5.91 KB
/
panel.vim
File metadata and controls
173 lines (161 loc) · 5.91 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
scriptencoding utf-8
if !exists('s:panel_id')
let s:panel_id = 0
endif
let s:separator = repeat('─', 72)
function! s:Render(state) abort
let bufnr = bufnr('^' . a:state.panel . '$')
let state = a:state
if !bufloaded(bufnr)
return
endif
let sorted = a:state.items
if !empty(get(a:state, 'error'))
let lines = ['Error: ' . a:state.error.message]
let sorted = []
elseif get(a:state, 'percentage') == 100
let lines = ['Synthesized ' . (len(sorted) == 1 ? '1 completion' : len(sorted) . ' completions')]
else
let lines = [substitute('Synthesizing ' . matchstr(get(a:state, 'message', ''), '\d\+\%(/\d\+\)\=') . ' completions', ' \+', ' ', 'g')]
endif
if len(sorted)
call add(lines, 'Press <CR> on a completion to accept')
endif
let leads = {}
for item in sorted
let insert = split(item.insertText, "\r\n\\=\\|\n", 1)
let insert[0] = strpart(a:state.line, 0, copilot#util#UTF16ToByteIdx(a:state.line, item.range.start.character)) . insert[0]
let lines += [s:separator] + insert
if !has_key(leads, string(item.range.start))
let match = insert[0 : a:state.position.line - item.range.start.line]
let match[-1] = strpart(match[-1], 0, copilot#util#UTF16ToByteIdx(match[-1], a:state.position.character))
call map(match, { k, v -> escape(v, '][^$.*\~') })
let leads[string(item.range.start)] = join(match, '\n')
endif
endfor
try
call setbufvar(bufnr, '&modifiable', 1)
call setbufvar(bufnr, '&readonly', 0)
call setbufline(bufnr, 1, lines)
finally
call setbufvar(bufnr, '&modifiable', 0)
endtry
call clearmatches()
call matchadd('CopilotSuggestion', '\C^' . s:separator . '\n\zs\%(' . join(sort(values(leads), { a, b -> len(b) - len(a) }), '\|') . '\)', 10, 4)
endfunction
function! s:PartialResult(state, value) abort
let items = type(a:value) == v:t_list ? a:value : a:value.items
call extend(a:state.items, items)
call s:Render(a:state)
endfunction
function! s:WorkDone(state, value) abort
if has_key(a:value, 'message')
let a:state.message = a:value.message
endif
if has_key(a:value, 'percentage')
let a:state.percentage = a:value.percentage
call s:Render(a:state)
endif
endfunction
function! copilot#panel#Accept(...) abort
let state = get(b:, 'copilot_panel', {})
if empty(state.items)
return ''
endif
if !has_key(state, 'bufnr') || !bufloaded(get(state, 'bufnr', -1))
return "echoerr 'Buffer was closed'"
endif
let at = a:0 ? a:1 : line('.')
let index = 0
for lnum in range(1, at)
if getline(lnum) ==# s:separator
let index += 1
endif
endfor
if index > 0 && index <= len(state.items)
let item = state.items[index - 1]
let lnum = item.range.start.line + 1
if getbufline(state.bufnr, lnum) !=# [state.line]
return 'echoerr "Buffer has changed since synthesizing completion"'
endif
let lines = split(item.insertText, '\r\n\=\|\n', 1)
let old_first = getbufline(state.bufnr, item.range.start.line + 1)[0]
let byte_offset_start = copilot#util#UTF16ToByteIdx(old_first, item.range.start.character)
let lines[0] = strpart(old_first, 0, byte_offset_start) . lines[0]
let old_last = getbufline(state.bufnr, item.range.end.line + 1)[0]
let byte_offset_end = copilot#util#UTF16ToByteIdx(old_last, item.range.end.character)
let lines[-1] .= strpart(old_last, byte_offset_end)
call deletebufline(state.bufnr, item.range.start.line + 1, item.range.end.line + 1)
call appendbufline(state.bufnr, item.range.start.line, lines)
call copilot#Request('workspace/executeCommand', item.command)
bwipeout
let win = bufwinnr(state.bufnr)
if win > 0
exe win . 'wincmd w'
exe item.range.start.line + len(lines)
if state.was_insert
startinsert!
else
normal! $
endif
endif
endif
return ''
endfunction
function! s:Initialize(state) abort
try
let &l:filetype = 'copilotpanel' . (empty(a:state.filetype) ? '' : '.' . a:state.filetype)
catch
let &l:filetype = 'copilotpanel'
endtry
let &l:tabstop = a:state.tabstop
nmap <buffer><script> <CR> <Cmd>exe copilot#panel#Accept()<CR>
nmap <buffer><script> [[ <Cmd>call search('^─\{9,}\n.', 'bWe')<CR>
nmap <buffer><script> ]] <Cmd>call search('^─\{9,}\n.', 'We')<CR>
endfunction
function! s:BufReadCmd() abort
setlocal bufhidden=wipe buftype=nofile nobuflisted nomodifiable
let state = get(b:, 'copilot_panel')
if type(state) != v:t_dict
return
endif
call s:Initialize(state)
call s:Render(state)
return ''
endfunction
function! s:Result(state, result) abort
let a:state.percentage = 100
call s:PartialResult(a:state, a:result)
endfunction
function! s:Error(state, error) abort
let a:state.error = a:error
call s:Render(a:state)
endfunction
function! copilot#panel#Open(opts) abort
let s:panel_id += 1
let state = {'items': [], 'filetype': &filetype, 'was_insert': mode() =~# '^[iR]', 'bufnr': bufnr(''), 'tabstop': &tabstop}
let state.panel = 'copilot:///panel/' . s:panel_id
if state.was_insert
let state.position = copilot#util#AppendPosition()
stopinsert
else
let state.position = {'line': a:opts.line1 >= 1 ? a:opts.line1 - 1 : 0, 'character': copilot#util#UTF16Width(getline('.'))}
endif
let state.line = getline(state.position.line + 1)
let params = {
\ 'textDocument': {'uri': state.bufnr},
\ 'position': state.position,
\ 'partialResultToken': function('s:PartialResult', [state]),
\ 'workDoneToken': function('s:WorkDone', [state]),
\ }
let response = copilot#Request('textDocument/copilotPanelCompletion', params, function('s:Result', [state]), function('s:Error', [state]))
exe substitute(a:opts.mods, '\C\<tab\>', '-tab', 'g') 'keepalt split' state.panel
let b:copilot_panel = state
call s:Initialize(state)
call s:Render(state)
return ''
endfunction
augroup github_copilot_panel
autocmd!
autocmd BufReadCmd copilot:///panel/* exe s:BufReadCmd()
augroup END