2014-10-31 21:30:24 +00:00
|
|
|
" install necessary Go tools
|
|
|
|
if exists("g:go_loaded_install")
|
2016-06-26 11:12:36 +00:00
|
|
|
finish
|
2014-10-31 21:30:24 +00:00
|
|
|
endif
|
|
|
|
let g:go_loaded_install = 1
|
|
|
|
|
2018-12-17 11:28:27 +00:00
|
|
|
" don't spam the user when Vim is started in Vi compatibility mode
|
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
2018-09-30 20:58:57 +00:00
|
|
|
function! s:checkVersion() abort
|
|
|
|
" Not using the has('patch-7.4.2009') syntax because that wasn't added until
|
|
|
|
" 7.4.237, and we want to be sure this works for everyone (this is also why
|
|
|
|
" we're not using utils#EchoError()).
|
|
|
|
"
|
|
|
|
" Version 7.4.2009 was chosen because that's greater than what the most recent Ubuntu LTS
|
|
|
|
" release (16.04) uses and has a couple of features we need (e.g. execute()
|
|
|
|
" and :message clear).
|
2017-11-24 13:54:40 +00:00
|
|
|
|
2018-09-30 20:58:57 +00:00
|
|
|
let l:unsupported = 0
|
|
|
|
if go#config#VersionWarning() != 0
|
|
|
|
if has('nvim')
|
|
|
|
let l:unsupported = !has('nvim-0.3.1')
|
|
|
|
else
|
|
|
|
let l:unsupported = (v:version < 704 || (v:version == 704 && !has('patch2009')))
|
|
|
|
endif
|
|
|
|
|
|
|
|
if l:unsupported == 1
|
|
|
|
echohl Error
|
|
|
|
echom "vim-go requires Vim 7.4.2009 or Neovim 0.3.1, but you're using an older version."
|
|
|
|
echom "Please update your Vim for the best vim-go experience."
|
|
|
|
echom "If you really want to continue you can set this to make the error go away:"
|
|
|
|
echom " let g:go_version_warning = 0"
|
|
|
|
echom "Note that some features may error out or behave incorrectly."
|
|
|
|
echom "Please do not report bugs unless you're using Vim 7.4.2009 or newer or Neovim 0.3.1."
|
|
|
|
echohl None
|
|
|
|
|
|
|
|
" Make sure people see this.
|
|
|
|
sleep 2
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:checkVersion()
|
2017-11-24 13:54:40 +00:00
|
|
|
|
2014-10-31 21:30:24 +00:00
|
|
|
" these packages are used by vim-go and can be automatically installed if
|
2018-09-25 00:40:17 +00:00
|
|
|
" needed by the user with GoInstallBinaries.
|
2017-11-24 13:54:40 +00:00
|
|
|
let s:packages = {
|
|
|
|
\ 'asmfmt': ['github.com/klauspost/asmfmt/cmd/asmfmt'],
|
2018-03-31 14:56:26 +00:00
|
|
|
\ 'dlv': ['github.com/derekparker/delve/cmd/dlv'],
|
2017-11-24 13:54:40 +00:00
|
|
|
\ 'errcheck': ['github.com/kisielk/errcheck'],
|
|
|
|
\ 'fillstruct': ['github.com/davidrjenni/reftools/cmd/fillstruct'],
|
2018-09-25 00:40:17 +00:00
|
|
|
\ 'gocode': ['github.com/mdempsky/gocode', {'windows': ['-ldflags', '-H=windowsgui']}],
|
2018-11-01 10:03:42 +00:00
|
|
|
\ 'gocode-gomod': ['github.com/stamblerre/gocode'],
|
2017-11-24 13:54:40 +00:00
|
|
|
\ 'godef': ['github.com/rogpeppe/godef'],
|
|
|
|
\ 'gogetdoc': ['github.com/zmb3/gogetdoc'],
|
|
|
|
\ 'goimports': ['golang.org/x/tools/cmd/goimports'],
|
2018-11-01 10:03:42 +00:00
|
|
|
\ 'golint': ['golang.org/x/lint/golint'],
|
2017-11-24 13:54:40 +00:00
|
|
|
\ 'gometalinter': ['github.com/alecthomas/gometalinter'],
|
|
|
|
\ 'gomodifytags': ['github.com/fatih/gomodifytags'],
|
|
|
|
\ 'gorename': ['golang.org/x/tools/cmd/gorename'],
|
|
|
|
\ 'gotags': ['github.com/jstemmer/gotags'],
|
|
|
|
\ 'guru': ['golang.org/x/tools/cmd/guru'],
|
|
|
|
\ 'impl': ['github.com/josharian/impl'],
|
2018-08-25 16:13:42 +00:00
|
|
|
\ 'keyify': ['honnef.co/go/tools/cmd/keyify'],
|
2017-11-24 13:54:40 +00:00
|
|
|
\ 'motion': ['github.com/fatih/motion'],
|
2018-07-04 10:53:25 +00:00
|
|
|
\ 'iferr': ['github.com/koron/iferr'],
|
2017-11-24 13:54:40 +00:00
|
|
|
\ }
|
2014-10-31 21:30:24 +00:00
|
|
|
|
2015-07-13 10:22:46 +00:00
|
|
|
" These commands are available on any filetypes
|
2017-11-24 13:54:40 +00:00
|
|
|
command! -nargs=* -complete=customlist,s:complete GoInstallBinaries call s:GoInstallBinaries(-1, <f-args>)
|
|
|
|
command! -nargs=* -complete=customlist,s:complete GoUpdateBinaries call s:GoInstallBinaries(1, <f-args>)
|
2015-07-13 10:22:46 +00:00
|
|
|
command! -nargs=? -complete=dir GoPath call go#path#GoPath(<f-args>)
|
2014-10-31 21:30:24 +00:00
|
|
|
|
2017-11-24 13:54:40 +00:00
|
|
|
fun! s:complete(lead, cmdline, cursor)
|
|
|
|
return filter(keys(s:packages), 'strpart(v:val, 0, len(a:lead)) == a:lead')
|
|
|
|
endfun
|
|
|
|
|
|
|
|
" GoInstallBinaries downloads and installs binaries defined in s:packages to
|
|
|
|
" $GOBIN or $GOPATH/bin. GoInstallBinaries will update already installed
|
|
|
|
" binaries only if updateBinaries = 1. By default, all packages in s:packages
|
|
|
|
" will be installed, but the set can be limited by passing the desired
|
|
|
|
" packages in the unnamed arguments.
|
|
|
|
function! s:GoInstallBinaries(updateBinaries, ...)
|
2017-04-01 11:22:06 +00:00
|
|
|
let err = s:CheckBinaries()
|
|
|
|
if err != 0
|
2016-06-26 11:12:36 +00:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2017-04-01 11:22:06 +00:00
|
|
|
if go#path#Default() == ""
|
|
|
|
echohl Error
|
|
|
|
echomsg "vim.go: $GOPATH is not set and 'go env GOPATH' returns empty"
|
|
|
|
echohl None
|
2016-06-26 11:12:36 +00:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let go_bin_path = go#path#BinPath()
|
|
|
|
|
|
|
|
" 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
|
|
|
|
let $PATH = go_bin_path . go#util#PathListSep() . $PATH
|
|
|
|
|
|
|
|
" 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
|
|
|
|
|
2018-09-25 00:40:17 +00:00
|
|
|
let l:dl_cmd = ['go', 'get', '-v', '-d']
|
2016-06-26 11:12:36 +00:00
|
|
|
if get(g:, "go_get_update", 1) != 0
|
2018-09-25 00:40:17 +00:00
|
|
|
let l:dl_cmd += ['-u']
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
|
|
|
|
2017-11-24 13:54:40 +00:00
|
|
|
" Filter packages from arguments (if any).
|
|
|
|
let l:packages = {}
|
|
|
|
if a:0 > 0
|
|
|
|
for l:bin in a:000
|
|
|
|
let l:pkg = get(s:packages, l:bin, [])
|
|
|
|
if len(l:pkg) == 0
|
|
|
|
call go#util#EchoError('unknown binary: ' . l:bin)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let l:packages[l:bin] = l:pkg
|
|
|
|
endfor
|
|
|
|
else
|
|
|
|
let l:packages = s:packages
|
|
|
|
endif
|
2016-06-26 11:12:36 +00:00
|
|
|
|
2017-11-24 13:54:40 +00:00
|
|
|
let l:platform = ''
|
|
|
|
if go#util#IsWin()
|
|
|
|
let l:platform = 'windows'
|
|
|
|
endif
|
|
|
|
|
|
|
|
for [binary, pkg] in items(l:packages)
|
|
|
|
let l:importPath = pkg[0]
|
2018-06-14 10:31:12 +00:00
|
|
|
|
2018-09-25 00:40:17 +00:00
|
|
|
let l:run_cmd = copy(l:dl_cmd)
|
2018-06-14 10:31:12 +00:00
|
|
|
if len(l:pkg) > 1 && get(l:pkg[1], l:platform, '') isnot ''
|
|
|
|
let l:run_cmd += get(l:pkg[1], l:platform, '')
|
|
|
|
endif
|
2017-11-24 13:54:40 +00:00
|
|
|
|
2018-09-25 00:40:17 +00:00
|
|
|
let bin_setting_name = "go_" . binary . "_bin"
|
2017-11-24 13:54:40 +00:00
|
|
|
|
2018-09-25 00:40:17 +00:00
|
|
|
if exists("g:{bin_setting_name}")
|
|
|
|
let bin = g:{bin_setting_name}
|
|
|
|
else
|
|
|
|
if go#util#IsWin()
|
|
|
|
let bin = binary . '.exe'
|
|
|
|
else
|
|
|
|
let bin = binary
|
|
|
|
endif
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
if !executable(bin) || a:updateBinaries == 1
|
|
|
|
if a:updateBinaries == 1
|
2017-11-24 13:54:40 +00:00
|
|
|
echo "vim-go: Updating " . binary . ". Reinstalling ". importPath . " to folder " . go_bin_path
|
2016-06-26 11:12:36 +00:00
|
|
|
else
|
2017-11-24 13:54:40 +00:00
|
|
|
echo "vim-go: ". binary ." not found. Installing ". importPath . " to folder " . go_bin_path
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
|
|
|
|
2018-09-25 00:40:17 +00:00
|
|
|
" first download the binary
|
2018-06-14 10:31:12 +00:00
|
|
|
let [l:out, l:err] = go#util#Exec(l:run_cmd + [l:importPath])
|
2018-09-25 00:40:17 +00:00
|
|
|
if l:err
|
|
|
|
echom "Error downloading " . l:importPath . ": " . l:out
|
|
|
|
endif
|
|
|
|
|
|
|
|
" and then build and install it
|
|
|
|
let l:build_cmd = ['go', 'build', '-o', go_bin_path . go#util#PathSep() . bin, l:importPath]
|
2018-12-17 11:28:27 +00:00
|
|
|
let [l:out, l:err] = go#util#Exec(l:build_cmd)
|
2018-06-14 10:31:12 +00:00
|
|
|
if l:err
|
|
|
|
echom "Error installing " . l:importPath . ": " . l:out
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
" restore back!
|
|
|
|
let $PATH = old_path
|
|
|
|
if resetshellslash
|
|
|
|
set shellslash
|
|
|
|
endif
|
2018-09-25 00:40:17 +00:00
|
|
|
|
|
|
|
if a:updateBinaries == 1
|
|
|
|
call go#util#EchoInfo('updating finished!')
|
|
|
|
else
|
|
|
|
call go#util#EchoInfo('installing finished!')
|
|
|
|
endif
|
2014-10-31 21:30:24 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" CheckBinaries checks if the necessary binaries to install the Go tool
|
|
|
|
" commands are available.
|
|
|
|
function! s:CheckBinaries()
|
2016-06-26 11:12:36 +00:00
|
|
|
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
|
2014-10-31 21:30:24 +00:00
|
|
|
endfunction
|
|
|
|
|
2015-01-18 12:58:28 +00:00
|
|
|
" Autocommands
|
|
|
|
" ============================================================================
|
2016-02-20 13:13:10 +00:00
|
|
|
"
|
|
|
|
function! s:echo_go_info()
|
2016-12-27 14:46:49 +00:00
|
|
|
if !get(g:, "go_echo_go_info", 1)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
if !exists('v:completed_item') || empty(v:completed_item)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let item = v:completed_item
|
2016-02-20 13:13:10 +00:00
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
if !has_key(item, "info")
|
|
|
|
return
|
|
|
|
endif
|
2016-02-20 13:13:10 +00:00
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
if empty(item.info)
|
|
|
|
return
|
|
|
|
endif
|
2016-02-20 13:13:10 +00:00
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
redraws! | echo "vim-go: " | echohl Function | echon item.info | echohl None
|
2016-02-20 13:13:10 +00:00
|
|
|
endfunction
|
2015-01-18 12:58:28 +00:00
|
|
|
|
2016-08-02 12:48:32 +00:00
|
|
|
function! s:auto_type_info()
|
2016-06-26 11:12:36 +00:00
|
|
|
" GoInfo automatic update
|
|
|
|
if get(g:, "go_auto_type_info", 0)
|
2018-09-25 00:40:17 +00:00
|
|
|
call go#tool#Info(0)
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
2016-08-02 12:48:32 +00:00
|
|
|
endfunction
|
2016-06-26 11:12:36 +00:00
|
|
|
|
2016-08-02 12:48:32 +00:00
|
|
|
function! s:auto_sameids()
|
2016-07-16 18:30:35 +00:00
|
|
|
" GoSameId automatic update
|
|
|
|
if get(g:, "go_auto_sameids", 0)
|
2018-09-25 00:40:17 +00:00
|
|
|
call go#guru#SameIds(0)
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
2016-08-02 12:48:32 +00:00
|
|
|
endfunction
|
2016-06-26 11:12:36 +00:00
|
|
|
|
2016-08-02 12:48:32 +00:00
|
|
|
function! s:fmt_autosave()
|
2016-06-26 11:12:36 +00:00
|
|
|
" Go code formatting on save
|
|
|
|
if get(g:, "go_fmt_autosave", 1)
|
2016-08-02 12:48:32 +00:00
|
|
|
call go#fmt#Format(-1)
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
2016-08-02 12:48:32 +00:00
|
|
|
endfunction
|
2016-06-26 11:12:36 +00:00
|
|
|
|
2016-08-02 12:48:32 +00:00
|
|
|
function! s:asmfmt_autosave()
|
2016-06-26 11:12:36 +00:00
|
|
|
" Go asm formatting on save
|
2016-12-27 14:46:49 +00:00
|
|
|
if get(g:, "go_asmfmt_autosave", 0)
|
2016-08-02 12:48:32 +00:00
|
|
|
call go#asmfmt#Format()
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
2016-08-02 12:48:32 +00:00
|
|
|
endfunction
|
2016-06-26 11:12:36 +00:00
|
|
|
|
2018-09-25 00:40:17 +00:00
|
|
|
function! s:modfmt_autosave()
|
|
|
|
" go.mod code formatting on save
|
|
|
|
if get(g:, "go_mod_fmt_autosave", 1)
|
|
|
|
call go#mod#Format()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2016-08-02 12:48:32 +00:00
|
|
|
function! s:metalinter_autosave()
|
2016-06-26 11:12:36 +00:00
|
|
|
" run gometalinter on save
|
|
|
|
if get(g:, "go_metalinter_autosave", 0)
|
2018-12-17 11:28:27 +00:00
|
|
|
call go#lint#Gometa(0, 1)
|
2016-06-26 11:12:36 +00:00
|
|
|
endif
|
2016-08-02 12:48:32 +00:00
|
|
|
endfunction
|
2016-07-03 11:53:59 +00:00
|
|
|
|
2016-08-02 12:48:32 +00:00
|
|
|
function! s:template_autocreate()
|
2016-07-03 11:53:59 +00:00
|
|
|
" create new template from scratch
|
|
|
|
if get(g:, "go_template_autocreate", 1)
|
2016-08-02 12:48:32 +00:00
|
|
|
call go#template#create()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
augroup vim-go
|
|
|
|
autocmd!
|
|
|
|
|
|
|
|
autocmd CursorHold *.go call s:auto_type_info()
|
|
|
|
autocmd CursorHold *.go call s:auto_sameids()
|
|
|
|
|
|
|
|
" Echo the identifier information when completion is done. Useful to see
|
|
|
|
" the signature of a function, etc...
|
|
|
|
if exists('##CompleteDone')
|
|
|
|
autocmd CompleteDone *.go call s:echo_go_info()
|
2016-07-03 11:53:59 +00:00
|
|
|
endif
|
2016-08-02 12:48:32 +00:00
|
|
|
|
|
|
|
autocmd BufWritePre *.go call s:fmt_autosave()
|
2018-09-25 00:40:17 +00:00
|
|
|
autocmd BufWritePre *.mod call s:modfmt_autosave()
|
2016-08-02 12:48:32 +00:00
|
|
|
autocmd BufWritePre *.s call s:asmfmt_autosave()
|
|
|
|
autocmd BufWritePost *.go call s:metalinter_autosave()
|
|
|
|
autocmd BufNewFile *.go call s:template_autocreate()
|
2016-11-09 17:22:55 +00:00
|
|
|
" clear SameIds when the buffer is unloaded so that loading another buffer
|
|
|
|
" in the same window doesn't highlight the most recently matched
|
|
|
|
" identifier's positions.
|
|
|
|
autocmd BufWinEnter *.go call go#guru#ClearSameIds()
|
2017-12-13 14:05:24 +00:00
|
|
|
|
|
|
|
autocmd BufEnter *.go
|
2018-06-14 10:31:12 +00:00
|
|
|
\ if go#config#AutodetectGopath() && !exists('b:old_gopath')
|
2017-12-13 14:05:24 +00:00
|
|
|
\| let b:old_gopath = exists('$GOPATH') ? $GOPATH : -1
|
|
|
|
\| let $GOPATH = go#path#Detect()
|
|
|
|
\| endif
|
|
|
|
autocmd BufLeave *.go
|
|
|
|
\ if exists('b:old_gopath')
|
|
|
|
\| if b:old_gopath isnot -1
|
|
|
|
\| let $GOPATH = b:old_gopath
|
|
|
|
\| endif
|
|
|
|
\| unlet b:old_gopath
|
|
|
|
\| endif
|
|
|
|
augroup end
|
2015-01-18 12:58:28 +00:00
|
|
|
|
2018-12-17 11:28:27 +00:00
|
|
|
" restore Vi compatibility settings
|
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
|
|
|
|
2016-06-26 11:12:36 +00:00
|
|
|
" vim: sw=2 ts=2 et
|