vim 语法高亮:以“#”开头的区域并以“#”结尾或行尾

发布于 2025-01-12 07:05:04 字数 739 浏览 4 评论 0原文

正在为一种语言编写 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 "#.+?(?=#|$)"

这似乎工作正常,但没有达到我想要的效果。它确实以第二个“#”结束内联注释,但随后从该主题标签开始新注释,直到换行符。 正则表达式tester result

如何告诉 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.
regular expression tester result

How do I tell the vim syntax file to properly recognize the comments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2025-01-19 07:05:06

这个非常简单

syn region Comment start=/#/ end=/#/ end=/$/

This one is pretty straightforward

syn region Comment start=/#/ end=/#/ end=/$/
独﹏钓一江月 2025-01-19 07:05:06

我认为另一个解决方案是使用类似于字符串匹配的代码(无需转义,即对于字符串,您将使用 " 而不是 #):

syn match hashComment "#[^#]*\(#\|$\)"

只有我们需要结束标记不过,# 或行尾 ($) 可能更清晰。

I think another solution is to use code similar to string matching (without escaping, i.e. for strings you would use " instead of #):

syn match hashComment "#[^#]*\(#\|$\)"

Only we need the end marker to be either # or the end of the line ($). Matt's solution is probably cleaner, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文