godotengine / godot-vscode-plugin

Godot development tools for VSCode
MIT License
1.47k stars 148 forks source link

Support for '##' comments grammar scope (for themes) #659

Open GustJc opened 1 month ago

GustJc commented 1 month ago

Godot version

4.2.2.stable

VS Code version

1.87.0

Godot Tools VS Code extension version

v2.0.0

System information

Windows 10

Problem statement

Could it be possible to add a different scope for the double sharp comments? I'm trying to replicate 1-to-1 the theme from Godot into VSCode. (based on godot-vscode-theme)

But it seems the scope for # commentsand ## comments are the same. In Godot, the double symbol comments have a slight different color. I'd like to achieve that with a theme.

Here's the comment grammar location https://github.com/godotengine/godot-vscode-plugin/blob/df445b8390b5575c6e2b63ca7d6bdc1763154604/syntaxes/GDScript.tmLanguage.json#L85-L89

Proposed solution

I've never worked with this, but possibly adding a new grammar entry like: Edit: This works, it can differentiate single and double comments

Adding base_pattern

{ "include": "#comment_double" },

And

"comment": {
    "match": "(#(?!#)).*$\\n?",
    "name": "comment.line.number-sign.gdscript",
    "captures": { "1": { "name": "punctuation.definition.comment.number-sign.gdscript" } }
},
"comment_double": {
    "match": "(##).*$\\n?",
    "name": "comment.line.number-sign-double.gdscript",
    "captures": { "1": { "name": "punctuation.definition.comment.number-sign.gdscript" } }
},

Edit2: I've been able to colorize the double comments, and add TODO/FIXME colored keywords inside comments with grammar injections with the theme plugin.

Here are some of the keypoints. I replaced the 'comment' from using match regex to begin/end

    "repository": {
        "comment": {
            "begin": "#(?!#)",
            "end": "$",
            "name": "comment.line.number-sign.gdscript",
            "beginCaptures": { "1": { "name": "punctuation.definition.comment.number-sign.gdscript" } }
        },
        "comment_double": {
            "name": "comment.line.double-number-sign.gdscript",
            "begin": "##",
            "end": "$"
        }
    }

Changed the "comment" to use the "Begin/End" regex instead of "Match" so it could inject TODO/FIXME scopes inside comments. It wouldn't work when comments were using match.

{
    "injectionSelector": "L:comment.line",
    "scopeName": "comment-keywords.injection",
    "patterns": [
      {"include": "#todo_keywords" }
    ],
    "repository": {
        "todo_keywords": {
            "name": "comment.keywords.todo.gdscript",
            "match": "TODO|FIXME"
        }
    }
  }

The other missing color I found is the global vs local function color. But that is a way more complicated problem that I'm not sure how to solve. I think it needs a custom semantic token for that.