summaryrefslogtreecommitdiff
path: root/autoload
diff options
context:
space:
mode:
authorMartino Pilia <martino.pilia@gmail.com>2019-05-31 17:26:53 +0200
committerMartino Pilia <martino.pilia@gmail.com>2019-05-31 17:58:27 +0200
commit7053d468cc45b1afc18a12cc26dea0278fe9567c (patch)
treec0cfa052e1a89d7536a4f67104ad2b9318573ba1 /autoload
parent27146ade32d6686fefde27de76b65bcdf353eab5 (diff)
downloadale-7053d468cc45b1afc18a12cc26dea0278fe9567c.zip
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
Diffstat (limited to 'autoload')
-rw-r--r--autoload/ale/lsp.vim32
-rw-r--r--autoload/ale/lsp_linter.vim26
2 files changed, 54 insertions, 4 deletions
diff --git a/autoload/ale/lsp.vim b/autoload/ale/lsp.vim
index 017096cd..1ffdba2e 100644
--- a/autoload/ale/lsp.vim
+++ b/autoload/ale/lsp.vim
@@ -5,6 +5,9 @@
let s:connections = get(s:, 'connections', {})
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,
" create a new connection if needed. Return a unique ID for the connection.
function! ale#lsp#Register(executable_or_address, project, init_options) abort
@@ -296,10 +299,19 @@ function! ale#lsp#HandleMessage(conn_id, message) abort
" responses.
if l:conn.initialized
for l:response in l:response_list
- " 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
+ if has_key(l:response, 'id') && has_key(s:custom_callbacks, l:response.id)
+ " Response to a custom request, call the registered one-shot handler.
+ try
+ 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
endif
endfunction
@@ -525,6 +537,18 @@ function! ale#lsp#Send(conn_id, message) abort
return l:id == 0 ? -1 : l:id
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.
" If a document is opened, 1 will be returned, otherwise 0 will be returned.
function! ale#lsp#OpenDocument(conn_id, buffer, language_id) abort
diff --git a/autoload/ale/lsp_linter.vim b/autoload/ale/lsp_linter.vim
index 4f439b28..82ae60a2 100644
--- a/autoload/ale/lsp_linter.vim
+++ b/autoload/ale/lsp_linter.vim
@@ -413,3 +413,29 @@ endfunction
function! ale#lsp_linter#SetLSPLinterMap(replacement_map) abort
let s:lsp_linter_map = a:replacement_map
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