Skip to content

Commit 9ebbbb5

Browse files
committed
Merge pull request #493 from guns/cmd-arguments
Improve :GoCommand argument handling
2 parents 5ce1fb1 + b60ac3b commit 9ebbbb5

File tree

6 files changed

+92
-60
lines changed

6 files changed

+92
-60
lines changed

autoload/go/cmd.vim

+29-31
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ endfunction
1414
" dependent files for the current folder and passes it to go build.
1515
function! go#cmd#Build(bang, ...)
1616
let default_makeprg = &makeprg
17-
let gofiles = join(go#tool#Files(), '" "')
1817

1918
let old_gopath = $GOPATH
2019
let $GOPATH = go#path#Detect()
2120

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

2830
echon "vim-go: " | echohl Identifier | echon "building ..."| echohl None
@@ -52,13 +54,11 @@ endfunction
5254
" suitable for long running apps, because vim is blocking by default and
5355
" calling long running apps will block the whole UI.
5456
function! go#cmd#Run(bang, ...)
55-
let goFiles = '"' . join(go#tool#Files(), '" "') . '"'
56-
5757
let old_gopath = $GOPATH
5858
let $GOPATH = go#path#Detect()
5959

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

72+
" :make expands '%' and '#' wildcards, so they must also be escaped
7273
let default_makeprg = &makeprg
73-
if !len(a:000)
74-
let &makeprg = 'go run ' . goFiles
74+
if a:0 == 0
75+
let &makeprg = 'go run ' . go#util#Shelljoin(go#tool#Files(), 1)
7576
else
76-
let &makeprg = "go run " . expand(a:1)
77+
let &makeprg = "go run " . go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
7778
endif
7879

7980
if g:go_dispatch_enabled && exists(':Make') == 2
@@ -96,8 +97,7 @@ endfunction
9697
" is given(which are passed directly to 'go insta'') it tries to install those
9798
" packages. Errors are populated in the quickfix window.
9899
function! go#cmd#Install(bang, ...)
99-
let pkgs = join(a:000, '" "')
100-
let command = 'go install "' . pkgs . '"'
100+
let command = 'go install ' . go#util#Shelljoin(a:000)
101101
call go#cmd#autowrite()
102102
let out = go#tool#ExecuteInDir(command)
103103
if v:shell_error
@@ -122,15 +122,11 @@ function! go#cmd#Test(bang, compile, ...)
122122
" don't run the test, only compile it. Useful to capture and fix errors or
123123
" to create a test binary.
124124
if a:compile
125-
let command .= "-c"
126-
endif
127-
128-
if len(a:000)
129-
let command .= expand(a:1)
125+
let command .= "-c "
130126
endif
131127

132-
if len(a:000) == 2
133-
let command .= a:2
128+
if a:0
129+
let command .= go#util#Shelljoin(map(copy(a:000), "expand(v:val)"))
134130
endif
135131

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

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

187-
let a1 = ""
188-
if len(a:000)
189-
let a1 = a:1
190-
191-
" add extra space
192-
let flag = " " . flag
183+
if a:0
184+
call extend(args, a:000)
193185
endif
194186

195-
call go#cmd#Test(a:bang, 0, a1, flag)
187+
call call('go#cmd#Test', args)
196188
endfunction
197189

198190
" Coverage creates a new cover profile with 'go test -coverprofile' and opens
199191
" a new HTML coverage page from that profile.
200192
function! go#cmd#Coverage(bang, ...)
201193
let l:tmpname=tempname()
202194

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

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

224216
" Vet calls "go vet' on the current directory. Any warnings are populated in
225217
" the quickfix window
226-
function! go#cmd#Vet(bang)
218+
function! go#cmd#Vet(bang, ...)
227219
call go#cmd#autowrite()
228220
echon "vim-go: " | echohl Identifier | echon "calling vet..." | echohl None
229-
let out = go#tool#ExecuteInDir('go vet')
221+
if a:0 == 0
222+
let out = go#tool#ExecuteInDir('go vet')
223+
else
224+
let out = go#tool#ExecuteInDir('go tool vet ' . go#util#Shelljoin(a:000))
225+
endif
230226
if v:shell_error
231227
call go#tool#ShowErrors(out)
232228
else
@@ -246,15 +242,17 @@ endfunction
246242
" Generate runs 'go generate' in similar fashion to go#cmd#Build()
247243
function! go#cmd#Generate(bang, ...)
248244
let default_makeprg = &makeprg
249-
let gofiles = join(go#tool#Files(), '" "')
250245

251246
let old_gopath = $GOPATH
252247
let $GOPATH = go#path#Detect()
253248

249+
" :make expands '%' and '#' wildcards, so they must also be escaped
250+
let goargs = go#util#Shelljoin(map(copy(a:000), "expand(v:val)"), 1)
254251
if v:shell_error
255-
let &makeprg = "go generate " . join(a:000, ' ')
252+
let &makeprg = "go generate " . goargs
256253
else
257-
let &makeprg = "go generate " . join(a:000, ' ') . ' "' . gofiles . '"'
254+
let gofiles = go#util#Shelljoin(go#tool#Files(), 1)
255+
let &makeprg = "go generate " . goargs . ' ' . gofiles
258256
endif
259257

260258
echon "vim-go: " | echohl Identifier | echon "generating ..."| echohl None

autoload/go/errcheck.vim

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ endif
44

55
function! go#errcheck#Run(...) abort
66
if a:0 == 0
7-
let package = go#package#ImportPath(expand('%:p:h'))
8-
if package == -1
7+
let goargs = go#package#ImportPath(expand('%:p:h'))
8+
if goargs == -1
99
echohl Error | echomsg "vim-go: package is not inside GOPATH src" | echohl None
1010
return
1111
endif
1212
else
13-
let package = a:1
14-
end
13+
let goargs = go#util#Shelljoin(a:000)
14+
endif
1515

1616
let bin_path = go#path#CheckBinPath(g:go_errcheck_bin)
1717
if empty(bin_path)
1818
return
1919
endif
2020

2121
echon "vim-go: " | echohl Identifier | echon "errcheck analysing ..." | echohl None
22-
let out = system(bin_path . ' ' . package)
22+
let out = system(bin_path . ' ' . goargs)
2323
if v:shell_error
2424
let errors = []
2525
let mx = '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)'

autoload/go/lint.vim

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@
88
"
99
" This filetype plugin add a new commands for go buffers:
1010
"
11-
" :GoLint
11+
" :GoLint [options]
1212
"
1313
" Run golint for the current Go file.
1414
"
1515
if !exists("g:go_golint_bin")
1616
let g:go_golint_bin = "golint"
1717
endif
1818

19-
function! go#lint#Run() abort
19+
function! go#lint#Run(...) abort
2020
let bin_path = go#path#CheckBinPath(g:go_golint_bin)
2121
if empty(bin_path)
2222
return
2323
endif
2424

25-
silent cexpr system(bin_path . " " . shellescape(expand('%')))
25+
if a:0 == 0
26+
let goargs = shellescape(expand('%'))
27+
else
28+
let goargs = go#util#Shelljoin(a:000)
29+
endif
30+
silent cexpr system(bin_path . " " . goargs)
2631
cwindow
2732
endfunction
2833

autoload/go/util.vim

+10
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ function! go#util#StripPathSep(path)
4848
return a:path
4949
endfunction
5050

51+
" Shelljoin returns a shell-safe string representation of arglist. The
52+
" {special} argument of shellescape() may optionally be passed.
53+
function! go#util#Shelljoin(arglist, ...)
54+
if a:0
55+
return join(map(copy(a:arglist), 'shellescape(v:val, ' . a:1 . ')'), ' ')
56+
else
57+
return join(map(copy(a:arglist), 'shellescape(v:val)'), ' ')
58+
endif
59+
endfunction
60+
5161
" vim:ts=4:sw=4:et

doc/vim-go.txt

+30-11
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ COMMANDS *go-commands*
140140
displayed and the buffer will be untouched.
141141

142142
*:GoLint*
143-
:GoLint
143+
:GoLint [packages]
144144

145-
Run golint for the current Go file.
145+
Run golint for the current Go file, or for given packages.
146146

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

181181
*:GoVet*
182-
:GoVet[!]
182+
:GoVet[!] [options]
183183

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

190+
You may optionally pass any valid go tool vet flags/options. In this case,
191+
`go tool vet` is run in place of `go vet`. For a full list please see
192+
`go tool vet -h`.
193+
190194
If [!] is not given the first error is jumped to.
191195

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

211+
You may optionally pass any valid go run flags/options. For a full list
212+
please see `go help run`.
213+
207214
If [!] is not given the first error is jumped to.
208215

209216
*:GoBuild*
210-
:GoBuild[!] [options]
217+
:GoBuild[!] [expand]
211218

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

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

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

221228
*:GoGenerate*
222-
:GoGenerate[!] [options]
229+
:GoGenerate[!] [expand]
223230

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

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

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

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

238245

239246
*:GoInstall*
240-
:GoInstall[!]
247+
:GoInstall[!] [options]
248+
249+
Install your package with `go install`.
241250

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

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

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

263+
You may optionally pass any valid go test flags/options. For a full list
264+
please see `go help test`.
265+
253266
If [!] is not given the first error is jumped to.
254267

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

278291
*:GoCoverage*
279-
:GoCoverage[!]
292+
:GoCoverage[!] [options]
280293

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

297+
You may optionally pass any valid go test flags/options, such as
298+
`-covermode set,count,atomic`. For a full list please see `go help test`.
299+
284300
If [!] is not given the first error is jumped to.
285301

286302
*:GoErrCheck*
287-
:GoErrCheck
303+
:GoErrCheck [options]
288304

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

308+
You may optionally pass any valid errcheck flags/options. For a full list
309+
please see `errcheck -h`.
310+
292311
*:GoFiles*
293312
:GoFiles
294313

ftplugin/go/commands.vim

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ endif
1515

1616

1717
" Some handy plug mappings
18-
nnoremap <silent> <Plug>(go-run) :<C-u>call go#cmd#Run(!g:go_jump_to_error,expand('%'))<CR>
19-
nnoremap <silent> <Plug>(go-build) :<C-u>call go#cmd#Build(!g:go_jump_to_error,'')<CR>
20-
nnoremap <silent> <Plug>(go-generate) :<C-u>call go#cmd#Generate(!g:go_jump_to_error,'')<CR>
18+
nnoremap <silent> <Plug>(go-run) :<C-u>call go#cmd#Run(!g:go_jump_to_error, '%')<CR>
19+
nnoremap <silent> <Plug>(go-build) :<C-u>call go#cmd#Build(!g:go_jump_to_error)<CR>
20+
nnoremap <silent> <Plug>(go-generate) :<C-u>call go#cmd#Generate(!g:go_jump_to_error)<CR>
2121
nnoremap <silent> <Plug>(go-install) :<C-u>call go#cmd#Install(!g:go_jump_to_error)<CR>
22-
nnoremap <silent> <Plug>(go-test) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 0, '')<CR>
23-
nnoremap <silent> <Plug>(go-test-func) :<C-u>call go#cmd#TestFunc(!g:go_jump_to_error, '')<CR>
24-
nnoremap <silent> <Plug>(go-test-compile) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 1, '')<CR>
25-
nnoremap <silent> <Plug>(go-coverage) :<C-u>call go#cmd#Coverage(!g:go_jump_to_error, '')<CR>
22+
nnoremap <silent> <Plug>(go-test) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 0)<CR>
23+
nnoremap <silent> <Plug>(go-test-func) :<C-u>call go#cmd#TestFunc(!g:go_jump_to_error)<CR>
24+
nnoremap <silent> <Plug>(go-test-compile) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 1)<CR>
25+
nnoremap <silent> <Plug>(go-coverage) :<C-u>call go#cmd#Coverage(!g:go_jump_to_error)<CR>
2626
nnoremap <silent> <Plug>(go-vet) :<C-u>call go#cmd#Vet(!g:go_jump_to_error)<CR>
2727
2828
nnoremap <silent> <Plug>(go-files) :<C-u>call go#tool#Files()<CR>
@@ -82,7 +82,7 @@ command! -nargs=* -bang GoTest call go#cmd#Test(<bang>0, 0, <f-args>)
8282
command! -nargs=* -bang GoTestFunc call go#cmd#TestFunc(<bang>0, <f-args>)
8383
command! -nargs=* -bang GoTestCompile call go#cmd#Test(<bang>0, 1, <f-args>)
8484
command! -nargs=* -bang GoCoverage call go#cmd#Coverage(<bang>0, <f-args>)
85-
command! -nargs=0 -bang GoVet call go#cmd#Vet(<bang>0)
85+
command! -nargs=* -bang GoVet call go#cmd#Vet(<bang>0, <f-args>)
8686

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

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

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

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

0 commit comments

Comments
 (0)