如何在VS Code中使函数定义的颜色与函数调用的颜色不同?

huangapple go评论58阅读模式
英文:

How can I make the colour for a function definition to differ from that of a function call in VS Code?

问题

如何使函数定义/声明的颜色与函数调用的颜色不同?

例如,在JavaScript中:

function foo() { foo(); }
// 将第一个 "foo" 变为红色,下一个变为绿色。
英文:

How can I make the colour for a function definition/declaration to differ from that of a function call?

For example, in JavaScript

function foo() { foo(); }
// make the first "foo" red and the next one green.

答案1

得分: 1

使用命令面板中的Developer: Inspect Editor Tokens and Scopes命令。

有了语义高亮,查看定义中是否有特定修饰符或调用。使用VS Code内置的JavaScript扩展,定义具有declaration修饰符,因此可以像这样操作:

"editor.semanticTokenColorCustomizations": {
    "[颜色主题名称]": { // 可选将其包装在颜色主题块中,以仅应用于一个颜色主题
        "rules": {
            "function.declaration:javascript": {
                "foreground": "#FF0000", // TODO
            },
            "function:javascript": {
                "foreground": "#00FF00", // TODO
            },
        },
    },
},

您可以在此处找到有关语义高亮的文档:https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide。

对于令牌自定义,只需查看可用的令牌自定义。有时,有些东西可能不允许您以不同的方式自定义它们。例如(我认为)如果定义和调用都具有共同的“top-priority”作用域。我尝试为JavaScript制定一些工作,但我认为遇到了这个问题。这是我的尝试,只是为了给出可能的外观:

"editor.tokenColorCustomizations": {
    // 可选择将此项包装在颜色主题块中,以仅适用于一个主题
    "textMateRules": [
        {
            "scope": "meta.definition.function.js",
            "settings": {
                "foreground": "#FF0000", // TODO
            },
        },
        {
            "scope": "meta.function-call.js",
            "settings": {
                "foreground": "#00FF00", // TODO
            },
        },
    ],
},
英文:

Use the Developer: Inspect Editor Tokens and Scopes command in the command palette.

With semantic highlighting, look to see if either definitions have a specific modifier, or calls. With VS Code's builtin JavaScript extension, definitions have the declaration modifier, so one can do this like so:

"editor.semanticTokenColorCustomizations": {
    "[name of colour theme]": { // optionally wrap in colour theme block to apply customizations to only one colour theme
        "rules": {
            "function.declaration:javascript": {
                "foreground": "#FF0000", // TODO
            },
            "function:javascript": {
                "foreground": "#00FF00", // TODO
            },
        },
    },
},

You can find docs on semantic highlighting here: https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide.

With token customizations, just see what token customizations are available. Sometimes things just might not allow you to customize them differently. Ex. (I think) if both definitions and calls have a common "top-priority" scope. I tried to get something working for JavaScript but I think it ran into that issue. Here's my attempt for that just to give an idea of what it might look like:

"editor.tokenColorCustomizations": {
    // optionally enclose this with a colour theme block to only apply to one theme
    "textMateRules": [
        {
            "scope": "meta.definition.function.js",
            "settings": {
                "foreground": "#FF0000", // TODO
            },
        },
        {
            "scope": "meta.function-call.js",
            "settings": {
                "foreground": "#00FF00", // TODO
            },
        },
    ],
},

huangapple
  • 本文由 发表于 2023年7月23日 16:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定