gpt4 book ai didi

How to highlight syntax of block and inline comments in VSCode extensions?(如何在VSCode扩展中突出显示块和内联注释的语法?)

转载 作者:bug小助手 更新时间:2023-10-25 19:32:49 26 4
gpt4 key购买 nike



This is my goal to achieve:
enter image description here

这是我要实现的目标:


This is what I get:
enter image description here

这就是我得到的:


My little syntax highlighting vscode extension looks like this:

我突出显示vscode扩展的小语法如下所示:


package.json (significant excerpt)

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

Language-configuration.json


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

syntaxes/foo.tmLanguage.json

语法/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"
}

themes/dark-color-theme.json

主题/深色主题.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"
}
}
]
}

What I tried is to replace the longComments specification in:

我尝试的是将LongComments规范替换为:


syntaxes/foo.tmLanguage.json (excerpt with replacement)

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


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

but then I see this:

但后来我看到了这个:


enter image description here


This is my test text. You can copy-paste to reproduce the behavior.

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



// 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 . */


How can I achieve my goal (see first screenshot)?

我如何实现我的目标(见第一个屏幕截图)?


更多回答
优秀答案推荐

I could fix it. The problem was that my regex was not partially correct in one case also interfered with the way strings are escaped in JSON. The correct way to capture comments in a TextMate grammar (in my case) was:

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


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

更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com