Skip to content

Commit abb1d79

Browse files
authored
Merge pull request #2566 from bhcleek/references/choice
Add an option to choose between guru and gopls for :GoReferrers
2 parents dfa71c9 + ac1b780 commit abb1d79

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

autoload/go/config.vim

+4
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,10 @@ function! go#config#Updatetime() abort
485485
return go_updatetime == 0 ? &updatetime : go_updatetime
486486
endfunction
487487

488+
function! go#config#ReferrersMode() abort
489+
return get(g:, 'go_referrers_mode', 'gopls')
490+
endfunction
491+
488492
" Set the default value. A value of "1" is a shortcut for this, for
489493
" compatibility reasons.
490494
if exists("g:go_gorename_prefill") && g:go_gorename_prefill == 1

autoload/go/guru.vim

+20-4
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,26 @@ endfunction
404404

405405
" Show all refs to entity denoted by selected identifier
406406
function! go#guru#Referrers(selected) abort
407-
let [l:line, l:col] = getpos('.')[1:2]
408-
let [l:line, l:col] = go#lsp#lsp#Position(l:line, l:col)
409-
let l:fname = expand('%:p')
410-
call go#lsp#Referrers(l:fname, l:line, l:col, funcref('s:parse_guru_output'))
407+
let l:mode = go#config#ReferrersMode()
408+
if l:mode == 'guru'
409+
let args = {
410+
\ 'mode': 'referrers',
411+
\ 'format': 'plain',
412+
\ 'selected': a:selected,
413+
\ 'needs_scope': 0,
414+
\ }
415+
416+
call s:run_guru(args)
417+
return
418+
elseif l:mode == 'gopls'
419+
let [l:line, l:col] = getpos('.')[1:2]
420+
let [l:line, l:col] = go#lsp#lsp#Position(l:line, l:col)
421+
let l:fname = expand('%:p')
422+
call go#lsp#Referrers(l:fname, l:line, l:col, funcref('s:parse_guru_output'))
423+
return
424+
else
425+
call go#util#EchoWarning('unknown value for g:go_referrers_mode')
426+
endif
411427
endfunction
412428

413429
function! go#guru#SameIds(showstatus) abort

autoload/go/lsp.vim

+32-3
Original file line numberDiff line numberDiff line change
@@ -681,17 +681,26 @@ endfunction
681681
function! s:referencesHandler(next, msg) abort dict
682682
let l:result = []
683683

684+
call sort(a:msg, funcref('s:compareLocations'))
685+
684686
for l:loc in a:msg
685687
let l:fname = go#path#FromURI(l:loc.uri)
686688
let l:line = l:loc.range.start.line+1
687689
let l:bufnr = bufnr(l:fname)
690+
let l:bufinfo = getbufinfo(l:fname)
688691

689692
try
690-
if l:bufnr == -1
691-
let l:content = readfile(l:fname, '', l:line)[-1]
693+
if l:bufnr == -1 || len(l:bufinfo) == 0 || l:bufinfo[0].loaded == 0
694+
let l:filecontents = readfile(l:fname, '', l:line)
692695
else
693-
let l:content = getbufline(l:fname, l:line)[-1]
696+
let l:filecontents = getbufline(l:fname, l:line)
697+
endif
698+
699+
if len(l:filecontents) == 0
700+
continue
694701
endif
702+
703+
let l:content = l:filecontents[-1]
695704
catch
696705
call go#util#EchoError(printf('%s (line %s): %s at %s', l:fname, l:line, v:exception, v:throwpoint))
697706
endtry
@@ -969,6 +978,26 @@ function! s:debug(event, data, ...) abort
969978
call timer_start(10, function('s:debugasync', [a:event, a:data]))
970979
endfunction
971980

981+
function! s:compareLocations(left, right) abort
982+
if a:left.uri < a:right.uri
983+
return -1
984+
endif
985+
986+
if a:left.uri == a:right.uri && a:left.range.start.line < a:right.range.start.line
987+
return -1
988+
endif
989+
990+
if a:left.uri == a:right.uri && a:left.range.start.line == a:right.range.start.line && a:left.range.start.character < a:right.range.start.character
991+
return -1
992+
endif
993+
994+
if a:left.uri == a:right.uri && a:left.range.start.line == a:right.range.start.line && a:left.range.start.character == a:right.range.start.character
995+
return 0
996+
endif
997+
998+
return 1
999+
endfunction
1000+
9721001
" restore Vi compatibility settings
9731002
let &cpo = s:cpo_save
9741003
unlet s:cpo_save

doc/vim-go.txt

+10
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,16 @@ accuracy or `godef` for its performance. Valid options are `godef`, `gopls`,
14041404
and `guru`.
14051405
>
14061406
let g:go_def_mode = 'gopls'
1407+
<
1408+
*'g:go_referrers_mode'*
1409+
1410+
Use this option to define the command to be used for |:GoReferrers|. By
1411+
default `gopls` is used, because it is the fastest and works with Go modules.
1412+
One might also use `guru` for its ability to show references from other
1413+
packages. This option will be removed after `gopls` can show references from
1414+
other packages. Valid options are `gopls` and `guru`.
1415+
>
1416+
let g:go_referrers_mode = 'gopls'
14071417
<
14081418
*'g:go_def_mapping_enabled'*
14091419

0 commit comments

Comments
 (0)