Skip to content

Commit

Permalink
go: remove :GoPathClear and use :GoPath "" for reseting the GOPATH
Browse files Browse the repository at this point in the history
  • Loading branch information
fatih committed May 26, 2015
1 parent 299f440 commit c069666
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 119 deletions.
220 changes: 110 additions & 110 deletions autoload/go/path.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,163 +6,163 @@ let s:initial_go_path = ""

" GoPath sets or returns the current GOPATH. If no arguments are passed it
" echoes the current GOPATH, if an argument is passed it replaces the current
" GOPATH with it.
" GOPATH with it. If two double quotes are passed (the empty string in go),
" it'll clear the GOPATH and will restore to the initial GOPATH.
function! go#path#GoPath(...)
" we have an argument, replace GOPATH
if len(a:000)
echon "vim-go: " | echohl Function | echon "GOPATH changed to ". a:1 | echohl None
let s:initial_go_path = $GOPATH
let $GOPATH = a:1
return
endif

echo go#path#Detect()
" we have an argument, replace GOPATH
if len(a:000)
" clears the current manually set GOPATH and restores it to the
" initial GOPATH, which was set when Vim was started.
if len(a:000) == 1 && a:1 == '""'
if !empty(s:initial_go_path)
let $GOPATH = s:initial_go_path
let s:initial_go_path = ""
endif

echon "vim-go: " | echohl Function | echon "GOPATH restored to ". $GOPATH | echohl None
return
endif

echon "vim-go: " | echohl Function | echon "GOPATH changed to ". a:1 | echohl None
let s:initial_go_path = $GOPATH
let $GOPATH = a:1
return
endif

echo go#path#Detect()
endfunction

" GoPathClear clears the current manually set GOPATH and restores it to the
" initial GOPATH, which was set when Vim was started.
function! go#path#GoPathClear()
if !empty(s:initial_go_path)
let $GOPATH = s:initial_go_path
let s:initial_go_path = ""
endif

echon "vim-go: " | echohl Function | echon "GOPATH restored to ". $GOPATH | echohl None
endfunction



" Default returns the default GOPATH. If there is a single GOPATH it returns
" it. For multiple GOPATHS separated with a the OS specific separator, only
" the first one is returned
function! go#path#Default()
let go_paths = split($GOPATH, go#util#PathListSep())
let go_paths = split($GOPATH, go#util#PathListSep())

if len(go_paths) == 1
return $GOPATH
endif
if len(go_paths) == 1
return $GOPATH
endif

return go_paths[0]
return go_paths[0]
endfunction

" HasPath checks whether the given path exists in GOPATH environment variable
" or not
function! go#path#HasPath(path)
let go_paths = split($GOPATH, go#util#PathListSep())
let last_char = strlen(a:path) - 1

" check cases of '/foo/bar/' and '/foo/bar'
if a:path[last_char] == go#util#PathSep()
let withSep = a:path
let noSep = strpart(a:path, 0, last_char)
else
let withSep = a:path . go#util#PathSep()
let noSep = a:path
endif

let hasA = index(go_paths, withSep) != -1
let hasB = index(go_paths, noSep) != -1
return hasA || hasB
let go_paths = split($GOPATH, go#util#PathListSep())
let last_char = strlen(a:path) - 1

" check cases of '/foo/bar/' and '/foo/bar'
if a:path[last_char] == go#util#PathSep()
let withSep = a:path
let noSep = strpart(a:path, 0, last_char)
else
let withSep = a:path . go#util#PathSep()
let noSep = a:path
endif

let hasA = index(go_paths, withSep) != -1
let hasB = index(go_paths, noSep) != -1
return hasA || hasB
endfunction

" Detect returns the current GOPATH. If a package manager is used, such
" as Godeps or something like gb (not supported yet), it will modify the
" GOPATH so those directories take precedence over the current GOPATH. It also
" detects diretories whose are outside GOPATH.
function! go#path#Detect()
let gopath = $GOPATH
let gopath = $GOPATH

" don't lookup for godeps if autodetect is disabled.
if !get(g:, "go_autodetect_gopath", 1)
return gopath
endif
" don't lookup for godeps if autodetect is disabled.
if !get(g:, "go_autodetect_gopath", 1)
return gopath
endif

let current_dir = fnameescape(expand('%:p:h'))
let current_dir = fnameescape(expand('%:p:h'))

" TODO(arslan): this should be changed so folders or files should be
" fetched from a customizable list. The user should define any new package
" management tool by it's own.
" TODO(arslan): this should be changed so folders or files should be
" fetched from a customizable list. The user should define any new package
" management tool by it's own.

" src folder outside $GOPATH
let src_root = finddir("src", current_dir .";")
if !empty(src_root)
let src_path = fnamemodify(src_root, ':p:h:h') . go#util#PathSep()
" src folder outside $GOPATH
let src_root = finddir("src", current_dir .";")
if !empty(src_root)
let src_path = fnamemodify(src_root, ':p:h:h') . go#util#PathSep()

if !go#path#HasPath(src_path)
let gopath = src_path . go#util#PathListSep() . gopath
endif
endif
if !go#path#HasPath(src_path)
let gopath = src_path . go#util#PathListSep() . gopath
endif
endif

" Godeps
let godeps_root = finddir("Godeps", current_dir .";")
if !empty(godeps_root)
let godeps_path = join([fnamemodify(godeps_root, ':p:h:h'), "Godeps", "_workspace" ], go#util#PathSep())
" Godeps
let godeps_root = finddir("Godeps", current_dir .";")
if !empty(godeps_root)
let godeps_path = join([fnamemodify(godeps_root, ':p:h:h'), "Godeps", "_workspace" ], go#util#PathSep())

if !go#path#HasPath(godeps_path)
let gopath = godeps_path . go#util#PathListSep() . gopath
endif
endif
if !go#path#HasPath(godeps_path)
let gopath = godeps_path . go#util#PathListSep() . gopath
endif
endif

return gopath
return gopath
endfunction


" BinPath returns the binary path of installed go tools.
function! go#path#BinPath()
let bin_path = ""

" check if our global custom path is set, if not check if $GOBIN is set so
" we can use it, otherwise use $GOPATH + '/bin'
if exists("g:go_bin_path")
let bin_path = g:go_bin_path
elseif $GOBIN != ""
let bin_path = $GOBIN
elseif $GOPATH != ""
let bin_path = expand(go#path#Default() . "/bin/")
else
" could not find anything
endif

return bin_path
let bin_path = ""

" check if our global custom path is set, if not check if $GOBIN is set so
" we can use it, otherwise use $GOPATH + '/bin'
if exists("g:go_bin_path")
let bin_path = g:go_bin_path
elseif $GOBIN != ""
let bin_path = $GOBIN
elseif $GOPATH != ""
let bin_path = expand(go#path#Default() . "/bin/")
else
" could not find anything
endif

return bin_path
endfunction

" CheckBinPath checks whether the given binary exists or not and returns the
" path of the binary. It returns an empty string doesn't exists.
function! go#path#CheckBinPath(binpath)
" remove whitespaces if user applied something like 'goimports '
let binpath = substitute(a:binpath, '^\s*\(.\{-}\)\s*$', '\1', '')
" remove whitespaces if user applied something like 'goimports '
let binpath = substitute(a:binpath, '^\s*\(.\{-}\)\s*$', '\1', '')

" if it's in PATH just return it
if executable(binpath)
return binpath
endif
" if it's in PATH just return it
if executable(binpath)
return binpath
endif


" just get the basename
let basename = fnamemodify(binpath, ":t")
" just get the basename
let basename = fnamemodify(binpath, ":t")

" check if we have an appropriate bin_path
let go_bin_path = go#path#BinPath()
if empty(go_bin_path)
echo "vim-go: could not find '" . basename . "'. Run :GoInstallBinaries to fix it."
return ""
endif
" check if we have an appropriate bin_path
let go_bin_path = go#path#BinPath()
if empty(go_bin_path)
echo "vim-go: could not find '" . basename . "'. Run :GoInstallBinaries to fix it."
return ""
endif

" append our GOBIN and GOPATH paths and be sure they can be found there...
" let us search in our GOBIN and GOPATH paths
let old_path = $PATH
let $PATH = $PATH . go#util#PathListSep() .go_bin_path
" append our GOBIN and GOPATH paths and be sure they can be found there...
" let us search in our GOBIN and GOPATH paths
let old_path = $PATH
let $PATH = $PATH . go#util#PathListSep() .go_bin_path

if !executable(basename)
echo "vim-go: could not find '" . basename . "'. Run :GoInstallBinaries to fix it."
" restore back!
let $PATH = old_path
return ""
endif
if !executable(basename)
echo "vim-go: could not find '" . basename . "'. Run :GoInstallBinaries to fix it."
" restore back!
let $PATH = old_path
return ""
endif

let $PATH = old_path
let $PATH = old_path

return go_bin_path . go#util#PathSep() . basename
return go_bin_path . go#util#PathSep() . basename
endfunction

" vim:ts=4:sw=4:et
11 changes: 3 additions & 8 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,9 @@ COMMANDS *go-commands*
:GoPath [path]

GoPath sets and ovverides GOPATH with the given {path}. If no {path} is
given it shows the current GOPATH.

*:GoPathClear*
:GoPathClear

GoPathClear clears the current `GOPATH` which was set with |GoPath| and
restores `GOPATH` back to the inital value which was sourced when Vim was
started.
given it shows the current GOPATH. If `""` is given as path, it clears
current `GOPATH` which was set with |GoPath| and restores `GOPATH` back to
the inital value which was sourced when Vim was started.

*:GoImport*
:GoImport [path]
Expand Down
1 change: 0 additions & 1 deletion plugin/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ let s:packages = [
command! GoInstallBinaries call s:GoInstallBinaries(-1)
command! GoUpdateBinaries call s:GoInstallBinaries(1)
command! -nargs=? -complete=dir GoPath call go#path#GoPath(<f-args>)
command! GoPathClear call go#path#GoPathClear()

" GoInstallBinaries downloads and install all necessary binaries stated in the
" packages variable. It uses by default $GOBIN or $GOPATH/bin as the binary
Expand Down

0 comments on commit c069666

Please sign in to comment.