Skip to content

Commit 2f32e1a

Browse files
committed
Copilot.vim 1.20.0
1 parent 1a28401 commit 2f32e1a

File tree

8 files changed

+390
-451
lines changed

8 files changed

+390
-451
lines changed

autoload/copilot.vim

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ function! s:Start() abort
5959
\ 'statusNotification': function('s:StatusNotification'),
6060
\ 'PanelSolution': function('copilot#panel#Solution'),
6161
\ 'PanelSolutionsDone': function('copilot#panel#SolutionsDone'),
62-
\ 'copilot/openURL': function('s:OpenURL'),
6362
\ },
6463
\ 'editorConfiguration' : s:EditorConfiguration()})
6564
endfunction
@@ -518,21 +517,6 @@ function! copilot#Browser() abort
518517
endif
519518
endfunction
520519

521-
function! s:OpenURL(params) abort
522-
echo a:params.target
523-
let browser = copilot#Browser()
524-
if empty(browser)
525-
return v:false
526-
endif
527-
let status = {}
528-
call copilot#job#Stream(browser + [a:params.target], v:null, v:null, function('s:BrowserCallback', [status]))
529-
let time = reltime()
530-
while empty(status) && reltimefloat(reltime(time)) < 1
531-
sleep 10m
532-
endwhile
533-
return get(status, 'code') ? v:false : v:true
534-
endfunction
535-
536520
let s:commands = {}
537521

538522
function! s:EnabledStatusMessage() abort
@@ -705,12 +689,20 @@ function! s:commands.version(opts) abort
705689
let editorInfo = copilot#agent#EditorInfo()
706690
echo editorInfo.name . ' ' . editorInfo.version
707691
if s:Running()
708-
let versions = s:agent.Call('getVersion', {})
709-
echo 'dist/agent.js ' . versions.version
710-
echo 'Node.js ' . get(s:agent, 'node_version', substitute(get(versions, 'runtimeVersion', '?'), '^node/', '', 'g'))
692+
let versions = s:agent.Request('getVersion', {})
693+
if exists('s:agent.serverInfo.version')
694+
echo s:agent.serverInfo.name . ' ' . s:agent.serverInfo.version
695+
else
696+
echo 'dist/agent.js ' . versions.Await().version
697+
endif
698+
if exists('s:agent.node_version')
699+
echo 'Node.js ' . s:agent.node_version
700+
else
701+
echo 'Node.js ' . substitute(get(versions.Await(), 'runtimeVersion', '?'), '^node/', '', 'g')
702+
endif
711703
call s:NodeVersionWarning()
712704
else
713-
echo 'dist/agent.js not running'
705+
echo 'Not running'
714706
endif
715707
endfunction
716708

autoload/copilot/agent.vim

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,6 @@ function! s:BufferText(bufnr) abort
145145
return join(getbufline(a:bufnr, 1, '$'), "\n") . "\n"
146146
endfunction
147147

148-
function! s:LogMessage(params) abort
149-
call copilot#logger#Raw(get(a:params, 'type', 6), get(a:params, 'message', ''))
150-
endfunction
151-
152-
function! s:ShowMessageRequest(params) abort
153-
let choice = inputlist([a:params.message . "\n\nRequest Actions:"] +
154-
\ map(copy(get(a:params, 'actions', [])), { i, v -> (i + 1) . '. ' . v.title}))
155-
return choice > 0 ? get(a:params.actions, choice - 1, v:null) : v:null
156-
endfunction
157-
158148
function! s:SendRequest(agent, request) abort
159149
if empty(s:Send(a:agent, a:request)) && has_key(a:agent.requests, a:request.id)
160150
call s:RejectRequest(remove(a:agent.requests, a:request.id), {'code': 257, 'message': 'Write failed'})
@@ -198,7 +188,11 @@ function! s:AgentRequest(method, params, ...) dict abort
198188
endif
199189
let doc.version = doc_version
200190
endfor
201-
call timer_start(0, { _ -> s:SendRequest(self, request) })
191+
if has_key(self, 'initialization_pending')
192+
call add(self.initialization_pending, request)
193+
else
194+
call timer_start(0, { _ -> s:SendRequest(self, request) })
195+
endif
202196
return call('s:SetUpRequest', [self, s:id, a:method, a:params] + a:000)
203197
endfunction
204198

@@ -255,8 +249,10 @@ function! s:OnMessage(agent, body, ...) abort
255249
let params = get(request, 'params', v:null)
256250
if has_key(a:agent.methods, request.method)
257251
return s:DispatchMessage(a:agent, request.method, a:agent.methods[request.method], id, params)
258-
elseif !empty(id)
252+
elseif id isnot# v:null
259253
call s:Send(a:agent, {"id": id, "error": {"code": -32700, "message": "Method not found: " . request.method}})
254+
elseif request.method !~# '^\$/'
255+
call copilot#logger#Warn('Unexpected notification ' . request.method . ' called with ' . json_encode(params))
260256
endif
261257
endfunction
262258

@@ -289,7 +285,7 @@ function! s:OnResponse(agent, response, ...) abort
289285
endfunction
290286

291287
function! s:OnErr(agent, line, ...) abort
292-
if !has_key(a:agent, 'capabilities')
288+
if !has_key(a:agent, 'serverInfo')
293289
call copilot#logger#Bare('<-! ' . a:line)
294290
endif
295291
endfunction
@@ -467,12 +463,22 @@ function! copilot#agent#Settings() abort
467463
endfunction
468464

469465
function! s:InitializeResult(result, agent) abort
470-
let a:agent.capabilities = get(a:result, 'capabilities', {})
466+
let a:agent.serverInfo = get(a:result, 'serverInfo', {})
467+
if !has_key(a:agent, 'node_version') && has_key(a:result.serverInfo, 'nodeVersion')
468+
let a:agent.node_version = a:result.serverInfo.nodeVersion
469+
endif
471470
let info = {
472471
\ 'editorInfo': copilot#agent#EditorInfo(),
473472
\ 'editorPluginInfo': copilot#agent#EditorPluginInfo(),
474473
\ 'editorConfiguration': extend(copilot#agent#Settings(), a:agent.editorConfiguration)}
474+
let pending = get(a:agent, 'initialization_pending', [])
475+
if has_key(a:agent, 'initialization_pending')
476+
call remove(a:agent, 'initialization_pending')
477+
endif
475478
call a:agent.Request('setEditorInfo', info)
479+
for request in pending
480+
call timer_start(0, { _ -> s:SendRequest(a:agent, request) })
481+
endfor
476482
endfunction
477483

478484
function! s:InitializeError(error, agent) abort
@@ -485,16 +491,26 @@ function! s:InitializeError(error, agent) abort
485491
endfunction
486492

487493
function! s:AgentStartupError() dict abort
488-
while (has_key(self, 'job') || has_key(self, 'client_id')) && !has_key(self, 'startup_error') && !has_key(self, 'capabilities')
494+
while (has_key(self, 'job') || has_key(self, 'client_id')) && !has_key(self, 'startup_error') && !has_key(self, 'serverInfo')
489495
sleep 10m
490496
endwhile
491-
if has_key(self, 'capabilities')
497+
if has_key(self, 'serverInfo')
492498
return ''
493499
else
494500
return get(self, 'startup_error', 'Something unexpected went wrong spawning the agent')
495501
endif
496502
endfunction
497503

504+
let s:vim_handlers = {
505+
\ 'window/showMessageRequest': function('copilot#handlers#window_showMessageRequest'),
506+
\ 'window/showDocument': function('copilot#handlers#window_showDocument'),
507+
\ }
508+
509+
let s:vim_capabilities = {
510+
\ 'workspace': {'workspaceFolders': v:true},
511+
\ 'window': {'showDocument': {'support': v:true}},
512+
\ }
513+
498514
function! copilot#agent#New(...) abort
499515
let opts = a:0 ? a:1 : {}
500516
let instance = {'requests': {},
@@ -507,7 +523,7 @@ function! copilot#agent#New(...) abort
507523
\ 'StartupError': function('s:AgentStartupError'),
508524
\ }
509525
let instance.methods = extend({
510-
\ 'window/logMessage': function('s:LogMessage'),
526+
\ 'window/logMessage': function('copilot#handlers#window_logMessage'),
511527
\ }, get(opts, 'methods', {}))
512528
let [command, node_version, command_error] = s:Command()
513529
if len(command_error)
@@ -522,23 +538,18 @@ function! copilot#agent#New(...) abort
522538
if !empty(node_version)
523539
let instance.node_version = node_version
524540
endif
525-
let initializationOptions = {'copilotCapabilities': {}}
526-
for name in keys(instance.methods)
527-
if name =~# '^copilot/'
528-
let initializationOptions.copilotCapabilities[matchstr(name, '/\zs.*')] = v:true
529-
endif
530-
endfor
541+
let opts = {'initializationOptions': {}}
531542
if has('nvim')
532543
call extend(instance, {
533544
\ 'Close': function('s:LspClose'),
534545
\ 'Notify': function('s:LspNotify'),
535546
\ 'Request': function('s:LspRequest')})
536-
let instance.client_id = eval("v:lua.require'_copilot'.lsp_start_client(command, keys(instance.methods), initializationOptions)")
547+
let instance.client_id = eval("v:lua.require'_copilot'.lsp_start_client(command, keys(instance.methods), opts)")
537548
let instance.id = instance.client_id
538549
else
539550
let state = {'headers': {}, 'mode': 'headers', 'buffer': ''}
540551
let instance.open_buffers = {}
541-
let instance.methods = extend({'window/showMessageRequest': function('s:ShowMessageRequest')}, instance.methods)
552+
let instance.methods = extend(s:vim_handlers, instance.methods)
542553
let instance.job = job_start(command, {
543554
\ 'cwd': copilot#job#Cwd(),
544555
\ 'in_mode': 'lsp',
@@ -548,12 +559,10 @@ function! copilot#agent#New(...) abort
548559
\ 'exit_cb': { j, d -> timer_start(0, function('s:OnExit', [instance, d])) },
549560
\ })
550561
let instance.id = job_info(instance.job).process
551-
let opts = {
552-
\ 'capabilities': {'workspace': {'workspaceFolders': v:true}},
553-
\ 'initializationOptions': initializationOptions,
554-
\ 'processId': getpid(),
555-
\ }
562+
let opts.capabilities = s:vim_capabilities
563+
let opts.processId = getpid()
556564
let request = instance.Request('initialize', opts, function('s:InitializeResult'), function('s:InitializeError'), instance)
565+
let instance.initialization_pending = []
557566
endif
558567
let s:instances[instance.id] = instance
559568
return instance

autoload/copilot/handlers.vim

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function! copilot#handlers#window_logMessage(params) abort
2+
call copilot#logger#Raw(get(a:params, 'type', 6), get(a:params, 'message', ''))
3+
endfunction
4+
5+
function! copilot#handlers#window_showMessageRequest(params) abort
6+
let choice = inputlist([a:params.message . "\n\nRequest Actions:"] +
7+
\ map(copy(get(a:params, 'actions', [])), { i, v -> (i + 1) . '. ' . v.title}))
8+
return choice > 0 ? get(a:params.actions, choice - 1, v:null) : v:null
9+
endfunction
10+
11+
function! s:BrowserCallback(into, code) abort
12+
let a:into.code = a:code
13+
endfunction
14+
15+
function! copilot#handlers#window_showDocument(params) abort
16+
echo a:params.uri
17+
if empty(get(a:params, 'external'))
18+
return {'success': v:false}
19+
endif
20+
let browser = copilot#Browser()
21+
if empty(browser)
22+
return {'success': v:false}
23+
endif
24+
let status = {}
25+
call copilot#job#Stream(browser + [a:params.uri], v:null, v:null, function('s:BrowserCallback', [status]))
26+
let time = reltime()
27+
while empty(status) && reltimefloat(reltime(time)) < 1
28+
sleep 10m
29+
endwhile
30+
return {'success': get(status, 'code') ? v:false : v:true}
31+
endfunction

autoload/copilot/version.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
function! copilot#version#String() abort
2-
return '1.19.2'
2+
return '1.20.0'
33
endfunction

dist/agent.js

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

dist/agent.js.map

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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
local copilot = {}
22

3-
copilot.lsp_start_client = function(cmd, handler_names, init_options)
4-
local handlers = {}
3+
local showDocument = function(err, result, ctx, _)
4+
if result.external and vim.g.copilot_browser then
5+
return vim.fn['copilot#handlers#window_showDocument'](result.uri)
6+
else
7+
return vim.lsp.handlers['window/showDocument'](err, result, ctx, _)
8+
end
9+
end
10+
11+
copilot.lsp_start_client = function(cmd, handler_names, opts)
12+
local handlers = {['window/showDocument'] = showDocument}
513
local id
614
for _, name in ipairs(handler_names) do
715
handlers[name] = function(err, result)
@@ -17,7 +25,7 @@ copilot.lsp_start_client = function(cmd, handler_names, init_options)
1725
cmd = cmd,
1826
cmd_cwd = vim.call('copilot#job#Cwd'),
1927
name = 'copilot',
20-
init_options = init_options,
28+
init_options = opts.initializationOptions,
2129
handlers = handlers,
2230
get_language_id = function(bufnr, filetype)
2331
return vim.call('copilot#doc#LanguageForFileType', filetype)

plugin/copilot.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ if !get(g:, 'copilot_no_maps')
9696
imap <M-Right> <Plug>(copilot-accept-word)
9797
endif
9898
if empty(mapcheck('<M-C-Right>', 'i'))
99-
imap <M-Down> <Plug>(copilot-accept-line)
99+
imap <M-C-Right> <Plug>(copilot-accept-line)
100100
endif
101101
finally
102102
if exists('s:restore_encoding')

0 commit comments

Comments
 (0)