123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- let s:initial_go_path = ""
- function! go#path#GoPath(...) abort
-
- if len(a:000) == 0
- echo go#path#Default()
- return
- endif
-
-
-
- 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
- endfunction
- function! go#path#Default() abort
- if $GOPATH == ""
-
- return go#util#env("gopath")
- endif
- return $GOPATH
- endfunction
- function! s:HasPath(path) abort
- let go_paths = split(go#path#Default(), go#util#PathListSep())
- let last_char = strlen(a:path) - 1
-
- 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
- function! go#path#Detect() abort
- let gopath = go#path#Default()
- let current_dir = fnameescape(expand('%:p:h'))
-
-
-
-
- let src_roots = finddir("src", current_dir .";", -1)
-
-
- let src_root = ""
- if len(src_roots) > 0
- let src_root = src_roots[-1]
- endif
- if !empty(src_root)
- let src_path = fnamemodify(src_root, ':p:h:h') . go#util#PathSep()
-
-
- let gb_vendor_root = src_path . "vendor" . go#util#PathSep()
- if isdirectory(gb_vendor_root) && !s:HasPath(gb_vendor_root)
- let gopath = gb_vendor_root . go#util#PathListSep() . gopath
- endif
- if !s:HasPath(src_path)
- let gopath = src_path . go#util#PathListSep() . gopath
- endif
- endif
-
- 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 !s:HasPath(godeps_path)
- let gopath = godeps_path . go#util#PathListSep() . gopath
- endif
- endif
-
-
- let gopath = substitute(gopath, ":$", "", "")
- return gopath
- endfunction
- function! go#path#BinPath() abort
- let bin_path = ""
-
-
- if exists("g:go_bin_path")
- let bin_path = g:go_bin_path
- elseif $GOBIN != ""
- let bin_path = $GOBIN
- else
- let go_paths = split(go#path#Default(), go#util#PathListSep())
- if len(go_paths) == 0
- return ""
- endif
- let bin_path = expand(go_paths[0] . "/bin/")
- endif
- return bin_path
- endfunction
- function! go#path#CheckBinPath(binpath) abort
-
- let binpath = substitute(a:binpath, '^\s*\(.\{-}\)\s*$', '\1', '')
-
- let old_path = $PATH
-
- let go_bin_path = go#path#BinPath()
- if !empty(go_bin_path)
-
-
- let $PATH = go_bin_path . go#util#PathListSep() . $PATH
- endif
-
- if executable(binpath)
- if exists('*exepath')
- let binpath = exepath(binpath)
- endif
- let $PATH = old_path
- if go#util#IsUsingCygwinShell() == 1
- return s:CygwinPath(binpath)
- endif
- return binpath
- endif
-
- let basename = fnamemodify(binpath, ":t")
- if !executable(basename)
- call go#util#EchoError(printf("could not find '%s'. Run :GoInstallBinaries to fix it", basename))
-
- let $PATH = old_path
- return ""
- endif
- let $PATH = old_path
- if go#util#IsUsingCygwinShell() == 1
- return s:CygwinPath(a:binpath)
- endif
- return go_bin_path . go#util#PathSep() . basename
- endfunction
- function! s:CygwinPath(path)
- return substitute(a:path, '\\', '/', "g")
- endfunction
|