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

VS Code语法高亮:如何匹配fn/class后括号内的敏感关键字?

VS Code语法高亮配置解决方案

原配置的问题在于,TextMate语法规范中,captures字段仅支持直接为捕获组指定作用域,无法在捕获组内嵌套patterns进行二次匹配。要实现fn/class后方括号内目标关键词的高亮,需改用begin/end结构来定义作用域块,在块内匹配目标标识符。

正确配置示例:

"decorations": {
    "name": "decorations.lang",
    "begin": "(fn|class)\\s*\\[",
    "beginCaptures": {
        "1": {
            "name": "keyword.control.lang"
        }
    },
    "end": "\\]",
    "contentName": "decorations.captures.lang",
    "patterns": [
        {
            "name": "entity.name.function.decorator.lang",
            "match": "\\b(public|private|abstract)\\b"
        }
    ]
}

配置说明:

  • begin:匹配fn或class关键字及后续的左方括号,标记作用域的起始位置
  • beginCaptures:可选,给fn/class本身指定语法作用域(可根据需求调整)
  • end:匹配右方括号,标记作用域的结束位置
  • contentName:给方括号内的所有内容指定一个基础作用域
  • patterns:在方括号的作用域内,精准匹配public/private/abstract关键词并指定高亮作用域

配置完成后,使用VS Code的Developer: Inspect Editor Tokens and Scopes工具检查,即可看到目标关键词被正确标记为entity.name.function.decorator.lang作用域,实现预期高亮效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 18:50:23