add lsp#stream() (#884)

This commit is contained in:
Prabir Shrestha
2020-08-22 22:39:52 -07:00
committed by GitHub
parent 47bf0112d9
commit a18783de25
2 changed files with 43 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
let s:enabled = 0
let s:already_setup = 0
let s:Stream = lsp#callbag#makeSubject()
let s:servers = {} " { lsp_id, server_info, init_callbacks, init_result, buffers: { path: { changed_tick } }
let s:last_command_id = 0
let s:notification_callbacks = [] " { name, callback }
@@ -739,6 +740,12 @@ function! s:on_notification(server_name, id, data, event) abort
let l:server_info = l:server['server_info']
let l:lsp_diagnostics_config_enabled = get(get(l:server_info, 'config', {}), 'diagnostics', v:true)
let l:stream_data = { 'server': a:server_name, 'response': l:response }
if has_key(a:data, 'request')
let l:stream_data['request'] = a:data['request']
endif
call s:Stream(1, l:stream_data) " notify stream before callbacks
if lsp#client#is_server_instantiated_notification(a:data)
if has_key(l:response, 'method')
if g:lsp_diagnostics_enabled && l:lsp_diagnostics_config_enabled && l:response['method'] ==# 'textDocument/publishDiagnostics'
@@ -903,8 +910,26 @@ function! s:get_versioned_text_document_identifier(buf, buffer_info) abort
\ }
endfunction
" lsp#request {{{
" lsp#stream {{{
"
" example:
"
" function! s:on_textDocumentDiagnostics(x) abort
" echom 'Diagnostics for ' . a:x['server'] . ' ' . json_encode(a:x['response'])
" endfunction
"
" au User lsp_setup call lsp#callbag#pipe(
" \ lsp#stream(),
" \ lsp#callbag#filter({x-> has_key(x, 'response') && !has_key(x['response'], 'error') && get(x['response'], 'method', '') == 'textDocument/publishDiagnostics'}),
" \ lsp#callbag#subscribe({ 'next':{x->s:on_textDocumentDiagnostics(x)} }),
" \ )
"
function! lsp#stream() abort
return s:Stream
endfunction
" }}}
" lsp#request {{{
function! lsp#request(server_name, request) abort
let l:ctx = {
\ 'server_name': a:server_name,

View File

@@ -52,6 +52,7 @@ CONTENTS *vim-lsp-contents*
lsp#disable |lsp#disable()|
lsp#register_server |lsp#register_server()|
lsp#register_command |lsp#register_command()|
lsp#stream |lsp#stream()|
lsp#stop_server |lsp#stop_server()|
lsp#utils#find_nearest_parent_file_directory()
|lsp#utils#find_nearest_parent_file_directory()|
@@ -962,6 +963,22 @@ For example, the rust-analyzer expects the client handles some custom command as
endfunction
call lsp#register_command('rust-analyzer.applySourceChange', function('s:rust_analyzer_apply_source_change'))
<
lsp#stream() *lsp#stream()*
Stream api to listen to responses and notifications from language server or
vim-lsp. Always verify the existence of request, response and server before
accessing. Subscribing to stream should never throw an error.
>
function! s:on_textDocumentDiagnostics(x) abort
echom 'Diagnostics for ' . a:x['server'] . ' ' . json_encode(a:x['response'])
endfunction
au User lsp_setup call lsp#callbag#pipe(
\ lsp#stream(),
\ lsp#callbag#filter({x-> has_key(x, 'response') && !has_key(x['response'], 'error') && get(x['response'], 'method', '') == 'textDocument/publishDiagnostics'}),
\ lsp#callbag#subscribe({ 'next':{x->s:on_textDocumentDiagnostics(x)} }),
\ )
<
lsp#stop_server({name-of-server}) *lsp#stop_server()*