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

VS Code TextMate语法高亮Regex失效:func后标识符无法特殊着色

解决函数名与普通标识符的语法高亮区分问题

你的问题出在两个核心点:

  1. 语法规则的匹配顺序错误,keywords会先单独匹配func关键字,导致后续的func_declre规则无法捕获完整的func 函数名结构
  2. func_declre的规则写法不符合TextMate语法的匹配逻辑,begin/end的设置完全错误

下面是修正后的.tmLanguage.json配置,直接实现你要的效果:

{
    "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
    "name": "samir_script",
    "patterns": [
        {
            "include": "#func_declre"
        },
        {
            "include": "#keywords"
        },
        {
            "include": "#strings"
        },
        {
            "include": "#comments"
        },
        {
            "include": "#digits"
        },
        {
            "include": "#opperators"
        },
        {
            "include": "#reserved_words"
        },
        {
            "include": "#identifiers"
        }
    ],
    "repository": {
        "keywords": {
            "patterns": [{
                "name": "keyword.control.smr",
                "match": "\\b(if|while|for|return|match|elif|else|case|var|do|lambda|then|with|break|continue|in|print|println|import|as)\\b"
            }]
        },
        "strings": {
            "name": "string.quoted.double.smr",
            "begin": "\"",
            "end": "\"",
            "patterns": [
                {
                    "name": "constant.character.escape.smr",
                    "match": "\\\\."
                }
            ]
        },
        "comments":{
            "name": "comment.line",
            "begin": "#",
            "end": "\n"
        },
        "digits":{
            "name":"constant.numeric",
            "match": "\\b(\\d+)\\b"
        },
        "opperators":{
            "name": "keyword.operator",
            "match": "(==|!=|\\+|\\-|\\*|\\/|=)"
        },
        "identifiers":{
            "name": "support.variable",
            "match": "\\b(\\w+)\\b"
        },
        "reserved_words":{
                "name": "constant.language",
                "match": "\\b(true|false|nil)\\b"
        },
        "func_declre":{
            "match": "\\b(func)(\\s+)(\\w+)\\b",
            "captures": {
                "1": { "name": "keyword.control.smr" },
                "3": { "name": "entity.name.function" }
            }
        }
    },
    "scopeName": "source.smr"
}

关键修改说明:

  • 调整匹配顺序:把#func_declre移到#keywords前面,优先匹配完整的func 函数名结构,避免func被单独识别为普通关键字
  • 重构func_declre规则:用match直接捕获整个结构,通过captures给func设置你指定的keyword.control.smr(紫色),给后面的标识符设置entity.name.function(黄色)
  • 修正运算符规则:修复原正则的语法错误,支持==这类双字符运算符,同时覆盖基础单字符运算符

现在测试func abc时:

  • func会被标记为keyword.control.smr(紫色)
  • abc会被标记为entity.name.function(黄色)
  • 普通标识符abc依然会被标记为support.variable(蓝色)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:43:13