XML grammar: only the closing --> of a comment gets the scope comment.block.xml

未关闭 适合新手
#145 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@alexeyfala 已经在做这个了。

开始于 2026年12月5日。

  • #146 来自 @alexeyfala —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp, json
领域
tooling

调研方向

从 src/TextMateSharp.Grammars/Resources/Grammars/xml/syntaxes/xml.tmLanguage.json 开始,重点查看第 352–392 行附近的两条注释规则,并将它们与链接的 VS Code grammar 进行比较。对于两种注释形式,都将规则 scope 设为 comment.block.xml,并将结束捕获 scope 设为 punctuation.definition.comment.xml。完成标准是:对任一种形式进行 tokenizing 后,都能得到 issue 中展示的预期 scopes。

由索引模型根据 Issue 内容生成。

描述

Describe the bug

In the XML grammar of TextMateSharp.Grammars, the comment rules (<!-- --> and <%-- --%>) have no name; comment.block.xml is given only to the end capture:

https://github.com/danipen/TextMateSharp/blob/1176721ab747d6f2f78992f50111c0a04b942e72/src/TextMateSharp.Grammars/Resources/Grammars/xml/syntaxes/xml.tmLanguage.json#L352-L392

So the opening <!-- and the text of the comment get no comment scope, and a theme colors only the closing -->. In Light+ the comment text stays in the default foreground, and only --> is green. The XSL grammar includes text.xml, so it shows the same.

The grammar comes from atom/language-xml (per cgmanifest.json), where the <!-- rule has "name": "comment.block.xml" and marks both ends as punctuation; VS Code ships it that way:

https://github.com/microsoft/vscode/blob/10a1d2a50a2882f5ae85bdb51eb04d3064fb9de9/extensions/xml/syntaxes/xml.tmLanguage.json#L364-L383

The file has not changed since 2022-02-15, so 2.0.4, 2.0.5 and 3.0.0-preview.1 ship the same rules.

To Reproduce

RegistryOptions options = new(ThemeName.LightPlus);
IGrammar grammar = new Registry(options).LoadGrammar("text.xml");

foreach (IToken token in grammar.TokenizeLine("<!-- a comment -->").Tokens)
{
    Console.WriteLine(string.Join(" ", token.Scopes));
}

Output with TextMateSharp.Grammars 2.0.3:

[<!--] text.xml punctuation.definition.comment.xml
[ a comment ] text.xml
[-->] text.xml comment.block.xml

<%-- a comment --%> gives the same pattern.

Expected behavior

The whole comment has comment.block.xml, and both marks also have punctuation.definition.comment.xml:

[<!--] text.xml comment.block.xml punctuation.definition.comment.xml
[ a comment ] text.xml comment.block.xml
[-->] text.xml comment.block.xml punctuation.definition.comment.xml

Screenshots

None: the token scopes above show it.

Desktop (please complete the following information):

  • OS: Windows 11 Pro 10.0.26300 (the grammar is platform independent)
  • Version: TextMateSharp.Grammars 2.0.3; master has the same file

Additional context

This change of xml.tmLanguage.json gives the expected output above for both kinds of comments (checked by loading the changed file with LoadGrammarFromPathSync):

 					"end": "--%>",
 					"endCaptures": {
 						"0": {
-							"name": "comment.block.xml"
+							"name": "punctuation.definition.comment.xml"
 						}
-					}
+					},
+					"name": "comment.block.xml"
 				},
 					"end": "-->",
 					"endCaptures": {
 						"0": {
-							"name": "comment.block.xml"
+							"name": "punctuation.definition.comment.xml"
 						}
 					},
+					"name": "comment.block.xml",
 					"patterns": [

Until then we repair the raw grammar after reading it: for each rule of repository.comments whose end capture "0" is named comment.block.xml, we set that name on the rule (IRawRule.SetName) and rename the capture to punctuation.definition.comment.xml.

I can open a pull request with this change if that helps.

主要语言
C#
星标
170
派生
35
平均合并
2 小时 52 分钟
30 天内合并 PR
1

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

danipen/TextMateSharp 的其他 Issue

查看 danipen/TextMateSharp 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。