Skip to content

Commit

Permalink
Merge pull request #2224 from bhcleek/lsp/go-info
Browse files Browse the repository at this point in the history
add a gopls option for g:go_info_mode
  • Loading branch information
bhcleek committed Apr 13, 2019
2 parents 9a7cf4a + d1ebea1 commit 94fe300
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
33 changes: 17 additions & 16 deletions autoload/go/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,6 @@ endfunction
function! s:async_info(echo, showstatus)
let state = {'echo': a:echo}

function! s:complete(job, exit_status, messages) abort dict
if a:exit_status != 0
return
endif

if &encoding != 'utf-8'
let i = 0
while i < len(a:messages)
let a:messages[i] = iconv(a:messages[i], 'utf-8', &encoding)
let i += 1
endwhile
endif

let result = s:info_filter(self.echo, join(a:messages, "\n"))
call s:info_complete(self.echo, result)
endfunction
" explicitly bind complete to state so that within it, self will
" always refer to state. See :help Partial for more information.
let state.complete = function('s:complete', [], state)
Expand Down Expand Up @@ -151,6 +135,23 @@ function! s:async_info(echo, showstatus)
call go#job#Start(cmd, opts)
endfunction

function! s:complete(job, exit_status, messages) abort dict
if a:exit_status != 0
return
endif

if &encoding != 'utf-8'
let i = 0
while i < len(a:messages)
let a:messages[i] = iconv(a:messages[i], 'utf-8', &encoding)
let i += 1
endwhile
endif

let result = s:info_filter(self.echo, join(a:messages, "\n"))
call s:info_complete(self.echo, result)
endfunction

function! s:gocodeFile()
let file = tempname()
call writefile(go#util#GetLines(), file)
Expand Down
2 changes: 1 addition & 1 deletion autoload/go/guru.vim
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function! go#guru#DescribeInfo(showstatus) abort
\ 'selected': -1,
\ 'needs_scope': 0,
\ 'custom_parse': function('s:info'),
\ 'disable_progress': 1,
\ 'disable_progress': a:showstatus == 0,
\ }

call s:run_guru(args)
Expand Down
49 changes: 47 additions & 2 deletions autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ function! s:newlsp() abort

if has_key(l:response, 'error')
call l:handler.requestComplete(0)
call go#util#EchoError(l:response.error.message)
if has_key(l:handler, 'error')
call call(l:handler.error, [l:response.error.message])
else
call go#util#EchoError(l:response.error.message)
endif
return
endif
Expand Down Expand Up @@ -251,7 +252,7 @@ function! s:newlsp() abort
return l:lsp
endfunction

function! s:noop() abort
function! s:noop(...) abort
endfunction

function! s:newHandlerState(statustype) abort
Expand Down Expand Up @@ -382,6 +383,13 @@ function! go#lsp#DidOpen(fname) abort
endfunction

function! go#lsp#DidChange(fname) abort
" DidChange is called even when fname isn't open in a buffer (e.g. via
" go#lsp#Info); don't report the file as open or as having changed when it's
" not actually a buffer.
if bufnr(a:fname) == -1
return
endif

call go#lsp#DidOpen(a:fname)

if !filereadable(a:fname)
Expand Down Expand Up @@ -454,6 +462,7 @@ function! go#lsp#Hover(fname, line, col, handler) abort
let l:msg = go#lsp#message#Hover(a:fname, a:line, a:col)
let l:state = s:newHandlerState('hover')
let l:state.handleResult = funcref('s:hoverHandler', [function(a:handler, [], l:state)], l:state)
let l:state.error = funcref('s:noop')
call l:lsp.sendMessage(l:msg, l:state)
endfunction

Expand All @@ -464,6 +473,42 @@ function! s:hoverHandler(next, msg) abort dict
call call(a:next, l:args)
endfunction

function! go#lsp#Info(showstatus)
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:state.handleResult = funcref('s:infoDefinitionHandler', [function('s:info', [])], 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)
endfunction

function! s:infoDefinitionHandler(next, msg) abort dict
" gopls returns a []Location; just take the first one.
let l:msg = a:msg[0]

let l:fname = go#path#FromURI(l:msg.uri)
let l:line = l:msg.range.start.line+1
let l:col = l:msg.range.start.character+1

let l:lsp = s:lspfactory.get()
let l:msg = go#lsp#message#Hover(l:fname, l:line, l:col)
let l:state = s:newHandlerState('info')
let l:state.handleResult = funcref('s:hoverHandler', [function('s:info', [], l:state)], l:state)
let l:state.error = funcref('s:noop')
call l:lsp.sendMessage(l:msg, l:state)
endfunction

function! s:info(content) abort dict
" strip off the method set and fields of structs and interfaces.
let l:content = substitute(a:content, '{.*', '', '')
call go#util#ShowInfo(l:content)
endfunction

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
Expand Down
1 change: 0 additions & 1 deletion autoload/go/lsp/message.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function! go#lsp#message#Definition(file, line, col) abort
\ }
endfunction


function! go#lsp#message#TypeDefinition(file, line, col) abort
return {
\ 'notification': 0,
Expand Down
4 changes: 3 additions & 1 deletion autoload/go/tool.vim
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ function! go#tool#Info(showstatus) abort
call go#complete#Info(a:showstatus)
elseif l:mode == 'guru'
call go#guru#DescribeInfo(a:showstatus)
elseif l:mode == 'gopls'
call go#lsp#Info(a:showstatus)
else
call go#util#EchoError('go_info_mode value: '. l:mode .' is not valid. Valid values are: [gocode, guru]')
call go#util#EchoError('go_info_mode value: '. l:mode .' is not valid. Valid values are: [gocode, guru, gopls]')
endif
endfunction

Expand Down
4 changes: 2 additions & 2 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1251,8 +1251,8 @@ updated. By default it's disabled. The delay can be configured with the

Use this option to define the command to be used for |:GoInfo|. By default
`gocode` is being used as it's the fastest option. But one might also use
`guru` as it's covers more cases and is more accurate. Current valid options
are: `[gocode, guru]` >
`gopls` or `guru` as they cover more cases and are more accurate. Current
valid options are: `[gocode, guru, gopls]` >
let g:go_info_mode = 'gocode'
<
Expand Down

0 comments on commit 94fe300

Please sign in to comment.