Skip to content

Commit 7233bb2

Browse files
Add LspAddTreeReferences (#1484)
1 parent aa93b2a commit 7233bb2

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

autoload/lsp/ui/vim.vim

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ function! lsp#ui#vim#definition(in_preview, ...) abort
3535
call s:list_location('definition', l:ctx)
3636
endfunction
3737

38-
function! lsp#ui#vim#references() abort
39-
let l:ctx = { 'jump_if_one': 0 }
38+
function! lsp#ui#vim#references(ctx) abort
39+
let l:ctx = extend({ 'jump_if_one': 0 }, a:ctx)
4040
let l:request_params = { 'context': { 'includeDeclaration': v:false } }
4141
call s:list_location('references', l:ctx, l:request_params)
4242
endfunction
4343

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)
47+
endfunction
48+
4449
function! s:list_location(method, ctx, ...) abort
4550
" typeDefinition => type definition
4651
let l:operation = substitute(a:method, '\u', ' \l\0', 'g')
@@ -307,10 +312,21 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list
307312
echo 'Retrieved ' . a:type
308313
redraw
309314
elseif !a:ctx['in_preview']
315+
if get(a:ctx, 'add_tree', v:false)
316+
let l:qf = getqflist({'idx' : 0, 'items': []})
317+
let l:pos = l:qf.idx
318+
let l:parent = l:qf.items
319+
let l:level = count(l:parent[l:pos-1].text, g:lsp_tree_incoming_prefix)
320+
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)
321+
endif
310322
call setqflist([])
311323
call setqflist(a:ctx['list'])
312324
echo 'Retrieved ' . a:type
313325
botright copen
326+
if get(a:ctx, 'add_tree', v:false)
327+
" move the cursor to the newly added item
328+
execute l:pos + 1
329+
endif
314330
else
315331
let l:lines = readfile(l:loc['filename'])
316332
if has_key(l:loc,'viewstart') " showing a locationLink

doc/vim-lsp.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ CONTENTS *vim-lsp-contents*
114114
lsp#document_hover_preview_winid() |lsp#document_hover_preview_winid()|
115115
Commands |vim-lsp-commands|
116116
LspAddTreeCallHierarchyIncoming |:LspAddTreeCallHierarchyIncoming|
117+
LspAddTreeReferences |:LspAddTreeReferences|
117118
LspCallHierarchyIncoming |:LspCallHierarchyIncoming|
118119
LspCallHierarchyOutgoing |:LspCallHierarchyOutgoing|
119120
LspCodeAction |:LspCodeAction|
@@ -877,7 +878,9 @@ g:lsp_tree_incoming_prefix *g:lsp_tree_incoming_prefix*
877878
Type: |String|
878879
Default: `"<= "`
879880

880-
Specifies the prefix of items added by |LspAddTreeCallHierarchyIncoming|.
881+
Specifies the prefix of items added by following commands.
882+
* |LspAddTreeCallHierarchyIncoming|
883+
* |LspAddTreeReferences|
881884

882885
Example: >
883886
let g:lsp_tree_incoming_prefix = "← "
@@ -1680,6 +1683,11 @@ LspAddTreeCallHierarchyIncoming *:LspAddTreeCallHierarchyIncoming*
16801683
Just like |LspCallHierarchyIncoming| , but instead of making a new list the
16811684
result is appended to the current list.
16821685

1686+
LspAddTreeReferences *:LspAddTreeReferences*
1687+
1688+
Just like |LspReferences| , but instead of making a new list the result is
1689+
appended to the current list.
1690+
16831691
LspCallHierarchyIncoming *:LspCallHierarchyIncoming*
16841692

16851693
Find incoming call hierarchy for the symbol under cursor.

plugin/lsp.vim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ command! -nargs=* LspNextWarning call lsp#internal#diagnostics#movement#_next_wa
128128
command! -nargs=* LspPreviousWarning call lsp#internal#diagnostics#movement#_previous_warning(<f-args>)
129129
command! -nargs=* LspNextDiagnostic call lsp#internal#diagnostics#movement#_next_diagnostics(<f-args>)
130130
command! -nargs=* LspPreviousDiagnostic call lsp#internal#diagnostics#movement#_previous_diagnostics(<f-args>)
131-
command! LspReferences call lsp#ui#vim#references()
131+
command! LspReferences call lsp#ui#vim#references({})
132+
command! LspAddTreeReferences call lsp#ui#vim#add_tree_references()
132133
command! LspRename call lsp#ui#vim#rename()
133134
command! LspTypeDefinition call lsp#ui#vim#type_definition(0, <q-mods>)
134135
command! LspTypeHierarchy call lsp#internal#type_hierarchy#show()
@@ -189,7 +190,7 @@ nnoremap <silent> <plug>(lsp-next-diagnostic) :<c-u>call lsp#internal#diagnostic
189190
nnoremap <silent> <plug>(lsp-next-diagnostic-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_next_diagnostics("-wrap=0")<cr>
190191
nnoremap <silent> <plug>(lsp-previous-diagnostic) :<c-u>call lsp#internal#diagnostics#movement#_previous_diagnostics()<cr>
191192
nnoremap <silent> <plug>(lsp-previous-diagnostic-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_previous_diagnostics("-wrap=0")<cr>
192-
nnoremap <silent> <plug>(lsp-references) :<c-u>call lsp#ui#vim#references()<cr>
193+
nnoremap <silent> <plug>(lsp-references) :<c-u>call lsp#ui#vim#references({})<cr>
193194
nnoremap <silent> <plug>(lsp-rename) :<c-u>call lsp#ui#vim#rename()<cr>
194195
nnoremap <silent> <plug>(lsp-type-definition) :<c-u>call lsp#ui#vim#type_definition(0)<cr>
195196
nnoremap <silent> <plug>(lsp-type-hierarchy) :<c-u>call lsp#internal#type_hierarchy#show()<cr>

0 commit comments

Comments
 (0)