vim 语法高亮:以“#”开头的区域并以“#”结尾或行尾
我
正在为一种语言编写 vim 语法文件,该语言允许遵循此方案进行内联注释:
This is code # and here is comment # but this is code again
该语言还允许注释以“#”开头并以换行符结尾,如下所示:
This is code # and until the end of the line, this is a comment
想要什么
我 喜欢两者都被我的语法文件识别为注释。
我尝试过的
当我使用: 时
syn region comment start="#" end="#"
,结果是注释不由换行符分隔。现在,我已经做了一些研究,并尝试使用以下正则表达式匹配:
syn match comment "#.+?(?=#|$)"
这似乎工作正常,但没有达到我想要的效果。它确实以第二个“#”结束内联注释,但随后从该主题标签开始新注释,直到换行符。
如何告诉 vim 语法文件正确识别注释?
The situation
I am in the process of writing a vim syntax file for a language that allows inline comments following this scheme:
This is code # and here is comment # but this is code again
The language also allows comment to start with a "#" and end with a newline, like so:
This is code # and until the end of the line, this is a comment
What I want
I would like both to be recognized by my syntax file as comments.
What I've tried
When I use:
syn region comment start="#" end="#"
, the result is that comments are not delimited by newlines. Now, I've done some research and tried it with the following regex match:
syn match comment "#.+?(?=#|$)"
And that seems to work properly, but doesn't do what I want. It does end the inline comment by the second "#", but then starts a new comment from that hashtag until the newline.
How do I tell the vim syntax file to properly recognize the comments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个非常简单
This one is pretty straightforward
我认为另一个解决方案是使用类似于字符串匹配的代码(无需转义,即对于字符串,您将使用
"
而不是#
):只有我们需要结束标记不过,
#
或行尾 ($
) 可能更清晰。I think another solution is to use code similar to string matching (without escaping, i.e. for strings you would use
"
instead of#
):Only we need the end marker to be either
#
or the end of the line ($
). Matt's solution is probably cleaner, though.