簡體   English   中英

Vim errorformat:在消息字符串中包含表達式的一部分

[英]Vim errorformat: include part of expression in message string

使用vim的errorformat語法,有沒有辦法在過濾結果時使用部分消息?

例如,除了錯誤本身之外,一些鏈接器錯誤沒有任何明確的區別將它們區分為行上的錯誤:

/path/to/foo.cpp:42: undefined reference to 'UnimplementedFunction'

要么

/path/to/foo.cpp:43: multiple definition of 'MultiplyDefinedFunction'

使用錯誤格式:

set efm=%f:%l:\ %m

會正確地捕獲並顯示這兩個,但會錯誤地匹配許多其他情況(任何以“[string]:[number]:”開頭的行)。

或者,明確指定它們:

set efm=
set efm+=%f:%l:\ undefined\ reference\ to\ %m
set efm+=%f:%l:\ multiple\ definition\ of\ %m

消除誤報,但'消息'變得不那么有用 - 不再包含實際錯誤(只是在它之后的任何內容)。


我缺少什么語法來處理這種情況?

理想情況下,我希望能夠說出以下內容:

set efm+=%f:%l:\ %{StartMessage}undefined\ reference\ to\ %*\\S%{EndMessage}
set efm+=%f:%l:\ %{StartMessage}multiple\ definition\ of\ %*\\S%{EndMessage}

...將StartMessage和EndMessage之間匹配的所有內容用作錯誤消息。

errorformat也可以使用vim的正則表達式語法(雖然以相當笨拙的方式),它為我們提供了解決問題的方法。 我們可以使用非捕獲組和零寬度斷言來要求存在這些信令短語而不消耗它們。 然后,這允許%m拾取它們。 作為簡單的正則表達式語法,這個零寬度斷言看起來像:

\%(undefined reference\|multiple definition\)\@=

但是,為了使用它在efm我們需要替換\\%\\%%%和在使用:set行,我們需要逃避反斜杠,空間和豎桿上,我們終於有:

:set efm=%f:%l:\ %\\%%(undefined\ reference%\\\|multiple\ definitions%\\)%\\@=%m

有了那個錯誤文件

/path/to/foo.cpp:42: undefined reference to 'UnimplementedFunction'
/path/to/foo.cpp:43: multiple definition of 'MultiplyDefinedFunction'
notafile:123: just some other text

出現如下:copen

/path/to/foo.cpp|42| undefined reference to 'UnimplementedFunction'
/path/to/foo.cpp|43| multiple definition of 'MultiplyDefinedFunction'
|| notafile:123: just some other text

我一直在使用sed在這樣的情況下重寫輸出,我希望在quickfix窗口中獲得一些不必要的任意輸出。

您可以編寫make.sh來觸發make(或者您正在構建的任何內容)並修剪掉您不關心的內容:

make | sed '/undefined reference\|multiple definition/!d'

(刪除不包含“未定義引用”或“多重定義”的行)

如果由於你關心的錯誤字符串的數量而變得太不合適,你可以做反過來,只是殺掉你不關心的東西:

make | sed 's/some garbage\|other useless message//'

然后:set makeprg=make.sh在vim中:set makeprg=make.sh

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM