Skip to content

Commit

Permalink
Merge pull request #493 from guns/cmd-arguments
Browse files Browse the repository at this point in the history
Improve :GoCommand argument handling
  • Loading branch information
fatih committed Jul 25, 2015
2 parents 5ce1fb1 + b60ac3b commit 9ebbbb5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 60 deletions.
60 changes: 29 additions & 31 deletions autoload/go/cmd.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ endfunction
" dependent files for the current folder and passes it to go build.
function! go#cmd#Build(bang, ...)
let default_makeprg = &makeprg
let gofiles = join(go#tool#Files(), '" "')

let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()

if v:shell_error
let &makeprg = "go build . errors"
else
let &makeprg = "go build -o /dev/null " . join(a:000, ' ') . ' "' . gofiles . '"'
" :make expands '%' and '#' wildcards, so they must also be escaped
let goargs = go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
let gofiles = go#util#Shelljoin(go#tool#Files(), 1)
let &makeprg = "go build -o /dev/null " . goargs . ' ' . gofiles
endif

echon "vim-go: " | echohl Identifier | echon "building ..."| echohl None
Expand Down Expand Up @@ -52,13 +54,11 @@ endfunction
" suitable for long running apps, because vim is blocking by default and
" calling long running apps will block the whole UI.
function! go#cmd#Run(bang, ...)
let goFiles = '"' . join(go#tool#Files(), '" "') . '"'

let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()

if go#util#IsWin()
exec '!go run ' . goFiles
exec '!go run ' . go#util#Shelljoin(go#tool#Files())
if v:shell_error
redraws! | echon "vim-go: [run] " | echohl ErrorMsg | echon "FAILED"| echohl None
else
Expand All @@ -69,11 +69,12 @@ function! go#cmd#Run(bang, ...)
return
endif

" :make expands '%' and '#' wildcards, so they must also be escaped
let default_makeprg = &makeprg
if !len(a:000)
let &makeprg = 'go run ' . goFiles
if a:0 == 0
let &makeprg = 'go run ' . go#util#Shelljoin(go#tool#Files(), 1)
else
let &makeprg = "go run " . expand(a:1)
let &makeprg = "go run " . go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
endif

if g:go_dispatch_enabled && exists(':Make') == 2
Expand All @@ -96,8 +97,7 @@ endfunction
" is given(which are passed directly to 'go insta'') it tries to install those
" packages. Errors are populated in the quickfix window.
function! go#cmd#Install(bang, ...)
let pkgs = join(a:000, '" "')
let command = 'go install "' . pkgs . '"'
let command = 'go install ' . go#util#Shelljoin(a:000)
call go#cmd#autowrite()
let out = go#tool#ExecuteInDir(command)
if v:shell_error
Expand All @@ -122,15 +122,11 @@ function! go#cmd#Test(bang, compile, ...)
" don't run the test, only compile it. Useful to capture and fix errors or
" to create a test binary.
if a:compile
let command .= "-c"
endif

if len(a:000)
let command .= expand(a:1)
let command .= "-c "
endif

if len(a:000) == 2
let command .= a:2
if a:0
let command .= go#util#Shelljoin(map(copy(a:000), "expand(v:val)"))
endif

call go#cmd#autowrite()
Expand Down Expand Up @@ -182,25 +178,21 @@ function! go#cmd#TestFunc(bang, ...)

let line = getline(test)
let name = split(split(line, " ")[1], "(")[0]
let flag = "-run \"" . name . "$\""
let args = [a:bang, 0, "-run", name . "$"]

let a1 = ""
if len(a:000)
let a1 = a:1

" add extra space
let flag = " " . flag
if a:0
call extend(args, a:000)
endif

call go#cmd#Test(a:bang, 0, a1, flag)
call call('go#cmd#Test', args)
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, ...)
let l:tmpname=tempname()

let command = "go test -coverprofile=".l:tmpname
let command = "go test -coverprofile=" . l:tmpname . ' ' . go#util#Shelljoin(a:000)

call go#cmd#autowrite()
let out = go#tool#ExecuteInDir(command)
Expand All @@ -223,10 +215,14 @@ endfunction

" Vet calls "go vet' on the current directory. Any warnings are populated in
" the quickfix window
function! go#cmd#Vet(bang)
function! go#cmd#Vet(bang, ...)
call go#cmd#autowrite()
echon "vim-go: " | echohl Identifier | echon "calling vet..." | echohl None
let out = go#tool#ExecuteInDir('go vet')
if a:0 == 0
let out = go#tool#ExecuteInDir('go vet')
else
let out = go#tool#ExecuteInDir('go tool vet ' . go#util#Shelljoin(a:000))
endif
if v:shell_error
call go#tool#ShowErrors(out)
else
Expand All @@ -246,15 +242,17 @@ endfunction
" Generate runs 'go generate' in similar fashion to go#cmd#Build()
function! go#cmd#Generate(bang, ...)
let default_makeprg = &makeprg
let gofiles = join(go#tool#Files(), '" "')

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 v:shell_error
let &makeprg = "go generate " . join(a:000, ' ')
let &makeprg = "go generate " . goargs
else
let &makeprg = "go generate " . join(a:000, ' ') . ' "' . gofiles . '"'
let gofiles = go#util#Shelljoin(go#tool#Files(), 1)
let &makeprg = "go generate " . goargs . ' ' . gofiles
endif

echon "vim-go: " | echohl Identifier | echon "generating ..."| echohl None
Expand Down
10 changes: 5 additions & 5 deletions autoload/go/errcheck.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ endif

function! go#errcheck#Run(...) abort
if a:0 == 0
let package = go#package#ImportPath(expand('%:p:h'))
if package == -1
let goargs = go#package#ImportPath(expand('%:p:h'))
if goargs == -1
echohl Error | echomsg "vim-go: package is not inside GOPATH src" | echohl None
return
endif
else
let package = a:1
end
let goargs = go#util#Shelljoin(a:000)
endif

let bin_path = go#path#CheckBinPath(g:go_errcheck_bin)
if empty(bin_path)
return
endif

echon "vim-go: " | echohl Identifier | echon "errcheck analysing ..." | echohl None
let out = system(bin_path . ' ' . package)
let out = system(bin_path . ' ' . goargs)
if v:shell_error
let errors = []
let mx = '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)'
Expand Down
11 changes: 8 additions & 3 deletions autoload/go/lint.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
"
" This filetype plugin add a new commands for go buffers:
"
" :GoLint
" :GoLint [options]
"
" Run golint for the current Go file.
"
if !exists("g:go_golint_bin")
let g:go_golint_bin = "golint"
endif

function! go#lint#Run() abort
function! go#lint#Run(...) abort
let bin_path = go#path#CheckBinPath(g:go_golint_bin)
if empty(bin_path)
return
endif

silent cexpr system(bin_path . " " . shellescape(expand('%')))
if a:0 == 0
let goargs = shellescape(expand('%'))
else
let goargs = go#util#Shelljoin(a:000)
endif
silent cexpr system(bin_path . " " . goargs)
cwindow
endfunction

Expand Down
10 changes: 10 additions & 0 deletions autoload/go/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ function! go#util#StripPathSep(path)
return a:path
endfunction

" Shelljoin returns a shell-safe string representation of arglist. The
" {special} argument of shellescape() may optionally be passed.
function! go#util#Shelljoin(arglist, ...)
if a:0
return join(map(copy(a:arglist), 'shellescape(v:val, ' . a:1 . ')'), ' ')
else
return join(map(copy(a:arglist), 'shellescape(v:val)'), ' ')
endif
endfunction

" vim:ts=4:sw=4:et
41 changes: 30 additions & 11 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ COMMANDS *go-commands*
displayed and the buffer will be untouched.

*:GoLint*
:GoLint
:GoLint [packages]

Run golint for the current Go file.
Run golint for the current Go file, or for given packages.

*:GoDoc*
:GoDoc [word]
Expand Down Expand Up @@ -179,14 +179,18 @@ COMMANDS *go-commands*
'xterm-clipboard' otherwise it's get yanked into the `""` register.

*:GoVet*
:GoVet[!]
:GoVet[!] [options]

Run `go vet` for the directory under your current file. Vet examines Go
source code and reports suspicious constructs, such as Printf calls whose
arguments do not align with the format string. Vet uses heuristics that do not
guarantee all reports are genuine problems, but it can find errors not caught
by the compilers.

You may optionally pass any valid go tool vet flags/options. In this case,
`go tool vet` is run in place of `go vet`. For a full list please see
`go tool vet -h`.

If [!] is not given the first error is jumped to.

*:GoDef*
Expand All @@ -204,28 +208,31 @@ COMMANDS *go-commands*
current file is used. If an argument is passed, 'expand' is used as file
selector. For example use `:GoRun %` to select the current file only.

You may optionally pass any valid go run flags/options. For a full list
please see `go help run`.

If [!] is not given the first error is jumped to.

*:GoBuild*
:GoBuild[!] [options]
:GoBuild[!] [expand]

Build your package with `go build`. It automatically builds only the files
that depends on the current file. GoBuild doesn't produce a result file.
Use 'make' to create a result file.

You may optionally pass any valid go build flags/options. For a full list
please see `go help build`.
please see `go help build`. Options are expanded with 'expand'.

If [!] is not given the first error is jumped to.

*:GoGenerate*
:GoGenerate[!] [options]
:GoGenerate[!] [expand]

Creates or updates your auto-generated source files by running `go
generate`.

You may optionally pass any valid go generate flags/options. For a full list
please see `go help generate`.
please see `go help generate`. Options are expanded with 'expand'.

If [!] is not given the first error is jumped to.

Expand All @@ -237,9 +244,12 @@ COMMANDS *go-commands*


*:GoInstall*
:GoInstall[!]
:GoInstall[!] [options]

Install your package with `go install`.

Install your package with `go install`.
You may optionally pass any valid go install flags/options. For a full list
please see `go help install`.

If [!] is not given the first error is jumped to.

Expand All @@ -250,6 +260,9 @@ COMMANDS *go-commands*
are populated in quickfix window. If an argument is passed, 'expand' is
used as file selector (useful for cases like `:GoTest ./...`).

You may optionally pass any valid go test flags/options. For a full list
please see `go help test`.

If [!] is not given the first error is jumped to.

*:GoTestFunc*
Expand All @@ -276,19 +289,25 @@ COMMANDS *go-commands*
If [!] is not given the first error is jumped to.

*:GoCoverage*
:GoCoverage[!]
:GoCoverage[!] [options]

Create a coverage profile and open a browser to display the annotated
source code of the current package.

You may optionally pass any valid go test flags/options, such as
`-covermode set,count,atomic`. For a full list please see `go help test`.

If [!] is not given the first error is jumped to.

*:GoErrCheck*
:GoErrCheck
:GoErrCheck [options]

Check for unchecked errors in you current package. Errors are populated in
quickfix window.

You may optionally pass any valid errcheck flags/options. For a full list
please see `errcheck -h`.

*:GoFiles*
:GoFiles

Expand Down
20 changes: 10 additions & 10 deletions ftplugin/go/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ endif


" Some handy plug mappings
nnoremap <silent> <Plug>(go-run) :<C-u>call go#cmd#Run(!g:go_jump_to_error,expand('%'))<CR>
nnoremap <silent> <Plug>(go-build) :<C-u>call go#cmd#Build(!g:go_jump_to_error,'')<CR>
nnoremap <silent> <Plug>(go-generate) :<C-u>call go#cmd#Generate(!g:go_jump_to_error,'')<CR>
nnoremap <silent> <Plug>(go-run) :<C-u>call go#cmd#Run(!g:go_jump_to_error, '%')<CR>
nnoremap <silent> <Plug>(go-build) :<C-u>call go#cmd#Build(!g:go_jump_to_error)<CR>
nnoremap <silent> <Plug>(go-generate) :<C-u>call go#cmd#Generate(!g:go_jump_to_error)<CR>
nnoremap <silent> <Plug>(go-install) :<C-u>call go#cmd#Install(!g:go_jump_to_error)<CR>
nnoremap <silent> <Plug>(go-test) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 0, '')<CR>
nnoremap <silent> <Plug>(go-test-func) :<C-u>call go#cmd#TestFunc(!g:go_jump_to_error, '')<CR>
nnoremap <silent> <Plug>(go-test-compile) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 1, '')<CR>
nnoremap <silent> <Plug>(go-coverage) :<C-u>call go#cmd#Coverage(!g:go_jump_to_error, '')<CR>
nnoremap <silent> <Plug>(go-test) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 0)<CR>
nnoremap <silent> <Plug>(go-test-func) :<C-u>call go#cmd#TestFunc(!g:go_jump_to_error)<CR>
nnoremap <silent> <Plug>(go-test-compile) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 1)<CR>
nnoremap <silent> <Plug>(go-coverage) :<C-u>call go#cmd#Coverage(!g:go_jump_to_error)<CR>
nnoremap <silent> <Plug>(go-vet) :<C-u>call go#cmd#Vet(!g:go_jump_to_error)<CR>
nnoremap <silent> <Plug>(go-files) :<C-u>call go#tool#Files()<CR>
Expand Down Expand Up @@ -82,7 +82,7 @@ command! -nargs=* -bang GoTest call go#cmd#Test(<bang>0, 0, <f-args>)
command! -nargs=* -bang GoTestFunc call go#cmd#TestFunc(<bang>0, <f-args>)
command! -nargs=* -bang GoTestCompile call go#cmd#Test(<bang>0, 1, <f-args>)
command! -nargs=* -bang GoCoverage call go#cmd#Coverage(<bang>0, <f-args>)
command! -nargs=0 -bang GoVet call go#cmd#Vet(<bang>0)
command! -nargs=* -bang GoVet call go#cmd#Vet(<bang>0, <f-args>)

" -- play
command! -nargs=0 -range=% GoPlay call go#play#Share(<count>, <line1>, <line2>)
Expand All @@ -104,9 +104,9 @@ command! -nargs=1 -bang -complete=customlist,go#package#Complete GoImport call g
command! -nargs=* -bang -complete=customlist,go#package#Complete GoImportAs call go#import#SwitchImport(1, <f-args>, '<bang>')

" -- lint
command! GoLint call go#lint#Run()
command! -nargs=* GoLint call go#lint#Run(<f-args>)

" -- errcheck
command! -nargs=? -complete=customlist,go#package#Complete GoErrCheck call go#errcheck#Run(<f-args>)
command! -nargs=* -complete=customlist,go#package#Complete GoErrCheck call go#errcheck#Run(<f-args>)

" vim:ts=4:sw=4:et

0 comments on commit 9ebbbb5

Please sign in to comment.