vimrc/sources_non_forked/vim-go/autoload/go/cmd.vim

308 lines
9.1 KiB
VimL
Raw Normal View History

2015-07-13 10:22:46 +00:00
if !exists("g:go_dispatch_enabled")
let g:go_dispatch_enabled = 0
2014-10-31 21:30:24 +00:00
endif
2015-07-13 10:22:46 +00:00
function! go#cmd#autowrite()
if &autowrite == 1
silent wall
endif
endfunction
2015-12-08 13:20:04 +00:00
2015-07-13 10:22:46 +00:00
" Build buils the source code without producting any output binary. We live in
" 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.
function! go#cmd#Build(bang, ...)
2015-12-16 13:53:53 +00:00
" expand all wildcards(i.e: '%' to the current file name)
let goargs = map(copy(a:000), "expand(v:val)")
2015-07-13 10:22:46 +00:00
2015-12-16 13:53:53 +00:00
" escape all shell arguments before we pass it to make
let goargs = go#util#Shelllist(goargs, 1)
2015-07-13 10:22:46 +00:00
2015-12-16 13:53:53 +00:00
" create our command arguments. go build discards any results when it
" compiles multiple packages. So we pass the `errors` package just as an
" placeholder with the current folder (indicated with '.')
let args = ["build"] + goargs + [".", "errors"]
2015-12-08 13:20:04 +00:00
2015-12-16 13:53:53 +00:00
" if we have nvim, call it asynchronously and return early ;)
if has('nvim')
call go#jobcontrol#Spawn("build", args)
return
2015-07-13 10:22:46 +00:00
endif
2015-12-16 13:53:53 +00:00
let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()
let default_makeprg = &makeprg
let &makeprg = "go " . join(args, ' ')
2015-07-13 10:22:46 +00:00
if g:go_dispatch_enabled && exists(':Make') == 2
2015-12-16 13:53:53 +00:00
call go#util#EchoProgress("building dispatched ...")
2015-07-13 10:22:46 +00:00
silent! exe 'Make'
else
2015-12-08 13:20:04 +00:00
silent! exe 'lmake!'
2015-07-13 10:22:46 +00:00
endif
redraw!
2015-12-08 13:20:04 +00:00
let errors = go#list#Get()
call go#list#Window(len(errors))
2015-12-16 13:53:53 +00:00
if !empty(errors)
2015-07-13 10:22:46 +00:00
if !a:bang
2015-12-08 13:20:04 +00:00
call go#list#JumpToFirst()
2015-07-13 10:22:46 +00:00
endif
else
2015-12-16 13:53:53 +00:00
call go#util#EchoSuccess("[build] SUCCESS")
2015-07-13 10:22:46 +00:00
endif
let &makeprg = default_makeprg
let $GOPATH = old_gopath
endfunction
2015-12-16 13:53:53 +00:00
" Run runs the current file (and their dependencies if any) in a new terminal.
function! go#cmd#RunTerm(mode)
let cmd = "go run ". go#util#Shelljoin(go#tool#Files())
call go#term#newmode(cmd, a:mode)
endfunction
2015-07-13 10:22:46 +00:00
" Run runs the current file (and their dependencies if any) and outputs it.
" This is intented to test small programs and play with them. It's not
" suitable for long running apps, because vim is blocking by default and
" calling long running apps will block the whole UI.
2014-10-31 21:30:24 +00:00
function! go#cmd#Run(bang, ...)
2015-12-16 13:53:53 +00:00
if has('nvim')
call go#cmd#RunTerm('')
return
endif
2015-07-13 10:22:46 +00:00
let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()
if go#util#IsWin()
2015-12-08 13:20:04 +00:00
exec '!go run ' . go#util#Shelljoin(go#tool#Files())
2015-01-18 12:58:28 +00:00
if v:shell_error
redraws! | echon "vim-go: [run] " | echohl ErrorMsg | echon "FAILED"| echohl None
else
redraws! | echon "vim-go: [run] " | echohl Function | echon "SUCCESS"| echohl None
endif
2015-07-13 10:22:46 +00:00
let $GOPATH = old_gopath
2015-01-18 12:58:28 +00:00
return
endif
2015-12-08 13:20:04 +00:00
" :make expands '%' and '#' wildcards, so they must also be escaped
2014-10-31 21:30:24 +00:00
let default_makeprg = &makeprg
2015-12-08 13:20:04 +00:00
if a:0 == 0
let &makeprg = 'go run ' . go#util#Shelljoin(go#tool#Files(), 1)
2014-10-31 21:30:24 +00:00
else
2015-12-08 13:20:04 +00:00
let &makeprg = "go run " . go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
2014-10-31 21:30:24 +00:00
endif
2015-07-13 10:22:46 +00:00
if g:go_dispatch_enabled && exists(':Make') == 2
2015-12-08 13:20:04 +00:00
silent! exe 'Make'
2015-07-13 10:22:46 +00:00
else
2015-12-08 13:20:04 +00:00
exe 'lmake!'
2015-07-13 10:22:46 +00:00
endif
2015-12-08 13:20:04 +00:00
let items = go#list#Get()
2015-12-16 13:53:53 +00:00
let errors = go#tool#FilterValids(items)
2015-12-08 13:20:04 +00:00
call go#list#Populate(errors)
call go#list#Window(len(errors))
2015-07-13 10:22:46 +00:00
if !empty(errors) && !a:bang
2015-12-08 13:20:04 +00:00
call go#list#JumpToFirst()
2014-10-31 21:30:24 +00:00
endif
2015-07-13 10:22:46 +00:00
let $GOPATH = old_gopath
2014-10-31 21:30:24 +00:00
let &makeprg = default_makeprg
endfunction
2015-07-13 10:22:46 +00:00
" Install installs the package by simple calling 'go install'. If any argument
2015-12-08 13:20:04 +00:00
" is given(which are passed directly to 'go instal') it tries to install those
" packages. Errors are populated in the location window.
2015-07-13 10:22:46 +00:00
function! go#cmd#Install(bang, ...)
2015-12-08 13:20:04 +00:00
let command = 'go install ' . go#util#Shelljoin(a:000)
2015-07-13 10:22:46 +00:00
call go#cmd#autowrite()
2014-10-31 21:30:24 +00:00
let out = go#tool#ExecuteInDir(command)
if v:shell_error
2015-12-08 13:20:04 +00:00
let errors = go#tool#ParseErrors(split(out, '\n'))
call go#list#Populate(errors)
call go#list#Window(len(errors))
2015-07-13 10:22:46 +00:00
if !empty(errors) && !a:bang
2015-12-08 13:20:04 +00:00
call go#list#JumpToFirst()
2014-10-31 21:30:24 +00:00
endif
2015-07-13 10:22:46 +00:00
return
2015-12-08 13:20:04 +00:00
else
call go#list#Clean()
call go#list#Window()
2014-10-31 21:30:24 +00:00
endif
2015-07-13 10:22:46 +00:00
echon "vim-go: " | echohl Function | echon "installed to ". $GOPATH | echohl None
2014-10-31 21:30:24 +00:00
endfunction
2015-07-13 10:22:46 +00:00
" Test runs `go test` in the current directory. If compile is true, it'll
" compile the tests instead of running them (useful to catch errors in the
" test files). Any other argument is appendend to the final `go test` command
function! go#cmd#Test(bang, compile, ...)
2015-12-16 13:53:53 +00:00
let args = ["test"]
2015-03-14 20:02:10 +00:00
" don't run the test, only compile it. Useful to capture and fix errors or
" to create a test binary.
if a:compile
2015-12-16 13:53:53 +00:00
call add(args, "-c")
2015-03-14 20:02:10 +00:00
endif
2015-12-08 13:20:04 +00:00
if a:0
2015-12-16 13:53:53 +00: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 test
call extend(args, go#util#Shelllist(goargs, 1))
2015-12-08 13:20:04 +00:00
else
" only add this if no custom flags are passed
let timeout = get(g:, 'go_test_timeout', '10s')
2015-12-16 13:53:53 +00:00
call add(args, printf("-timeout=%s", timeout))
endif
if has('nvim')
if get(g:, 'go_term_enabled', 0)
call go#term#new(["go"] + args)
else
call go#jobcontrol#Spawn("test", args)
endif
return
2015-07-13 10:22:46 +00:00
endif
2015-03-14 20:02:10 +00:00
if a:compile
echon "vim-go: " | echohl Identifier | echon "compiling tests ..." | echohl None
else
echon "vim-go: " | echohl Identifier | echon "testing ..." | echohl None
2014-10-31 21:30:24 +00:00
endif
2015-12-16 13:53:53 +00:00
call go#cmd#autowrite()
2015-01-18 12:58:28 +00:00
redraw
2015-12-16 13:53:53 +00:00
let command = "go " . join(args, ' ')
2014-10-31 21:30:24 +00:00
let out = go#tool#ExecuteInDir(command)
if v:shell_error
2015-12-08 13:20:04 +00:00
let errors = go#tool#ParseErrors(split(out, '\n'))
2015-12-16 13:53:53 +00:00
let errors = go#tool#FilterValids(errors)
2015-12-08 13:20:04 +00:00
call go#list#Populate(errors)
call go#list#Window(len(errors))
2015-07-13 10:22:46 +00:00
if !empty(errors) && !a:bang
2015-12-08 13:20:04 +00:00
call go#list#JumpToFirst()
2014-10-31 21:30:24 +00:00
endif
2015-01-18 12:58:28 +00:00
echon "vim-go: " | echohl ErrorMsg | echon "[test] FAIL" | echohl None
2014-10-31 21:30:24 +00:00
else
2015-12-08 13:20:04 +00:00
call go#list#Clean()
call go#list#Window()
2015-03-14 20:02:10 +00:00
if a:compile
echon "vim-go: " | echohl Function | echon "[test] SUCCESS" | echohl None
else
echon "vim-go: " | echohl Function | echon "[test] PASS" | echohl None
endif
2014-10-31 21:30:24 +00:00
endif
endfunction
2015-07-13 10:22:46 +00:00
" Testfunc runs a single test that surrounds the current cursor position.
" Arguments are passed to the `go test` command.
function! go#cmd#TestFunc(bang, ...)
" search flags legend (used only)
" 'b' search backward instead of forward
" 'c' accept a match at the cursor position
" 'n' do Not move the cursor
" 'W' don't wrap around the end of the file
"
" for the full list
" :help search
let test = search("func Test", "bcnW")
if test == 0
echo "vim-go: [test] no test found immediate to cursor"
return
end
let line = getline(test)
let name = split(split(line, " ")[1], "(")[0]
2015-12-08 13:20:04 +00:00
let args = [a:bang, 0, "-run", name . "$"]
2015-07-13 10:22:46 +00:00
2015-12-08 13:20:04 +00:00
if a:0
call extend(args, a:000)
2015-07-13 10:22:46 +00:00
endif
2015-12-08 13:20:04 +00:00
call call('go#cmd#Test', args)
2015-07-13 10:22:46 +00:00
endfunction
" Coverage creates a new cover profile with 'go test -coverprofile' and opens
" a new HTML coverage page from that profile.
function! go#cmd#Coverage(bang, ...)
2014-10-31 21:30:24 +00:00
let l:tmpname=tempname()
2015-12-08 13:20:04 +00:00
let command = "go test -coverprofile=" . l:tmpname . ' ' . go#util#Shelljoin(a:000)
2014-10-31 21:30:24 +00:00
2015-07-13 10:22:46 +00:00
call go#cmd#autowrite()
2014-10-31 21:30:24 +00:00
let out = go#tool#ExecuteInDir(command)
if v:shell_error
2015-12-08 13:20:04 +00:00
let errors = go#tool#ParseErrors(split(out, '\n'))
call go#list#Populate(errors)
call go#list#Window(len(errors))
if !empty(errors) && !a:bang
call go#list#JumpToFirst()
endif
2014-10-31 21:30:24 +00:00
else
2015-12-08 13:20:04 +00:00
" clear previous location list
call go#list#Clean()
call go#list#Window()
2014-10-31 21:30:24 +00:00
let openHTML = 'go tool cover -html='.l:tmpname
call go#tool#ExecuteInDir(openHTML)
endif
2015-12-08 13:20:04 +00:00
2014-10-31 21:30:24 +00:00
call delete(l:tmpname)
endfunction
2015-07-13 10:22:46 +00:00
" Generate runs 'go generate' in similar fashion to go#cmd#Build()
function! go#cmd#Generate(bang, ...)
let default_makeprg = &makeprg
let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()
2015-12-08 13:20:04 +00:00
" :make expands '%' and '#' wildcards, so they must also be escaped
let goargs = go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
2015-07-13 10:22:46 +00:00
if v:shell_error
2015-12-08 13:20:04 +00:00
let &makeprg = "go generate " . goargs
2015-07-13 10:22:46 +00:00
else
2015-12-08 13:20:04 +00:00
let gofiles = go#util#Shelljoin(go#tool#Files(), 1)
let &makeprg = "go generate " . goargs . ' ' . gofiles
2015-07-13 10:22:46 +00:00
endif
echon "vim-go: " | echohl Identifier | echon "generating ..."| echohl None
if g:go_dispatch_enabled && exists(':Make') == 2
silent! exe 'Make'
else
2015-12-08 13:20:04 +00:00
silent! exe 'lmake!'
2015-07-13 10:22:46 +00:00
endif
redraw!
2015-12-08 13:20:04 +00:00
let errors = go#list#Get()
call go#list#Window(len(errors))
2015-07-13 10:22:46 +00:00
if !empty(errors)
if !a:bang
2015-12-08 13:20:04 +00:00
call go#list#JumpToFirst()
2015-07-13 10:22:46 +00:00
endif
else
redraws! | echon "vim-go: " | echohl Function | echon "[generate] SUCCESS"| echohl None
endif
let &makeprg = default_makeprg
let $GOPATH = old_gopath
endfunction
2014-10-31 21:30:24 +00:00
" vim:ts=4:sw=4:et