Skip to content

Commit 8c84164

Browse files
committed
Copilot.vim 1.8.1
1 parent 324ec9e commit 8c84164

File tree

5 files changed

+104
-37
lines changed

5 files changed

+104
-37
lines changed

autoload/copilot.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ function! s:EditorConfiguration() abort
2525
call extend(filetypes, g:copilot_filetypes)
2626
endif
2727
return {
28-
\ 'enableAutoCompletions': !empty(get(g:, 'copilot_enabled', 1)),
29-
\ 'disabledLanguages': sort(keys(filter(filetypes, { k, v -> empty(v) }))),
28+
\ 'enableAutoCompletions': empty(get(g:, 'copilot_enabled', 1)) ? v:false : v:true,
29+
\ 'disabledLanguages': sort(keys(filter(filetypes, { k, v -> empty(v) ? v:true : v:false }))),
3030
\ }
3131
endfunction
3232

autoload/copilot/agent.vim

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let g:autoloaded_copilot_agent = 1
55

66
scriptencoding utf-8
77

8-
let s:plugin_version = '1.8.0'
8+
let s:plugin_version = '1.8.1'
99

1010
let s:error_exit = -1
1111

@@ -117,10 +117,69 @@ function! s:SetUpRequest(agent, id, method, params, ...) abort
117117
return request
118118
endfunction
119119

120+
function! s:UrlEncode(str) abort
121+
return substitute(iconv(a:str, 'latin1', 'utf-8'),'[^A-Za-z0-9._~!$&''()*+,;=:@/-]','\="%".printf("%02X",char2nr(submatch(0)))','g')
122+
endfunction
123+
124+
let s:slash = exists('+shellslash') ? '\' : '/'
125+
function! s:UriFromBufnr(bufnr) abort
126+
let absolute = tr(bufname(a:bufnr), s:slash, '/')
127+
if absolute !~# '^\a\+:\|^/\|^$' && getbufvar(a:bufnr, 'buftype') =~# '^\%(nowrite\)\=$'
128+
let absolute = substitute(tr(getcwd(), s:slash, '/'), '/\=$', '/', '') . absolute
129+
endif
130+
if has('win32') && absolute =~# '^\a://\@!'
131+
return 'file:///' . strpart(absolute, 0, 2) . s:UrlEncode(strpart(absolute, 2))
132+
elseif absolute =~# '^/'
133+
return 'file://' . s:UrlEncode(absolute)
134+
elseif absolute =~# '^\a[[:alnum:].+-]*:\|^$'
135+
return absolute
136+
else
137+
return ''
138+
endif
139+
endfunction
140+
141+
function! s:BufferText(bufnr) abort
142+
return join(getbufline(a:bufnr, 1, '$'), "\n") . "\n"
143+
endfunction
144+
120145
function! s:AgentRequest(method, params, ...) dict abort
121146
let s:id += 1
122-
let request = {'method': a:method, 'params': a:params, 'id': s:id}
123-
call s:Send(self, request)
147+
let request = {'method': a:method, 'params': deepcopy(a:params), 'id': s:id}
148+
for doc in filter([get(request.params, 'doc', {}), get(request.params, 'textDocument',{})], 'type(get(v:val, "uri", "")) == v:t_number')
149+
let bufnr = doc.uri
150+
let doc.uri = s:UriFromBufnr(doc.uri)
151+
let uri = doc.uri
152+
let languageId = copilot#doc#LanguageForFileType(getbufvar(bufnr, '&filetype'))
153+
let doc_version = getbufvar(bufnr, 'changedtick')
154+
if has_key(self.open_buffers, bufnr) && (
155+
\ self.open_buffers[bufnr].uri !=# doc.uri ||
156+
\ self.open_buffers[bufnr].languageId !=# languageId)
157+
call remove(self.open_buffers, bufnr)
158+
sleep 1m
159+
endif
160+
if !has_key(self.open_buffers, bufnr)
161+
let td_item = {
162+
\ 'uri': doc.uri,
163+
\ 'version': doc_version,
164+
\ 'languageId': languageId,
165+
\ 'text': s:BufferText(bufnr)}
166+
call self.Notify('textDocument/didOpen', {'textDocument': td_item})
167+
let self.open_buffers[bufnr] = {
168+
\ 'uri': doc.uri,
169+
\ 'version': doc_version,
170+
\ 'languageId': languageId}
171+
else
172+
let vtd_id = {
173+
\ 'uri': doc.uri,
174+
\ 'version': getbufvar(bufnr, 'changedtick')}
175+
call self.Notify('textDocument/didChange', {
176+
\ 'textDocument': vtd_id,
177+
\ 'contentChanges': [{'text': s:BufferText(bufnr)}]})
178+
let self.open_buffers[bufnr].version = version
179+
endif
180+
let doc.version = doc_version
181+
endfor
182+
call timer_start(0, { _ -> s:Send(self, request) })
124183
return call('s:SetUpRequest', [self, s:id, a:method, a:params] + a:000)
125184
endfunction
126185

@@ -468,6 +527,7 @@ function! copilot#agent#New(...) abort
468527
let instance.id = instance.client_id
469528
else
470529
let state = {'headers': {}, 'mode': 'headers', 'buffer': ''}
530+
let instance.open_buffers = {}
471531
let instance.job = copilot#job#Stream(command,
472532
\ function('s:OnOut', [instance, state]),
473533
\ function('s:OnErr', [instance]),
@@ -507,3 +567,23 @@ function! copilot#agent#Error(request, callback) abort
507567
let a:request.waiting[timer_start(0, function('s:Callback', [a:request, 'error', a:callback]))] = 1
508568
endif
509569
endfunction
570+
571+
function! s:CloseBuffer(bufnr) abort
572+
for instance in values(s:instances)
573+
try
574+
if has_key(instance, 'job') && has_key(instance.open_buffers, a:bufnr)
575+
let buffer = remove(instance.open_buffers, a:bufnr)
576+
call instance.Notify('textDocument/didClose', {'textDocument': {'uri': buffer.uri}})
577+
endif
578+
catch
579+
call copilot#logger#Exception()
580+
endtry
581+
endfor
582+
endfunction
583+
584+
augroup copilot_agent
585+
autocmd!
586+
if !has('nvim')
587+
autocmd BufUnload * call s:CloseBuffer(+expand('<abuf>'))
588+
endif
589+
augroup END

autoload/copilot/doc.vim

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,14 @@ function! s:RelativePath(absolute) abort
5151
endif
5252
endfunction
5353

54-
function! s:UrlEncode(str) abort
55-
return substitute(iconv(a:str, 'latin1', 'utf-8'),'[^A-Za-z0-9._~!$&''()*+,;=:@/-]','\="%".printf("%02X",char2nr(submatch(0)))','g')
56-
endfunction
57-
5854
function! copilot#doc#Get() abort
5955
let absolute = tr(@%, s:slash, '/')
6056
if absolute !~# '^\a\+:\|^/\|^$' && &buftype =~# '^\%(nowrite\)\=$'
6157
let absolute = substitute(tr(getcwd(), s:slash, '/'), '/\=$', '/', '') . absolute
6258
endif
63-
if has('win32') && absolute =~# '^\a://\@!'
64-
let uri = 'file:///' . strpart(absolute, 0, 2) . s:UrlEncode(strpart(absolute, 2))
65-
elseif absolute =~# '^/'
66-
let uri = 'file://' . s:UrlEncode(absolute)
67-
elseif absolute =~# '^\a[[:alnum:].+-]*:\|^$'
68-
let uri = absolute
69-
else
70-
let uri = ''
71-
endif
7259
let doc = {
73-
\ 'languageId': copilot#doc#LanguageForFileType(&filetype),
74-
\ 'path': absolute,
75-
\ 'uri': uri,
60+
\ 'uri': bufnr(''),
61+
\ 'version': getbufvar('', 'changedtick'),
7662
\ 'relativePath': s:RelativePath(absolute),
7763
\ 'insertSpaces': &expandtab ? v:true : v:false,
7864
\ 'tabSize': shiftwidth(),
@@ -82,13 +68,6 @@ function! copilot#doc#Get() abort
8268
let col_byte = col('.') - (mode() =~# '^[iR]' || empty(line))
8369
let col_utf16 = copilot#doc#UTF16Width(strpart(line, 0, col_byte))
8470
let doc.position = {'line': line('.') - 1, 'character': col_utf16}
85-
if !has('nvim')
86-
let lines = getline(1, '$')
87-
if &eol
88-
call add(lines, "")
89-
endif
90-
let doc.source = join(lines, "\n")
91-
endif
9271
return doc
9372
endfunction
9473

@@ -97,7 +76,7 @@ function! copilot#doc#Params(...) abort
9776
let params = extend({'doc': extend(copilot#doc#Get(), get(extra, 'doc', {}))}, extra, 'keep')
9877
let params.textDocument = {
9978
\ 'uri': params.doc.uri,
100-
\ 'languageId': params.doc.languageId,
79+
\ 'version': params.doc.version,
10180
\ 'relativePath': params.doc.relativePath,
10281
\ }
10382
let params.position = params.doc.position

copilot/dist/agent.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lua/_copilot.lua

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,23 @@ copilot.lsp_start_client = function(cmd, handler_names)
2929
return id
3030
end
3131

32-
copilot.lsp_request = function(client_id, method, params, bufnr)
32+
copilot.lsp_request = function(client_id, method, params)
3333
local client = vim.lsp.get_client_by_id(client_id)
3434
if not client then return end
35-
bufnr = bufnr or 0
36-
vim.lsp.buf_attach_client(bufnr, client_id)
35+
vim.lsp.buf_attach_client(0, client_id)
36+
local bufnr
37+
for _, doc in ipairs({params.doc, params.textDocument}) do
38+
if doc and type(doc.uri) == 'number' then
39+
bufnr = doc.uri
40+
vim.lsp.buf_attach_client(bufnr, client_id)
41+
doc.uri = vim.uri_from_bufnr(bufnr)
42+
doc.version = vim.lsp.util.buf_versions[bufnr]
43+
end
44+
end
3745
local _, id
3846
_, id = client.request(method, params, function(err, result)
39-
vim.call('copilot#agent#LspResponse', client_id, {id = id, error = err, result = result}, bufnr)
40-
end)
47+
vim.call('copilot#agent#LspResponse', client_id, {id = id, error = err, result = result})
48+
end, bufnr)
4149
return id
4250
end
4351

0 commit comments

Comments
 (0)