forked from github/copilot.vim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot.vim
More file actions
78 lines (68 loc) · 2.5 KB
/
copilot.vim
File metadata and controls
78 lines (68 loc) · 2.5 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
if exists('g:loaded_copilot')
finish
endif
let g:loaded_copilot = 1
scriptencoding utf-8
command! -bang -nargs=? -range=-1 -complete=customlist,copilot#CommandComplete Copilot exe copilot#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>)
if v:version < 800
finish
endif
function! s:ColorScheme() abort
if &t_Co == 256
hi def CopilotSuggestion guifg=#808080 ctermfg=244
else
hi def CopilotSuggestion guifg=#808080 ctermfg=8
endif
endfunction
function! s:MapTab() abort
if get(g:, 'copilot_no_tab_map') || get(g:, 'copilot_no_maps')
return
endif
let tab_map = maparg('<Tab>', 'i', 0, 1)
if empty(tab_map)
imap <script><silent><nowait><expr> <Tab> copilot#Accept()
elseif tab_map.rhs !~# 'copilot'
if tab_map.expr
let tab_fallback = '{ -> ' . tab_map.rhs . ' }'
else
let tab_fallback = substitute(json_encode(tab_map.rhs), '<', '\\<', 'g')
endif
let tab_fallback = substitute(tab_fallback, '<SID>', '<SNR>' . get(tab_map, 'sid') . '_', 'g')
if get(tab_map, 'noremap') || get(tab_map, 'script') || mapcheck('<Left>', 'i') || mapcheck('<Del>', 'i')
exe 'imap <script><silent><nowait><expr> <Tab> copilot#Accept(' . tab_fallback . ')'
else
exe 'imap <silent><nowait><expr> <Tab> copilot#Accept(' . tab_fallback . ')'
endif
endif
endfunction
function! s:Event(type) abort
try
call call('copilot#On' . a:type, [])
catch
call copilot#logger#Exception()
endtry
endfunction
augroup github_copilot
autocmd!
autocmd InsertLeave * call s:Event('InsertLeave')
autocmd BufLeave * if mode() =~# '^[iR]'|call s:Event('InsertLeave')|endif
autocmd InsertEnter * call s:Event('InsertEnter')
autocmd BufEnter * if mode() =~# '^[iR]'|call s:Event('InsertEnter')|endif
autocmd CursorMovedI * call s:Event('CursorMovedI')
autocmd CompleteChanged * call s:Event('CompleteChanged')
autocmd ColorScheme,VimEnter * call s:ColorScheme()
autocmd VimEnter * call s:MapTab()
autocmd BufReadCmd copilot://* setlocal buftype=nofile bufhidden=wipe nobuflisted readonly nomodifiable
augroup END
call s:ColorScheme()
call s:MapTab()
if !get(g:, 'copilot_no_maps')
if empty(mapcheck('<C-]>', 'i'))
imap <silent><script><nowait><expr> <C-]> copilot#Dismiss() . "\<C-]>"
endif
endif
call copilot#Init()
let s:dir = expand('<sfile>:h:h')
if getftime(s:dir . '/doc/copilot.txt') > getftime(s:dir . '/doc/tags')
silent! execute 'helptags' fnameescape(s:dir . '/doc')
endif