1
- " vint: -ProhibitUnusedVariable
1
+ " https://microsoft.github.io/language-server-protocol/specification#textDocument_codeLens
2
2
3
- "
4
3
" @param option = {
5
4
" }
6
5
"
@@ -23,37 +22,39 @@ function! lsp#ui#vim#code_lens#do(option) abort
23
22
\ lsp#request (server, {
24
23
\ ' method' : ' textDocument/codeLens' ,
25
24
\ ' params' : {
26
- \ ' textDocument' : lsp#get_text_document_identifier (),
25
+ \ ' textDocument' : lsp#get_text_document_identifier (l: bufnr ),
27
26
\ },
28
27
\ }),
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 }}),
31
38
\ )
32
39
\ }),
33
40
\ 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
+ \ })}),
35
48
\ lsp#callbag#takeUntil (lsp#callbag#pipe (
36
49
\ lsp#stream (),
37
50
\ lsp#callbag#filter ({x - >has_key (x , ' command' )}),
38
51
\ )),
39
52
\ lsp#callbag#subscribe ({
40
- \ ' error' : {e - >s: error (x )},
53
+ \ ' error' : {e - >lsp#utils# error (' Error running codelens ' . json_encode ( e ) )},
41
54
\ }),
42
55
\ )
43
56
endfunction
44
57
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
-
57
58
function ! s: resolve_codelens (server, codelens) abort
58
59
" TODO: return callbag#lsp#empty() if codelens resolve not supported by server
59
60
return lsp#callbag#pipe (
@@ -68,28 +69,15 @@ endfunction
68
69
function ! s: chooseCodeLens (items , bufnr ) abort
69
70
redraw | echo ' Select codelens:'
70
71
if empty (a: items )
71
- call lsp#utils#error (' No codelens found' )
72
- return
72
+ return lsp#callbag#throwError (' No codelens found' )
73
73
endif
74
74
let l: index = inputlist (map (copy (a: items ), {i , value - >
75
75
\ printf (' %s - [%s] %s' , i + 1 , value[' server' ], value[' codelens' ][' command' ][' title' ])
76
76
\ }))
77
77
if l: index > 0 && l: index <= len (a: items )
78
78
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 ()
80
82
endif
81
83
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