From 8bc21febb35058010f02bec78c1252cf8ce2ebb1 Mon Sep 17 00:00:00 2001 From: Alex Huth Date: Mon, 19 Oct 2015 11:06:09 -0700 Subject: [PATCH] Drop bad filenames from qflist in GoRun Fix for https://github.com/fatih/vim-go/issues/287 When a line of output is parsed incorrectly as an error message, the quickfix feature of Vim creates an unlisted buffer containing a bad filename. On jumping to this item with `cc`, a blank, unsaved file is opened in the active window. It is never useful to jump to a nonexistent file from quickfix and users unused to navigating buffers in Vim may have trouble figuring out what happened. Since the line of output that was parsed may still be an important error message, rather than silently dropping it from the quickfix window, echo a message for each bad filename dropped in this way. (Known issue: If `cc 1` jumps to a new buffer immediately, the message `"{filename}" {linenumber}L, {colnumber}C` hides the drop messages.) While looping through qflist items, avoid checking the same `filename` more than once by remembering the return values of previous calls to `filereadable` during the current execution of `:GoRun`. Add comments to explain the compiler errorformat strings. Patterns must be appended in separate statements in order to use inline comments here. --- autoload/go/cmd.vim | 27 ++++++++++++++++++++++++--- compiler/go.vim | 17 +++++++++-------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/autoload/go/cmd.vim b/autoload/go/cmd.vim index 911e828df6..b926705537 100644 --- a/autoload/go/cmd.vim +++ b/autoload/go/cmd.vim @@ -83,12 +83,33 @@ function! go#cmd#Run(bang, ...) exe 'make!' endif - cwindow - let errors = getqflist() + " Remove any nonvalid filename from the qflist to avoid opening an empty + " buffer. See https://github.com/fatih/vim-go/issues/287 for details. + let qflist = getqflist() + let errors = [] + let is_readable = {} + + for item in qflist + let filename = bufname(item.bufnr) + if !has_key(is_readable, filename) + let is_readable[filename] = filereadable(filename) + endif + if is_readable[filename] + call add(errors, item) + endif + endfor + + for k in keys(filter(is_readable, '!v:val')) + echo "vim-go: " | echohl Identifier | echon "[run] Dropped " | echohl Constant | echon '"' . k . '"' + echohl Identifier | echon " from QuickFix list (nonvalid filename)" | echohl None + endfor + + call setqflist(errors) if !empty(errors) && !a:bang cc 1 "jump to first error if there is any endif - + cwindow + let $GOPATH = old_gopath let &makeprg = default_makeprg endfunction diff --git a/compiler/go.vim b/compiler/go.vim index 2ce350d39f..e7b90bb717 100644 --- a/compiler/go.vim +++ b/compiler/go.vim @@ -21,14 +21,15 @@ else CompilerSet makeprg=go\ build endif -CompilerSet errorformat= - \%-G#\ %.%#, - \%-G%.%#panic:\ %m, - \%Ecan\'t\ load\ package:\ %m, - \%A%f:%l:%c:\ %m, - \%A%f:%l:\ %m, - \%C%*\\s%m, - \%-G%.%# +" Define the patterns that will be recognized by QuickFix when parsing the output of GoRun. +" More information at http://vimdoc.sourceforge.net/htmldoc/quickfix.html#errorformat +CompilerSet errorformat =%-G#\ %.%# " Ignore lines beginning with '#' ('# command-line-arguments' line sometimes appears?) +CompilerSet errorformat+=%-G%.%#panic:\ %m " Ignore lines containing 'panic: message' +CompilerSet errorformat+=%Ecan\'t\ load\ package:\ %m " Start of multiline error string is 'can\'t load package' +CompilerSet errorformat+=%A%f:%l:%c:\ %m " Start of multiline unspecified string is 'filename:linenumber:columnnumber:' +CompilerSet errorformat+=%A%f:%l:\ %m " Start of multiline unspecified string is 'filename:linenumber:' +CompilerSet errorformat+=%C%*\\s%m " Continuation of multiline error message is indented +CompilerSet errorformat+=%-G%.%# " All lines not matching any of the above patterns are ignored let &cpo = s:save_cpo unlet s:save_cpo