Skip to content

Commit

Permalink
Merge pull request #2566 from bhcleek/references/choice
Browse files Browse the repository at this point in the history
Add an option to choose between guru and gopls for :GoReferrers
  • Loading branch information
bhcleek authored Nov 8, 2019
2 parents dfa71c9 + ac1b780 commit abb1d79
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
4 changes: 4 additions & 0 deletions autoload/go/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ function! go#config#Updatetime() abort
return go_updatetime == 0 ? &updatetime : go_updatetime
endfunction

function! go#config#ReferrersMode() abort
return get(g:, 'go_referrers_mode', 'gopls')
endfunction

" Set the default value. A value of "1" is a shortcut for this, for
" compatibility reasons.
if exists("g:go_gorename_prefill") && g:go_gorename_prefill == 1
Expand Down
24 changes: 20 additions & 4 deletions autoload/go/guru.vim
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,26 @@ endfunction

" Show all refs to entity denoted by selected identifier
function! go#guru#Referrers(selected) abort
let [l:line, l:col] = getpos('.')[1:2]
let [l:line, l:col] = go#lsp#lsp#Position(l:line, l:col)
let l:fname = expand('%:p')
call go#lsp#Referrers(l:fname, l:line, l:col, funcref('s:parse_guru_output'))
let l:mode = go#config#ReferrersMode()
if l:mode == 'guru'
let args = {
\ 'mode': 'referrers',
\ 'format': 'plain',
\ 'selected': a:selected,
\ 'needs_scope': 0,
\ }

call s:run_guru(args)
return
elseif l:mode == 'gopls'
let [l:line, l:col] = getpos('.')[1:2]
let [l:line, l:col] = go#lsp#lsp#Position(l:line, l:col)
let l:fname = expand('%:p')
call go#lsp#Referrers(l:fname, l:line, l:col, funcref('s:parse_guru_output'))
return
else
call go#util#EchoWarning('unknown value for g:go_referrers_mode')
endif
endfunction

function! go#guru#SameIds(showstatus) abort
Expand Down
35 changes: 32 additions & 3 deletions autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -681,17 +681,26 @@ endfunction
function! s:referencesHandler(next, msg) abort dict
let l:result = []

call sort(a:msg, funcref('s:compareLocations'))

for l:loc in a:msg
let l:fname = go#path#FromURI(l:loc.uri)
let l:line = l:loc.range.start.line+1
let l:bufnr = bufnr(l:fname)
let l:bufinfo = getbufinfo(l:fname)

try
if l:bufnr == -1
let l:content = readfile(l:fname, '', l:line)[-1]
if l:bufnr == -1 || len(l:bufinfo) == 0 || l:bufinfo[0].loaded == 0
let l:filecontents = readfile(l:fname, '', l:line)
else
let l:content = getbufline(l:fname, l:line)[-1]
let l:filecontents = getbufline(l:fname, l:line)
endif

if len(l:filecontents) == 0
continue
endif

let l:content = l:filecontents[-1]
catch
call go#util#EchoError(printf('%s (line %s): %s at %s', l:fname, l:line, v:exception, v:throwpoint))
endtry
Expand Down Expand Up @@ -969,6 +978,26 @@ function! s:debug(event, data, ...) abort
call timer_start(10, function('s:debugasync', [a:event, a:data]))
endfunction

function! s:compareLocations(left, right) abort
if a:left.uri < a:right.uri
return -1
endif

if a:left.uri == a:right.uri && a:left.range.start.line < a:right.range.start.line
return -1
endif

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
return -1
endif

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
return 0
endif

return 1
endfunction

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
Expand Down
10 changes: 10 additions & 0 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,16 @@ accuracy or `godef` for its performance. Valid options are `godef`, `gopls`,
and `guru`.
>
let g:go_def_mode = 'gopls'
<
*'g:go_referrers_mode'*

Use this option to define the command to be used for |:GoReferrers|. By
default `gopls` is used, because it is the fastest and works with Go modules.
One might also use `guru` for its ability to show references from other
packages. This option will be removed after `gopls` can show references from
other packages. Valid options are `gopls` and `guru`.
>
let g:go_referrers_mode = 'gopls'
<
*'g:go_def_mapping_enabled'*

Expand Down

0 comments on commit abb1d79

Please sign in to comment.