You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code中仅高亮Python函数定义而非函数调用

解决办法

问题核心是entity.name.function这个作用域范围太宽泛,不仅匹配函数定义,还会覆盖到函数调用里的相关符号。要实现只高亮函数定义,得用更精准的作用域。

1. 用VS Code工具查精准作用域

打开VS Code,按Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入Inspect Editor Tokens and Scopes并回车。把光标放在planetorbit上,查看弹出面板里的作用域链,找到最精准的那个(一般是最底层、和函数定义直接绑定的作用域,比如meta.function-definition entity.name.function或者带语言后缀的entity.name.function.lua)。

2. 修改配置文件

  • 先关闭语义高亮(避免它干扰自定义规则):把"editor.semanticHighlighting.enabled"改成false。
  • 替换原来的entity.name.function规则,用上刚才查到的精准作用域。

修改后的示例配置(假设查到的精准作用域是meta.function-definition entity.name.function):

"editor.semanticHighlighting.enabled": false,
"editor.tokenColorCustomizations": {
    "keywords": "#FF8000",
    "comments": "#DD0000",
    "strings": "#02ff02dc",
    "variables": "#FFFFFF",
    "functions": "#FFFFFF",
    "numbers": "#FFFFFF",
    "textMateRules": [
        {"scope": "variable.parameter",
        "settings": {"foreground": "#FFFFFF"}},
        {"scope": "meta",
        "settings": {"foreground": "#FFFFFF"}},
        {"scope": "support.function",
        "settings": {"foreground": "#5e5eff"}},
        {"scope": "keyword.operator",
        "settings": {"foreground": "#FFFFFF"}},
        {"scope": "punctuation.separator",
        "settings": {"foreground": "#FFFFFF"}},
        {"scope": "meta.function-definition entity.name.function",
        "settings": {"foreground": "#5E5EFF"}},
        {"scope": "entity.name.function-call",
        "settings": {"foreground": "#FFFFFF"}},
    ]
},
"workbench.colorCustomizations": {
    "editor.selectionBackground": "#7E7E7E",
    "editor.background": "#002240",
    "editorBracketHighlight.foreground1": "#FFFFFF",
    "editorBracketHighlight.foreground2": "#FFFFFF",
    "editorBracketHighlight.foreground3": "#FFFFFF",
},
"workbench.colorTheme": "Abyss",

3. 验证效果

保存配置后重启VS Code,查看代码高亮是否符合预期。如果还是有问题,再用Inspect工具检查其他元素的作用域,调整规则优先级。


内容的提问来源于stack exchange,提问作者You Theone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 16:32:52