forked from fatih/vim-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint.vim
468 lines (391 loc) · 12.5 KB
/
lint.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
function! go#lint#Gometa(bang, autosave, ...) abort
if a:0 == 0
let goargs = [expand('%:p:h')]
else
let goargs = a:000
endif
let l:metalinter = go#config#MetalinterCommand()
let cmd = []
if l:metalinter == 'golangci-lint'
let linters = a:autosave ? go#config#MetalinterAutosaveEnabled() : go#config#MetalinterEnabled()
let cmd = s:metalintercmd(l:metalinter, len(linters) != 0)
if empty(cmd)
return
endif
" add linters to cmd
for linter in linters
let cmd += ["--enable=".linter]
endfor
elseif l:metalinter != 'gopls'
" the user wants something else, let us use it.
let cmd = split(go#config#MetalinterCommand(), " ")
endif
if a:autosave
" redraw so that any messages that were displayed while writing the file
" will be cleared
redraw
if l:metalinter == "golangci-lint"
let goargs[0] = expand('%:p:h')
endif
endif
" Call metalinter asynchronously.
let deadline = go#config#MetalinterDeadline()
if deadline != ''
let cmd += ["--deadline=" . deadline]
endif
let cmd += goargs
let errformat = s:errorformat(l:metalinter)
if l:metalinter == 'gopls'
if a:autosave
let l:messages = go#lsp#AnalyzeFile(expand('%:p'))
else
let l:import_paths = l:goargs
if len(l:import_paths) == 0
let l:pkg = go#package#ImportPath()
if l:pkg == -1
call go#util#EchoError('could not determine package name')
return
endif
let l:import_paths = [l:pkg]
endif
let l:messages = call('go#lsp#Diagnostics', l:import_paths)
endif
let l:err = len(l:messages)
else
if go#util#has_job()
if a:autosave
let l:for = 'GoMetaLinterAutoSave'
else
let l:for = 'GoMetaLinter'
endif
call s:lint_job(l:metalinter, {'cmd': cmd, 'statustype': l:metalinter, 'errformat': errformat, 'for': l:for}, a:bang, a:autosave)
return
endif
let [l:out, l:err] = go#util#Exec(cmd)
let l:messages = split(out, "\n")
endif
if a:autosave
let l:listtype = go#list#Type('GoMetaLinterAutoSave')
let l:for = 'GoMetaLinterAutoSave'
else
let l:listtype = go#list#Type('GoMetaLinter')
let l:for = 'GoMetaLinterAuto'
endif
if l:err == 0
if !s:preserveerrors(a:autosave, l:listtype)
call go#list#Clean(l:listtype)
endif
call go#util#EchoSuccess('[metalinter] PASS')
else
let l:winid = win_getid(winnr())
" Parse and populate our location list
if a:autosave
call s:metalinterautosavecomplete(l:metalinter, fnamemodify(expand('%:p'), ":."), 0, 1, l:messages)
endif
call go#list#ParseFormat(l:listtype, errformat, l:messages, l:for, s:preserveerrors(a:autosave, l:listtype))
let errors = go#list#Get(l:listtype)
call go#list#Window(l:listtype, len(errors))
if a:autosave || a:bang
call win_gotoid(l:winid)
else
call go#list#JumpToFirst(l:listtype)
endif
endif
endfunction
function! go#lint#Diagnostics(bang, ...) abort
if a:0 == 0
let l:pkg = go#package#ImportPath()
if l:pkg == -1
call go#util#EchoError('could not determine package name')
return
endif
let l:import_paths = [l:pkg]
else
let l:import_paths = a:000
endif
let errformat = s:errorformat('gopls')
let l:messages = call('go#lsp#Diagnostics', l:import_paths)
let l:listtype = go#list#Type("GoDiagnostics")
if len(l:messages) == 0
call go#list#Clean(l:listtype)
call go#util#EchoSuccess('[diagnostics] PASS')
else
" Parse and populate the quickfix list
let l:winid = win_getid(winnr())
call go#list#ParseFormat(l:listtype, errformat, l:messages, 'GoDiagnostics', 0)
let errors = go#list#Get(l:listtype)
call go#list#Window(l:listtype, len(errors))
if a:bang
call win_gotoid(l:winid)
else
call go#list#JumpToFirst(l:listtype)
endif
endif
endfunction
" Golint calls 'golint' on the current directory. Any warnings are populated in
" the location list
function! go#lint#Golint(bang, ...) abort
call go#cmd#autowrite()
let l:type = 'golint'
let l:status = {
\ 'desc': 'current status',
\ 'type': l:type,
\ 'state': "started",
\ }
if go#config#EchoCommandInfo()
call go#util#EchoProgress(printf('[%s] analyzing...', l:type))
endif
call go#statusline#Update(expand('%:p:h'), l:status)
if a:0 == 0
let [l:out, l:err] = go#util#Exec([go#config#GolintBin(), expand('%:p:h')])
else
let [l:out, l:err] = go#util#Exec([go#config#GolintBin()] + a:000)
endif
let l:status.state = 'success'
let l:state = 'PASS'
let l:listtype = go#list#Type("GoLint")
if !empty(l:out)
let l:status.state = 'failed'
let l:state = 'FAIL'
let l:winid = win_getid(winnr())
call go#list#Parse(l:listtype, l:out, "GoLint", 0)
let l:errors = go#list#Get(l:listtype)
call go#list#Window(l:listtype, len(l:errors))
if a:bang
call win_gotoid(l:winid)
else
call go#list#JumpToFirst(l:listtype)
endif
if go#config#EchoCommandInfo()
call go#util#EchoError(printf('[%s] %s', l:type, l:state))
endif
else
call go#list#Clean(l:listtype)
if go#config#EchoCommandInfo()
call go#util#EchoSuccess(printf('[%s] %s', l:type, l:state))
endif
endif
call go#statusline#Update(expand('%:p:h'), l:status)
endfunction
" Vet calls 'go vet' on the current buffer's directory. Any warnings are
" populated in the location list
function! go#lint#Vet(bang, ...) abort
call go#cmd#autowrite()
let l:cmd = ['go', 'vet']
let buildtags = go#config#BuildTags()
if buildtags isnot ''
let l:cmd += ['-tags', buildtags]
endif
if a:0 == 0
let l:import_path = go#package#ImportPath()
if l:import_path == -1
call go#util#EchoError('could not determine package')
return
endif
let l:cmd = add(l:cmd, l:import_path)
else
let l:cmd = extend(l:cmd, a:000)
endif
let l:type = 'go vet'
if go#config#EchoCommandInfo()
call go#util#EchoProgress(printf('[%s] analyzing...', l:type))
endif
let l:status = {
\ 'desc': 'current status',
\ 'type': l:type,
\ 'state': "started",
\ }
call go#statusline#Update(expand('%:p:h'), l:status)
let [l:out, l:err] = go#util#ExecInDir(l:cmd)
let l:status.state = 'success'
let l:state = 'PASS'
let l:listtype = go#list#Type("GoVet")
if l:err != 0
let l:status.state = 'failed'
let l:state = 'FAIL'
let l:winid = win_getid(winnr())
let l:errorformat = "%-Gexit status %\\d%\\+," . &errorformat
let l:dir = go#util#Chdir(expand('%:p:h'))
try
call go#list#ParseFormat(l:listtype, l:errorformat, out, "GoVet", 0)
finally
call go#util#Chdir(l:dir)
endtry
let l:errors = go#list#Get(l:listtype)
if empty(l:errors)
call go#util#EchoError(l:out)
return
endif
call go#list#Window(l:listtype, len(l:errors))
if !empty(l:errors) && !a:bang
call go#list#JumpToFirst(l:listtype)
else
call win_gotoid(l:winid)
endif
if go#config#EchoCommandInfo()
call go#util#EchoError(printf('[%s] %s', l:type, l:state))
endif
else
call go#list#Clean(l:listtype)
if go#config#EchoCommandInfo()
call go#util#EchoSuccess(printf('[%s] %s', l:type, l:state))
endif
endif
call go#statusline#Update(expand('%:p:h'), l:status)
endfunction
" ErrCheck calls 'errcheck' for the given packages. Any warnings are populated in
" the location list
function! go#lint#Errcheck(bang, ...) abort
call go#cmd#autowrite()
let l:cmd = [go#config#ErrcheckBin(), '-abspath']
let buildtags = go#config#BuildTags()
if buildtags isnot ''
let l:cmd += ['-tags', buildtags]
endif
if a:0 == 0
let l:import_path = go#package#ImportPath()
if l:import_path == -1
call go#util#EchoError('could not determine package')
return
endif
let l:cmd = add(l:cmd, l:import_path)
else
let l:cmd = extend(l:cmd, a:000)
endif
let l:type = 'errcheck'
if go#config#EchoCommandInfo()
call go#util#EchoProgress(printf('[%s] analyzing...', l:type))
endif
let l:status = {
\ 'desc': 'current status',
\ 'type': l:type,
\ 'state': "started",
\ }
redraw
call go#statusline#Update(expand('%:p:h'), l:status)
let [l:out, l:err] = go#util#ExecInDir(l:cmd)
let l:status.state = 'success'
let l:state = 'PASS'
let l:listtype = go#list#Type("GoErrCheck")
if l:err != 0
let l:status.state = 'failed'
let l:state = 'FAIL'
let l:winid = win_getid(winnr())
if l:err == 1
let l:errformat = "%f:%l:%c:\ %m,%f:%l:%c\ %#%m"
" Parse and populate our location list
call go#list#ParseFormat(l:listtype, l:errformat, split(out, "\n"), 'Errcheck', 0)
endif
let l:errors = go#list#Get(l:listtype)
if empty(l:errors)
call go#util#EchoError(l:out)
return
endif
if !empty(errors)
call go#list#Populate(l:listtype, l:errors, 'Errcheck')
call go#list#Window(l:listtype, len(l:errors))
if !a:bang
call go#list#JumpToFirst(l:listtype)
else
call win_gotoid(l:winid)
endif
endif
if go#config#EchoCommandInfo()
call go#util#EchoError(printf('[%s] %s', l:type, l:state))
endif
else
call go#list#Clean(l:listtype)
if go#config#EchoCommandInfo()
call go#util#EchoSuccess(printf('[%s] %s', l:type, l:state))
endif
endif
call go#statusline#Update(expand('%:p:h'), l:status)
endfunction
function! go#lint#ToggleMetaLinterAutoSave() abort
if go#config#MetalinterAutosave()
call go#config#SetMetalinterAutosave(0)
call go#util#EchoProgress("auto metalinter disabled")
return
end
call go#config#SetMetalinterAutosave(1)
call go#util#EchoProgress("auto metalinter enabled")
endfunction
function! s:lint_job(metalinter, args, bang, autosave)
let l:opts = {
\ 'statustype': a:args.statustype,
\ 'errorformat': a:args.errformat,
\ 'for': 'GoMetaLinter',
\ 'bang': a:bang,
\ }
if a:autosave
let l:opts.for = 'GoMetaLinterAutoSave'
" s:metalinterautosavecomplete is really only needed for golangci-lint
let l:opts.complete = funcref('s:metalinterautosavecomplete', [a:metalinter, expand('%:p:t')])
let l:opts.preserveerrors = funcref('s:preserveerrors', [a:autosave])
endif
" autowrite is not enabled for jobs
call go#cmd#autowrite()
call go#job#Spawn(a:args.cmd, l:opts)
endfunction
function! s:metalintercmd(metalinter, haslinter)
let l:cmd = []
let bin_path = go#path#CheckBinPath(a:metalinter)
if !empty(bin_path)
if a:metalinter == "golangci-lint"
let l:cmd = s:golangcilintcmd(bin_path, a:haslinter)
endif
endif
return cmd
endfunction
function! s:golangcilintcmd(bin_path, haslinter)
let cmd = [a:bin_path]
let cmd += ["run"]
let cmd += ["--print-issued-lines=false"]
let cmd += ['--build-tags', go#config#BuildTags()]
" do not use the default exclude patterns, because doing so causes golint
" problems about missing doc strings to be ignored and other things that
" golint identifies.
let cmd += ["--exclude-use-default=false"]
if a:haslinter
let cmd += ["--disable-all"]
endif
return cmd
endfunction
function! s:metalinterautosavecomplete(metalinter, filepath, job, exit_code, messages)
if a:metalinter != 'golangci-lint'
return
endif
if len(a:messages) == 0
return
endif
let l:idx = len(a:messages) - 1
while l:idx >= 0
" leave in any messages that report errors about a:filepath or that report
" more general problems that prevent golangci-lint from linting
" a:filepath.
if a:messages[l:idx] !~# '^' . a:filepath . ':' && a:messages[l:idx] !~# '^level='
call remove(a:messages, l:idx)
endif
let l:idx -= 1
endwhile
endfunction
function! s:errorformat(metalinter) abort
if a:metalinter == 'golangci-lint'
" Golangci-lint can output the following:
" <file>:<line>:<column>: <message> (<linter>)
" This can be defined by the following errorformat:
return 'level=%tarning\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%tarning\ msg="%m",level=%trror\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%trror\ msg="%m",%f:%l:%c:\ %m,%f:%l\ %m'
elseif a:metalinter == 'gopls'
return '%f:%l:%c:%t:\ %m,%f:%l:%c::\ %m'
endif
endfunction
function! s:preserveerrors(autosave, listtype) abort
return a:autosave && a:listtype == go#list#Type("GoFmt") && go#config#FmtAutosave() && isdirectory(expand('%:p:h'))
endfunction
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: sw=2 ts=2 et