Highlighting unmatched brackets in vim

后端 未结 7 440
感动是毒
感动是毒 2020-12-29 09:18

I\'m getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they\'re handled for C code - vim highlights in red all of the curly br

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 10:13

    Not sure if it'll be more or less confusing for you, but you could look at the lisp.vim syntax file (especially the part where g:lisp_rainbow is handled) to see how you can highlight matching parens.

    If you manage to highlight all the matching parens, you could have the leftover parens (i.e. unmatched parens) have default Error highlighting. This is what the lisp file seems to be doing.

    EDIT: How about this:

    syn match parenError ")"
    syn region matchingParens transparent start="(" end=")" contains=matchingParens
    hi parenError guifg=red
    

    If you :syn clear and run those, it seems to work. Note that the order the syn commands are executed matters. Per :h :syn-priority, the rule matched last is the one that takes effect, which may be why your rules highlighted all the end-parens in the file.

    EDIT #2:

    What c.vim is actually doing is highlighting any {} inside of (), whether everything is properly closed or not. Try typing ({}) in C mode, it still highlights the {} as an error.

    I don't think this approach can be used to test directly for a ( with an unmatched ), because :syn region doesn't care whether the end-pattern is there or not.

    So you have to find something Python-specific that should never belong inside (). Then match against "(\_[^)]*the_forbidden_something". I don't know Python enough to know what that might be.

    If nothing else, you can do:

    syn match openParen "(\_[^)]*\%$"
    

    which matches an open paren with no closing parens before the end-of-file. This fails if it finds any closing paren at all, which means it won't even catch (().

提交回复
热议问题