From 03febea22359b45446a34b59eba7d809e1ea4a0e Mon Sep 17 00:00:00 2001 From: Billie Cleek Date: Sun, 26 May 2019 21:40:00 -0700 Subject: [PATCH] support g:go_info_mode='gopls' in go#complete#GetInfo --- autoload/go/complete.vim | 7 +++++- autoload/go/complete_test.vim | 6 ++++++ autoload/go/lsp.vim | 40 ++++++++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/autoload/go/complete.vim b/autoload/go/complete.vim index 786b1ddf72..296c269ac2 100644 --- a/autoload/go/complete.vim +++ b/autoload/go/complete.vim @@ -79,7 +79,12 @@ endfunction " go#complete#GoInfo returns the description of the identifier under the " cursor. function! go#complete#GetInfo() abort - return s:sync_info(0) + let l:mode = go#config#InfoMode() + if l:mode == 'gopls' && go#util#has_job() + return go#lsp#GetInfo() + else + return s:sync_info(0) + endif endfunction function! go#complete#Info(showstatus) abort diff --git a/autoload/go/complete_test.vim b/autoload/go/complete_test.vim index 59334d3d4c..1c9a091ff3 100644 --- a/autoload/go/complete_test.vim +++ b/autoload/go/complete_test.vim @@ -14,6 +14,12 @@ func! Test_GetInfo_guru() unlet g:go_info_mode endfunction +func! Test_GetInfo_gopls() + let g:go_info_mode = 'gopls' + call s:getinfo() + unlet g:go_info_mode +endfunction + func! s:getinfo() let l:filename = 'complete/complete.go' let l:tmp = gotest#load_fixture(l:filename) diff --git a/autoload/go/lsp.vim b/autoload/go/lsp.vim index 691a176126..554d2b8e58 100644 --- a/autoload/go/lsp.vim +++ b/autoload/go/lsp.vim @@ -542,13 +542,32 @@ function! go#lsp#Info(showstatus) let l:state = s:newHandlerState('') endif - let l:state.handleResult = funcref('s:infoDefinitionHandler', [a:showstatus], l:state) + let l:state.handleResult = funcref('s:infoDefinitionHandler', [function('s:info', [1], l:state), a:showstatus], l:state) let l:state.error = funcref('s:noop') let l:msg = go#lsp#message#Definition(l:fname, l:line, l:col) return l:lsp.sendMessage(l:msg, l:state) endfunction -function! s:infoDefinitionHandler(showstatus, msg) abort dict +function! go#lsp#GetInfo() + let l:fname = expand('%:p') + let [l:line, l:col] = getpos('.')[1:2] + + call go#lsp#DidChange(l:fname) + + let l:lsp = s:lspfactory.get() + + let l:state = s:newHandlerState('') + + let l:info = go#promise#New(function('s:info', [0], l:state), 10000, '') + + let l:state.handleResult = funcref('s:infoDefinitionHandler', [l:info.wrapper, 0], l:state) + let l:state.error = funcref('s:noop') + let l:msg = go#lsp#message#Definition(l:fname, l:line, l:col) + call l:lsp.sendMessage(l:msg, l:state) + return l:info.await() +endfunction + +function! s:infoDefinitionHandler(next, showstatus, msg) abort dict " gopls returns a []Location; just take the first one. let l:msg = a:msg[0] @@ -565,12 +584,22 @@ function! s:infoDefinitionHandler(showstatus, msg) abort dict let l:state = s:newHandlerState('') endif - let l:state.handleResult = funcref('s:hoverHandler', [function('s:info', [], l:state)], l:state) + let l:state.handleResult = funcref('s:hoverHandler', [a:next], l:state) let l:state.error = funcref('s:noop') return l:lsp.sendMessage(l:msg, l:state) endfunction -function! s:info(content) abort dict +function! s:info(show, content) abort dict + let l:content = s:infoFromHoverContent(a:content) + + if a:show + call go#util#ShowInfo(l:content) + endif + + return l:content +endfunction + +function! s:infoFromHoverContent(content) abort let l:content = a:content[0] " strip godoc summary @@ -580,7 +609,8 @@ function! s:info(content) abort dict if l:content =~# '^type [^ ]\+ \(struct\|interface\)' let l:content = substitute(l:content, '{.*', '', '') endif - call go#util#ShowInfo(l:content) + + return l:content endfunction " restore Vi compatibility settings