Syntax highlight your SQL HEREDOCs in VIM
I got tired of seeing my SQL in big, ugly grey walls of text in my Perl code, so I wanted to syntax highlight them. Of course, since I'm working with a large code base which has evolved over many years, it's simply not possible (or desirable) for me to rip these out and replace them with SQL::Abstract or something else (as has already been suggested) lest I spend a few weeks not developing features and hoping I didn't introduce bugs.
Fortunately, vim allows you to be rather flexible about this.
In our code, we usually have an SQL HEREDOC structure like this:
my $result = $object->do_something(<<'SQL', $var); SELECT this FROM that WHERE bar = ? SQL # or my $sql = <<"SQL"; SELECT this, that FROM $table WHERE $where SQL
The key takeaway here is that our delimiters tend to be static. I wrote the following quick hack and dumped it in my .vim/after/ftplugin/perl.vim file, but it was magnificentears on Twitter who clued me into the "unlet" syntax.
syntax on unlet b:current_syntax syntax include @SQL syntax/sql.vim syntax region sqlSnip matchgroup=Snip start=+<<['"]SQL['"].*;\s*$+ end=+^\s*SQL$+ contains=@SQL hi link Snip SpecialComment
Silly? Perhaps it is, but I like that extra bit of syntax coloring helping me see when I've screwed up some SQL.
Update: Here's his gist:
To those who believe I'm mistaken about it being undesirable to rip out this code, it's pretty straightforward: we refactor, but if and only if there is a clear, demonstrable benefit here and now. We do not do so for "speculative" reasons. It's one of the many reasons we have a very eye-opening work environment that is constantly causing me to reassess many of my fundamental faiths regarding how to develop software.