Skip to content

Commit 592269a

Browse files
authored
Merge pull request #2519 from bhcleek/lsp/sameids
sameids: use gopls instead of guru to find same ids
2 parents e98fef8 + f5759fb commit 592269a

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

autoload/go/guru.vim

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -428,18 +428,9 @@ function! go#guru#SameIds(showstatus) abort
428428
return
429429
endif
430430

431-
let args = {
432-
\ 'mode': 'what',
433-
\ 'format': 'json',
434-
\ 'selected': -1,
435-
\ 'needs_scope': 0,
436-
\ 'custom_parse': function('s:same_ids_highlight'),
437-
\ }
438-
if !a:showstatus
439-
let args.disable_progress = 1
440-
endif
441-
442-
call s:run_guru(args)
431+
let [l:line, l:col] = getpos('.')[1:2]
432+
let [l:line, l:col] = go#lsp#lsp#Position(l:line, l:col)
433+
call go#lsp#SameIDs(0, expand('%:p'), l:line, l:col, funcref('s:same_ids_highlight'))
443434
endfunction
444435

445436
function! s:same_ids_highlight(exit_val, output, mode) abort

autoload/go/lsp.vim

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,55 @@ function! s:completionErrorHandler(next, error) abort dict
605605
call call(a:next, [-1, []])
606606
endfunction
607607

608+
" go#lsp#Type calls gopls to get the type definition of the identifier at
609+
" line and col in fname. handler should be a dictionary function that takes a
610+
" list of strings in the form 'file:line:col: message'. handler will be
611+
" attached to a dictionary that manages state (statuslines, sets the winid,
612+
" etc.)
613+
function! go#lsp#SameIDs(showstatus, fname, line, col, handler) abort
614+
call go#lsp#DidChange(a:fname)
615+
616+
let l:lsp = s:lspfactory.get()
617+
let l:msg = go#lsp#message#References(a:fname, a:line, a:col)
618+
619+
if a:showstatus
620+
let l:state = s:newHandlerState('same ids')
621+
else
622+
let l:state = s:newHandlerState('')
623+
endif
624+
625+
let l:state.handleResult = funcref('s:sameIDsHandler', [function(a:handler, [], l:state)], l:state)
626+
let l:state.error = funcref('s:noop')
627+
return l:lsp.sendMessage(l:msg, l:state)
628+
endfunction
629+
630+
function! s:sameIDsHandler(next, msg) abort dict
631+
let l:furi = go#path#ToURI(expand('%:p'))
632+
633+
let l:result = {
634+
\ 'sameids': [],
635+
\ 'enclosing': [],
636+
\ }
637+
638+
for l:loc in a:msg
639+
if l:loc.uri !=# l:furi
640+
continue
641+
endif
642+
643+
if len(l:result.enclosing) == 0
644+
let l:result.enclosing = [{
645+
\ 'desc': 'identifier',
646+
\ 'start': l:loc.range.start.character+1,
647+
\ 'end': l:loc.range.end.character+1,
648+
\ }]
649+
endif
650+
651+
let l:result.sameids = add(l:result.sameids, printf('%s:%s:%s', go#path#FromURI(l:loc.uri), l:loc.range.start.line+1, l:loc.range.start.character+1))
652+
endfor
653+
654+
call call(a:next, [0, json_encode(l:result), ''])
655+
endfunction
656+
608657
function! go#lsp#Hover(fname, line, col, handler) abort
609658
call go#lsp#DidChange(a:fname)
610659

autoload/go/lsp/message.vim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ function! go#lsp#message#Completion(file, line, col) abort
136136
\ }
137137
endfunction
138138

139+
function! go#lsp#message#References(file, line, col) abort
140+
return {
141+
\ 'notification': 0,
142+
\ 'method': 'textDocument/references',
143+
\ 'params': {
144+
\ 'textDocument': {
145+
\ 'uri': go#path#ToURI(a:file)
146+
\ },
147+
\ 'position': s:position(a:line, a:col),
148+
\ 'context': {
149+
\ 'includeDeclaration': v:true,
150+
\ },
151+
\ }
152+
\ }
153+
endfunction
154+
139155
function! go#lsp#message#Hover(file, line, col) abort
140156
return {
141157
\ 'notification': 0,

0 commit comments

Comments
 (0)