【问题标题】:Vim errorformat and spectral lintVim 错误格式和光谱 lint
【发布时间】:2020-09-25 06:05:02
【问题描述】:

我正在尝试为 Spectral OpenAPI linter (https://github.com/stoplightio/spectral) 定义错误格式。我的代码如下,但我看到的是,在我运行:make 之后,quickfix 窗口会填充来自 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

【问题讨论】:

    标签: vim swagger openapi spectral


    【解决方案1】:

    以下内容应该足够了:

    setlocal errorformat=%f:%l:%c\ %t%.%\\{-}\ %m
    
    • %f:%l:%c 匹配以冒号分隔的文件名、行和列,
    • %t 匹配单个字符,用于推断错误的类型(error if e, warning if w, info if i, hint is not supported),
    • %.%\\{-} 跳过“类型”字的其余部分,
    • %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
    

    【讨论】:

    • 这非常有用,谢谢!从您的消息中得到提示,我继续创建编译器定义并将副本推送到 Github,以防它对其他人有用:github.com/michaeljmcd/spectral.vim
    猜你喜欢
    • 2011-04-12
    • 2011-08-17
    • 1970-01-01
    • 2017-03-27
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    相关资源
    最近更新 更多