为 ghcide 产生的多行错误消息设置错误格式

Setup errorformat for multi line error message produces by ghcide

我正在尝试 setup errorformat for ghcide,它会产生以下输出。

ghcide version: 0.0.4 (GHC: 8.6.5)
Ghcide setup tester in /opt/devel/haskell/haskell-vim-now.
Report bugs at https://github.com/digital-asset/ghcide/issues

Step 1/6: Finding files to test in /opt/devel/haskell/haskell-vim-now
Found 1 files

Step 2/6: Looking for hie.yaml files that control setup
Found 1 cradle

Step 3/6, Cradle 1/1: Implicit cradle for /opt/devel/haskell/haskell-vim-now
Cradle {cradleRootDir = "/opt/devel/haskell/haskell-vim-now", cradleOptsProg = CradleAction: default}

Step 4/6, Cradle 1/1: Loading GHC Session

Step 5/6: Initializing the IDE

Step 6/6: Type checking the files
File:     foo.hs
Range:    1:4-1:5
Source:   typecheck
Severity: DsError
Message: 
  ESC[0;91mfoo.hs:1:5: error:
  • No instance for (Num String) arising from the literal ‘5’
  • In the expression: 5
  In an equation for ‘x’: x = 5ESC[0m
Completed (1 file worked, 0 files failed)

与以下 ~/.vim/compiler/ghcide.vim:

CompilerSet  errorformat=%-Gghcide\ %s
CompilerSet  errorformat+=%-GReport\ bugs\ at\ %s
CompilerSet  errorformat+=%-GStep\ %s
CompilerSet  errorformat+=%-GFound\ %s
CompilerSet  errorformat+=%-GCradle\ %s
CompilerSet  errorformat+=%-GRange:\ %s
CompilerSet  errorformat+=%-GFile:\ %s
CompilerSet  errorformat+=%-GSource:\ %s
CompilerSet  errorformat+=%-GSeverity:\ %s
CompilerSet  errorformat+=%-GCompleted\ %s
" exclude empty or whitespace-only lines
CompilerSet  errorformat+=%-G\s%#
CompilerSet  errorformat+=%E!\ Message:%>%trror%m
setlocal makeprg=ghcide\ %

Quickfix 缓冲区仅显示剩余的四行:

:clist                                                                                                                                                                                                             
 1: Message:-                                                                                                                                                                                                      
 2:   ^[[0;91mfoo.hs:1:5: error:                                                                                                                                                                                   
 3:   • No instance for (Num String) arising from the literal ‘5’                                                                                                                                                  
 4:   • In the expression: 5  

由于我自己无法弄清楚上述多行消息如何映射到单个 clist 条目,我正在寻求帮助。

你可以试试这个:

CompilerSet errorformat=%-Gghcide\ %s
CompilerSet errorformat+=%-GReport\ bugs\ at\ %s
CompilerSet errorformat+=%-GStep\ %s
CompilerSet errorformat+=%-GFound\ %s
CompilerSet errorformat+=%-GCradle\ %s
CompilerSet errorformat+=%-GRange:\ %s
CompilerSet errorformat+=%-GFile:\ %s
CompilerSet errorformat+=%-GSource:\ %s
CompilerSet errorformat+=%-GSeverity:\ %s
CompilerSet errorformat+=%-GCompleted\ %s
" exclude empty or whitespace-only lines
CompilerSet errorformat+=%-G\s%#
CompilerSet errorformat+=%EMessage:%\s%#%>
CompilerSet errorformat+=%C%\s%#ESC[%\d%#;%\d%#m%f:%l:%c:%m
CompilerSet errorformat+=%C%m
CompilerSet errorformat+=%ZCompleted%m

与原始代码相比的不同之处在于最后 4 行:

CompilerSet errorformat+=%EMessage:%\s%#%>
CompilerSet errorformat+=%C%\s%#ESC[%\d%#;%\d%#m%f:%l:%c:%m
CompilerSet errorformat+=%C%m
CompilerSet errorformat+=%ZCompleted%m

原来你用的是这个:

CompilerSet  errorformat+=%E!\ Message:%>%trror%m

但是在您提供的编译器输出中,Message: 行不是以 bang 和 space 开头,所以我认为应该将其删除。

此外,%E已经将条目的类型设置为error,因此%t不是必需的。 而且该行没有消息(消息在下一行),所以不需要%m。 最后,在输出中,有一个尾随的白色space,所以你需要%\s%#.

总而言之:

CompilerSet errorformat+=%EMessage:%\s%#%>

但是你还需要提取文件名和line/column位置,你可以通过resp. %f%l%c

CompilerSet errorformat+=%C%\s%#ESC[%\d%#;%\d%#m%f:%l:%c:%m

请注意,格式以 %C 开头,这告诉 Vim 它匹配多行消息的延续(参见 :h errorformat-multi-line)。

最后,我想你想要消息的其余部分,直到 Completed 行,你可以通过以下方式获得:

CompilerSet errorformat+=%C%m
CompilerSet errorformat+=%ZCompleted%m