@@ -32,6 +32,10 @@ function! lsp#ui#vim#code_action#do(option) abort
32
32
let l: range = lsp#utils#range#_get_current_line_range ()
33
33
endif
34
34
35
+ let l: ctx = {
36
+ \ ' count' : len (l: server_names ),
37
+ \ ' results' : [],
38
+ \}
35
39
let l: bufnr = bufnr (' %' )
36
40
let l: command_id = lsp#_new_command ()
37
41
for l: server_name in l: server_names
@@ -47,48 +51,74 @@ function! lsp#ui#vim#code_action#do(option) abort
47
51
\ },
48
52
\ },
49
53
\ ' 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 ]),
51
55
\ })
52
56
endfor
53
57
echo ' Retrieving code actions ...'
54
58
endfunction
55
59
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
57
61
" Ignore old request.
58
62
if a: command_id != lsp#_last_command ()
59
63
return
60
64
endif
61
65
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
65
72
return
66
73
endif
67
74
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
72
105
echo ' No code actions found'
73
106
return
74
107
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 )
80
109
81
110
" Prompt to choose code actions when empty query provided.
82
111
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' ])
86
115
\ }))
87
116
endif
88
117
89
118
" 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' ])
92
122
endif
93
123
endfunction
94
124
0 commit comments