All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 function! s:not_supported(what) abort
2 return lsp#utils#error(printf("%s not supported for filetype '%s'", a:what, &filetype))
5 function! lsp#ui#vim#implementation(in_preview, ...) abort
6 let l:ctx = { 'in_preview': a:in_preview }
8 let l:ctx['mods'] = a:1
10 call s:list_location('implementation', l:ctx)
13 function! lsp#ui#vim#type_definition(in_preview, ...) abort
14 let l:ctx = { 'in_preview': a:in_preview }
16 let l:ctx['mods'] = a:1
18 call s:list_location('typeDefinition', l:ctx)
22 function! lsp#ui#vim#declaration(in_preview, ...) abort
23 let l:ctx = { 'in_preview': a:in_preview }
25 let l:ctx['mods'] = a:1
27 call s:list_location('declaration', l:ctx)
30 function! lsp#ui#vim#definition(in_preview, ...) abort
31 let l:ctx = { 'in_preview': a:in_preview }
33 let l:ctx['mods'] = a:1
35 call s:list_location('definition', l:ctx)
38 function! lsp#ui#vim#references(ctx) abort
39 let l:ctx = extend({ 'jump_if_one': 0 }, a:ctx)
40 let l:request_params = { 'context': { 'includeDeclaration': v:true } }
41 call s:list_location('references', l:ctx, l:request_params)
44 function! lsp#ui#vim#add_tree_references() abort
45 let l:ctx = { 'add_tree': v:true }
46 call lsp#ui#vim#references(l:ctx)
49 function! s:list_location(method, ctx, ...) abort
50 " typeDefinition => type definition
51 let l:operation = substitute(a:method, '\u', ' \l\0', 'g')
53 let l:capabilities_func = printf('lsp#capabilities#has_%s_provider(v:val)', substitute(l:operation, ' ', '_', 'g'))
54 let l:servers = filter(lsp#get_allowed_servers(), l:capabilities_func)
55 let l:command_id = lsp#_new_command()
58 let l:ctx = extend({ 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id, 'jump_if_one': 1, 'mods': '', 'in_preview': 0 }, a:ctx)
59 if len(l:servers) == 0
60 call s:not_supported('Retrieving ' . l:operation)
65 \ 'textDocument': lsp#get_text_document_identifier(),
66 \ 'position': lsp#get_position(),
69 call extend(l:params, a:1)
71 for l:server in l:servers
72 call lsp#send_request(l:server, {
73 \ 'method': 'textDocument/' . a:method,
75 \ 'on_notification': function('s:handle_location', [l:ctx, l:server, l:operation]),
79 echo printf('Retrieving %s ...', l:operation)
82 function! s:rename(server, new_name, pos) abort
84 echo '... Renaming aborted ...'
88 " needs to flush existing open buffers
89 call lsp#send_request(a:server, {
90 \ 'method': 'textDocument/rename',
92 \ 'textDocument': lsp#get_text_document_identifier(),
94 \ 'newName': a:new_name,
96 \ 'on_notification': function('s:handle_workspace_edit', [a:server, lsp#_last_command(), 'rename']),
99 echo ' ... Renaming ...'
102 function! lsp#ui#vim#rename() abort
103 let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_rename_prepare_provider(v:val)')
104 let l:prepare_support = 1
105 if len(l:servers) == 0
106 let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_rename_provider(v:val)')
107 let l:prepare_support = 0
110 let l:command_id = lsp#_new_command()
112 if len(l:servers) == 0
113 call s:not_supported('Renaming')
117 " TODO: ask the user which server it should use to rename if there are multiple
118 let l:server = l:servers[0]
121 call lsp#send_request(l:server, {
122 \ 'method': 'textDocument/prepareRename',
124 \ 'textDocument': lsp#get_text_document_identifier(),
125 \ 'position': lsp#get_position(),
127 \ 'on_notification': function('s:handle_rename_prepare', [l:server, l:command_id, 'rename_prepare', expand('<cword>'), lsp#get_position()]),
132 call s:rename(l:server, input('new name: ', expand('<cword>')), lsp#get_position())
135 function! s:stop_all_servers() abort
136 for l:server in lsp#get_server_names()
137 if !lsp#is_server_running(l:server)
141 echo 'Stopping' l:server 'server ...'
142 call lsp#stop_server(l:server)
146 function! s:stop_named_server(name) abort
147 if !lsp#is_valid_server_name(a:name)
148 call lsp#utils#warning('No LSP servers named "' . a:name . '"')
152 if lsp#is_server_running(a:name)
153 echo 'Stopping "' . a:name . '" server...'
154 call lsp#stop_server(a:name)
156 call lsp#utils#warning(
157 \ 'Server "' . a:name . '" is not running: '
158 \ . lsp#get_server_status(a:name)
163 function! s:stop_buffer_servers() abort
164 let l:servers = lsp#get_allowed_servers()
166 \ filter(l:servers, {idx, name -> lsp#is_server_running(name)})
169 call lsp#utils#warning('No active LSP servers for the current buffer')
173 for l:server in l:servers
174 echo 'Stopping "' . l:server . '" server ...'
175 call lsp#stop_server(l:server)
179 function! lsp#ui#vim#stop_server(stop_all, ...) abort
180 if a:0 != 0 && a:0 != 1
181 call lsp#utils#error(
182 \ 'lsp#ui#vim#stop_server(): expected 1 optional "name" argument.'
183 \ . ' Got: "' . join(a:000, '", "') . '".')
186 let l:stop_all = a:stop_all ==# '!'
187 let l:name = get(a:000, 0, '')
191 call lsp#utils#error(
192 \ '"!" stops all servers: name is ignored: "' . l:name . '"')
195 call s:stop_all_servers()
200 call s:stop_named_server(l:name)
204 call s:stop_buffer_servers()
207 function! lsp#ui#vim#workspace_symbol(query) abort
208 let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_workspace_symbol_provider(v:val)')
209 let l:command_id = lsp#_new_command()
211 if len(l:servers) == 0
212 call s:not_supported('Retrieving workspace symbols')
217 let l:query = a:query
219 let l:query = inputdialog('query>', '', "\<ESC>")
220 if l:query ==# "\<ESC>"
225 for l:server in l:servers
226 call lsp#send_request(l:server, {
227 \ 'method': 'workspace/symbol',
231 \ 'on_notification': function('s:handle_symbol', [l:server, l:command_id, 'workspaceSymbol']),
236 echo 'Retrieving workspace symbols ...'
239 function! lsp#ui#vim#document_symbol() abort
240 let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_document_symbol_provider(v:val)')
241 let l:command_id = lsp#_new_command()
243 if len(l:servers) == 0
244 call s:not_supported('Retrieving symbols')
248 for l:server in l:servers
249 call lsp#send_request(l:server, {
250 \ 'method': 'textDocument/documentSymbol',
252 \ 'textDocument': lsp#get_text_document_identifier(),
254 \ 'on_notification': function('s:handle_symbol', [l:server, l:command_id, 'documentSymbol']),
258 echo 'Retrieving document symbols ...'
261 function! s:handle_symbol(server, last_command_id, type, data) abort
262 if a:last_command_id != lsp#_last_command()
266 if lsp#client#is_error(a:data['response'])
267 call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
271 let l:list = lsp#ui#vim#utils#symbols_to_loc_list(a:server, a:data)
273 call lsp#ui#vim#utils#setqflist(l:list, a:type)
276 call lsp#utils#error('No ' . a:type .' found')
278 echo 'Retrieved ' . a:type
283 function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list, last_command_id, jump_if_one, mods, in_preview}
284 if a:ctx['last_command_id'] != lsp#_last_command()
288 let a:ctx['counter'] = a:ctx['counter'] - 1
290 if lsp#client#is_error(a:data['response']) || !has_key(a:data['response'], 'result')
291 call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
293 let a:ctx['list'] = a:ctx['list'] + lsp#utils#location#_lsp_to_vim_list(a:data['response']['result'])
296 if a:ctx['counter'] == 0
297 if empty(a:ctx['list'])
298 call lsp#utils#error('No ' . a:type .' found')
300 call lsp#utils#tagstack#_update()
302 let l:loc = a:ctx['list'][0]
304 if len(a:ctx['list']) == 1 && a:ctx['jump_if_one'] && !a:ctx['in_preview']
305 call lsp#utils#location#_open_vim_list_item(l:loc, a:ctx['mods'])
306 echo 'Retrieved ' . a:type
308 elseif !a:ctx['in_preview']
309 if get(a:ctx, 'add_tree', v:false)
310 let l:qf = getqflist({'idx' : 0, 'items': []})
312 let l:parent = l:qf.items
313 let l:level = count(l:parent[l:pos-1].text, g:lsp_tree_incoming_prefix)
314 let a:ctx['list'] = extend(l:parent, map(a:ctx['list'], 'extend(v:val, {"text": repeat("' . g:lsp_tree_incoming_prefix . '", l:level+1) . v:val.text})'), l:pos)
316 call lsp#ui#vim#utils#setqflist(a:ctx['list'], a:type)
317 echo 'Retrieved ' . a:type
319 if get(a:ctx, 'add_tree', v:false)
320 " move the cursor to the newly added item
324 let l:lines = readfile(l:loc['filename'])
325 if has_key(l:loc,'viewstart') " showing a locationLink
326 let l:view = l:lines[l:loc['viewstart'] : l:loc['viewend']]
327 call lsp#ui#vim#output#preview(a:server, l:view, {
328 \ 'statusline': ' LSP Peek ' . a:type,
329 \ 'filetype': &filetype
331 else " showing a location
332 call lsp#ui#vim#output#preview(a:server, l:lines, {
333 \ 'statusline': ' LSP Peek ' . a:type,
334 \ 'cursor': { 'line': l:loc['lnum'], 'col': l:loc['col'], 'align': g:lsp_peek_alignment },
335 \ 'filetype': &filetype
343 function! s:handle_rename_prepare(server, last_command_id, type, cword, position, data) abort
344 if a:last_command_id != lsp#_last_command()
348 if lsp#client#is_error(a:data['response'])
349 call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
352 let l:result = a:data['response']['result']
354 " Check response: null.
356 echo 'The ' . a:server . ' returns for ' . a:type . ' (The rename request may be invalid at the given position).'
360 " Check response: { defaultBehavior: boolean }.
361 if has_key(l:result, 'defaultBehavior')
362 call timer_start(1, {x->s:rename(a:server, input('new name: ', a:cword), a:position)})
366 " Check response: { placeholder: string }
367 if has_key(l:result, 'placeholder') && !empty(l:result['placeholder'])
368 call timer_start(1, {x->s:rename(a:server, input('new name: ', a:cword), a:position)})
372 " Check response: { range: Range } | Range
373 let l:range = get(l:result, 'range', l:result)
374 let l:lines = getline(1, '$')
375 let [l:start_line, l:start_col] = lsp#utils#position#lsp_to_vim('%', l:range['start'])
376 let [l:end_line, l:end_col] = lsp#utils#position#lsp_to_vim('%', l:range['end'])
377 if l:start_line ==# l:end_line
378 let l:name = l:lines[l:start_line - 1][l:start_col - 1 : l:end_col - 2]
380 let l:name = l:lines[l:start_line - 1][l:start_col - 1 :]
381 for l:i in range(l:start_line, l:end_line - 2)
382 let l:name .= "\n" . l:lines[l:i]
387 let l:name .= l:lines[l:end_line - 1][: l:end_col - 2]
391 call timer_start(1, {x->s:rename(a:server, input('new name: ', l:name), l:range['start'])})
394 function! s:handle_workspace_edit(server, last_command_id, type, data) abort
395 if a:last_command_id != lsp#_last_command()
399 if lsp#client#is_error(a:data['response'])
400 call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
404 call lsp#utils#workspace_edit#apply_workspace_edit(a:data['response']['result'])
409 function! s:handle_text_edit(server, last_command_id, type, data) abort
410 if a:last_command_id != lsp#_last_command()
414 if lsp#client#is_error(a:data['response'])
415 call lsp#utils#error('Failed to '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
419 call lsp#utils#text_edit#apply_text_edits(a:data['request']['params']['textDocument']['uri'], a:data['response']['result'])
421 redraw | echo 'Document formatted'
424 function! lsp#ui#vim#code_action(opts) abort
425 call lsp#ui#vim#code_action#do(extend({
427 \ 'selection': v:false,
432 function! lsp#ui#vim#code_lens() abort
433 call lsp#ui#vim#code_lens#do({
438 function! lsp#ui#vim#add_tree_call_hierarchy_incoming() abort
439 let l:ctx = { 'add_tree': v:true }
440 call lsp#ui#vim#call_hierarchy_incoming(l:ctx)
443 function! lsp#ui#vim#call_hierarchy_incoming(ctx) abort
444 let l:ctx = extend({ 'method': 'incomingCalls', 'key': 'from' }, a:ctx)
445 call s:prepare_call_hierarchy(l:ctx)
448 function! lsp#ui#vim#call_hierarchy_outgoing() abort
449 let l:ctx = { 'method': 'outgoingCalls', 'key': 'to' }
450 call s:prepare_call_hierarchy(l:ctx)
453 function! s:prepare_call_hierarchy(ctx) abort
454 let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_call_hierarchy_provider(v:val)')
455 let l:command_id = lsp#_new_command()
457 let l:ctx = extend({ 'counter': len(l:servers), 'list':[], 'last_command_id': l:command_id }, a:ctx)
458 if len(l:servers) == 0
459 call s:not_supported('Retrieving call hierarchy')
463 for l:server in l:servers
464 call lsp#send_request(l:server, {
465 \ 'method': 'textDocument/prepareCallHierarchy',
467 \ 'textDocument': lsp#get_text_document_identifier(),
468 \ 'position': lsp#get_position(),
470 \ 'on_notification': function('s:handle_prepare_call_hierarchy', [l:ctx, l:server, 'prepare_call_hierarchy']),
474 echo 'Preparing call hierarchy ...'
477 function! s:handle_prepare_call_hierarchy(ctx, server, type, data) abort
478 if a:ctx['last_command_id'] != lsp#_last_command()
482 if lsp#client#is_error(a:data['response']) || !has_key(a:data['response'], 'result')
483 call lsp#utils#error('Failed to '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
486 if empty(a:data['response']['result'])
487 call lsp#utils#warning('Failed to '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
491 for l:item in a:data['response']['result']
492 call s:call_hierarchy(a:ctx, a:server, l:item)
496 function! s:call_hierarchy(ctx, server, item) abort
497 call lsp#send_request(a:server, {
498 \ 'method': 'callHierarchy/' . a:ctx['method'],
502 \ 'on_notification': function('s:handle_call_hierarchy', [a:ctx, a:server, 'call_hierarchy']),
506 function! s:handle_call_hierarchy(ctx, server, type, data) abort
507 if a:ctx['last_command_id'] != lsp#_last_command()
511 let a:ctx['counter'] = a:ctx['counter'] - 1
513 if lsp#client#is_error(a:data['response']) || !has_key(a:data['response'], 'result')
514 call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
515 elseif a:data['response']['result'] isnot v:null
516 for l:item in a:data['response']['result']
517 let l:loc = s:hierarchy_item_to_vim(l:item[a:ctx['key']], a:server)
518 if l:loc isnot v:null
519 let a:ctx['list'] += [l:loc]
524 if a:ctx['counter'] == 0
525 if empty(a:ctx['list'])
526 call lsp#utils#error('No ' . a:type .' found')
528 call lsp#utils#tagstack#_update()
529 if get(a:ctx, 'add_tree', v:false)
530 let l:qf = getqflist({'idx' : 0, 'items': []})
532 let l:parent = l:qf.items
533 let l:level = count(l:parent[l:pos-1].text, g:lsp_tree_incoming_prefix)
534 let a:ctx['list'] = extend(l:parent, map(a:ctx['list'], 'extend(v:val, {"text": repeat("' . g:lsp_tree_incoming_prefix . '", l:level+1) . v:val.text})'), l:pos)
536 call lsp#ui#vim#utils#setqflist(a:ctx['list'], a:type)
537 echo 'Retrieved ' . a:type
539 if get(a:ctx, 'add_tree', v:false)
540 " move the cursor to the newly added item
547 function! s:hierarchy_item_to_vim(item, server) abort
548 let l:uri = a:item['uri']
549 if !lsp#utils#is_file_uri(l:uri)
553 let l:path = lsp#utils#uri_to_path(l:uri)
554 let [l:line, l:col] = lsp#utils#position#lsp_to_vim(l:path, a:item['range']['start'])
555 let l:text = '[' . lsp#ui#vim#utils#_get_symbol_text_from_kind(a:server, a:item['kind']) . '] ' . a:item['name']
556 if has_key(a:item, 'detail')
557 let l:text .= ": " . a:item['detail']
561 \ 'filename': l:path,