Skip to content

Commit 85a03f2

Browse files
authored
Merge codeAction(s) (#736)
* Merge codeAction(s) * Print server name * Fix indentation * Use empty()
1 parent 39e127f commit 85a03f2

File tree

1 file changed

+49
-19
lines changed

1 file changed

+49
-19
lines changed

autoload/lsp/ui/vim/code_action.vim

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ function! lsp#ui#vim#code_action#do(option) abort
3232
let l:range = lsp#utils#range#_get_current_line_range()
3333
endif
3434

35+
let l:ctx = {
36+
\ 'count': len(l:server_names),
37+
\ 'results': [],
38+
\}
3539
let l:bufnr = bufnr('%')
3640
let l:command_id = lsp#_new_command()
3741
for l:server_name in l:server_names
@@ -47,48 +51,74 @@ function! lsp#ui#vim#code_action#do(option) abort
4751
\ },
4852
\ },
4953
\ 'sync': l:sync,
50-
\ 'on_notification': function('s:handle_code_action', [l:server_name, l:command_id, l:sync, l:query, l:bufnr]),
54+
\ 'on_notification': function('s:handle_code_action', [l:ctx, l:server_name, l:command_id, l:sync, l:query, l:bufnr]),
5155
\ })
5256
endfor
5357
echo 'Retrieving code actions ...'
5458
endfunction
5559

56-
function! s:handle_code_action(server_name, command_id, sync, query, bufnr, data) abort
60+
function! s:handle_code_action(ctx, server_name, command_id, sync, query, bufnr, data) abort
5761
" Ignore old request.
5862
if a:command_id != lsp#_last_command()
5963
return
6064
endif
6165

62-
" Check response error.
63-
if lsp#client#is_error(a:data['response'])
64-
call lsp#utils#error('Failed to CodeAction for ' . a:server_name . ': ' . lsp#client#error_message(a:data['response']))
66+
call add(a:ctx['results'], {
67+
\ 'server_name': a:server_name,
68+
\ 'data': a:data,
69+
\})
70+
let a:ctx['count'] -= 1
71+
if a:ctx['count'] ># 0
6572
return
6673
endif
6774

68-
" Check code actions.
69-
let l:code_actions = a:data['response']['result']
70-
call lsp#log('s:handle_code_action', l:code_actions)
71-
if len(l:code_actions) == 0
75+
let l:total_code_actions = []
76+
for l:result in a:ctx['results']
77+
let l:server_name = l:result['server_name']
78+
let l:data = l:result['data']
79+
" Check response error.
80+
if lsp#client#is_error(l:data['response'])
81+
call lsp#utils#error('Failed to CodeAction for ' . l:server_name . ': ' . lsp#client#error_message(l:data['response']))
82+
continue
83+
endif
84+
85+
" Check code actions.
86+
let l:code_actions = l:data['response']['result']
87+
88+
" Filter code actions.
89+
if !empty(a:query)
90+
let l:code_actions = filter(l:code_actions, { _, action -> get(action, 'kind', '') =~# '^' . a:query })
91+
endif
92+
if empty(l:code_actions)
93+
continue
94+
endif
95+
96+
for l:code_action in l:code_actions
97+
call add(l:total_code_actions, {
98+
\ 'server_name': l:server_name,
99+
\ 'code_action': l:code_action,
100+
\})
101+
endfor
102+
endfor
103+
104+
if len(l:total_code_actions) == 0
72105
echo 'No code actions found'
73106
return
74107
endif
75-
76-
" Filter code actions.
77-
if !empty(a:query)
78-
let l:code_actions = filter(l:code_actions, { _, action -> get(action, 'kind', '') =~# '^' . a:query })
79-
endif
108+
call lsp#log('s:handle_code_action', l:total_code_actions)
80109

81110
" Prompt to choose code actions when empty query provided.
82111
let l:index = 1
83-
if len(l:code_actions) > 1 || empty(a:query)
84-
let l:index = inputlist(map(copy(l:code_actions), { i, action ->
85-
\ printf('%s - %s', i + 1, action['title'])
112+
if len(l:total_code_actions) > 1 || empty(a:query)
113+
let l:index = inputlist(map(copy(l:total_code_actions), { i, action ->
114+
\ printf('%s - [%s] %s', i + 1, action['server_name'], action['code_action']['title'])
86115
\ }))
87116
endif
88117

89118
" Execute code action.
90-
if 0 < l:index && l:index <= len(l:code_actions)
91-
call s:handle_one_code_action(a:server_name, a:sync, a:bufnr, l:code_actions[l:index - 1])
119+
if 0 < l:index && l:index <= len(l:total_code_actions)
120+
let l:selected = l:total_code_actions[l:index - 1]
121+
call s:handle_one_code_action(l:selected['server_name'], a:sync, a:bufnr, l:selected['code_action'])
92122
endif
93123
endfunction
94124

0 commit comments

Comments
 (0)