mirror of
https://github.com/prabirshrestha/vim-lsp.git
synced 2025-12-14 20:35:59 +01:00
* Implemente register command * Add server_name to command context * Expose lsp_to_vim, vim_to_lsp API's as public
65 lines
1.8 KiB
VimL
65 lines
1.8 KiB
VimL
let s:commands = {}
|
|
|
|
"
|
|
" @param {name} = string
|
|
" @param {callback} = funcref
|
|
"
|
|
function! lsp#ui#vim#execute_command#_register(command_name, callback) abort
|
|
if has_key(s:commands, a:command_name)
|
|
throw printf('lsp#ui#vim#execute_command#_register_command: %s already registered.', a:command_name)
|
|
endif
|
|
|
|
let s:commands[a:command_name] = a:callback
|
|
endfunction
|
|
|
|
"
|
|
" TODO: This method does not handle any return value.
|
|
"
|
|
function! lsp#ui#vim#execute_command#_execute(params) abort
|
|
let l:command_name = a:params['command_name']
|
|
let l:command_args = get(a:params, 'command_args', v:null)
|
|
let l:server_name = get(a:params, 'server_name', '')
|
|
let l:bufnr = get(a:params, 'bufnr', -1)
|
|
let l:sync = get(a:params, 'sync', v:false)
|
|
|
|
" create command.
|
|
let l:command = { 'command': l:command_name }
|
|
if l:command_args isnot v:null
|
|
let l:command['arguments'] = l:command_args
|
|
endif
|
|
|
|
" execute command on local.
|
|
if has_key(s:commands, l:command_name)
|
|
try
|
|
call s:commands[l:command_name]({
|
|
\ 'bufnr': l:bufnr,
|
|
\ 'server_name': l:server_name,
|
|
\ 'command': l:command,
|
|
\ })
|
|
catch /.*/
|
|
call lsp#utils#error(printf('Execute command failed: %s', string(a:params)))
|
|
endtry
|
|
return
|
|
endif
|
|
|
|
" execute command on server.
|
|
if !empty(l:server_name)
|
|
call lsp#send_request(l:server_name, {
|
|
\ 'method': 'workspace/executeCommand',
|
|
\ 'params': l:command,
|
|
\ 'sync': l:sync,
|
|
\ 'on_notification': function('s:handle_execute_command', [l:server_name, l:command]),
|
|
\ })
|
|
endif
|
|
endfunction
|
|
|
|
"
|
|
" handle workspace/executeCommand response
|
|
"
|
|
function! s:handle_execute_command(server_name, command, data) abort
|
|
if lsp#client#is_error(a:data['response'])
|
|
call lsp#utils#error('Execute command failed on ' . a:server_name . ': ' . string(a:command) . ' -> ' . string(a:data))
|
|
endif
|
|
endfunction
|
|
|