Skip to content

Commit 8e372df

Browse files
committed
Merge pull request #553 from fatih/gometalinter
Gometalinter integration
2 parents 587154f + d7d0dc7 commit 8e372df

File tree

8 files changed

+256
-163
lines changed

8 files changed

+256
-163
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ disabled/enabled easily.
2626
* Change or display `GOPATH` with `:GoPath`
2727
* Create a coverage profile and display annotated source code in browser to see
2828
which functions are covered with `:GoCoverage`
29+
* Call `gometalinter`, which is a tool that invokes all possible linters
30+
(golint, vet, errcheck, deadcode, etc..) and shows the warnings/errors
2931
* Lint your code with `:GoLint`
3032
* Run your code through `:GoVet` to catch static errors
3133
* Advanced source analysis tools utilizing oracle, such as `:GoImplements`,

autoload/go/cmd.vim

-27
Original file line numberDiff line numberDiff line change
@@ -216,32 +216,6 @@ function! go#cmd#Coverage(bang, ...)
216216
call delete(l:tmpname)
217217
endfunction
218218

219-
" Vet calls "go vet' on the current directory. Any warnings are populated in
220-
" the quickfix window
221-
function! go#cmd#Vet(bang, ...)
222-
call go#cmd#autowrite()
223-
echon "vim-go: " | echohl Identifier | echon "calling vet..." | echohl None
224-
if a:0 == 0
225-
let out = go#tool#ExecuteInDir('go vet')
226-
else
227-
let out = go#tool#ExecuteInDir('go tool vet ' . go#util#Shelljoin(a:000))
228-
endif
229-
if v:shell_error
230-
call go#tool#ShowErrors(out)
231-
else
232-
call setqflist([])
233-
endif
234-
235-
let errors = getqflist()
236-
if !empty(errors)
237-
if !a:bang
238-
cc 1 "jump to first error if there is any
239-
endif
240-
else
241-
redraw | echon "vim-go: " | echohl Function | echon "[vet] PASS" | echohl None
242-
endif
243-
endfunction
244-
"
245219
" Generate runs 'go generate' in similar fashion to go#cmd#Build()
246220
function! go#cmd#Generate(bang, ...)
247221
let default_makeprg = &makeprg
@@ -281,4 +255,3 @@ function! go#cmd#Generate(bang, ...)
281255
endfunction
282256

283257
" vim:ts=4:sw=4:et
284-
"

autoload/go/errcheck.vim

-58
This file was deleted.

autoload/go/lint.vim

+157-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,83 @@
1-
" Copyright 2013 The Go Authors. All rights reserved.
2-
" Use of this source code is governed by a BSD-style
3-
" license that can be found in the LICENSE file.
4-
"
5-
" lint.vim: Vim command to lint Go files with golint.
6-
"
7-
" https://github.com/golang/lint
8-
"
9-
" This filetype plugin add a new commands for go buffers:
10-
"
11-
" :GoLint [options]
12-
"
13-
" Run golint for the current Go file.
14-
"
1+
if !exists("g:go_metalinter_command")
2+
let g:go_metalinter_command = ""
3+
endif
4+
5+
if !exists("g:go_metalinter_enabled")
6+
let g:go_metalinter_enabled = ['vet', 'golint', 'errcheck']
7+
endif
8+
9+
if !exists("g:go_metalinter_path")
10+
let g:go_metalinter_path = "./..."
11+
endif
12+
1513
if !exists("g:go_golint_bin")
1614
let g:go_golint_bin = "golint"
1715
endif
1816

19-
function! go#lint#Run(...) abort
17+
if !exists("g:go_errcheck_bin")
18+
let g:go_errcheck_bin = "errcheck"
19+
endif
20+
21+
function! go#lint#Gometa(path_to_lint) abort
22+
let meta_command = "gometalinter --disable-all"
23+
if empty(g:go_metalinter_command)
24+
let bin_path = go#path#CheckBinPath("gometalinter")
25+
if empty(bin_path)
26+
return
27+
endif
28+
29+
if empty(g:go_metalinter_enabled)
30+
echohl Error | echomsg "vim-go: please enable linters with the setting g:go_metalinter_enabled" | echohl None
31+
return
32+
endif
33+
34+
for linter in g:go_metalinter_enabled
35+
let meta_command .= " --enable=".linter
36+
endfor
37+
38+
39+
" by default we search for all underlying files
40+
let path = g:go_metalinter_path
41+
if !empty(a:path_to_lint)
42+
let path = a:path_to_lint
43+
endif
44+
45+
let meta_command .= path
46+
else
47+
" the user wants something else, let us use it.
48+
let meta_command = g:go_metalinter_command
49+
endif
50+
51+
" comment out the following two lines for debugging
52+
" echo meta_command
53+
" return
54+
55+
let out = go#tool#ExecuteInDir(meta_command)
56+
57+
if v:shell_error == 0
58+
redraw | echo
59+
call setqflist([])
60+
echon "vim-go: " | echohl Function | echon "[metalinter] PASS" | echohl None
61+
else
62+
" backup users errorformat, will be restored once we are finished
63+
let old_errorformat = &errorformat
64+
65+
" GoMetaLinter can output one of the two, so we look for both of them
66+
" <file>:<line>:[<column>]: <message> (<linter>)
67+
" <file>:<line>:: <message> (<linter>)
68+
let &errorformat = "%f:%l:%c:%t%*[^:]:\ %m,%f:%l::%t%*[^:]:\ %m"
69+
70+
" create the quickfix list and open it
71+
cgetexpr split(out, "\n")
72+
cwindow
73+
74+
let &errorformat = old_errorformat
75+
endif
76+
endfunction
77+
78+
" Golint calls 'golint' on the current directory. Any warnings are populated in
79+
" the quickfix window
80+
function! go#lint#Golint(...) abort
2081
let bin_path = go#path#CheckBinPath(g:go_golint_bin)
2182
if empty(bin_path)
2283
return
@@ -31,5 +92,86 @@ function! go#lint#Run(...) abort
3192
cwindow
3293
endfunction
3394

95+
" Vet calls 'go vet' on the current directory. Any warnings are populated in
96+
" the quickfix window
97+
function! go#lint#Vet(bang, ...)
98+
call go#cmd#autowrite()
99+
echon "vim-go: " | echohl Identifier | echon "calling vet..." | echohl None
100+
if a:0 == 0
101+
let out = go#tool#ExecuteInDir('go vet')
102+
else
103+
let out = go#tool#ExecuteInDir('go tool vet ' . go#util#Shelljoin(a:000))
104+
endif
105+
if v:shell_error
106+
call go#tool#ShowErrors(out)
107+
else
108+
call setqflist([])
109+
endif
110+
111+
cwindow
112+
let errors = getqflist()
113+
if !empty(errors)
114+
if !a:bang
115+
cc 1 "jump to first error if there is any
116+
endif
117+
else
118+
redraw | echon "vim-go: " | echohl Function | echon "[vet] PASS" | echohl None
119+
endif
120+
endfunction
121+
122+
" ErrCheck calls 'errcheck' for the given packages. Any warnings are populated in
123+
" the quickfix window.
124+
function! go#lint#Errcheck(...) abort
125+
if a:0 == 0
126+
let goargs = go#package#ImportPath(expand('%:p:h'))
127+
if goargs == -1
128+
echohl Error | echomsg "vim-go: package is not inside GOPATH src" | echohl None
129+
return
130+
endif
131+
else
132+
let goargs = go#util#Shelljoin(a:000)
133+
endif
134+
135+
let bin_path = go#path#CheckBinPath(g:go_errcheck_bin)
136+
if empty(bin_path)
137+
return
138+
endif
139+
140+
echon "vim-go: " | echohl Identifier | echon "errcheck analysing ..." | echohl None
141+
redraw
142+
143+
let command = bin_path . ' ' . goargs
144+
let out = go#tool#ExecuteInDir(command)
145+
146+
if v:shell_error
147+
let errors = []
148+
let mx = '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)'
149+
for line in split(out, '\n')
150+
let tokens = matchlist(line, mx)
151+
if !empty(tokens)
152+
call add(errors, {"filename": expand(go#path#Default() . "/src/" . tokens[1]),
153+
\"lnum": tokens[2],
154+
\"col": tokens[3],
155+
\"text": tokens[4]})
156+
endif
157+
endfor
158+
159+
if empty(errors)
160+
echohl Error | echomsg "GoErrCheck returned error" | echohl None
161+
echo out
162+
endif
163+
164+
if !empty(errors)
165+
redraw | echo
166+
call setqflist(errors, 'r')
167+
endif
168+
else
169+
redraw | echo
170+
call setqflist([])
171+
echon "vim-go: " | echohl Function | echon "[errcheck] PASS" | echohl None
172+
endif
173+
174+
cwindow
175+
endfunction
34176

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

doc/vim-go.txt

+38
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,17 @@ COMMANDS *go-commands*
416416
object as does the selected identifier, within any package in the analysis
417417
scope.
418418

419+
*:GoMetaLinter*
420+
:GoMetaLinter [path]
421+
422+
Calls the underlying `gometalinter` tool and displays all warnings and
423+
errors in a quickfix window. By default it lints the files for the path
424+
`./...` . This can be changed with the 'path' argument temoporary or with the
425+
|g:go_metalinter_path| variable permanently. By default the following
426+
linters are enabled: "'vet', 'golint', 'errcheck'". This can be changed
427+
with the |g:go_metalinter_enabled| variable. To override the command
428+
completely use the variable |g:go_metalinter_command|
429+
419430

420431
===============================================================================
421432
MAPPINGS *go-mappings*
@@ -565,6 +576,9 @@ Show send/receive corresponding to selected channel op
565576

566577
Show all refs to entity denoted by selected identifier
567578

579+
*(go-metalinter)*
580+
581+
Calls `go-metalinter` for the current directory
568582

569583
===============================================================================
570584
TEXT OBJECTS *go-text-objects*
@@ -817,7 +831,31 @@ supports will be added. By default it's enabled. >
817831
Adds custom text objects. By default it's enabled. >
818832
819833
let g:go_textobj_enabled = 1
834+
<
820835

836+
*'g:go_metalinter_enabled'*
837+
838+
Specifies the currently enabled linters for the |GoMetaLinter| command. By
839+
default it's using `vet`, `golint` and `errcheck`
840+
>
841+
let g:go_metalinter_enabled = ['vet', 'golint', 'errcheck']
842+
<
843+
*'g:go_metalinter_command'*
844+
845+
Overrides the command to be executed when |GoMetaLinter| is called. This is
846+
an advanced settings and is for users who want to have a complete control
847+
over of how `gometalinter` should be executed. By default it's empty.
848+
>
849+
let g:go_metalinter_command = ""
850+
<
851+
*'g:go_metalinter_path'*
852+
853+
Defines the default path to be linted for the |GoMetaLinter| command. By
854+
default it's set to `./...`, which recursively lints all files underd the
855+
current directory.
856+
>
857+
let g:go_metalinter_path = "./..."
858+
<
821859

822860
===============================================================================
823861
TROUBLESHOOTING *go-troubleshooting*

0 commit comments

Comments
 (0)