2

我一直在尝试为我们在文档合成工具中使用的规则编写自己的语法突出显示扩展。

我已经能够让它大部分工作正常,但我无法让范围检查员识别这些评论。

我从 tmLanguage 文件中删除了所有其他工作代码以排除冲突,并留下以下内容

ote-rules.tmLanguage.json

{
    "scopeName": "source.ote-rules",
    "patterns": [{
        "include": "#expression"
    }],
    "repository": {
        "expression": {
            "patterns": [
                {
                    "include": "#comments-ote"
                }
            ]
        },
        "comments-ote": {
            "patterns": [{
                    "include": "#comments-block"
                },
                {
                    "include": "#comments-line"
                }
            ],
            "comments-line": {
                "match": "\\/\\/.*?$",
                "name": "comment.line.double-slash.ote-rules"
            },
            "comments-block": {
                "begin": "\\/\\*",
                "end": "\\*\\/",
                "beginCaptures":{
                    "0":{"name":"punctuation.definition.comment.ote-rules"}
                },
                "endCaptures":{
                    "0":{"name":"punctuation.definition.comment.ote-rules"}
                }
            }
        }
    }
}

我要匹配的文件是纯文本文件(*.txt扩展名),注释是带有双斜杠的行注释或分别以/*and开头和结尾的块注释*/

测试文本文件.txt

// just some comment text 
  // indented comment
//
// left the above line empty apart from slashes
/*
inline block comment 
*/

当我用范围检查器查看上面的文本时,它认识到它来自 source.ote-rules 但将令牌类型显示为“其他”

范围检查器输出的屏幕截图显示范围为其他

我已经检查了 rubular.com 上的正则表达式,它们似乎适用于我展示的示例,所以我错过了什么?

4

1 回答 1

2

问题是#引用中包含的模式必须直接"repository""comments-ote". 它根本找不到它们。

此外,您可能想给您"comments-block"的范围名称,以便将其突出显示为注释,例如"name": "comment.block.ote-rules".

{
    "scopeName": "source.ote-rules",
    "patterns": [
        {
            "include": "#expression"
        }
    ],
    "repository": {
        "expression": {
            "patterns": [
                {
                    "include": "#comments-ote"
                }
            ]
        },
        "comments-ote": {
            "patterns": [
                {
                    "include": "#comments-block"
                },
                {
                    "include": "#comments-line"
                }
            ]
        },
        "comments-line": {
            "match": "\\/\\/.*?$",
            "name": "comment.line.double-slash.ote-rules"
        },
        "comments-block": {
            "begin": "\\/\\*",
            "end": "\\*\\/",
            "beginCaptures": {
                "0": {
                    "name": "punctuation.definition.comment.ote-rules"
                }
            },
            "endCaptures": {
                "0": {
                    "name": "punctuation.definition.comment.ote-rules"
                }
            },
            "name": "comment.block.ote-rules"
        }
    }
}

注意:你不需要使用"include",你也可以直接在里面指定模式"patterns"

"comments-ote": {
    "patterns": [
        {
            "match": "\\/\\/.*?$",
            "name": "comment.line.double-slash.ote-rules"
        },
        {
            "begin": "\\/\\*",
            "end": "\\*\\/",
            "beginCaptures": {
                "0": {
                    "name": "punctuation.definition.comment.ote-rules"
                }
            },
            "endCaptures": {
                "0": {
                    "name": "punctuation.definition.comment.ote-rules"
                }
            },
            "name": "comment.block.ote-rules"
        }
    ]
}
于 2020-06-25T19:44:42.710 回答