Wednesday, February 3, 2010

Re: Syntax region problem

On Wed, 3 Feb 2010, Stahlman Family wrote:

> Gregor Uhlenheuer wrote:
> > Hi,
> >
> > I have got a small problem with a syntax region meant to match some
> > nested comments in a syntax script.
> >
> > That's the line:
> > syn region mComment start=/\/\*/ end=/\*\// contains=mComment
> >
> > short example:
> >
> > /* this should be highlighted as a /* comment */ */
> > this text is correctly not highlighted
> > this text is correctly not highlighted
> > this text is correctly not highlighted
> >
> > /* this should be highlighted as a /* comment */*/
> > this text is highlighted, but shouldn't
> > this text is highlighted, but shouldn't
> > this text is highlighted, but shouldn't
> >
> > Somehow the syntax needs that whitespace between the two comment
> > ends, but it should not. I hope I described my problem
> > understandable :)
>
> Gregor,
>
> Try something like this...
> syn region mComment matchgroup=mCommentMg start=/\/\*/ end=/\*\//
> \ contains=mComment
> hi link mComment Comment
> hi link mCommentMg Comment
>
> :help matchgroup
>

Interesting. I thought there was something among the things I
mentioned. This indeed solves the particular case of */*/ within a
comment. To see how it works, link mCommentMg to a different hilight:

:hi link mCommentMg Todo

It's possibly not a perfect solution. E.g.:

/*/*/ (I'm presuming that should parse as a '/' in a comment)

Also interesting, though, is that Vim's C syntax highlighting tags that
construct as 'cCommentStartError'. (because it appears to be a start of a nested
comment, and C-style comments aren't nestable).

Gregor, out of curiosity, for what language are you trying to write a syntax
script? If it's for something you're developing, it seems like you
could avoid a headache by disallowing nesting. Otherwise, have fun.
:-)

If you're just worried about forgetting to check for
comments-within-comments when commenting large blocks, check out The
NERD Commenter:

http://www.vim.org/scripts/script.php?script_id=1218

--
Best,
Ben

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: