【问题标题】:Can't get the scope inspector to recognise comments无法让范围检查员识别评论
【发布时间】:2020-10-16 04:02:04
【问题描述】:

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

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

我从 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 扩展名),cmets 是带有双斜杠的行 cmets 或分别以 /**/ 开头和结尾的块 cmets

测试文本文件.txt

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

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

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

【问题讨论】:

    标签: visual-studio-code vscode-extensions tmlanguage


    【解决方案1】:

    问题是# 引用中包含的模式必须直接在"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"
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多