Guidelines

This site is for tech Q&A. Please keep your posts focused on the subject at hand.

Ask one question at a time. Don't conflate multiple problems into a single question.

Make sure to include all relevant information in your posts. Try to avoid linking to external sites.

Links to documentation are fine, but in addition you should also quote the relevant parts in your posts.

0 votes
26 views
26 views

I want vim to automatically run a syntax check whenever I save a Puppet manifest. The command for validating a manifest is

puppet parser validate FILE

and I can run the command on the file in the current buffer by setting makeprg for Puppet manifests and then running the configured make command:

autocmd FileType puppet set makeprg=puppet\ parser\ validate\ %
autocmd BufWritePost *.pp :silent make | redraw!

However, I only see the validation output when I tell vim to always display the quickfix window:

autocmd QuickFixCmdPost [^l]* nested copen

What I would like is to have the quickfix window displayed only if there are errors (and automatically closed if there are none):

autocmd QuickFixCmdPost [^l]* nested cwindow
autocmd QuickFixCmdPost l* nested lwindow

But with the latter settings, the quickfix window is never shown, even if the parser detects errors (verified by manually running the command).

in Scripting
by (100)
3 9
edit history

Your answer

Preview

Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
By submitting this post you agree to our Terms & Conditions.
To avoid this verification in future, please log in or register.

1 Answer

0 votes
 

The problem seems to be that the parser produces its output in a different format than what quickfix expects. The error coordinates in the parser output are at the end of the error message:

Error: Could not parse for environment production: Syntax error at 'foo' (file: /some/dir/file.pp, line: 42, column: 23)

whereas quickfix would expect them at the beginning of the message:

/some/dir/file.pp:42:23: Error: Could not parse for environment production: Syntax error at 'foo'

You should be able to mitigate the issue by re-arranging the error information with a script like this:

#!/bin/sh
puppet parser validate "$1" 2>&1 |
  sed -e 's/\(.*\) (file: \(.*\), line: \(.*\), column: \(.*\))$/\2:\3:\4: \1/'

and than calling that script as the make command in vim:

autocmd FileType puppet set makeprg=puppet_validate.sh\ %
autocmd BufWritePost *.pp :silent make | redraw!
autocmd QuickFixCmdPost [^l]* nested cwindow
autocmd QuickFixCmdPost l* nested lwindow

Note that the script location must be in the PATH, otherwise you have to specify the full path to the script in the autocmd statement.

by (100)
3 9
edit history
...