Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

surface gopls diagnostics #2612

Merged
merged 5 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions autoload/go/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ function! go#config#GoplsFuzzyMatching() abort
return get(g:, 'go_gopls_fuzzy_matching', 1)
endfunction

function! go#config#GoplsStaticCheck() abort
return get(g:, 'go_gopls_staticcheck', 0)
endfunction

function! go#config#GoplsUsePlaceholders() abort
return get(g:, 'go_gopls_use_placeholders', 0)
endfunction
Expand All @@ -533,6 +537,10 @@ function! go#config#GoplsEnabled() abort
return get(g:, 'go_gopls_enabled', 1)
endfunction

function! go#config#DiagnosticsEnabled() abort
return get(g:, 'go_diagnostics_enabled', 0)
endfunction

" Set the default value. A value of "1" is a shortcut for this, for
" compatibility reasons.
if exists("g:go_gorename_prefill") && g:go_gorename_prefill == 1
Expand Down
100 changes: 81 additions & 19 deletions autoload/go/lint.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function! go#lint#Gometa(bang, autosave, ...) abort

let l:metalinter = go#config#MetalinterCommand()

let cmd = []
if l:metalinter == 'golangci-lint'
let cmd = s:metalintercmd(l:metalinter)
if empty(cmd)
Expand All @@ -22,7 +23,7 @@ function! go#lint#Gometa(bang, autosave, ...) abort
for linter in linters
let cmd += ["--enable=".linter]
endfor
else
elseif l:metalinter != 'gopls'
" the user wants something else, let us use it.
let cmd = split(go#config#MetalinterCommand(), " ")
endif
Expand All @@ -45,17 +46,35 @@ function! go#lint#Gometa(bang, autosave, ...) abort

let cmd += goargs

" Golangci-lint can output the following:
" <file>:<line>:<column>: <message> (<linter>)
" This can be defined by the following errorformat:
let errformat = "%f:%l:%c:\ %m"
let errformat = s:errorformat(l:metalinter)

if go#util#has_job()
call s:lint_job({'cmd': cmd, 'statustype': l:metalinter, 'errformat': errformat}, a:bang, a:autosave)
return
endif
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()
call s:lint_job({'cmd': cmd, 'statustype': l:metalinter, 'errformat': errformat}, a:bang, a:autosave)
return
endif

let [l:out, l:err] = go#util#Exec(cmd)
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")
Expand All @@ -70,9 +89,7 @@ function! go#lint#Gometa(bang, autosave, ...) abort
let l:winid = win_getid(winnr())
" Parse and populate our location list

let l:messages = split(out, "\n")

if a:autosave
if a:autosave && l:metalinter != 'gopls'
call s:metalinterautosavecomplete(fnamemodify(expand('%:p'), ":."), 0, 1, l:messages)
endif
call go#list#ParseFormat(l:listtype, errformat, l:messages, 'GoMetaLinter')
Expand All @@ -88,6 +105,44 @@ function! go#lint#Gometa(bang, autosave, ...) abort
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')

let errors = go#list#Get(l:listtype)
call go#list#Window(l:listtype, len(errors))

if a:bang
call win_gotoid(l:winid)
return
endif
call go#list#JumpToFirst(l:listtype)
endif
endfunction

" Golint calls 'golint' on the current directory. Any warnings are populated in
" the location list
function! go#lint#Golint(bang, ...) abort
Expand Down Expand Up @@ -275,18 +330,25 @@ function! s:metalinterautosavecomplete(filepath, job, exit_code, messages)

let l:idx = len(a:messages) - 1
while l:idx >= 0
" Go 1.13 changed how go vet output is formatted by prepending a leading
" 'vet :', so account for that, too. This function is really needed for
" gometalinter at all, so the check for Go 1.13's go vet output shouldn't
" be neeeded, but s:lint_job hooks this up even when the
" g:go_metalinter_command is golangci-lint.
if a:messages[l:idx] !~# '^' . a:filepath . ':' && a:messages[l:idx] !~# '^vet: \.[\\/]' . a:filepath . ':'
if a:messages[l:idx] !~# '^' . a:filepath . ':'
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 '%f:%l:%c:\ %m'
elseif a:metalinter == 'gopls'
return '%f:%l:%c:%t:\ %m,%f:%l:%c::\ %m'
endif

endfunction

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
Expand Down
1 change: 1 addition & 0 deletions autoload/go/list.vim
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ endfunction
" in g:go_list_type_commands.
let s:default_list_type_commands = {
\ "GoBuild": "quickfix",
\ "GoDiagnostics": "quickfix",
\ "GoDebug": "quickfix",
\ "GoErrCheck": "quickfix",
\ "GoFmt": "locationlist",
Expand Down
Loading