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

How to highlight syntax of block and inline comments in VSCode extensions?

提问人:bookofproofs 提问时间:9/1/2023 最后编辑:bookofproofs 更新时间:9/10/2023 访问量:84

问:

这是我要实现的目标enter image description here

这是我得到的:enter image description here

我的小语法突出显示 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"
      }
    ]
  }

language-configuration.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"
}

主题/dark-color-theme.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

语法/foo.tmLanguage.json(摘录与替换)

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

但后来我看到这个:

enter image description here

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


// 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 语法高亮显示 textmate tmlanguage

评论


答:

0赞 bookofproofs 9/10/2023 #1

我可以修复它。问题是我的正则表达式在一种情况下没有部分正确,这也干扰了 JSON 中字符串的转义方式。在 TextMate 语法中捕获注释的正确方法(在我的情况下)是:

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