Skip to content

Commit 8ba151a

Browse files
committed
Copilot.vim 1.6.0
1 parent af9da64 commit 8ba151a

File tree

7 files changed

+98
-55
lines changed

7 files changed

+98
-55
lines changed

autoload/copilot.vim

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,16 @@ if s:has_vim_ghost_text && empty(prop_type_get(s:annot_hlgroup))
1919
call prop_type_add(s:annot_hlgroup, {'highlight': s:annot_hlgroup})
2020
endif
2121

22-
if len($XDG_CONFIG_HOME)
23-
let s:config_root = $XDG_CONFIG_HOME
24-
elseif has('win32')
25-
let s:config_root = expand('~/AppData/Local')
26-
else
27-
let s:config_root = expand('~/.config')
28-
endif
29-
let s:config_root .= '/github-copilot'
30-
if !isdirectory(s:config_root)
31-
call mkdir(s:config_root, 'p', 0700)
32-
endif
33-
34-
let s:config_hosts = s:config_root . '/hosts.json'
22+
function! s:EditorConfiguration() abort
23+
let filetypes = copy(s:filetype_defaults)
24+
if type(get(g:, 'copilot_filetypes')) == v:t_dict
25+
call extend(filetypes, g:copilot_filetypes)
26+
endif
27+
return {
28+
\ 'enableAutoCompletions': !empty(get(g:, 'copilot_enabled', 1)),
29+
\ 'disabledLanguages': sort(keys(filter(filetypes, { k, v -> empty(v) }))),
30+
\ }
31+
endfunction
3532

3633
function! s:StatusNotification(params, ...) abort
3734
let status = get(a:params, 'status', '')
@@ -46,15 +43,20 @@ function! copilot#Init(...) abort
4643
call timer_start(0, { _ -> s:Start() })
4744
endfunction
4845

46+
function! s:Running() abort
47+
return exists('s:agent.job') || exists('s:agent.client_id')
48+
endfunction
49+
4950
function! s:Start() abort
50-
if exists('s:agent.job') || exists('s:agent.client_id')
51+
if s:Running()
5152
return
5253
endif
5354
let s:agent = copilot#agent#New({'notifications': {
5455
\ 'statusNotification': function('s:StatusNotification'),
5556
\ 'PanelSolution': function('copilot#panel#Solution'),
5657
\ 'PanelSolutionsDone': function('copilot#panel#SolutionsDone'),
57-
\ }})
58+
\ },
59+
\ 'editorConfiguration' : s:EditorConfiguration()})
5860
endfunction
5961

6062
function! s:Stop() abort
@@ -84,20 +86,6 @@ function! copilot#Notify(method, params, ...) abort
8486
return call(agent.Notify, [a:method, a:params] + a:000)
8587
endfunction
8688

87-
function! s:ReadTerms() abort
88-
let file = s:config_root . '/terms.json'
89-
try
90-
if filereadable(file)
91-
let terms = json_decode(join(readfile(file)))
92-
if type(terms) == v:t_dict
93-
return terms
94-
endif
95-
endif
96-
catch
97-
endtry
98-
return {}
99-
endfunction
100-
10189
function! copilot#NvimNs() abort
10290
return nvim_create_namespace('github-copilot')
10391
endfunction
@@ -137,14 +125,14 @@ let s:filetype_defaults = {
137125

138126
function! s:BufferDisabled() abort
139127
if exists('b:copilot_disabled')
140-
return b:copilot_disabled ? 3 : 0
128+
return empty(b:copilot_disabled) ? 0 : 3
141129
endif
142130
if exists('b:copilot_enabled')
143-
return b:copilot_enabled ? 0 : 4
131+
return empty(b:copilot_enabled) ? 4 : 0
144132
endif
145133
let short = empty(&l:filetype) ? '.' : split(&l:filetype, '\.', 1)[0]
146134
let config = get(g:, 'copilot_filetypes', {})
147-
if has_key(config, &l:filetype)
135+
if type(config) == v:t_dict && has_key(config, &l:filetype)
148136
return empty(config[&l:filetype])
149137
elseif has_key(config, short)
150138
return empty(config[short])
@@ -372,6 +360,11 @@ function! s:UpdatePreview() abort
372360
let data.hl_mode = 'combine'
373361
call nvim_buf_set_extmark(0, copilot#NvimNs(), line('.')-1, col('.')-1, data)
374362
else
363+
let trail = strpart(getline('.'), col('.') - 1)
364+
while !empty(trail) && trail[-1] ==# text[0][-1]
365+
let trail = trail[:-2]
366+
let text[0] = text[0][:-2]
367+
endwhile
375368
call prop_add(line('.'), col('.'), {'type': s:hlgroup, 'text': text[0]})
376369
for line in text[1:]
377370
call prop_add(line('.'), 0, {'type': s:hlgroup, 'text_align': 'below', 'text': line})
@@ -398,21 +391,22 @@ function! s:HandleTriggerResult(result) abort
398391
call s:UpdatePreview()
399392
endfunction
400393

394+
function! copilot#Suggest() abort
395+
try
396+
call copilot#Complete(function('s:HandleTriggerResult'), function('s:HandleTriggerResult'))
397+
catch
398+
call copilot#logger#Exception()
399+
endtry
400+
return ''
401+
endfunction
402+
401403
function! s:Trigger(bufnr, timer) abort
402404
let timer = get(g:, '_copilot_timer', -1)
403405
unlet! g:_copilot_timer
404406
if a:bufnr !=# bufnr('') || a:timer isnot# timer || mode() !=# 'i'
405407
return
406408
endif
407-
if exists('s:auth_request')
408-
let g:_copilot_timer = timer_start(100, function('s:Trigger', [a:bufnr]))
409-
return
410-
endif
411-
try
412-
call copilot#Complete(function('s:HandleTriggerResult'), function('s:HandleTriggerResult'))
413-
catch
414-
call copilot#logger#Exception()
415-
endtry
409+
return copilot#Suggest()
416410
endfunction
417411

418412
function! copilot#IsMapped() abort
@@ -579,6 +573,12 @@ function! s:commands.status(opts) abort
579573
return
580574
endif
581575

576+
let status = copilot#Call('checkStatus', {})
577+
if status.status ==# 'NotAuthorized'
578+
echo 'Copilot: Not authorized'
579+
return
580+
endif
581+
582582
echo 'Copilot: Enabled and online'
583583
endfunction
584584

@@ -610,8 +610,10 @@ function! s:commands.setup(opts) abort
610610

611611
if has_key(data, 'verificationUri')
612612
let uri = data.verificationUri
613-
let @+ = data.userCode
614-
let @* = data.userCode
613+
if has('clipboard')
614+
let @+ = data.userCode
615+
let @* = data.userCode
616+
endif
615617
echo "First copy your one-time code: " . data.userCode
616618
try
617619
if len(&mouse)
@@ -677,6 +679,16 @@ function! s:commands.version(opts) abort
677679
endif
678680
endfunction
679681

682+
function! s:UpdateEditorConfiguration() abort
683+
try
684+
if s:Running()
685+
call copilot#Notify('notifyChangeConfiguration', {'settings': s:EditorConfiguration()})
686+
endif
687+
catch
688+
call copilot#logger#Exception()
689+
endtry
690+
endfunction
691+
680692
let s:feedback_url = 'https://github.com/github-community/community/discussions/categories/copilot'
681693
function! s:commands.feedback(opts) abort
682694
echo s:feedback_url
@@ -697,10 +709,12 @@ endfunction
697709

698710
function! s:commands.disable(opts) abort
699711
let g:copilot_enabled = 0
712+
call s:UpdateEditorConfiguration()
700713
endfunction
701714

702715
function! s:commands.enable(opts) abort
703716
let g:copilot_enabled = 1
717+
call s:UpdateEditorConfiguration()
704718
endfunction
705719

706720
function! s:commands.panel(opts) abort

autoload/copilot/agent.vim

Lines changed: 7 additions & 6 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.5.4'
8+
let s:plugin_version = '1.6.0'
99

1010
let s:error_exit = -1
1111

@@ -385,9 +385,9 @@ function! s:Command() abort
385385
return [v:null, node_version, 'Node.js version 12.x–17.x required but found ' . node_version]
386386
endif
387387
endif
388-
let agent = s:root . '/copilot/dist/agent.js'
389-
if !filereadable(agent)
390-
let agent = get(g:, 'copilot_agent_command', '')
388+
let agent = get(g:, 'copilot_agent_command', '')
389+
if empty(agent) || !filereadable(agent)
390+
let agent = s:root . '/copilot/dist/agent.js'
391391
if !filereadable(agent)
392392
return [v:null, node_version, 'Could not find agent.js (bad install?)']
393393
endif
@@ -417,7 +417,7 @@ function! copilot#agent#EditorInfo() abort
417417
endif
418418
let match = matchlist(proxy, '\C^\%([^:]\+://\)\=\%(\([^/:#]\+@\)\)\=\%(\([^/:#]\+\)\|\[\([[:xdigit:]:]\+\)\]\)\%(:\(\d\+\)\)\=\%(/\|$\)')
419419
if !empty(match)
420-
let info.networkProxy = {'host': match[2] . match[3], 'port': empty(match[4]) ? 80 : match[4]}
420+
let info.networkProxy = {'host': match[2] . match[3], 'port': empty(match[4]) ? 80 : +match[4]}
421421
if !empty(match[1])
422422
let info.networkProxy.username = s:UrlDecode(matchstr(match[1], '^[^:]*'))
423423
let info.networkProxy.password = s:UrlDecode(matchstr(match[1], ':\zs.*'))
@@ -428,7 +428,7 @@ endfunction
428428

429429
function! s:GetCapabilitiesResult(result, agent) abort
430430
let a:agent.capabilities = get(a:result, 'capabilities', {})
431-
call a:agent.Request('setEditorInfo', copilot#agent#EditorInfo())
431+
call a:agent.Request('setEditorInfo', extend({'editorConfiguration': a:agent.editorConfiguration}, copilot#agent#EditorInfo()))
432432
endfunction
433433

434434
function! s:GetCapabilitiesError(error, agent) abort
@@ -456,6 +456,7 @@ function! copilot#agent#New(...) abort
456456
let instance = {'requests': {},
457457
\ 'methods': get(opts, 'methods', {}),
458458
\ 'notifications': get(opts, 'notifications', {}),
459+
\ 'editorConfiguration': get(opts, 'editorConfiguration', {}),
459460
\ 'Close': function('s:AgentClose'),
460461
\ 'Notify': function('s:AgentNotify'),
461462
\ 'Request': function('s:AgentRequest'),

autoload/copilot/doc.vim

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@ function copilot#doc#UTF16Width(str) abort
1212
endfunction
1313

1414
let s:language_normalization_map = {
15+
\ "bash": "shellscript",
16+
\ "bst": "bibtex",
17+
\ "cs": "csharp",
18+
\ "cuda": "cuda-cpp",
19+
\ "dosbatch": "bat",
20+
\ "dosini": "ini",
21+
\ "gitcommit": "git-commit",
22+
\ "gitrebase": "git-rebase",
23+
\ "make": "makefile",
24+
\ "objc": "objective-c",
25+
\ "objcpp": "objective-cpp",
26+
\ "ps1": "powershell",
27+
\ "raku": "perl6",
28+
\ "sh": "shellscript",
1529
\ "text": "plaintext",
16-
\ "javascriptreact": "javascript",
17-
\ "jsx": "javascript",
18-
\ "typescriptreact": "typescript",
1930
\ }
20-
2131
function copilot#doc#LanguageForFileType(filetype) abort
2232
let filetype = substitute(a:filetype, '\..*', '', '')
2333
return get(s:language_normalization_map, empty(filetype) ? "text" : filetype, filetype)

copilot/dist/agent.js

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

copilot/dist/agent.js.LICENSE.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/*!
2+
* buildToken
3+
* Builds OAuth token prefix (helper function)
4+
*
5+
* @name buildToken
6+
* @function
7+
* @param {GitUrl} obj The parsed Git url object.
8+
* @return {String} token prefix
9+
*/
10+
111
/** @license URI.js v4.4.1 (c) 2011 Gary Court. License: http://github.com/garycourt/uri-js */
212

313
/** @preserve

doc/copilot.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ Other Maps ~
113113
<M-[> Cycle to the previous suggestion.
114114
<Plug>(copilot-previous)
115115

116+
*copilot-i_ALT-\*
117+
<M-\> Explicitly request a suggestion, even if Copilot
118+
<Plug>(copilot-suggest) is disabled.
119+
116120
SYNTAX HIGHLIGHTING *copilot-highlighting*
117121

118122
Inline suggestions are highlighted using the CopilotSuggestion group,

plugin/copilot.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ if !get(g:, 'copilot_no_maps')
7272
endif
7373
imap <Plug>(copilot-next) <Cmd>call copilot#Next()<CR>
7474
imap <Plug>(copilot-previous) <Cmd>call copilot#Previous()<CR>
75+
imap <Plug>(copilot-suggest) <Cmd>call copilot#Suggest()<CR>
7576
if empty(mapcheck('<M-]>', 'i'))
7677
imap <M-]> <Plug>(copilot-next)
7778
endif
7879
if empty(mapcheck('<M-[>', 'i'))
7980
imap <M-[> <Plug>(copilot-previous)
8081
endif
82+
if empty(mapcheck('<M-Bslash>', 'i'))
83+
imap <M-Bslash> <Plug>(copilot-suggest)
84+
endif
8185
endif
8286

8387
call copilot#Init()

0 commit comments

Comments
 (0)