1
0
Fork 0
mirror of synced 2024-11-12 20:28:58 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/cmd.vim

307 lines
8.7 KiB
VimL
Raw Normal View History

2018-12-17 06:28:27 -05:00
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
2016-12-27 09:46:49 -05:00
function! go#cmd#autowrite() abort
2018-02-04 06:35:08 -05:00
if &autowrite == 1 || &autowriteall == 1
2016-12-27 09:46:49 -05:00
silent! wall
2018-06-14 06:31:12 -04:00
else
for l:nr in range(0, bufnr('$'))
if buflisted(l:nr) && getbufvar(l:nr, '&modified')
" Sleep one second to make sure people see the message. Otherwise it is
" often immediately overwritten by the async messages (which also
" doesn't invoke the "hit ENTER" prompt).
2018-06-14 06:31:12 -04:00
call go#util#EchoWarning('[No write since last change]')
sleep 1
return
endif
endfor
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
" Create our command arguments. go build discards any results when it
2016-06-26 07:12:36 -04:00
" compiles multiple packages. So we pass the `errors` package just as an
2019-01-08 05:11:54 -05:00
" placeholder with the current folder (indicated with '.').
2018-06-14 06:31:12 -04:00
let l:args =
\ ['build', '-tags', go#config#BuildTags()] +
\ map(copy(a:000), "expand(v:val)") +
2018-03-31 10:56:26 -04:00
\ [".", "errors"]
2016-06-26 07:12:36 -04:00
2018-07-19 08:52:53 -04:00
" Vim and Neovim async.
2018-09-24 20:40:17 -04:00
if go#util#has_job()
2016-12-27 09:46:49 -05:00
call s:cmd_job({
\ 'cmd': ['go'] + args,
\ 'bang': a:bang,
\ 'for': 'GoBuild',
2018-09-24 20:40:17 -04:00
\ 'statustype': 'build'
2016-12-27 09:46:49 -05:00
\})
" Vim 7.4 without async
else
let default_makeprg = &makeprg
let &makeprg = "go " . join(go#util#Shelllist(args), ' ')
let l:listtype = go#list#Type("GoBuild")
" 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"))
if l:listtype == "locationlist"
silent! exe 'lmake!'
else
silent! exe 'make!'
endif
redraw!
finally
execute cd . fnameescape(dir)
2019-01-08 05:11:54 -05:00
let &makeprg = default_makeprg
endtry
let errors = go#list#Get(l:listtype)
call go#list#Window(l:listtype, len(errors))
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")
2015-07-13 06:22:46 -04:00
endif
2016-06-26 07:12:36 -04:00
endif
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
2018-06-14 06:31:12 -04:00
let v = a:1
if v == '""' || v == "''"
let v = ""
endif
call go#config#SetBuildTags(v)
let tags = go#config#BuildTags()
if empty(tags)
2017-03-14 11:16:07 -04:00
call go#util#EchoSuccess("build tags are cleared")
else
2018-06-14 06:31:12 -04:00
call go#util#EchoSuccess("build tags are changed to: " . tags)
2017-03-14 11:16:07 -04:00
endif
return
endif
2018-06-14 06:31:12 -04:00
let tags = go#config#BuildTags()
if empty(tags)
2017-03-14 11:16:07 -04:00
call go#util#EchoSuccess("build tags are not set")
else
2018-06-14 06:31:12 -04:00
call go#util#EchoSuccess("current build tags: " . tags)
2017-03-14 11:16:07 -04:00
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
2018-07-04 06:53:25 -04:00
let cmd = "go run "
let tags = go#config#BuildTags()
if len(tags) > 0
let cmd .= "-tags " . go#util#Shellescape(tags) . " "
endif
2016-06-26 07:12:36 -04:00
if empty(a:files)
2018-07-04 06:53:25 -04:00
let cmd .= go#util#Shelljoin(go#tool#Files())
2016-06-26 07:12:36 -04:00
else
2018-07-04 06:53:25 -04:00
let cmd .= go#util#Shelljoin(map(copy(a:files), "expand(v:val)"), 1)
2016-06-26 07:12:36 -04:00
endif
2019-05-16 15:48:47 -04:00
call go#term#newmode(a:bang, cmd, s:runerrorformat(), 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
2018-07-04 06:53:25 -04:00
let cmd = "go run "
let tags = go#config#BuildTags()
if len(tags) > 0
let cmd .= "-tags " . go#util#Shellescape(tags) . " "
endif
2016-06-26 07:12:36 -04:00
if go#util#IsWin()
2018-07-30 17:18:16 -04:00
if a:0 == 0
exec '!' . cmd . go#util#Shelljoin(go#tool#Files(), 1)
else
exec '!' . cmd . go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
endif
2016-06-26 07:12:36 -04:00
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
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
2018-07-04 06:53:25 -04:00
let &makeprg = cmd . go#util#Shelljoin(go#tool#Files(), 1)
2016-06-26 07:12:36 -04:00
else
2018-07-04 06:53:25 -04:00
let &makeprg = cmd . go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
2016-06-26 07:12:36 -04:00
endif
let l:listtype = go#list#Type("GoRun")
2016-06-26 07:12:36 -04:00
2019-01-08 05:11:54 -05:00
try
2019-05-16 15:48:47 -04:00
" backup user's errorformat, will be restored once we are finished
let l:old_errorformat = &errorformat
let &errorformat = s:runerrorformat()
2019-01-08 05:11:54 -05:00
if l:listtype == "locationlist"
exe 'lmake!'
else
exe 'make!'
endif
finally
2019-05-16 15:48:47 -04:00
"restore errorformat
let &errorformat = l:old_errorformat
2019-01-08 05:11:54 -05:00
let &makeprg = default_makeprg
endtry
2016-06-26 07:12:36 -04:00
2019-05-16 15:48:47 -04:00
let l:errors = go#list#Get(l:listtype)
2016-06-26 07:12:36 -04:00
2019-05-16 15:48:47 -04:00
call go#list#Window(l:listtype, len(l:errors))
if !empty(l:errors) && !a:bang
2016-06-26 07:12:36 -04:00
call go#list#JumpToFirst(l:listtype)
endif
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)")
call s:cmd_job({
2018-06-14 06:31:12 -04:00
\ 'cmd': ['go', 'install', '-tags', go#config#BuildTags()] + goargs,
2016-12-27 09:46:49 -05:00
\ 'bang': a:bang,
\ 'for': 'GoInstall',
2018-09-24 20:40:17 -04:00
\ 'statustype': 'install'
2016-12-27 09:46:49 -05:00
\})
return
endif
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("GoInstall")
2016-06-26 07:12:36 -04:00
" 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)
2019-01-08 05:11:54 -05:00
let &makeprg = default_makeprg
2016-06-26 07:12:36 -04:00
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
call go#util#EchoSuccess("installed to ". go#path#Default())
2016-06-26 07:12:36 -04:00
endif
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
" :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("GoGenerate")
2016-06-26 07:12:36 -04:00
echon "vim-go: " | echohl Identifier | echon "generating ..."| echohl None
2019-01-08 05:11:54 -05:00
try
if l:listtype == "locationlist"
silent! exe 'lmake!'
else
silent! exe 'make!'
endif
finally
redraw!
let &makeprg = default_makeprg
endtry
2016-06-26 07:12:36 -04:00
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
endfunction
2014-10-31 17:30:24 -04:00
2019-05-16 15:48:47 -04:00
function! s:runerrorformat()
let l:panicaddress = "%\\t%#%f:%l +0x%[0-9A-Fa-f]%\\+"
let l:errorformat = '%A' . l:panicaddress . "," . &errorformat
return l:errorformat
endfunction
2016-12-27 09:46:49 -05:00
" ---------------------
" | Vim job callbacks |
" ---------------------
2018-09-24 20:40:17 -04:00
function! s:cmd_job(args) abort
2016-12-27 09:46:49 -05:00
" autowrite is not enabled for jobs
call go#cmd#autowrite()
2018-07-19 08:52:53 -04:00
call go#job#Spawn(a:args.cmd, a:args)
2016-12-27 09:46:49 -05:00
endfunction
2018-12-17 06:28:27 -05:00
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
2016-06-26 07:12:36 -04:00
" vim: sw=2 ts=2 et