rpc.vim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. scriptencoding utf-8
  2. let s:is_win = has("win32") || has("win64")
  3. let s:client = v:null
  4. let s:name = 'coc'
  5. let s:is_vim = !has('nvim')
  6. function! coc#rpc#start_server()
  7. if get(g:, 'coc_node_env', '') ==# 'test'
  8. " server already started
  9. let s:client = coc#client#create(s:name, [])
  10. let chan_id = get(g:, 'coc_node_channel_id', 0)
  11. let s:client['running'] = chan_id != 0
  12. let s:client['chan_id'] = chan_id
  13. return
  14. endif
  15. if empty(s:client)
  16. let cmd = coc#util#job_command()
  17. if empty(cmd) | return | endif
  18. let $COC_VIMCONFIG = coc#util#get_config_home()
  19. let $COC_DATA_HOME = coc#util#get_data_home()
  20. let s:client = coc#client#create(s:name, cmd)
  21. endif
  22. if !coc#client#is_running('coc')
  23. call s:client['start']()
  24. endif
  25. endfunction
  26. function! coc#rpc#started() abort
  27. return !empty(s:client)
  28. endfunction
  29. function! coc#rpc#ready()
  30. if empty(s:client) || s:client['running'] == 0
  31. return 0
  32. endif
  33. return 1
  34. endfunction
  35. function! coc#rpc#set_channel(chan_id) abort
  36. let g:coc_node_channel_id = a:chan_id
  37. if a:chan_id != 0
  38. let s:client['running'] = 1
  39. let s:client['chan_id'] = a:chan_id
  40. endif
  41. endfunction
  42. function! coc#rpc#kill()
  43. let pid = get(g:, 'coc_process_pid', 0)
  44. if !pid | return | endif
  45. if s:is_win
  46. call system('taskkill /PID '.pid)
  47. else
  48. call system('kill -9 '.pid)
  49. endif
  50. endfunction
  51. function! coc#rpc#get_errors()
  52. return split(execute('messages'), "\n")
  53. endfunction
  54. function! coc#rpc#stop()
  55. if empty(s:client)
  56. return
  57. endif
  58. try
  59. if s:is_vim
  60. call job_stop(ch_getjob(s:client['channel']), 'term')
  61. else
  62. call jobstop(s:client['chan_id'])
  63. endif
  64. catch /.*/
  65. " ignore
  66. endtry
  67. endfunction
  68. function! coc#rpc#restart()
  69. if empty(s:client)
  70. call coc#rpc#start_server()
  71. else
  72. call coc#highlight#clear_all()
  73. call coc#ui#sign_unplace()
  74. call coc#float#close_all()
  75. call coc#rpc#request('detach', [])
  76. sleep 100m
  77. let s:client['command'] = coc#util#job_command()
  78. call coc#client#restart(s:name)
  79. echohl MoreMsg | echom 'starting coc.nvim service' | echohl None
  80. endif
  81. endfunction
  82. function! coc#rpc#request(method, args) abort
  83. if !coc#rpc#ready()
  84. return ''
  85. endif
  86. return s:client['request'](a:method, a:args)
  87. endfunction
  88. function! coc#rpc#notify(method, args) abort
  89. if !coc#rpc#ready()
  90. return ''
  91. endif
  92. call s:client['notify'](a:method, a:args)
  93. return ''
  94. endfunction
  95. function! coc#rpc#request_async(method, args, cb) abort
  96. if !coc#rpc#ready()
  97. return cb('coc.nvim service not started.')
  98. endif
  99. call s:client['request_async'](a:method, a:args, a:cb)
  100. endfunction
  101. " receive async response
  102. function! coc#rpc#async_response(id, resp, isErr) abort
  103. if empty(s:client)
  104. return
  105. endif
  106. call coc#client#on_response(s:name, a:id, a:resp, a:isErr)
  107. endfunction
  108. " send async response to server
  109. function! coc#rpc#async_request(id, method, args)
  110. let l:Cb = {err, ... -> coc#rpc#notify('nvim_async_response_event', [a:id, err, get(a:000, 0, v:null)])}
  111. let args = a:args + [l:Cb]
  112. try
  113. call call(a:method, args)
  114. catch /.*/
  115. call coc#rpc#notify('nvim_async_response_event', [a:id, v:exception, v:null])
  116. endtry
  117. endfunction