4

我想为任何不遵循以下模式之一的左大括号创建一个匹配模式:

  1. {\n\n
  2. {\s*\/\/.*\n\(\s*\/\/.*\)\?\n

更普遍的问题是在工作中突出显示违反编码规范的行为,这会在后面强制一个空行{

澄清一下,我正在寻找这个来捕获如下代码:

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();
}

我真正需要的是:{\(\n\n\|\s*\/\/.*\n\(\s*\/\/.*\)\?\n\)\!

其中虚构符号\!表示“与这两个选项中的任何一个都不匹配”。我看到了一种对单个字符执行此操作的方法,但对于更长的字符串却没有。

4

2 回答 2

6

找到了:

我在寻找\@!

记录于:h /\@!

{\(\n\n\|\s*\/\/.*\n\(\s*\/\/.*\)\?\n\)\@!
于 2010-08-20T19:02:11.557 回答
1

将以下内容添加到 .vimrc 文件的末尾:

:highlight InvalidStyle ctermbg=red guibg=red ctermfg=black guifg=black
:match InvalidStyle /{\s*[^\t \/]\+.*$/

第一行定义了一个新的高亮样式(红底黑字),下一行试图找到任何在其后有非注释内容的花括号,并对其应用高亮显示。

于 2010-08-20T16:58:19.050 回答