Skip to content

Commit 51adba8

Browse files
rhysdmattn
andauthored
use quickpick to choose Code Action (#1123)
Co-authored-by: mattn <[email protected]>
1 parent 800c878 commit 51adba8

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

autoload/lsp/ui/vim/code_action.vim

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,44 @@ function! s:handle_code_action(ctx, server_name, command_id, sync, query, bufnr,
107107
endif
108108
call lsp#log('s:handle_code_action', l:total_code_actions)
109109

110-
" Prompt to choose code actions when empty query provided.
111-
let l:index = 1
112-
if len(l:total_code_actions) > 1 || empty(a:query)
113-
let l:index = inputlist(map(copy(l:total_code_actions), funcref('s:get_action_list_item_text')))
110+
if len(l:total_code_actions) == 1 && !empty(a:query)
111+
let l:action = l:total_code_actions[0]
112+
if s:handle_disabled_action(l:action) | return | endif
113+
" Clear 'Retrieving code actions ...' message
114+
echo ''
115+
call s:handle_one_code_action(l:action['server_name'], a:sync, a:bufnr, l:action['code_action'])
116+
return
114117
endif
115118

116-
" Execute code action.
117-
if 0 < l:index && l:index <= len(l:total_code_actions)
118-
let l:selected = l:total_code_actions[l:index - 1]
119-
if has_key(l:selected, 'disabled')
120-
let l:reason = l:selected['disabled']['reason']
121-
" Avoid the message is overwritten by inputlist() call
122-
redraw
123-
echo 'This action is disabled: ' . l:reason
124-
return
119+
" Prompt to choose code actions when empty query provided.
120+
let l:items = []
121+
for l:action in l:total_code_actions
122+
let l:title = printf('[%s] %s', l:action['server_name'], l:action['code_action']['title'])
123+
if has_key(l:action['code_action'], 'kind') && l:action['code_action']['kind'] !=# ''
124+
let l:title .= ' (' . l:action['code_action']['kind'] . ')'
125125
endif
126-
call s:handle_one_code_action(l:selected['server_name'], a:sync, a:bufnr, l:selected['code_action'])
126+
call add(l:items, { 'title': l:title, 'item': l:action })
127+
endfor
128+
call lsp#internal#ui#quickpick#open({
129+
\ 'items': l:items,
130+
\ 'key': 'title',
131+
\ 'on_accept': funcref('s:accept_code_action', [a:sync, a:bufnr]),
132+
\ })
133+
endfunction
134+
135+
function! s:accept_code_action(sync, bufnr, data, ...) abort
136+
call lsp#internal#ui#quickpick#close()
137+
let l:selected = a:data['items'][0]['item']
138+
if s:handle_disabled_action(l:selected) | return | endif
139+
call s:handle_one_code_action(l:selected['server_name'], a:sync, a:bufnr, l:selected['code_action'])
140+
endfunction
141+
142+
function! s:handle_disabled_action(code_action) abort
143+
if has_key(a:code_action, 'disabled')
144+
echo 'This action is disabled: ' . a:code_action['disabled']['reason']
145+
return v:true
127146
endif
147+
return v:false
128148
endfunction
129149

130150
function! s:handle_one_code_action(server_name, sync, bufnr, command_or_code_action) abort
@@ -154,11 +174,3 @@ function! s:handle_one_code_action(server_name, sync, bufnr, command_or_code_act
154174
\ })
155175
endif
156176
endfunction
157-
158-
function! s:get_action_list_item_text(index, action) abort
159-
let l:text = printf('%s - [%s] %s', a:index + 1, a:action['server_name'], a:action['code_action']['title'])
160-
if has_key(a:action['code_action'], 'kind') && a:action['code_action']['kind'] !=# ''
161-
let l:text .= ' (' . a:action['code_action']['kind'] . ')'
162-
endif
163-
return l:text
164-
endfunction

0 commit comments

Comments
 (0)