VIM 如何计算 / 确定快速修复窗口中错误数

10

这可能听起来有点傻,但我在帮助文档中没有找到它。

如何确定在运行:make之后QuickFix中的错误数量?

或者至少能否查看是否存在任何错误,即错误数> 0?

3个回答

13

您可以使用getqflist()来以编程方式获取错误列表:

getqflist()                     *getqflist()*
        Returns a list with all the current quickfix errors.  Each
        list item is a dictionary with these entries:
            bufnr   number of buffer that has the file name, use
                bufname() to get the name
            lnum    line number in the buffer (first line is 1)
            col column number (first column is 1)
            vcol    non-zero: "col" is visual column
                zero: "col" is byte index
            nr  error number
            pattern search pattern used to locate the error
            text    description of the error
            type    type of the error, 'E', '1', etc.
            valid   non-zero: recognized error message

        When there is no error list or it's empty an empty list is
        returned. Quickfix list entries with non-existing buffer
        number are returned with "bufnr" set to zero.

        Useful application: Find pattern matches in multiple files and
        do something with them: >
            :vimgrep /theword/jg *.c
            :for d in getqflist()
            :   echo bufname(d.bufnr) ':' d.lnum '=' d.text
            :endfor

如果你只想获取总数,使用len(getqflist())。例如:

:echo len(getqflist())

如果你只是想交互式地了解,:cw将在窗口中打开列表,如果有任何错误(如果它已经打开并且没有错误,则关闭它)。该缓冲区中的行数就是错误数。


3
如果快速修复窗口包含未被识别为错误的文本,则getqflist()返回的列表将包含每行的条目。因此,即使len(getqflist())返回非零值,实际上仍可能没有错误。您需要检查结果列表中的“valid”标志。使用filter()函数进行筛选。 - Ben
3
len(filter(getqflist(), 'v:val.valid')) 这段代码将会给你有效的快速修复条目数量,通常情况下它等于所有条目数量,但不总是如此。查看:help filter()可以帮助你理解这一点。;-) - Ben
感谢您的回复。我刚刚阅读了一些教程,并编写了一个不使用 filter() 的常规版本,请查看我的代码片段。显然,使用 filter() 更加优雅。@Ben - nn0p

1
你可以直接使用getqflist()函数(参见:help getqflist()):
:echo printf("Have %d errors", len(getqflist()))

1
如果你想确定快速修复中有多少错误而不仅仅是有多少条目,那么你需要过滤getqflist:
" 'errorformat' matched %t as an error.
let error_count = len(filter(getqflist(), { k,v -> v.type == 'E' }))

" Anything with a destination file.
let jumpable_count = len(filter(getqflist(), { k,v -> v.bufnr != 0 }))

因此,如果您的快速修复看起来像:

test.py|387|  import io, os datetime
||                   ^
|| SyntaxError: invalid syntax

然后 error_count 等于 0,jumpable_count 等于 1。保留HTML标签不做解释。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接