如何在 VSCode 扩展中突出显示块注释和内联注释的语法?

问题描述 投票:0回答:1

这是我的要实现的目标

这就是我得到的:

我的小语法高亮 vscode 扩展看起来像这样:

package.json(重要摘录)

"contributes": {
    "languages": [{
      "id": "foo",
      "extensions": [".foo"],
      "configuration": "./language-configuration.json"
    }],
    "grammars": [{
      "language": "foo",
      "scopeName": "source.foo",
      "path": "./syntaxes/foo.tmLanguage.json"
    }],
    "themes": [
      {
        "label": "dark",
        "uiTheme": "vs-dark",
        "path": "./themes/d-color-theme.json"
      }
    ]
  }

语言配置.json

{
    "comments": {
        "lineComment": "//",
        "blockComment": [ "\/*", "*\/" ]
    }
}

语法/foo.tmLanguage.json

{
    "name": "Foo Language",
    "patterns": [
        {
            "include": "#longComments"
        },
        {
            "include": "#shortComments"
        },
        {
            "include": "#pascalCase"
        },
        {
            "include": "#camelCase"
        }

    ],
    "repository": {
        "longComments": {
            "patterns": [{
                "name": "blockComment.foo",
                "match": "\/*((?:.|\n)*?)*\/"
            }]
        },
        "camelCase": {
            "patterns": [{
                "name": "camelCase.foo",
                "match": "[a-z][a-z0-9A-Z_]*"
            }]
        },
        "pascalCase": {
            "patterns": [{
                "name": "pascalCase.foo",
                "match": "[A-Z][a-z0-9A-Z_]*"
            }]
        },
        "shortComments": {
            "patterns": [{
                "name": "lineComment.foo",
                "match": "\/\/[^\n]*"
            }]
        }
    },
    "scopeName": "source.foo"
}

主题/深色主题.json

{
    "name": "dark",
    "colors": {
        "editor.background": "#263238",
        "editor.foreground": "#eeffff",
        "activityBarBadge.background": "#007acc",
        "sideBarTitle.foreground": "#bbbbbb"
    },
    "tokenColors": [
        {
            "name": "Pascal Case Identifiers",
            "scope": [
                "pascalCase.foo"
            ],
            "settings": {
                "foreground": "#06d44a"
            }
        },
        {
            "name": "Comment",
            "scope": [
                "blockComment.foo",
                "lineComment.foo"
            ],
            "settings": {
                "fontStyle": "italic",
                "foreground": "#546E7A"
            }
        },
        {
            "name": "Camel Case Identifiers",
            "scope": [
                "camelCase.foo"
            ],
            "settings": {
                "foreground": "#f7c63f"
            }
        }
    ]
}

我尝试的是替换

longComments
规范:

syntaxes/foo.tmLanguage.json(摘录并替换)

       "longComments": {
            "name": "blockComment.foo",
            "begin": "\/*",
            "end": "*\/",
            "patterns": [
                {
                    "name": "character.foo",
                    "match": "((?:.|\n)*?)*"
                }
            ]
        }

但后来我看到了这个:

这是我的测试文本。您可以复制粘贴来重现该行为。


// an Inline comment 
// an Inline comment that is even longer
PascalCaseWord AnotherPascalCaseWord
camelCaseWord, anotherCamelCaseWord
/* This is another multiline comment with some PascalCaseWord and some camelCaseWord */
/* This is another multiline comment with some PascalCaseWord and some camelCaseWord
that goes over two lines . */
/* This is another multiline comment with 
some PascalCaseWord 
and some camelCaseWord
that goes over three lines . */

我怎样才能实现我的目标(见第一张截图)?

visual-studio-code vscode-extensions syntax-highlighting textmate tmlanguage
1个回答
0
投票

我可以修好它。问题是我的正则表达式在一种情况下不部分正确,并且还干扰了 JSON 中字符串的转义方式。捕获TextMate语法中的注释(就我而言)的正确方法是:

"longComments": {
    "patterns": [{
        "name": "blockComment.foo",
        "begin": "/\\*",
        "end": "\\*/"
    }]
 },
"shortComments": {
     "patterns": [{
        "name": "lineComment.foo",
        "match": "(//).*\\n?"
     }]
 }
© www.soinside.com 2019 - 2024. All rights reserved.