coc.vim 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. scriptencoding utf-8
  2. let g:coc#_context = {'start': 0, 'preselect': -1,'candidates': []}
  3. let g:coc_user_config = get(g:, 'coc_user_config', {})
  4. let g:coc_global_extensions = get(g:, 'coc_global_extensions', [])
  5. let g:coc_selected_text = ''
  6. let g:coc_vim_commands = []
  7. let s:watched_keys = []
  8. let s:is_vim = !has('nvim')
  9. let s:error_sign = get(g:, 'coc_status_error_sign', has('mac') ? '❌ ' : 'E')
  10. let s:warning_sign = get(g:, 'coc_status_warning_sign', has('mac') ? '⚠️ ' : 'W')
  11. let s:select_api = exists('*nvim_select_popupmenu_item')
  12. let s:callbacks = {}
  13. let s:hide_pum = has('nvim-0.6.1') || has('patch-8.2.3389')
  14. function! coc#expandable() abort
  15. return coc#rpc#request('snippetCheck', [1, 0])
  16. endfunction
  17. function! coc#jumpable() abort
  18. return coc#rpc#request('snippetCheck', [0, 1])
  19. endfunction
  20. function! coc#expandableOrJumpable() abort
  21. return coc#rpc#request('snippetCheck', [1, 1])
  22. endfunction
  23. " add vim command to CocCommand list
  24. function! coc#add_command(id, cmd, ...)
  25. let config = {'id':a:id, 'cmd':a:cmd, 'title': get(a:,1,'')}
  26. call add(g:coc_vim_commands, config)
  27. if !coc#rpc#ready() | return | endif
  28. call coc#rpc#notify('addCommand', [config])
  29. endfunction
  30. function! coc#on_enter()
  31. call coc#rpc#notify('CocAutocmd', ['Enter', bufnr('%')])
  32. return ''
  33. endfunction
  34. function! coc#_insert_key(method, key, ...) abort
  35. let prefix = ''
  36. if get(a:, 1, 1)
  37. if pumvisible()
  38. if s:hide_pum
  39. let prefix = "\<C-x>\<C-z>"
  40. else
  41. let g:coc_disable_space_report = 1
  42. let prefix = "\<space>\<bs>"
  43. endif
  44. endif
  45. endif
  46. return prefix."\<c-r>=coc#rpc#".a:method."('doKeymap', ['".a:key."'])\<CR>"
  47. endfunction
  48. function! coc#_complete() abort
  49. let items = get(g:coc#_context, 'candidates', [])
  50. let preselect = get(g:coc#_context, 'preselect', -1)
  51. let startcol = g:coc#_context.start + 1
  52. if s:select_api && len(items) && preselect != -1
  53. noa call complete(startcol, items)
  54. call nvim_select_popupmenu_item(preselect, v:false, v:false, {})
  55. " use <cmd> specific key to preselect item at once
  56. call feedkeys("\<Cmd>\<CR>" , 'i')
  57. else
  58. if pumvisible()
  59. let g:coc_disable_complete_done = 1
  60. endif
  61. call complete(startcol, items)
  62. endif
  63. return ''
  64. endfunction
  65. function! coc#_do_complete(start, items, preselect, changedtick)
  66. if b:changedtick != a:changedtick
  67. return
  68. endif
  69. let g:coc#_context = {
  70. \ 'start': a:start,
  71. \ 'candidates': a:items,
  72. \ 'preselect': a:preselect
  73. \}
  74. if mode() =~# 'i'
  75. call coc#_complete()
  76. endif
  77. endfunction
  78. function! coc#_cancel(...)
  79. call coc#pum#close()
  80. endfunction
  81. " used for statusline
  82. function! coc#status()
  83. let info = get(b:, 'coc_diagnostic_info', {})
  84. let msgs = []
  85. if !empty(info) && get(info, 'error', 0)
  86. call add(msgs, s:error_sign . info['error'])
  87. endif
  88. if !empty(info) && get(info, 'warning', 0)
  89. call add(msgs, s:warning_sign . info['warning'])
  90. endif
  91. return coc#compat#trim(join(msgs, ' ') . ' ' . get(g:, 'coc_status', ''))
  92. endfunction
  93. function! coc#config(section, value)
  94. let g:coc_user_config[a:section] = a:value
  95. call coc#rpc#notify('updateConfig', [a:section, a:value])
  96. endfunction
  97. function! coc#add_extension(...)
  98. if a:0 == 0 | return | endif
  99. call extend(g:coc_global_extensions, a:000)
  100. endfunction
  101. function! coc#_watch(key)
  102. if s:is_vim | return | endif
  103. if index(s:watched_keys, a:key) == -1
  104. call add(s:watched_keys, a:key)
  105. call dictwatcheradd(g:, a:key, function('s:GlobalChange'))
  106. endif
  107. endfunction
  108. function! coc#_unwatch(key)
  109. if s:is_vim | return | endif
  110. let idx = index(s:watched_keys, a:key)
  111. if idx != -1
  112. call remove(s:watched_keys, idx)
  113. call dictwatcherdel(g:, a:key, function('s:GlobalChange'))
  114. endif
  115. endfunction
  116. function! s:GlobalChange(dict, key, val)
  117. call coc#rpc#notify('GlobalChange', [a:key, get(a:val, 'old', v:null), get(a:val, 'new', v:null)])
  118. endfunction
  119. function! coc#on_notify(id, method, Cb)
  120. let key = a:id. '-'.a:method
  121. let s:callbacks[key] = a:Cb
  122. call coc#rpc#notify('registNotification', [a:id, a:method])
  123. endfunction
  124. function! coc#do_notify(id, method, result)
  125. let key = a:id. '-'.a:method
  126. let Fn = s:callbacks[key]
  127. if !empty(Fn)
  128. call Fn(a:result)
  129. endif
  130. endfunction
  131. function! coc#start(...)
  132. let opt = coc#util#get_complete_option()
  133. call CocActionAsync('startCompletion', extend(opt, get(a:, 1, {})))
  134. return ''
  135. endfunction
  136. function! coc#refresh() abort
  137. return "\<c-r>=coc#start()\<CR>"
  138. endfunction
  139. function! coc#_select_confirm() abort
  140. call timer_start(10, { -> coc#pum#select_confirm()})
  141. return s:is_vim || has('nvim-0.5.0') ? "\<Ignore>" : "\<space>\<bs>"
  142. endfunction
  143. function! coc#complete_indent() abort
  144. let curpos = getcurpos()
  145. let indent_len = len(matchstr(getline('.'), '^\s*'))
  146. let startofline = &startofline
  147. let virtualedit = &virtualedit
  148. set nostartofline
  149. set virtualedit=all
  150. normal! ==
  151. let &startofline = startofline
  152. let &virtualedit = virtualedit
  153. let shift = len(matchstr(getline('.'), '^\s*')) - indent_len
  154. let curpos[2] += shift
  155. let curpos[4] += shift
  156. call cursor(curpos[1:])
  157. if shift != 0
  158. if s:is_vim
  159. call timer_start(0, { -> execute('redraw')})
  160. endif
  161. endif
  162. endfunction