Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add g:go:_info_mode='gopls' support to go#complete#GetInfo #2313

Merged
merged 3 commits into from
Jun 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion autoload/go/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 19 additions & 9 deletions autoload/go/complete_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@
let s:cpo_save = &cpo
set cpo&vim

func! Test_GetInfo()
func! Test_GetInfo_gocode()
let g:go_info_mode = 'gocode'
call s:getinfo()
unlet g:go_info_mode
endfunction

func! Test_GetInfo_guru()
let g:go_info_mode = 'guru'
call s:getinfo()
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)

call cursor(8, 3)

let g:go_info_mode = 'gocode'
let expected = 'func Example(s string)'
let actual = go#complete#GetInfo()
call assert_equal(expected, actual)

let g:go_info_mode = 'guru'
call go#config#InfoMode()
let actual = go#complete#GetInfo()
call assert_equal(expected, actual)

unlet g:go_info_mode
endfunction

" restore Vi compatibility settings
Expand Down
40 changes: 35 additions & 5 deletions autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand All @@ -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
Expand Down
50 changes: 50 additions & 0 deletions autoload/go/promise.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim

scriptencoding utf-8

" New returns a promise. A promise's primary purpose is to make async jobs
" synchronous by awaiting fn.
"
" A promise is a dictionary with two keys:
" 'wrapper':
" A function that wraps fn. It can be used in place of fn.
" 'await':
" A function that waits for wrapper to be called and returns the value
" returned by fn. Returns default if timeout expires.
function! go#promise#New(fn, timeout, default) abort
let l:state = {}

" explicitly bind to state so that within l:promise's methods, self will
" always refer to state. See :help Partial for more information.
return {
\ 'wrapper': function('s:wrapper', [a:fn], l:state),
\ 'await': function('s:await', [a:timeout, a:default], l:state),
\ }
endfunction

function! s:wrapper(fn, ...) dict
let self.retval = call(a:fn, a:000)
return self.retval
endfunction

function! s:await(timeout, default) dict
let l:timer = timer_start(a:timeout, function('s:setretval', [a:default], self))
while !has_key(self, 'retval')
sleep 50m
endwhile
call timer_stop(l:timer)

return self.retval
endfunction

function! s:setretval(val, timer) dict
let self.retval = a:val
endfunction

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save

" vim: sw=2 ts=2 et
41 changes: 41 additions & 0 deletions autoload/go/promise_test.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim

func! Test_PromiseNew() abort
let l:sut = go#promise#New(function('s:work', []), 100, -1)
call assert_true(has_key(l:sut, 'wrapper'))
call assert_true(has_key(l:sut, 'await'))
endfunc

func! Test_PromiseAwait() abort
let l:expected = 1
let l:default = -1
let l:sut = go#promise#New(function('s:work', [l:expected]), 100, l:default)

call timer_start(10, l:sut.wrapper)

let l:actual = call(l:sut.await, [])
call assert_equal(l:expected, l:actual)
endfunc

func! Test_PromiseAwait_Timeout() abort
let l:desired = 1
let l:expected = -1
let l:sut = go#promise#New(function('s:work', [l:desired]), 10, l:expected)

call timer_start(100, l:sut.wrapper)

let l:actual = call(l:sut.await, [])
call assert_equal(l:expected, l:actual)
endfunc

func! s:work(val, timer)
return a:val
endfunc

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save

" vim: sw=2 ts=2 et