Skip to content

Commit 299f440

Browse files
committed
Merge pull request #441 from fatih/error-jumping
commands: use bang attribute to jump to errors
2 parents 3037995 + b6e2b14 commit 299f440

File tree

3 files changed

+86
-69
lines changed

3 files changed

+86
-69
lines changed

autoload/go/cmd.vim

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
if !exists("g:go_jump_to_error")
2-
let g:go_jump_to_error = 1
3-
endif
4-
51
if !exists("g:go_dispatch_enabled")
62
let g:go_dispatch_enabled = 0
73
endif
@@ -36,16 +32,15 @@ function! go#cmd#Build(bang, ...)
3632
silent! exe 'make!'
3733
endif
3834
redraw!
39-
if !a:bang
40-
cwindow
41-
let errors = getqflist()
42-
if !empty(errors)
43-
if g:go_jump_to_error
44-
cc 1 "jump to first error if there is any
45-
endif
46-
else
47-
redraws! | echon "vim-go: " | echohl Function | echon "[build] SUCCESS"| echohl None
35+
36+
cwindow
37+
let errors = getqflist()
38+
if !empty(errors)
39+
if !a:bang
40+
cc 1 "jump to first error if there is any
4841
endif
42+
else
43+
redraws! | echon "vim-go: " | echohl Function | echon "[build] SUCCESS"| echohl None
4944
endif
5045

5146
let &makeprg = default_makeprg
@@ -86,14 +81,11 @@ function! go#cmd#Run(bang, ...)
8681
else
8782
exe 'make!'
8883
endif
89-
if !a:bang
90-
cwindow
91-
let errors = getqflist()
92-
if !empty(errors)
93-
if g:go_jump_to_error
94-
cc 1 "jump to first error if there is any
95-
endif
96-
endif
84+
85+
cwindow
86+
let errors = getqflist()
87+
if !empty(errors) && !a:bang
88+
cc 1 "jump to first error if there is any
9789
endif
9890

9991
let $GOPATH = old_gopath
@@ -103,7 +95,7 @@ endfunction
10395
" Install installs the package by simple calling 'go install'. If any argument
10496
" is given(which are passed directly to 'go insta'') it tries to install those
10597
" packages. Errors are populated in the quickfix window.
106-
function! go#cmd#Install(...)
98+
function! go#cmd#Install(bang, ...)
10799
let pkgs = join(a:000, '" "')
108100
let command = 'go install "' . pkgs . '"'
109101
call go#cmd#autowrite()
@@ -112,10 +104,8 @@ function! go#cmd#Install(...)
112104
call go#tool#ShowErrors(out)
113105
cwindow
114106
let errors = getqflist()
115-
if !empty(errors)
116-
if g:go_jump_to_error
117-
cc 1 "jump to first error if there is any
118-
endif
107+
if !empty(errors) && !a:bang
108+
cc 1 "jump to first error if there is any
119109
endif
120110
return
121111
endif
@@ -126,7 +116,7 @@ endfunction
126116
" Test runs `go test` in the current directory. If compile is true, it'll
127117
" compile the tests instead of running them (useful to catch errors in the
128118
" test files). Any other argument is appendend to the final `go test` command
129-
function! go#cmd#Test(compile, ...)
119+
function! go#cmd#Test(bang, compile, ...)
130120
let command = "go test "
131121

132122
" don't run the test, only compile it. Useful to capture and fix errors or
@@ -156,10 +146,8 @@ function! go#cmd#Test(compile, ...)
156146
call go#tool#ShowErrors(out)
157147
cwindow
158148
let errors = getqflist()
159-
if !empty(errors)
160-
if g:go_jump_to_error
161-
cc 1 "jump to first error if there is any
162-
endif
149+
if !empty(errors) && !a:bang
150+
cc 1 "jump to first error if there is any
163151
endif
164152
echon "vim-go: " | echohl ErrorMsg | echon "[test] FAIL" | echohl None
165153
else
@@ -176,7 +164,7 @@ endfunction
176164

177165
" Testfunc runs a single test that surrounds the current cursor position.
178166
" Arguments are passed to the `go test` command.
179-
function! go#cmd#TestFunc(...)
167+
function! go#cmd#TestFunc(bang, ...)
180168
" search flags legend (used only)
181169
" 'b' search backward instead of forward
182170
" 'c' accept a match at the cursor position
@@ -204,12 +192,12 @@ function! go#cmd#TestFunc(...)
204192
let flag = " " . flag
205193
endif
206194

207-
call go#cmd#Test(0, a1, flag)
195+
call go#cmd#Test(a:bang, 0, a1, flag)
208196
endfunction
209197

210198
" Coverage creates a new cover profile with 'go test -coverprofile' and opens
211199
" a new HTML coverage page from that profile.
212-
function! go#cmd#Coverage(...)
200+
function! go#cmd#Coverage(bang, ...)
213201
let l:tmpname=tempname()
214202

215203
let command = "go test -coverprofile=".l:tmpname
@@ -226,20 +214,16 @@ function! go#cmd#Coverage(...)
226214
call go#tool#ExecuteInDir(openHTML)
227215
endif
228216
cwindow
229-
230217
let errors = getqflist()
231-
if !empty(errors)
232-
if g:go_jump_to_error
233-
cc 1 "jump to first error if there is any
234-
endif
218+
if !empty(errors) && !a:bang
219+
cc 1 "jump to first error if there is any
235220
endif
236-
237221
call delete(l:tmpname)
238222
endfunction
239223

240224
" Vet calls "go vet' on the current directory. Any warnings are populated in
241225
" the quickfix window
242-
function! go#cmd#Vet()
226+
function! go#cmd#Vet(bang)
243227
call go#cmd#autowrite()
244228
echon "vim-go: " | echohl Identifier | echon "calling vet..." | echohl None
245229
let out = go#tool#ExecuteInDir('go vet')
@@ -248,11 +232,10 @@ function! go#cmd#Vet()
248232
else
249233
call setqflist([])
250234
endif
251-
cwindow
252235

253236
let errors = getqflist()
254-
if !empty(errors)
255-
if g:go_jump_to_error
237+
if !empty(errors)
238+
if !a:bang
256239
cc 1 "jump to first error if there is any
257240
endif
258241
else

doc/vim-go.txt

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,16 @@ COMMANDS *go-commands*
182182
'xterm-clipboard' otherwise it's get yanked into the `""` register.
183183

184184
*:GoVet*
185-
:GoVet
185+
:GoVet[!]
186186

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

193+
If [!] is not given the first error is jumped to.
194+
193195
*:GoDef*
194196
:GoDef [identifier]
195197

@@ -227,19 +229,23 @@ COMMANDS *go-commands*
227229

228230

229231
*:GoInstall*
230-
:GoInstall
232+
:GoInstall[!]
231233

232234
Install your package with `go install`.
233235

236+
If [!] is not given the first error is jumped to.
237+
234238
*:GoTest*
235-
:GoTest [expand]
239+
:GoTest[!] [expand]
236240

237241
Run the tests on your _test.go files via in your current directory. Errors
238242
are populated in quickfix window. If an argument is passed, 'expand' is
239243
used as file selector (useful for cases like `:GoTest ./...`).
240244

245+
If [!] is not given the first error is jumped to.
246+
241247
*:GoTestFunc*
242-
:GoTestFunc [expand]
248+
:GoTestFunc[!] [expand]
243249

244250
Runs :GoTest, but only on the single test function immediate to your
245251
cursor using 'go test's '-run' flag.
@@ -248,21 +254,27 @@ COMMANDS *go-commands*
248254
a matching `func Test` pattern is found or top of file is reached. Search
249255
will not wrap around when at the top of the file.
250256

257+
If [!] is not given the first error is jumped to.
258+
251259
*:GoTestCompile*
252-
:GoTestCompile [expand]
260+
:GoTestCompile[!] [expand]
253261

254262
Compile your _test.go files via in your current directory. Errors are
255263
populated in quickfix window. If an argument is passed, 'expand' is used
256264
as file selector (useful for cases like `:GoTest ./...`). Useful to not
257265
run the tests and capture/fix errors before running the tests or to
258266
create test binary.
259267

268+
If [!] is not given the first error is jumped to.
269+
260270
*:GoCoverage*
261-
:GoCoverage
271+
:GoCoverage[!]
262272

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

276+
If [!] is not given the first error is jumped to.
277+
266278
*:GoErrCheck*
267279
:GoErrCheck
268280

@@ -566,6 +578,18 @@ By default it's disabled >
566578
567579
let g:go_auto_type_info = 0
568580
<
581+
582+
*'g:go_jump_to_error'*
583+
584+
Use this option to enable/disable passing the bang attribute to the mappings
585+
|(go-build)|, |(go-run)|, etc.. When enabled it will jump to the first error
586+
automatically (means it will NOT pass the bang attribute to the appropriate
587+
command, i.e: (go-run) -> :GoRun ). Note, that calling this doesn't have any
588+
affect on calling the commands manually. This setting is only useful for
589+
changing the behaviour of our custom static mappings. By default it's enabled.
590+
>
591+
let g:go_jump_to_error = 1
592+
<
569593
*'g:go_fmt_autosave'*
570594

571595
Use this option to auto |:GoFmt| on save. By default it's enabled >
@@ -599,10 +623,10 @@ fails. By default it's disabled. >
599623

600624
Use this option to enable fmt's experimental mode. This experimental mode is
601625
superior to the current mode as it fully saves the undo history, so undo/redo
602-
doesn't break. However it's causing problems on some Vim versions. By default
603-
it's disabled. >
626+
doesn't break. However it's slows (creates/deletes a file for every save) and
627+
it's causing problems on some Vim versions. By default it's disabled. >
604628
605-
let g:go_fmt_experimental = 1
629+
let g:go_fmt_experimental = 0
606630
<
607631
*'g:go_doc_keywordprg_enabled'*
608632

ftplugin/go/commands.vim

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,27 @@ if exists("g:go_loaded_commands")
33
endif
44
let g:go_loaded_commands = 1
55

6+
" go_jump_to_error defines whether we should pass the bang attribute to the
7+
" command or not. This is only used for mappings, because the user can't pass
8+
" the bang attribute to the plug mappings below. So instead of hardcoding it
9+
" as 0 (no '!' attribute) or 1 (with '!' attribute) we pass the user setting,
10+
" which by default is enabled. For commands the user has the ability to pass
11+
" the '!', such as :GoBuild or :GoBuild!
12+
if !exists("g:go_jump_to_error")
13+
let g:go_jump_to_error = 1
14+
endif
15+
616

717
" Some handy plug mappings
8-
nnoremap <silent> <Plug>(go-run) :<C-u>call go#cmd#Run(expand('%'))<CR>
9-
nnoremap <silent> <Plug>(go-build) :<C-u>call go#cmd#Build('')<CR>
10-
nnoremap <silent> <Plug>(go-install) :<C-u>call go#cmd#Install()<CR>
11-
nnoremap <silent> <Plug>(go-test) :<C-u>call go#cmd#Test(0, '')<CR>
12-
nnoremap <silent> <Plug>(go-test-func) :<C-u>call go#cmd#TestFunc('')<CR>
13-
nnoremap <silent> <Plug>(go-test-compile) :<C-u>call go#cmd#Test(1, '')<CR>
14-
nnoremap <silent> <Plug>(go-coverage) :<C-u>call go#cmd#Coverage('')<CR>
15-
nnoremap <silent> <Plug>(go-vet) :<C-u>call go#cmd#Vet()<CR>
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-install) :<C-u>call go#cmd#Install(!g:go_jump_to_error)<CR>
21+
nnoremap <silent> <Plug>(go-test) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 0, '')<CR>
22+
nnoremap <silent> <Plug>(go-test-func) :<C-u>call go#cmd#TestFunc(!g:go_jump_to_error, '')<CR>
23+
nnoremap <silent> <Plug>(go-test-compile) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 1, '')<CR>
24+
nnoremap <silent> <Plug>(go-coverage) :<C-u>call go#cmd#Coverage(!g:go_jump_to_error, '')<CR>
25+
nnoremap <silent> <Plug>(go-vet) :<C-u>call go#cmd#Vet(!g:go_jump_to_error)<CR>
26+
1627
nnoremap <silent> <Plug>(go-files) :<C-u>call go#tool#Files()<CR>
1728
nnoremap <silent> <Plug>(go-deps) :<C-u>call go#tool#Deps()<CR>
1829
nnoremap <silent> <Plug>(go-info) :<C-u>call go#complete#Info()<CR>
@@ -62,15 +73,14 @@ command! -nargs=0 GoDeps echo go#tool#Deps()
6273
command! -nargs=* GoInfo call go#complete#Info()
6374

6475
" cmd
65-
command! -nargs=* -bang GoRun call go#cmd#Run(<bang>0,<f-args>)
6676
command! -nargs=* -bang GoBuild call go#cmd#Build(<bang>0,<f-args>)
67-
command! -nargs=* GoInstall call go#cmd#Install(<f-args>)
68-
command! -nargs=* GoTest call go#cmd#Test(0, <f-args>)
69-
command! -nargs=* GoTestFunc call go#cmd#TestFunc(<f-args>)
70-
command! -nargs=* GoTestCompile call go#cmd#Test(1, <f-args>)
71-
command! -nargs=* GoCoverage call go#cmd#Coverage(<f-args>)
72-
command! -nargs=0 GoVet call go#cmd#Vet()
73-
command! -nargs=0 GoErrCheck call go#errcheck#Run(<f-args>)
77+
command! -nargs=* -bang GoRun call go#cmd#Run(<bang>0,<f-args>)
78+
command! -nargs=* -bang GoInstall call go#cmd#Install(<bang>0, <f-args>)
79+
command! -nargs=* -bang GoTest call go#cmd#Test(<bang>0, 0, <f-args>)
80+
command! -nargs=* -bang GoTestFunc call go#cmd#TestFunc(<bang>0, <f-args>)
81+
command! -nargs=* -bang GoTestCompile call go#cmd#Test(<bang>0, 1, <f-args>)
82+
command! -nargs=* -bang GoCoverage call go#cmd#Coverage(<bang>0, <f-args>)
83+
command! -nargs=0 -bang GoVet call go#cmd#Vet(<bang>0)
7484

7585
" -- play
7686
command! -nargs=0 -range=% GoPlay call go#play#Share(<count>, <line1>, <line2>)

0 commit comments

Comments
 (0)