Search code examples
vimswaggeropenapispectral

Vim errorformat and spectral lint


I am attempting to define an error format for the Spectral OpenAPI linter (https://github.com/stoplightio/spectral). The code I have is below, but what I'm seeing is that after I run :make the quickfix window populates with lines from Spectral, but I can't navigate to error spots using it. There are no errors in Vim, the quick fix window just doesn't do much.

The messages from Spectral look like this:

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

My current Vimscript looks like this:

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

Solution

  • The following should be enough:

    setlocal errorformat=%f:%l:%c\ %t%.%\\{-}\ %m
    
    • %f:%l:%c matches the colon-separated filename, line, and column,
    • %t matches a single character, used to infer the type of the error (error if e, warning if w, info if i, hint is not supported),
    • %.%\\{-} skips over the rest of the "type" word,
    • %m matches the rest of the message.

    Also, the right place for :help 'errorformat' and :help 'makeprg' would be a :help :compiler file:

    " 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
    

    and the right place for that OpenAPI detection logic would be a :help ftplugin:

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

    example