如何让Vim解析我的多行错误消息以在快速修复窗口中显示?

4
我想使用make在vim中运行我的应用程序,并且希望quickfix窗口显示我的错误信息。
因此,我有了这个格式,首先以Error:开头,然后是由:分隔的文件名、行号和列号,然后在下一行,将会有一个没有特殊格式的多行消息,消息以ErrorEnd结束。
以下是一个示例:
Error: /somefile/something/something.c:12:123
SOME MESSAGE 
ANOTHER HELPFUL MESSAGE
ANOTHER MESSAGE
ErrorEnd

我在如何使它与这些行匹配的文档中有些迷失了。一切都看起来很混乱,而且示例与此不同。我知道如何使其与第一行匹配,但不知道如何使其与下一行(作为错误消息)匹配。因此,问题是什么样的错误格式字符串可以解析所有内容。

3个回答

3

您说得对,解析quickfix的多行错误消息确实很困难。我甚至不确定是否可能解析此类块中的错误作为单独的错误。

针对难以处理的错误输出,我采用了一种解决方法,即在'makeprg'中附加一个转换步骤(通常使用sed),将多行错误转换为传统的每个错误一行的消息;例如:

Error: /somefile/something/something.c:12:123 SOME MESSAGE
Error: /somefile/something/something.c:12:123 ANOTHER HELPFUL MESSAGE
Error: /somefile/something/something.c:12:123 ANOTHER MESSAGE

在您的情况下。

是的,这就是我正在做的事情。希望有更多的支持来处理这样的错误。 - Farid Nouri Neshat

3

从vim errorformat帮助页面:

Multi-line messages             *errorformat-multi-line*

It is possible to read the output of programs that produce multi-line
messages, i.e. error strings that consume more than one line.  Possible
prefixes are:
    %E      start of a multi-line error message
    %W      start of a multi-line warning message
    %I      start of a multi-line informational message
    %A      start of a multi-line message (unspecified type)
    %>      for next line start with current pattern again |efm-%>|
    %C      continuation of a multi-line message
    %Z      end of a multi-line message
These can be used with '+' and '-', see |efm-ignore| below.

Using "\n" in the pattern won't work to match multi-line messages.

Example: Your compiler happens to write out errors in the following format
(leading line numbers not being part of the actual output):

     1  Error 275 
     2  line 42 
     3  column 3 
     4  ' ' expected after '--' 

The appropriate error format string has to look like this:
   :set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m

编辑:啊,你是指多行错误。没错,那更难些。

3
你可以使用%+前缀来捕获错误消息中的多行文本,具体请参见:help efm-ignore,结合%E%C%Z等多行错误格式说明符,具体请参见:help errorformat-multi-line。在你的特定示例中,以下内容似乎有效:
let &l:efm='%EError: %f:%l:%c,%-ZErrorEnd,%+C%.%#'

请注意%+C项,它匹配行中的任何文本并将其添加到错误消息中。还要注意我必须在此项之前放置%-Z项才能使用它,因为解析行时将使用第一个匹配的errorformat项。

这是唯一一个正确回答问题的响应,但它(目前)却有最少的赞数?嗯。 - Christian Abbott
注意:Vim将源多行转换为单行条目,以便在快速修复列表中显示。据我所知,快速修复无法显示原始的多行条目,只能显示已转换为单行的条目,并使用格式字符串。花了我一些时间才意识到这个区别。 - undefined

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