3

For a new filetype, I was trying to make block comments in the syntax highlighting but they kept disappearing while I was moving around. I have managed to reduce it to the following. First, in the syntax file, there is just this:

syn region testComment start=/a/ end=/b/
hi def link testComment Comment

In my file created to highlight (pun not intended) the problem, I have this:

a
()
 (

)
(
)
b

According to the rule, the entire file should be colored like a comment, and it does normally. However, when moving the cursor down through the whole file while staying on the first column, once I hit the final set of parentheses, the last three visible characters ('(', ')', and 'b') are un-colored. This can be fixed in the moment by redrawing the screen.

I noticed that the matchparen plugin is involved. While moving the cursor down, the first set of parentheses is matched correctly, the second set is not matched, and then when the third set should be matched is when the color goes off. Disabling the plugin through :NoMatchParen makes the problem not appear.

Several things cause the problem not to happen. If I remove the empty line it works. If I go to the second opening parenthesis on the way down it works. If I remove the empty line but add an extra space before the second close parenthesis, the problem comes back. It seems like the plugin has a hard time reading the second set of parentheses and then freaks out on the third one.

I am pretty sure this is a bug in the matchparen plugin. However, block comments work just fine in other languages, like C. So, my final question is this: How can I change my syntax file to make this problem not happen, maybe similarly to how C does it?

I have Vim 8.1.

  • try with the X connection and clipboard disabled to make sure this isn't bug github.com/vim/vim/issues/4201 – Mass Jul 13 at 0:54
  • It still happens. I don't really know why what I have would be related to that bug. This has nothing to do with visually selecting. That bug also seems non-deterministic, while mine is deterministic. – sudgy Jul 13 at 0:59
2

When parentheses are highlighted and unhighlighted vim needs to re-synchronize the syntax highlighting. What is happening is vim doesn't know the lines are inside a comment because it tries to avoid looking backwards if it doesn't need to. See :help :syn-sync. There are four methods available.

The simplest method is using syntax sync fromstart, but this makes syntax highlighting slow.

The second method is literally hardwired for C comments (/* */) and will only work for them.

The third method is similar to fromstart, except looks back a certain number of lines:

:syntax sync minlines=50

The fourth method works when you have start and end delimiters like a and b:

:syntax sync match startComment groupthere testComment "a"
:syntax sync match endComment groupthere NONE "b"

There are more complex uses shown in the help.

  • 1
    The fourth method seems to be most appropriate here, in particular using :syn sync match startComment groupthere testComment "a" and :syn sync match endComment groupthere NONE "b" seems to solve the particular example raised by the OP. Of course, as the docs highlight, this gets complicated quickly as delimiters for a region can exist in different contexts (such as inside a string.) – filbranden Jul 13 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.