2015-07-13 06:22:46 -04:00
|
|
|
if !exists("g:go_dispatch_enabled")
|
2016-06-26 14:30:37 -04:00
|
|
|
let g:go_dispatch_enabled = 0
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
function! go#cmd#autowrite()
|
2016-06-26 14:30:37 -04:00
|
|
|
if &autowrite == 1
|
|
|
|
silent wall
|
|
|
|
endif
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
|
2016-02-20 08:13:10 -05:00
|
|
|
" Build builds the source code without producting 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.
|
|
|
|
function! go#cmd#Build(bang, ...)
|
2016-06-26 14:30:37 -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
|
|
|
|
let goargs = go#util#Shelllist(goargs, 1)
|
|
|
|
|
|
|
|
" 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"]
|
|
|
|
|
|
|
|
" if we have nvim, call it asynchronously and return early ;)
|
|
|
|
if has('nvim')
|
|
|
|
call go#util#EchoProgress("building dispatched ...")
|
|
|
|
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"))
|
|
|
|
if g:go_dispatch_enabled && exists(':Make') == 2
|
|
|
|
call go#util#EchoProgress("building dispatched ...")
|
|
|
|
silent! exe 'Make'
|
|
|
|
elseif l:listtype == "locationlist"
|
|
|
|
silent! exe 'lmake!'
|
|
|
|
else
|
|
|
|
silent! exe 'make!'
|
2015-07-13 06:22:46 -04:00
|
|
|
endif
|
2016-06-26 14:30:37 -04:00
|
|
|
redraw!
|
|
|
|
finally
|
|
|
|
execute cd . fnameescape(dir)
|
|
|
|
endtry
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
let errors = go#list#Get(l:listtype)
|
|
|
|
call go#list#Window(l:listtype, len(errors))
|
2015-12-08 08:20:04 -05:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
if !empty(errors)
|
|
|
|
if !a:bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
2015-07-13 06:22:46 -04:00
|
|
|
endif
|
2016-06-26 14:30:37 -04:00
|
|
|
else
|
|
|
|
call go#util#EchoSuccess("[build] SUCCESS")
|
|
|
|
endif
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 14:30:37 -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
|
|
|
|
|
|
|
" Run runs the current file (and their dependencies if any) in a new terminal.
|
2016-03-14 06:04:57 -04:00
|
|
|
function! go#cmd#RunTerm(bang, mode, files)
|
2016-06-26 14:30:37 -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.
|
|
|
|
" 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 17:30:24 -04:00
|
|
|
function! go#cmd#Run(bang, ...)
|
2016-06-26 14:30:37 -04:00
|
|
|
if has('nvim')
|
|
|
|
call go#cmd#RunTerm(a:bang, '', a:000)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
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 14:30:37 -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 14:30:37 -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")
|
|
|
|
|
|
|
|
if g:go_dispatch_enabled && exists(':Make') == 2
|
|
|
|
silent! exe 'Make'
|
|
|
|
elseif l:listtype == "locationlist"
|
|
|
|
exe 'lmake!'
|
|
|
|
else
|
|
|
|
exe 'make!'
|
|
|
|
endif
|
|
|
|
|
|
|
|
let items = go#list#Get(l:listtype)
|
|
|
|
let errors = go#tool#FilterValids(items)
|
|
|
|
|
|
|
|
call go#list#Populate(l:listtype, errors)
|
|
|
|
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-02-20 08:13:10 -05:00
|
|
|
" is given(which are passed directly to 'go install') it tries to install those
|
2015-12-08 08:20:04 -05:00
|
|
|
" packages. Errors are populated in the location window.
|
2015-07-13 06:22:46 -04:00
|
|
|
function! go#cmd#Install(bang, ...)
|
2016-06-26 14:30:37 -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"))
|
|
|
|
if g:go_dispatch_enabled && exists(':Make') == 2
|
|
|
|
call go#util#EchoProgress("building dispatched ...")
|
|
|
|
silent! exe 'Make'
|
|
|
|
elseif l:listtype == "locationlist"
|
|
|
|
silent! exe 'lmake!'
|
2015-12-08 08:20:04 -05:00
|
|
|
else
|
2016-06-26 14:30:37 -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))
|
|
|
|
if !empty(errors)
|
|
|
|
if !a:bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
2016-06-26 14:30:37 -04:00
|
|
|
else
|
|
|
|
redraws! | echon "vim-go: " | echohl Function | echon "installed to ". $GOPATH | echohl None
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
let &makeprg = default_makeprg
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-07-13 06:22:46 -04: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, ...)
|
2016-06-26 14:30:37 -04:00
|
|
|
let args = ["test"]
|
2015-03-14 16:02:10 -04:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
" don't run the test, only compile it. Useful to capture and fix errors.
|
|
|
|
if a:compile
|
|
|
|
let compile_file = "vim-go-test-compile"
|
|
|
|
call extend(args, ["-c", "-o", compile_file])
|
|
|
|
endif
|
2015-03-14 16:02:10 -04:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
if a:0
|
|
|
|
" expand all wildcards(i.e: '%' to the current file name)
|
|
|
|
let goargs = map(copy(a:000), "expand(v:val)")
|
|
|
|
if !has('nvim')
|
|
|
|
let goargs = go#util#Shelllist(goargs, 1)
|
|
|
|
endif
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
call extend(args, goargs, 1)
|
|
|
|
else
|
|
|
|
" only add this if no custom flags are passed
|
|
|
|
let timeout = get(g:, 'go_test_timeout', '10s')
|
|
|
|
call add(args, printf("-timeout=%s", timeout))
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:compile
|
|
|
|
echon "vim-go: " | echohl Identifier | echon "compiling tests ..." | echohl None
|
|
|
|
else
|
|
|
|
echon "vim-go: " | echohl Identifier | echon "testing ..." | echohl None
|
|
|
|
endif
|
|
|
|
|
|
|
|
if has('nvim')
|
|
|
|
if get(g:, 'go_term_enabled', 0)
|
|
|
|
let id = go#term#new(a:bang, ["go"] + args)
|
2015-12-08 08:20:04 -05:00
|
|
|
else
|
2016-06-26 14:30:37 -04:00
|
|
|
let id = go#jobcontrol#Spawn(a:bang, "test", args)
|
2015-12-16 08:53:53 -05:00
|
|
|
endif
|
|
|
|
|
2016-02-20 08:13:10 -05:00
|
|
|
if a:compile
|
2016-06-26 14:30:37 -04:00
|
|
|
call go#jobcontrol#AddHandler(function('s:test_compile_handler'))
|
|
|
|
let s:test_compile_handlers[id] = compile_file
|
2016-02-20 08:13:10 -05:00
|
|
|
endif
|
2016-06-26 14:30:37 -04:00
|
|
|
return id
|
|
|
|
endif
|
2016-02-20 08:13:10 -05:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
call go#cmd#autowrite()
|
|
|
|
redraw
|
|
|
|
|
|
|
|
let command = "go " . join(args, ' ')
|
|
|
|
let out = go#tool#ExecuteInDir(command)
|
|
|
|
|
|
|
|
let l:listtype = "quickfix"
|
|
|
|
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let dir = getcwd()
|
|
|
|
execute cd fnameescape(expand("%:p:h"))
|
|
|
|
|
|
|
|
if a:compile
|
|
|
|
call delete(compile_file)
|
|
|
|
endif
|
|
|
|
|
|
|
|
if go#util#ShellError() != 0
|
|
|
|
let errors = go#tool#ParseErrors(split(out, '\n'))
|
|
|
|
let errors = go#tool#FilterValids(errors)
|
|
|
|
|
|
|
|
call go#list#Populate(l:listtype, errors)
|
|
|
|
call go#list#Window(l:listtype, len(errors))
|
|
|
|
if !empty(errors) && !a:bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
|
|
|
elseif empty(errors)
|
|
|
|
" failed to parse errors, output the original content
|
|
|
|
call go#util#EchoError(out)
|
2015-07-13 06:22:46 -04:00
|
|
|
endif
|
2016-06-26 14:30:37 -04:00
|
|
|
echon "vim-go: " | echohl ErrorMsg | echon "[test] FAIL" | echohl None
|
|
|
|
else
|
|
|
|
call go#list#Clean(l:listtype)
|
|
|
|
call go#list#Window(l:listtype)
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
if a:compile
|
|
|
|
echon "vim-go: " | echohl Function | echon "[test] SUCCESS" | echohl None
|
2014-10-31 17:30:24 -04:00
|
|
|
else
|
2016-06-26 14:30:37 -04:00
|
|
|
echon "vim-go: " | echohl Function | echon "[test] PASS" | echohl None
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
2016-06-26 14:30:37 -04:00
|
|
|
endif
|
|
|
|
execute cd . fnameescape(dir)
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-07-13 06:22:46 -04: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, ...)
|
2016-06-26 14:30:37 -04:00
|
|
|
" 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\|Example\)', "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]
|
|
|
|
let args = [a:bang, 0, "-run", name . "$"]
|
|
|
|
|
|
|
|
if a:0
|
|
|
|
call extend(args, a:000)
|
|
|
|
endif
|
|
|
|
|
|
|
|
call call('go#cmd#Test', args)
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Generate runs 'go generate' in similar fashion to go#cmd#Build()
|
|
|
|
function! go#cmd#Generate(bang, ...)
|
2016-06-26 14:30:37 -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
|
|
|
|
if g:go_dispatch_enabled && exists(':Make') == 2
|
|
|
|
silent! exe 'Make'
|
|
|
|
elseif l:listtype == "locationlist"
|
|
|
|
silent! exe 'lmake!'
|
|
|
|
else
|
|
|
|
silent! exe 'make!'
|
|
|
|
endif
|
|
|
|
redraw!
|
|
|
|
|
|
|
|
let errors = go#list#Get(l:listtype)
|
|
|
|
call go#list#Window(l:listtype, len(errors))
|
|
|
|
if !empty(errors)
|
|
|
|
if !a:bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
2015-07-13 06:22:46 -04:00
|
|
|
endif
|
2016-06-26 14:30:37 -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 14:30:37 -04:00
|
|
|
let &makeprg = default_makeprg
|
|
|
|
let $GOPATH = old_gopath
|
|
|
|
endfunction
|
2016-02-20 08:13:10 -05:00
|
|
|
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
" -----------------------
|
|
|
|
" | Neovim job handlers |
|
|
|
|
" -----------------------
|
|
|
|
let s:test_compile_handlers = {}
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
function! s:test_compile_handler(job, exit_status, data)
|
|
|
|
if !has_key(s:test_compile_handlers, a:job.id)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let l:compile_file = s:test_compile_handlers[a:job.id]
|
|
|
|
call delete(l:compile_file)
|
|
|
|
unlet s:test_compile_handlers[a:job.id]
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 14:30:37 -04:00
|
|
|
" vim: sw=2 ts=2 et
|