Skip to content

Commit

Permalink
Merge pull request #954 from fatih/fix-gocode-goroot
Browse files Browse the repository at this point in the history
util: add go#util#env function
  • Loading branch information
fatih authored Oct 23, 2016
2 parents 23c0cb0 + bae1529 commit fcd04ce
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions autoload/go/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function! s:gocodeCommand(cmd, preargs, args)
" we might hit cache problems, as gocode doesn't handle well different
" GOPATHS: https://github.com/nsf/gocode/issues/239
let old_gopath = $GOPATH
let old_goroot = $GOROOT
let $GOPATH = go#path#Detect()
let $GOROOT = go#util#env("goroot")

let socket_type = get(g:, 'go_gocode_socket_type', s:sock_type)
let cmd = printf('%s -sock %s %s %s %s',
Expand All @@ -45,6 +47,8 @@ function! s:gocodeCommand(cmd, preargs, args)

let result = go#util#System(cmd)
let $GOPATH = old_gopath
let $GOROOT = old_goroot

if go#util#ShellError() != 0
return "[\"0\", []]"
else
Expand Down
3 changes: 3 additions & 0 deletions autoload/go/tool.vim
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ endfunction

function! go#tool#ExecuteInDir(cmd) abort
let old_gopath = $GOPATH
let old_goroot = $GOROOT
let $GOPATH = go#path#Detect()
let $GOROOT = go#util#env("goroot")

let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
Expand All @@ -132,6 +134,7 @@ function! go#tool#ExecuteInDir(cmd) abort
execute cd . fnameescape(dir)
endtry

let $GOROOT = old_goroot
let $GOPATH = old_gopath
return out
endfunction
Expand Down
25 changes: 25 additions & 0 deletions autoload/go/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ function! go#util#IsWin()
return 0
endfunction

let s:env_cache = {}

" env returns the go environment variable for the given key. Where key can be
" GOARCH, GOOS, GOROOT, etc... It caches the result and returns the cached
" version.
function! go#util#env(key)
let l:key = tolower(a:key)
if has_key(s:env_cache, l:key)
return s:env_cache[l:key]
endif

if executable('go')
let l:var = call('go#util#'.l:key, [])
if go#util#ShellError() != 0
call go#util#EchoError(printf("'go env %s' failed", toupper(l:key)))
return ''
endif
else
let l:var = eval("$".toupper(a:key))
endif

let s:env_cache[l:key] = l:var
return l:var
endfunction

function! go#util#goarch()
return substitute(go#util#System('go env GOARCH'), '\n', '', 'g')
endfunction
Expand Down

0 comments on commit fcd04ce

Please sign in to comment.