I have a couple of YAML files that must contain valid YAML at all times, so I was hoping to use (something like) ale or syntastic to help me enforce that.
It looks like the way syntastic works is that after :w
it runs the checkers (post-write) and reports any errors. Ale works asynchronously but doesn't prevent writes if there are errors. And they both work great as advertised.
But can I use the checkers to prevent the files getting written to disk with :w
or :wq
if they don't pass the checker(s) in a sort of pre-write hook fashion?
Or are there alternatives to ale and syntastic that allow that?
The buffer still would have to be persisted (e.g. to a temp file) for a checker to be able to inspect it. That's additional effort, and I'm not sure whether Syntastic has something like that.
For ALE, as it works asynchronously, it can do this easily. All that's left to do is preventing the write in case of errors. The errors can be obtained via ale#statusline#Count(); you just need to check that in BufWritePre
and abort the write; this can be done by :throw
ing an exception in the handler:
autocmd! BufWritePre <buffer> if ale#statusline#Count(bufnr('')).error > 0 | throw "You have errors in the file; fix them first." | endif
To apply this automatically to the "couple of YAML files", you can define a custom :command
(e.g. :EnforceNoErrors
) that lets you easily set up the :autocmd
(preferably inside an :augroup
then), or use another :autocmd
to make this setting automatically:
:autocmd BufNewFile,BufRead /path/to/the/*.yaml autocmd! BufWritePre ...