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
+
15
13
if ! exists (" g:go_golint_bin" )
16
14
let g: go_golint_bin = " golint"
17
15
endif
18
16
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
20
81
let bin_path = go#path#CheckBinPath (g: go_golint_bin )
21
82
if empty (bin_path)
22
83
return
@@ -31,5 +92,86 @@ function! go#lint#Run(...) abort
31
92
cwindow
32
93
endfunction
33
94
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
34
176
35
177
" vim:ts = 4 :sw = 4 :et
0 commit comments