2016-12-27 09:46:49 -05:00
|
|
|
function! go#cmd#autowrite() abort
|
2016-06-26 07:12:36 -04:00
|
|
|
if &autowrite == 1
|
2016-12-27 09:46:49 -05:00
|
|
|
silent! wall
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-07-06 08:57:35 -04:00
|
|
|
" Build builds the source code without producing any output binary. We live in
|
2015-07-13 06:22:46 -04:00
|
|
|
" an editor so the best is to build it to catch errors and fix them. By
|
|
|
|
" default it tries to call simply 'go build', but it first tries to get all
|
|
|
|
" dependent files for the current folder and passes it to go build.
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#cmd#Build(bang, ...) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
" expand all wildcards(i.e: '%' to the current file name)
|
|
|
|
let goargs = map(copy(a:000), "expand(v:val)")
|
|
|
|
|
|
|
|
" escape all shell arguments before we pass it to make
|
2016-10-02 07:37:21 -04:00
|
|
|
if !has('nvim')
|
|
|
|
let goargs = go#util#Shelllist(goargs, 1)
|
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
" create our command arguments. go build discards any results when it
|
|
|
|
" compiles multiple packages. So we pass the `errors` package just as an
|
2017-07-06 08:57:35 -04:00
|
|
|
" placeholder with the current folder (indicated with '.'). We also pass -i
|
|
|
|
" that tries to install the dependencies, this has the side effect that it
|
|
|
|
" caches the build results, so every other build is faster.
|
|
|
|
let args = ["build"] + goargs + ["-i", ".", "errors"]
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
if go#util#has_job()
|
|
|
|
if get(g:, 'go_echo_command_info', 1)
|
|
|
|
call go#util#EchoProgress("building dispatched ...")
|
|
|
|
endif
|
|
|
|
|
|
|
|
call s:cmd_job({
|
|
|
|
\ 'cmd': ['go'] + args,
|
|
|
|
\ 'bang': a:bang,
|
|
|
|
\})
|
|
|
|
return
|
|
|
|
elseif has('nvim')
|
2017-02-11 08:01:38 -05:00
|
|
|
if get(g:, 'go_echo_command_info', 1)
|
|
|
|
call go#util#EchoProgress("building dispatched ...")
|
|
|
|
endif
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
" if we have nvim, call it asynchronously and return early ;)
|
2016-06-26 07:12:36 -04:00
|
|
|
call go#jobcontrol#Spawn(a:bang, "build", args)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let old_gopath = $GOPATH
|
|
|
|
let $GOPATH = go#path#Detect()
|
|
|
|
let default_makeprg = &makeprg
|
|
|
|
let &makeprg = "go " . join(args, ' ')
|
|
|
|
|
|
|
|
let l:listtype = go#list#Type("quickfix")
|
|
|
|
" execute make inside the source folder so we can parse the errors
|
|
|
|
" correctly
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let dir = getcwd()
|
|
|
|
try
|
|
|
|
execute cd . fnameescape(expand("%:p:h"))
|
2016-12-27 09:46:49 -05:00
|
|
|
if l:listtype == "locationlist"
|
2016-06-26 07:12:36 -04:00
|
|
|
silent! exe 'lmake!'
|
|
|
|
else
|
|
|
|
silent! exe 'make!'
|
2015-07-13 06:22:46 -04:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
redraw!
|
|
|
|
finally
|
|
|
|
execute cd . fnameescape(dir)
|
|
|
|
endtry
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let errors = go#list#Get(l:listtype)
|
|
|
|
call go#list#Window(l:listtype, len(errors))
|
2016-12-27 09:46:49 -05:00
|
|
|
if !empty(errors) && !a:bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
2016-06-26 07:12:36 -04:00
|
|
|
else
|
|
|
|
call go#util#EchoSuccess("[build] SUCCESS")
|
|
|
|
endif
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let &makeprg = default_makeprg
|
|
|
|
let $GOPATH = old_gopath
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2017-03-14 11:16:07 -04:00
|
|
|
" BuildTags sets or shows the current build tags used for tools
|
|
|
|
function! go#cmd#BuildTags(bang, ...) abort
|
|
|
|
if a:0
|
|
|
|
if a:0 == 1 && a:1 == '""'
|
|
|
|
unlet g:go_build_tags
|
|
|
|
call go#util#EchoSuccess("build tags are cleared")
|
|
|
|
else
|
|
|
|
let g:go_build_tags = a:1
|
|
|
|
call go#util#EchoSuccess("build tags are changed to: ". a:1)
|
|
|
|
endif
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !exists('g:go_build_tags')
|
|
|
|
call go#util#EchoSuccess("build tags are not set")
|
|
|
|
else
|
|
|
|
call go#util#EchoSuccess("current build tags: ". g:go_build_tags)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2015-12-16 08:53:53 -05:00
|
|
|
" Run runs the current file (and their dependencies if any) in a new terminal.
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#cmd#RunTerm(bang, mode, files) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
if empty(a:files)
|
|
|
|
let cmd = "go run ". go#util#Shelljoin(go#tool#Files())
|
|
|
|
else
|
|
|
|
let cmd = "go run ". go#util#Shelljoin(map(copy(a:files), "expand(v:val)"), 1)
|
|
|
|
endif
|
|
|
|
call go#term#newmode(a:bang, cmd, a:mode)
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
" Run runs the current file (and their dependencies if any) and outputs it.
|
2017-07-06 08:57:35 -04:00
|
|
|
" This is intended to test small programs and play with them. It's not
|
2015-07-13 06:22:46 -04:00
|
|
|
" suitable for long running apps, because vim is blocking by default and
|
|
|
|
" calling long running apps will block the whole UI.
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#cmd#Run(bang, ...) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
if has('nvim')
|
|
|
|
call go#cmd#RunTerm(a:bang, '', a:000)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
if go#util#has_job()
|
|
|
|
" NOTE(arslan): 'term': 'open' case is not implement for +jobs. This means
|
|
|
|
" executions waiting for stdin will not work. That's why we don't do
|
|
|
|
" anything. Once this is implemented we're going to make :GoRun async
|
|
|
|
endif
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let old_gopath = $GOPATH
|
|
|
|
let $GOPATH = go#path#Detect()
|
|
|
|
|
|
|
|
if go#util#IsWin()
|
|
|
|
exec '!go run ' . go#util#Shelljoin(go#tool#Files())
|
|
|
|
if v:shell_error
|
|
|
|
redraws! | echon "vim-go: [run] " | echohl ErrorMsg | echon "FAILED"| echohl None
|
2015-07-13 06:22:46 -04:00
|
|
|
else
|
2016-06-26 07:12:36 -04:00
|
|
|
redraws! | echon "vim-go: [run] " | echohl Function | echon "SUCCESS"| echohl None
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
let $GOPATH = old_gopath
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
" :make expands '%' and '#' wildcards, so they must also be escaped
|
|
|
|
let default_makeprg = &makeprg
|
|
|
|
if a:0 == 0
|
|
|
|
let &makeprg = 'go run ' . go#util#Shelljoin(go#tool#Files(), 1)
|
|
|
|
else
|
|
|
|
let &makeprg = "go run " . go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:listtype = go#list#Type("quickfix")
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
if l:listtype == "locationlist"
|
2016-06-26 07:12:36 -04:00
|
|
|
exe 'lmake!'
|
|
|
|
else
|
|
|
|
exe 'make!'
|
|
|
|
endif
|
|
|
|
|
|
|
|
let items = go#list#Get(l:listtype)
|
|
|
|
let errors = go#tool#FilterValids(items)
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#list#Populate(l:listtype, errors, &makeprg)
|
2016-06-26 07:12:36 -04:00
|
|
|
call go#list#Window(l:listtype, len(errors))
|
|
|
|
if !empty(errors) && !a:bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let $GOPATH = old_gopath
|
|
|
|
let &makeprg = default_makeprg
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
" Install installs the package by simple calling 'go install'. If any argument
|
2016-12-27 09:46:49 -05:00
|
|
|
" is given(which are passed directly to 'go install') it tries to install
|
|
|
|
" those packages. Errors are populated in the location window.
|
|
|
|
function! go#cmd#Install(bang, ...) abort
|
|
|
|
" use vim's job functionality to call it asynchronously
|
|
|
|
if go#util#has_job()
|
|
|
|
" expand all wildcards(i.e: '%' to the current file name)
|
|
|
|
let goargs = map(copy(a:000), "expand(v:val)")
|
|
|
|
|
|
|
|
if get(g:, 'go_echo_command_info', 1)
|
|
|
|
call go#util#EchoProgress("installing dispatched ...")
|
|
|
|
endif
|
|
|
|
|
|
|
|
call s:cmd_job({
|
|
|
|
\ 'cmd': ['go', 'install'] + goargs,
|
|
|
|
\ 'bang': a:bang,
|
|
|
|
\})
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2016-08-02 08:48:32 -04:00
|
|
|
let old_gopath = $GOPATH
|
|
|
|
let $GOPATH = go#path#Detect()
|
2016-06-26 07:12:36 -04:00
|
|
|
let default_makeprg = &makeprg
|
|
|
|
|
|
|
|
" :make expands '%' and '#' wildcards, so they must also be escaped
|
|
|
|
let goargs = go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
|
|
|
|
let &makeprg = "go install " . goargs
|
|
|
|
|
|
|
|
let l:listtype = go#list#Type("quickfix")
|
|
|
|
" execute make inside the source folder so we can parse the errors
|
|
|
|
" correctly
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let dir = getcwd()
|
|
|
|
try
|
|
|
|
execute cd . fnameescape(expand("%:p:h"))
|
2016-12-27 09:46:49 -05:00
|
|
|
if l:listtype == "locationlist"
|
2016-06-26 07:12:36 -04:00
|
|
|
silent! exe 'lmake!'
|
2015-12-08 08:20:04 -05:00
|
|
|
else
|
2016-06-26 07:12:36 -04:00
|
|
|
silent! exe 'make!'
|
|
|
|
endif
|
|
|
|
redraw!
|
|
|
|
finally
|
|
|
|
execute cd . fnameescape(dir)
|
|
|
|
endtry
|
|
|
|
|
|
|
|
let errors = go#list#Get(l:listtype)
|
|
|
|
call go#list#Window(l:listtype, len(errors))
|
2016-12-27 09:46:49 -05:00
|
|
|
if !empty(errors) && !a:bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
2016-06-26 07:12:36 -04:00
|
|
|
else
|
2017-04-01 07:22:06 -04:00
|
|
|
call go#util#EchoSuccess("installed to ". go#path#Detect())
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-08-02 08:48:32 -04:00
|
|
|
let $GOPATH = old_gopath
|
2016-06-26 07:12:36 -04:00
|
|
|
let &makeprg = default_makeprg
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
" Generate runs 'go generate' in similar fashion to go#cmd#Build()
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#cmd#Generate(bang, ...) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let default_makeprg = &makeprg
|
|
|
|
|
|
|
|
let old_gopath = $GOPATH
|
|
|
|
let $GOPATH = go#path#Detect()
|
|
|
|
|
|
|
|
" :make expands '%' and '#' wildcards, so they must also be escaped
|
|
|
|
let goargs = go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
|
|
|
|
if go#util#ShellError() != 0
|
|
|
|
let &makeprg = "go generate " . goargs
|
|
|
|
else
|
|
|
|
let gofiles = go#util#Shelljoin(go#tool#Files(), 1)
|
|
|
|
let &makeprg = "go generate " . goargs . ' ' . gofiles
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:listtype = go#list#Type("quickfix")
|
|
|
|
|
|
|
|
echon "vim-go: " | echohl Identifier | echon "generating ..."| echohl None
|
2016-12-27 09:46:49 -05:00
|
|
|
if l:listtype == "locationlist"
|
2016-06-26 07:12:36 -04:00
|
|
|
silent! exe 'lmake!'
|
|
|
|
else
|
|
|
|
silent! exe 'make!'
|
|
|
|
endif
|
|
|
|
redraw!
|
|
|
|
|
|
|
|
let errors = go#list#Get(l:listtype)
|
|
|
|
call go#list#Window(l:listtype, len(errors))
|
2017-03-07 12:04:28 -05:00
|
|
|
if !empty(errors)
|
2016-06-26 07:12:36 -04:00
|
|
|
if !a:bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
2015-07-13 06:22:46 -04:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
else
|
|
|
|
redraws! | echon "vim-go: " | echohl Function | echon "[generate] SUCCESS"| echohl None
|
|
|
|
endif
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let &makeprg = default_makeprg
|
|
|
|
let $GOPATH = old_gopath
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
" ---------------------
|
|
|
|
" | Vim job callbacks |
|
|
|
|
" ---------------------
|
|
|
|
|
|
|
|
function s:cmd_job(args) abort
|
|
|
|
let status_dir = expand('%:p:h')
|
|
|
|
let started_at = reltime()
|
|
|
|
|
|
|
|
call go#statusline#Update(status_dir, {
|
|
|
|
\ 'desc': "current status",
|
|
|
|
\ 'type': a:args.cmd[1],
|
|
|
|
\ 'state': "started",
|
|
|
|
\})
|
|
|
|
|
|
|
|
" autowrite is not enabled for jobs
|
|
|
|
call go#cmd#autowrite()
|
|
|
|
|
|
|
|
function! s:error_info_cb(job, exit_status, data) closure abort
|
|
|
|
let status = {
|
|
|
|
\ 'desc': 'last status',
|
|
|
|
\ 'type': a:args.cmd[1],
|
|
|
|
\ 'state': "success",
|
|
|
|
\ }
|
|
|
|
|
|
|
|
if a:exit_status
|
|
|
|
let status.state = "failed"
|
|
|
|
endif
|
|
|
|
|
|
|
|
let elapsed_time = reltimestr(reltime(started_at))
|
|
|
|
" strip whitespace
|
|
|
|
let elapsed_time = substitute(elapsed_time, '^\s*\(.\{-}\)\s*$', '\1', '')
|
|
|
|
let status.state .= printf(" (%ss)", elapsed_time)
|
|
|
|
|
|
|
|
call go#statusline#Update(status_dir, status)
|
|
|
|
endfunction
|
|
|
|
|
2017-02-11 08:01:38 -05:00
|
|
|
let a:args.error_info_cb = funcref('s:error_info_cb')
|
2016-12-27 09:46:49 -05:00
|
|
|
let callbacks = go#job#Spawn(a:args)
|
|
|
|
|
|
|
|
let start_options = {
|
|
|
|
\ 'callback': callbacks.callback,
|
2017-05-02 08:42:08 -04:00
|
|
|
\ 'exit_cb': callbacks.exit_cb,
|
2016-12-27 09:46:49 -05:00
|
|
|
\ }
|
|
|
|
|
|
|
|
" modify GOPATH if needed
|
|
|
|
let old_gopath = $GOPATH
|
|
|
|
let $GOPATH = go#path#Detect()
|
|
|
|
|
|
|
|
" pre start
|
|
|
|
let dir = getcwd()
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let jobdir = fnameescape(expand("%:p:h"))
|
|
|
|
execute cd . jobdir
|
|
|
|
|
|
|
|
call job_start(a:args.cmd, start_options)
|
|
|
|
|
|
|
|
" post start
|
|
|
|
execute cd . fnameescape(dir)
|
|
|
|
let $GOPATH = old_gopath
|
|
|
|
endfunction
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|