Skip to content

Commit 7d15d0f

Browse files
convert more of codelens to callbag (#1013)
1 parent 513fef9 commit 7d15d0f

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

autoload/lsp/ui/vim/code_lens.vim

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
" vint: -ProhibitUnusedVariable
1+
" https://microsoft.github.io/language-server-protocol/specification#textDocument_codeLens
22

3-
"
43
" @param option = {
54
" }
65
"
@@ -23,37 +22,39 @@ function! lsp#ui#vim#code_lens#do(option) abort
2322
\ lsp#request(server, {
2423
\ 'method': 'textDocument/codeLens',
2524
\ 'params': {
26-
\ 'textDocument': lsp#get_text_document_identifier(),
25+
\ 'textDocument': lsp#get_text_document_identifier(l:bufnr),
2726
\ },
2827
\ }),
29-
\ lsp#callbag#flatMap({x->s:resolve_if_required(server, x['response'])}),
30-
\ lsp#callbag#map({x->{ 'server': server, 'codelens': x }}),
28+
\ lsp#callbag#map({x->x['response']['result']}),
29+
\ lsp#callbag#filter({codelenses->!empty(codelenses)}),
30+
\ lsp#callbag#flatMap({codelenses->
31+
\ lsp#callbag#pipe(
32+
\ lsp#callbag#fromList(codelenses),
33+
\ lsp#callbag#flatMap({codelens->
34+
\ has_key(codelens, 'command') ? lsp#callbag#of(codelens) : s:resolve_codelens(server, codelens)}),
35+
\ )
36+
\ }),
37+
\ lsp#callbag#map({codelens->{ 'server': server, 'codelens': codelens }}),
3138
\ )
3239
\ }),
3340
\ lsp#callbag#reduce({acc,curr->add(acc, curr)}, []),
34-
\ lsp#callbag#tap({x->s:chooseCodeLens(x, l:bufnr)}),
41+
\ lsp#callbag#flatMap({x->s:chooseCodeLens(x, l:bufnr)}),
42+
\ lsp#callbag#tap({x-> lsp#ui#vim#execute_command#_execute({
43+
\ 'server_name': x['server'],
44+
\ 'command_name': get(x['codelens']['command'], 'command', ''),
45+
\ 'command_args': get(x['codelens']['command'], 'arguments', v:null),
46+
\ 'bufnr': l:bufnr,
47+
\ })}),
3548
\ lsp#callbag#takeUntil(lsp#callbag#pipe(
3649
\ lsp#stream(),
3750
\ lsp#callbag#filter({x->has_key(x, 'command')}),
3851
\ )),
3952
\ lsp#callbag#subscribe({
40-
\ 'error': {e->s:error(x)},
53+
\ 'error': {e->lsp#utils#error('Error running codelens ' . json_encode(e))},
4154
\ }),
4255
\ )
4356
endfunction
4457

45-
function! s:resolve_if_required(server, response) abort
46-
let l:codelens = a:response['result']
47-
if empty(l:codelens)
48-
return lsp#callbag#empty()
49-
endif
50-
51-
return lsp#callbag#pipe(
52-
\ lsp#callbag#fromList(l:codelens),
53-
\ lsp#callbag#flatMap({codelens-> has_key(codelens, 'command') ? lsp#callbag#of(codelens) : s:resolve_codelens(a:server, codelens) }),
54-
\ )
55-
endfunction
56-
5758
function! s:resolve_codelens(server, codelens) abort
5859
" TODO: return callbag#lsp#empty() if codelens resolve not supported by server
5960
return lsp#callbag#pipe(
@@ -68,28 +69,15 @@ endfunction
6869
function! s:chooseCodeLens(items, bufnr) abort
6970
redraw | echo 'Select codelens:'
7071
if empty(a:items)
71-
call lsp#utils#error('No codelens found')
72-
return
72+
return lsp#callbag#throwError('No codelens found')
7373
endif
7474
let l:index = inputlist(map(copy(a:items), {i, value ->
7575
\ printf('%s - [%s] %s', i + 1, value['server'], value['codelens']['command']['title'])
7676
\ }))
7777
if l:index > 0 && l:index <= len(a:items)
7878
let l:selected = a:items[l:index - 1]
79-
call s:handle_code_lens_command(l:selected['server'], l:selected['codelens'], a:bufnr)
79+
return lsp#callbag#of(l:selected)
80+
else
81+
return lsp#callbag#empty()
8082
endif
8183
endfunction
82-
83-
function! s:error(e) abort
84-
call lsp#utils#error('Echo occured during CodeLens' . a:e)
85-
endfunction
86-
87-
function! s:handle_code_lens_command(server, codelens, bufnr) abort
88-
call lsp#ui#vim#execute_command#_execute({
89-
\ 'server_name': a:server,
90-
\ 'command_name': get(a:codelens['command'], 'command', ''),
91-
\ 'command_args': get(a:codelens['command'], 'arguments', v:null),
92-
\ 'sync': 0,
93-
\ 'bufnr': a:bufnr,
94-
\ })
95-
endfunction

0 commit comments

Comments
 (0)