Skip to content

Commit 906bd2e

Browse files
Christian Clasonthomasfaingnaert
authored andcommitted
Use locationLink for definition preview (#498)
* use locationLink for definition preview * remove cursor setting for location link * make vint happy * also advertise support for declaration, typeDefinition, implementation * small refactor * refactor to avoid code duplication * restore lost check for file
1 parent b68bc8b commit 906bd2e

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

autoload/lsp.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,18 @@ function! lsp#default_get_supported_capabilities(server_info) abort
390390
\ 'valueSet': lsp#omni#get_completion_item_kinds()
391391
\ }
392392
\ },
393+
\ 'declaration': {
394+
\ 'linkSupport' : v:true
395+
\ },
396+
\ 'definition': {
397+
\ 'linkSupport' : v:true
398+
\ },
399+
\ 'typeDefinition': {
400+
\ 'linkSupport' : v:true
401+
\ },
402+
\ 'implementation': {
403+
\ 'linkSupport' : v:true
404+
\ },
393405
\ 'documentSymbol': {
394406
\ 'symbolKind': {
395407
\ 'valueSet': lsp#ui#vim#utils#get_symbol_kinds()

autoload/lsp/ui/vim.vim

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,19 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list
488488
botright copen
489489
else
490490
let l:lines = readfile(fnameescape(l:loc['filename']))
491-
call lsp#ui#vim#output#preview(a:server, l:lines, {
492-
\ 'statusline': ' LSP Peek ' . a:type,
493-
\ 'cursor': { 'line': l:loc['lnum'], 'col': l:loc['col'], 'align': g:lsp_peek_alignment },
494-
\ 'filetype': &filetype
495-
\ })
491+
if has_key(l:loc,'viewstart') " showing a locationLink
492+
let l:view = l:lines[l:loc['viewstart'] : l:loc['viewend']]
493+
call lsp#ui#vim#output#preview(a:server, l:view, {
494+
\ 'statusline': ' LSP Peek ' . a:type,
495+
\ 'filetype': &filetype
496+
\ })
497+
else " showing a location
498+
call lsp#ui#vim#output#preview(a:server, l:lines, {
499+
\ 'statusline': ' LSP Peek ' . a:type,
500+
\ 'cursor': { 'line': l:loc['lnum'], 'col': l:loc['col'], 'align': g:lsp_peek_alignment },
501+
\ 'filetype': &filetype
502+
\ })
503+
endif
496504
endif
497505
endif
498506
endif

autoload/lsp/ui/vim/utils.vim

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,26 @@ function! lsp#ui#vim#utils#locations_to_loc_list(result) abort
77

88
let l:locations = type(a:result['response']['result']) == type({}) ? [a:result['response']['result']] : a:result['response']['result']
99

10+
if has_key(l:locations[0],'targetUri') " server returns locationLinks
11+
let l:use_link = 1
12+
let l:uri = 'targetUri'
13+
let l:range = 'targetSelectionRange'
14+
else
15+
let l:use_link = 0
16+
let l:uri = 'uri'
17+
let l:range = 'range'
18+
endif
19+
1020
if !empty(l:locations) " some servers also return null so check to make sure it isn't empty
1121
let l:cache={}
1222
for l:location in l:locations
13-
if s:is_file_uri(l:location['uri'])
14-
let l:path = lsp#utils#uri_to_path(l:location['uri'])
15-
let l:line = l:location['range']['start']['line'] + 1
16-
let l:char = l:location['range']['start']['character']
23+
if s:is_file_uri(l:location[l:uri])
24+
let l:path = lsp#utils#uri_to_path(l:location[l:uri])
25+
let l:line = l:location[l:range]['start']['line'] + 1
26+
let l:char = l:location[l:range]['start']['character']
1727
let l:col = lsp#utils#to_col(l:path, l:line, l:char)
18-
let l:index = l:line - 1
1928

29+
let l:index = l:line - 1
2030
if has_key(l:cache, l:path)
2131
let l:text = l:cache[l:path][l:index]
2232
else
@@ -29,12 +39,25 @@ function! lsp#ui#vim#utils#locations_to_loc_list(result) abort
2939
let l:text = l:contents[l:index]
3040
endif
3141
endif
32-
call add(l:list, {
33-
\ 'filename': l:path,
34-
\ 'lnum': l:line,
35-
\ 'col': l:col,
36-
\ 'text': l:text,
37-
\ })
42+
if l:use_link
43+
let l:viewstart = l:location['targetRange']['start']['line']
44+
let l:viewend = l:location['targetRange']['end']['line']
45+
call add(l:list, {
46+
\ 'filename': l:path,
47+
\ 'lnum': l:line,
48+
\ 'col': l:col,
49+
\ 'text': l:text,
50+
\ 'viewstart': l:viewstart,
51+
\ 'viewend': l:viewend
52+
\ })
53+
else
54+
call add(l:list, {
55+
\ 'filename': l:path,
56+
\ 'lnum': l:line,
57+
\ 'col': l:col,
58+
\ 'text': l:text,
59+
\ })
60+
endif
3861
endif
3962
endfor
4063
endif

0 commit comments

Comments
 (0)