views:

49

answers:

2

I would like to create a match pattern for any opening brace that does not follow one of these patterns: A:

{\n\n

B:

{\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 match either of these two options". I see a way of doing that for individual characters, but not for a longer string

A: 

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.

dionyziz
Hmm... clarifying a few examples of what should be caught by this.
jkerian
+1  A: 

Found it... I was looking for \@!

Documented at :h /\@!

{\(\n\n\|\s*\/\/.*\n\(\s*\/\/.*\)\?\n\)\@!
jkerian