How do I make vim highlight any { not followed by either a C++ style comment, or two newlines?
I would like to create a match pattern for any opening brace that does not follow one of these patterns:
{\n\n
{\s*\/\/.*\n\(\s*\/\/.*\)\?\n
The more general problem is highlighting violations of a coding spec at work, which enforces a blank line following {
Clarification, I'm looking for this to catch code like the following:
if (foo) {
this_is_bad____no_blank_line_above();
} else {this_is_worse();}
while (1) { //This comment is allowed
//This one too
theres_nothing_wrong_with_this();
}
if (foo) {
....//<-- Ideally we could mark this as bad, due to the spaces here
otherwise_perfectly_good();
}
What I really need is: {\(\n\n\|\s*\/\/.*\n\(\s*\/\/.*\)\?\n\)\!
Where the made-up symbol \!
means "Does not matc开发者_开发技巧h either of these two options". I see a way of doing that for individual characters, but not for a longer string.
Found it:
I was looking for \@!
Documented at :h /\@!
{\(\n\n\|\s*\/\/.*\n\(\s*\/\/.*\)\?\n\)\@!
Add the following to the end of your .vimrc file:
:highlight InvalidStyle ctermbg=red guibg=red ctermfg=black guifg=black
:match InvalidStyle /{\s*[^\t \/]\+.*$/
The first line defines a new highlight style (black on red) and the next line tries to find any curly braces that have content after them which aren't comments and applies the highlighting on them.
精彩评论