Add API for custom LSP requests

Implement a function `ale#lsp_linter#SendRequest` that allows to send
custom LSP requests to an enabled LSP linter.

Resolves #2474
This commit is contained in:
Martino Pilia 2019-05-31 17:26:53 +02:00
parent 27146ade32
commit 7053d468cc
No known key found for this signature in database
GPG Key ID: CDEE463095565E17
4 changed files with 192 additions and 4 deletions

View File

@ -5,6 +5,9 @@
let s:connections = get(s:, 'connections', {}) let s:connections = get(s:, 'connections', {})
let g:ale_lsp_next_message_id = 1 let g:ale_lsp_next_message_id = 1
" A Dictionary to track one-shot callbacks for custom LSP requests
let s:custom_callbacks = get(s:, 'custom_callbacks', {})
" Given an id, which can be an executable or address, and a project path, " Given an id, which can be an executable or address, and a project path,
" create a new connection if needed. Return a unique ID for the connection. " create a new connection if needed. Return a unique ID for the connection.
function! ale#lsp#Register(executable_or_address, project, init_options) abort function! ale#lsp#Register(executable_or_address, project, init_options) abort
@ -296,10 +299,19 @@ function! ale#lsp#HandleMessage(conn_id, message) abort
" responses. " responses.
if l:conn.initialized if l:conn.initialized
for l:response in l:response_list for l:response in l:response_list
" Call all of the registered handlers with the response. if has_key(l:response, 'id') && has_key(s:custom_callbacks, l:response.id)
for l:Callback in l:conn.callback_list " Response to a custom request, call the registered one-shot handler.
call ale#util#GetFunction(l:Callback)(a:conn_id, l:response) try
endfor call s:custom_callbacks[l:response.id](l:response)
finally
call remove(s:custom_callbacks, l:response.id)
endtry
else
" Call all of the registered handlers with the response.
for l:Callback in l:conn.callback_list
call ale#util#GetFunction(l:Callback)(a:conn_id, l:response)
endfor
endif
endfor endfor
endif endif
endfunction endfunction
@ -525,6 +537,18 @@ function! ale#lsp#Send(conn_id, message) abort
return l:id == 0 ? -1 : l:id return l:id == 0 ? -1 : l:id
endfunction endfunction
" Send a custom request to an LSP server.
" The given callback is called on response.
function! ale#lsp#SendCustomRequest(conn_id, message, Callback) abort
let l:id = ale#lsp#Send(a:conn_id, a:message)
if l:id > 0
let s:custom_callbacks[l:id] = a:Callback
endif
return l:id
endfunction
" Notify LSP servers or tsserver if a document is opened, if needed. " Notify LSP servers or tsserver if a document is opened, if needed.
" If a document is opened, 1 will be returned, otherwise 0 will be returned. " If a document is opened, 1 will be returned, otherwise 0 will be returned.
function! ale#lsp#OpenDocument(conn_id, buffer, language_id) abort function! ale#lsp#OpenDocument(conn_id, buffer, language_id) abort

View File

@ -413,3 +413,29 @@ endfunction
function! ale#lsp_linter#SetLSPLinterMap(replacement_map) abort function! ale#lsp_linter#SetLSPLinterMap(replacement_map) abort
let s:lsp_linter_map = a:replacement_map let s:lsp_linter_map = a:replacement_map
endfunction endfunction
" Send a custom request to an LSP linter.
function! ale#lsp_linter#SendRequest(buffer, linter_name, method, parameters, Callback) abort
let l:filetype = ale#linter#ResolveFiletype(getbufvar(a:buffer, '&filetype'))
let l:linter_list = ale#linter#GetAll(l:filetype)
let l:linter_list = filter(l:linter_list, {_, v -> v.name is# a:linter_name})
if len(l:linter_list) < 1
throw 'Linter "' . a:linter_name . '" not found!'
endif
let l:linter = l:linter_list[0]
let l:executable_or_address = ''
if l:linter.lsp is# 'socket'
let l:executable_or_address = ale#linter#GetAddress(a:buffer, l:linter)
else
let l:executable_or_address = ale#linter#GetExecutable(a:buffer, l:linter)
endif
let l:root = ale#util#GetFunction(l:linter.project_root)(a:buffer)
let l:conn_id = l:executable_or_address . ':' . l:root
let l:message = [0, a:method, a:parameters]
return ale#lsp#SendCustomRequest(l:conn_id, l:message, a:Callback) == 0
endfunction

View File

@ -3191,6 +3191,24 @@ ale#linter#PreventLoading(filetype) *ale#linter#PreventLoading()*
|runtimepath| for that filetype. This function can be called from vimrc or |runtimepath| for that filetype. This function can be called from vimrc or
similar to prevent ALE from loading linters. similar to prevent ALE from loading linters.
ale#lsp_linter#SendRequest(buffer, linter_name, method, parameters, Callback)
*ale#lsp_linter#SendRequest()*
Send a custom request to an LSP linter.
`buffer` must be a valid buffer number, and `linter_name` is a |String|
identifying an LSP linter that is available and enabled for the |filetype|
of `buffer`. `method` is a |String| identifying an LSP method supported by
`linter`, while `parameters` is a |dictionary| of LSP parameters applicable
to `method`. `Callback` is a |Funcref| that is called when a response to the
request is received, and takes as unique argument a dictionary representing
the response to the request obtained from the server.
The function returns zero if the request is succesfully sent, non-zero
otherwise.
ale#other_source#ShowResults(buffer, linter_name, loclist) ale#other_source#ShowResults(buffer, linter_name, loclist)
*ale#other_source#ShowResults()* *ale#other_source#ShowResults()*

View File

@ -0,0 +1,120 @@
Before:
runtime autoload/ale/linter.vim
runtime autoload/ale/lsp.vim
runtime autoload/ale/lsp_linter.vim
let g:address = 'ccls_address'
let g:callback_result = 0
let g:conn_id = -1
let g:executable = 'ccls'
let g:linter_name = 'ccls'
let g:magic_number = 42
let g:message = -1
let g:message_id = 1
let g:method = '$ccls/call'
let g:parameters = {}
let g:project = '/project/root'
let g:return_value = -1
let g:linter_list = [{
\ 'output_stream': 'stdout',
\ 'lint_file': 0,
\ 'language': 'cpp',
\ 'name': g:linter_name,
\ 'project_root': {b -> g:project},
\ 'aliases': [],
\ 'language_callback': {b -> 'cpp'},
\ 'read_buffer': 1,
\ 'command': '%e'
\ }]
" Encode dictionary to jsonrpc
function! Encode(obj) abort
let l:body = json_encode(a:obj)
return 'Content-Length: ' . strlen(l:body) . "\r\n\r\n" . l:body
endfunction
" Register the server with given executable or address
function! InitServer(executable_or_address) abort
let g:conn_id = ale#lsp#Register(a:executable_or_address, g:project, {})
call ale#lsp#HandleMessage(g:conn_id, Encode({'method': 'initialize'}))
endfunction
" Dummy callback
function! Callback(response) abort
let g:callback_result = a:response.result.value
endfunction
" Replace the GetAll function to mock an LSP linter
function! ale#linter#GetAll(filetype) abort
return g:linter_list
endfunction
" Replace the Send function to mock an LSP linter
function! ale#lsp#Send(conn_id, message) abort
let g:message = a:message
return g:message_id
endfunction
" Code for a test case
function! TestCase() abort
" Test sending a custom request
let g:return_value = ale#lsp_linter#SendRequest(bufnr('%'), g:linter_name, g:method, g:parameters, function('Callback'))
AssertEqual
\ 0,
\ g:return_value
AssertEqual
\ [0, g:method, g:parameters],
\ g:message
" Mock an incoming response to the request
call ale#lsp#HandleMessage(g:conn_id, Encode({'id': g:message_id, 'jsonrpc': '2.0', 'result': {'value': g:magic_number}}))
AssertEqual
\ g:magic_number,
\ g:callback_result
endfunction
After:
if g:conn_id isnot v:null
call ale#lsp#RemoveConnectionWithID(g:conn_id)
endif
unlet! g:callback_result
unlet! g:conn_id
unlet! g:executable
unlet! g:linter_name
unlet! g:magic_number
unlet! g:message
unlet! g:message_id
unlet! g:method
unlet! g:parameters
unlet! g:project
unlet! g:return_value
delfunction Encode
delfunction InitServer
delfunction Callback
delfunction TestCase
runtime autoload/ale/linter.vim
runtime autoload/ale/lsp.vim
runtime autoload/ale/lsp_linter.vim
Given cpp(Empty cpp file):
Execute(Test custom request to server identified by executable):
call InitServer(g:executable)
let g:linter_list[0].executable = {b -> g:executable}
let g:linter_list[0].lsp = 'stdio'
call TestCase()
Given cpp(Empty cpp file):
Execute(Test custom request to server identified by address):
call InitServer(g:address)
let g:linter_list[0].address = {b -> g:address}
let g:linter_list[0].lsp = 'socket'
call TestCase()