## Small regex-based syntax highlighter for fenced code blocks in the chat ## dock. Not a real lexer — just colors comments/strings/numbers/keywords ## well enough to be readable. Unrecognized languages fall back to a plain ## (still escaped) code block. class_name ClaudeGodotSyntaxHighlight extends RefCounted const COLOR_COMMENT := "#6a9955" const COLOR_STRING := "#ce9178" const COLOR_NUMBER := "#b5cea8" const COLOR_KEYWORD := "#569cd6" const GDSCRIPT_KEYWORDS := ["func", "var", "const", "if", "elif", "else", "for", "while", "return", "class", "class_name", "extends", "signal", "await", "match", "in", "not", "and", "or", "true", "false", "null", "self", "static", "pass", "break", "continue", "enum", "preload", "load", "void", "new"] const PYTHON_KEYWORDS := ["def", "class", "if", "elif", "else", "for", "while", "return", "import", "from", "as", "in", "not", "and", "or", "True", "False", "None", "self", "pass", "break", "continue", "try", "except", "finally", "with", "lambda", "yield"] const JS_KEYWORDS := ["function", "const", "let", "var", "if", "else", "for", "while", "return", "class", "extends", "import", "export", "from", "as", "new", "this", "true", "false", "null", "undefined", "async", "await", "try", "catch", "finally", "typeof"] const C_KEYWORDS := ["int", "float", "double", "char", "void", "if", "else", "for", "while", "return", "struct", "typedef", "const", "static", "break", "continue", "switch", "case", "default"] const CSHARP_KEYWORDS := ["public", "private", "protected", "class", "void", "int", "float", "double", "string", "bool", "if", "else", "for", "foreach", "while", "return", "new", "static", "using", "namespace", "true", "false", "null"] const BASH_KEYWORDS := ["if", "then", "else", "fi", "for", "do", "done", "while", "function", "return", "echo", "local", "export"] const HASH_COMMENT_LANGS := ["gd", "gdscript", "py", "python", "sh", "bash", "shell", ""] static func _keywords_for(lang: String) -> Array: match lang.to_lower(): "gd", "gdscript": return GDSCRIPT_KEYWORDS "py", "python": return PYTHON_KEYWORDS "js", "jsx", "javascript", "ts", "tsx", "typescript": return JS_KEYWORDS "c", "h", "cpp", "cc", "cxx", "hpp": return C_KEYWORDS "cs", "csharp": return CSHARP_KEYWORDS "sh", "bash", "shell": return BASH_KEYWORDS _: return [] ## code: raw (unescaped) source text. lang: the fence's info string, may be "". ## Returns ready-to-render BBCode (brackets already escaped, dark background). static func highlight(code: String, lang: String) -> String: var text := code.replace("[", "[lb]") text = _sub(text, "\"([^\"\\n]*)\"", "[color=%s]\"$1\"[/color]" % COLOR_STRING) text = _sub(text, "'([^'\\n]*)'", "[color=%s]'$1'[/color]" % COLOR_STRING) if lang.to_lower() in HASH_COMMENT_LANGS: text = _sub(text, "(?m)#.*$", "[color=%s]$0[/color]" % COLOR_COMMENT) else: text = _sub(text, "(?m)//.*$", "[color=%s]$0[/color]" % COLOR_COMMENT) text = _sub(text, "\\b(\\d+\\.?\\d*)\\b", "[color=%s]$1[/color]" % COLOR_NUMBER) for kw in _keywords_for(lang): text = _sub(text, "\\b%s\\b" % kw, "[color=%s]%s[/color]" % [COLOR_KEYWORD, kw]) return "[bgcolor=#1e1e1e][color=#d4d4d4]" + text + "[/color][/bgcolor]" static func _sub(text: String, pattern: String, replacement: String) -> String: var re := RegEx.new() if re.compile(pattern) != OK: return text return re.sub(text, replacement, true)