1

我正在尝试为 Spectral OpenAPI linter ( https://github.com/stoplightio/spectral ) 定义错误格式。我的代码在下面,但我看到的是,在我运行:makequickfix 窗口后,会填充来自 Spectral 的行,但我无法使用它导航到错误点。Vim 中没有错误,快速修复窗口没有多大作用。

来自 Spectral 的消息如下所示:

/path/to/sample.yaml:25:9 error oas3-schema "Property `think` is not expected to be here."

我当前的 Vimscript 如下所示:

function! OpenAPIDetect()
    if getline(1) =~ 'openapi:'
        let &l:makeprg='spectral lint "' .  expand('%') . '" -f text'
        setlocal errorformat=%f:%l:%c\ (information\\|warning\\|error)\ (\\w\\|-)\+\ %m
        setlocal errorformat+=%-GOpenAPI\ 3.x\ detected
    endif
endfunction

augroup filetypedetect
    au BufRead,BufNewFile *.{yml,yaml} call OpenAPIDetect()
augroup END
4

1 回答 1

3

以下应该足够了:

setlocal errorformat=%f:%l:%c\ %t%.%\\{-}\ %m
  • %f:%l:%c匹配冒号分隔的文件名、行和列,
  • %t匹配单个字符,用于推断错误的类型(不支持errorif ewarningif winfoif i、 ),hint
  • %.%\\{-}跳过“类型”字的其余部分,
  • %m匹配消息的其余部分。

此外,正确的位置:help 'errorformat':help 'makeprg'将是一个:help :compiler文件:

" in a minimal compiler/spectral.vim
if exists("current_compiler")
    finish
endif
let current_compiler = "spectral"
CompilerSet makeprg=spectral\ lint\ %\ -f\ text
CompilerSet errorformat=%f:%l:%c\ %t%.%\\{-}\ %m

并且该 OpenAPI 检测逻辑的正确位置是:help ftplugin

" in a minimal after/ftplugin/yaml.vim
if getline(1) =~ 'openapi:'
    compiler spectral
endif

例子

于 2020-06-06T06:05:31.517 回答