2014-10-31 17:30:24 -04:00
|
|
|
" install necessary Go tools
|
|
|
|
if exists("g:go_loaded_install")
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:go_loaded_install = 1
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2014-10-31 17:30:24 -04:00
|
|
|
" these packages are used by vim-go and can be automatically installed if
|
|
|
|
" needed by the user with GoInstallBinaries
|
|
|
|
let s:packages = [
|
2015-01-18 07:58:28 -05:00
|
|
|
\ "github.com/nsf/gocode",
|
2015-12-08 08:20:04 -05:00
|
|
|
\ "github.com/alecthomas/gometalinter",
|
2015-01-18 07:58:28 -05:00
|
|
|
\ "golang.org/x/tools/cmd/goimports",
|
2015-02-04 05:43:54 -05:00
|
|
|
\ "github.com/rogpeppe/godef",
|
2015-01-18 07:58:28 -05:00
|
|
|
\ "golang.org/x/tools/cmd/oracle",
|
|
|
|
\ "golang.org/x/tools/cmd/gorename",
|
|
|
|
\ "github.com/golang/lint/golint",
|
2014-10-31 17:30:24 -04:00
|
|
|
\ "github.com/kisielk/errcheck",
|
|
|
|
\ "github.com/jstemmer/gotags",
|
2016-02-17 14:02:39 -05:00
|
|
|
\ "github.com/klauspost/asmfmt/cmd/asmfmt",
|
2016-03-20 14:01:44 -04:00
|
|
|
\ "github.com/fatih/motion",
|
2014-10-31 17:30:24 -04:00
|
|
|
\ ]
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
" These commands are available on any filetypes
|
2014-10-31 17:30:24 -04:00
|
|
|
command! GoInstallBinaries call s:GoInstallBinaries(-1)
|
|
|
|
command! GoUpdateBinaries call s:GoInstallBinaries(1)
|
2015-07-13 06:22:46 -04:00
|
|
|
command! -nargs=? -complete=dir GoPath call go#path#GoPath(<f-args>)
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
" GoInstallBinaries downloads and install all necessary binaries stated in the
|
|
|
|
" packages variable. It uses by default $GOBIN or $GOPATH/bin as the binary
|
|
|
|
" target install directory. GoInstallBinaries doesn't install binaries if they
|
|
|
|
" exist, to update current binaries pass 1 to the argument.
|
2015-01-18 07:58:28 -05:00
|
|
|
function! s:GoInstallBinaries(updateBinaries)
|
2014-10-31 17:30:24 -04:00
|
|
|
if $GOPATH == ""
|
2015-01-18 07:58:28 -05:00
|
|
|
echohl Error
|
2014-10-31 17:30:24 -04:00
|
|
|
echomsg "vim.go: $GOPATH is not set"
|
|
|
|
echohl None
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let err = s:CheckBinaries()
|
|
|
|
if err != 0
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
let go_bin_path = go#path#BinPath()
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
" change $GOBIN so go get can automatically install to it
|
|
|
|
let $GOBIN = go_bin_path
|
|
|
|
|
|
|
|
" old_path is used to restore users own path
|
|
|
|
let old_path = $PATH
|
|
|
|
|
|
|
|
" vim's executable path is looking in PATH so add our go_bin path to it
|
2015-07-13 06:22:46 -04:00
|
|
|
let $PATH = $PATH . go#util#PathListSep() .go_bin_path
|
2015-01-18 07:58:28 -05:00
|
|
|
|
|
|
|
" when shellslash is set on MS-* systems, shellescape puts single quotes
|
|
|
|
" around the output string. cmd on Windows does not handle single quotes
|
|
|
|
" correctly. Unsetting shellslash forces shellescape to use double quotes
|
|
|
|
" instead.
|
|
|
|
let resetshellslash = 0
|
|
|
|
if has('win32') && &shellslash
|
|
|
|
let resetshellslash = 1
|
|
|
|
set noshellslash
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
let cmd = "go get -u -v "
|
|
|
|
|
|
|
|
let s:go_version = matchstr(system("go version"), '\d.\d.\d')
|
|
|
|
|
|
|
|
" https://github.com/golang/go/issues/10791
|
|
|
|
if s:go_version > "1.4.0" && s:go_version < "1.5.0"
|
|
|
|
let cmd .= "-f "
|
|
|
|
endif
|
|
|
|
|
2014-10-31 17:30:24 -04:00
|
|
|
for pkg in s:packages
|
|
|
|
let basename = fnamemodify(pkg, ":t")
|
|
|
|
let binname = "go_" . basename . "_bin"
|
|
|
|
|
|
|
|
let bin = basename
|
|
|
|
if exists("g:{binname}")
|
|
|
|
let bin = g:{binname}
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !executable(bin) || a:updateBinaries == 1
|
2015-01-18 07:58:28 -05:00
|
|
|
if a:updateBinaries == 1
|
2014-10-31 17:30:24 -04:00
|
|
|
echo "vim-go: Updating ". basename .". Reinstalling ". pkg . " to folder " . go_bin_path
|
|
|
|
else
|
|
|
|
echo "vim-go: ". basename ." not found. Installing ". pkg . " to folder " . go_bin_path
|
|
|
|
endif
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
|
|
|
|
let out = system(cmd . shellescape(pkg))
|
2014-10-31 17:30:24 -04:00
|
|
|
if v:shell_error
|
|
|
|
echo "Error installing ". pkg . ": " . out
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
" restore back!
|
|
|
|
let $PATH = old_path
|
2015-01-18 07:58:28 -05:00
|
|
|
if resetshellslash
|
|
|
|
set shellslash
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" CheckBinaries checks if the necessary binaries to install the Go tool
|
|
|
|
" commands are available.
|
|
|
|
function! s:CheckBinaries()
|
|
|
|
if !executable('go')
|
|
|
|
echohl Error | echomsg "vim-go: go executable not found." | echohl None
|
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !executable('git')
|
|
|
|
echohl Error | echomsg "vim-go: git executable not found." | echohl None
|
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2015-01-18 07:58:28 -05:00
|
|
|
" Autocommands
|
|
|
|
" ============================================================================
|
2016-02-17 14:02:39 -05:00
|
|
|
"
|
|
|
|
function! s:echo_go_info()
|
|
|
|
if !exists('v:completed_item') || empty(v:completed_item)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let item = v:completed_item
|
|
|
|
|
|
|
|
if !has_key(item, "info")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if empty(item.info)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
redraws! | echo "vim-go: " | echohl Function | echon item.info | echohl None
|
|
|
|
endfunction
|
2015-01-18 07:58:28 -05:00
|
|
|
|
|
|
|
augroup vim-go
|
|
|
|
autocmd!
|
|
|
|
|
|
|
|
" GoInfo automatic update
|
|
|
|
if get(g:, "go_auto_type_info", 0)
|
2016-03-14 06:04:57 -04:00
|
|
|
autocmd CursorHold *.go nested call go#complete#Info(1)
|
2015-01-18 07:58:28 -05:00
|
|
|
endif
|
|
|
|
|
2016-02-17 14:02:39 -05:00
|
|
|
" Echo the identifier information when completion is done. Useful to see
|
|
|
|
" the signature of a function, etc...
|
|
|
|
if exists('##CompleteDone')
|
|
|
|
autocmd CompleteDone *.go nested call s:echo_go_info()
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Go code formatting on save
|
2015-01-18 07:58:28 -05:00
|
|
|
if get(g:, "go_fmt_autosave", 1)
|
|
|
|
autocmd BufWritePre *.go call go#fmt#Format(-1)
|
|
|
|
endif
|
|
|
|
|
2016-02-17 14:02:39 -05:00
|
|
|
" Go asm formatting on save
|
|
|
|
if get(g:, "go_asmfmt_autosave", 1)
|
|
|
|
autocmd BufWritePre *.s call go#asmfmt#Format()
|
|
|
|
endif
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
" run gometalinter on save
|
|
|
|
if get(g:, "go_metalinter_autosave", 0)
|
|
|
|
autocmd BufWritePost *.go call go#lint#Gometa(1)
|
|
|
|
endif
|
2015-01-18 07:58:28 -05:00
|
|
|
augroup END
|
|
|
|
|
2014-10-31 17:30:24 -04:00
|
|
|
|
|
|
|
" vim:ts=4:sw=4:et
|