Skip to content

Commit

Permalink
Merge pull request #2704 from bhcleek/guru/module-aware-mode
Browse files Browse the repository at this point in the history
guru: do not hookup guru commands when not in GOPATH
  • Loading branch information
bhcleek authored Nov 13, 2020
2 parents 5ae98e0 + ef2f24b commit 9ce65bd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
4 changes: 4 additions & 0 deletions autoload/go/guru.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ set cpo&vim
" example output:
" {'cmd' : ['guru', '-json', 'implements', 'demo/demo.go:#66']}
function! s:guru_cmd(args) range abort
if !go#package#InGOPATH()
return {'err': 'guru only supports packages within GOPATH'}
endif
let mode = a:args.mode

let format = a:args.format
let needs_scope = a:args.needs_scope
let selected = a:args.selected
Expand Down
29 changes: 29 additions & 0 deletions autoload/go/package.vim
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,35 @@ function! go#package#ImportPath() abort
return l:importpath
endfunction

let s:in_gopath = {}
" InGOPATH returns TRUE when the package of the current buffer is within
" GOPATH.
function! go#package#InGOPATH() abort
let l:dir = expand("%:p:h")
if has_key(s:in_gopath, dir)
return s:in_gopath[l:dir][0] !=# '_'
endif

try
" turn off module support so that `go list` will report the package name
" with a leading '_' when the current buffer is not within GOPATH.
let Restore_modules = go#util#SetEnv('GO111MODULE', 'off')
let [l:out, l:err] = go#util#ExecInDir(['go', 'list'])
if l:err != 0
return 0
endif

let l:importpath = split(l:out, '\n')[0]
if len(l:importpath) > 0
let s:in_gopath[l:dir] = l:importpath
endif
finally
call call(Restore_modules, [])
endtry

return len(l:importpath) > 0 && l:importpath[0] !=# '_'
endfunction

" go#package#FromPath returns the import path of arg. -1 is returned when arg
" does not specify a package. -2 is returned when arg is a relative path
" outside of GOPATH, not in a module, and not below the current working
Expand Down
23 changes: 13 additions & 10 deletions ftplugin/go/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
command! -nargs=? -complete=customlist,go#rename#Complete GoRename call go#rename#Rename(<bang>0, <f-args>)

" -- guru
command! -nargs=* -complete=customlist,go#package#Complete GoGuruScope call go#guru#Scope(<f-args>)
" do not configure commands that _require_ guru when not in GOPATH mode.
if go#package#InGOPATH()
command! -nargs=* -complete=customlist,go#package#Complete GoGuruScope call go#guru#Scope(<f-args>)
command! -range=% GoPointsTo call go#guru#PointsTo(<count>)
command! -range=% GoWhicherrs call go#guru#Whicherrs(<count>)
command! -range=% GoCallees call go#guru#Callees(<count>)
command! -range=% GoDescribe call go#guru#Describe(<count>)
command! -range=% GoCallers call go#guru#Callers(<count>)
command! -range=% GoCallstack call go#guru#Callstack(<count>)
command! -range=% GoFreevars call go#guru#Freevars(<count>)
command! -range=% GoChannelPeers call go#guru#ChannelPeers(<count>)
endif

command! -range=% GoImplements call go#implements#Implements(<count>)
command! -range=% GoPointsTo call go#guru#PointsTo(<count>)
command! -range=% GoWhicherrs call go#guru#Whicherrs(<count>)
command! -range=% GoCallees call go#guru#Callees(<count>)
command! -range=% GoDescribe call go#guru#Describe(<count>)
command! -range=% GoCallers call go#guru#Callers(<count>)
command! -range=% GoCallstack call go#guru#Callstack(<count>)
command! -range=% GoFreevars call go#guru#Freevars(<count>)
command! -range=% GoChannelPeers call go#guru#ChannelPeers(<count>)
command! -range=% GoReferrers call go#referrers#Referrers(<count>)

command! -range=0 GoSameIds call go#guru#SameIds(1)
command! -range=0 GoSameIdsClear call go#guru#ClearSameIds()
command! -range=0 GoSameIdsToggle call go#guru#ToggleSameIds()
Expand Down
3 changes: 0 additions & 3 deletions plugin/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,9 @@ function! s:GoInstallBinaries(updateBinaries, ...)
if l:err
call go#util#EchoError(printf('Error installing %s: %s', l:importPath, l:out))
endif

call call(Restore_modules, [])
finally
call go#util#Chdir(l:dir)
endtry
call call(Restore_modules, [])
else
let l:get_cmd = copy(l:get_base_cmd)
let l:get_cmd += ['-d']
Expand Down

0 comments on commit 9ce65bd

Please sign in to comment.