Fix keyword highlighting to use word boundaries via context matching

This commit is contained in:
Michael Netshipise 2026-01-19 22:26:18 +02:00
parent 6c4e8d3d5e
commit eeb333acbd
3 changed files with 186 additions and 54 deletions

View File

@ -29,7 +29,7 @@ module.exports = grammar({
// Rust imports
use_statement: ($) =>
seq("@use", $.rust_path, optional(seq("as", $.identifier))),
seq(seq("@", "use"), $.rust_path, optional(seq("as", $.identifier))),
// Use token() to properly handle :: in paths
rust_path: ($) =>
@ -42,14 +42,14 @@ module.exports = grammar({
// Template imports - supports both quoted "path" and unquoted /path
import_statement: ($) =>
seq("@import", choice($.string_literal, $.import_path), "as", $.identifier),
seq(seq("@", "import"), choice($.string_literal, $.import_path), "as", $.identifier),
import_path: ($) => /\/[^\s]+/,
// Struct definition
struct_definition: ($) =>
seq(
"@struct",
seq("@", "struct"),
optional($.attribute_list),
$.identifier,
optional($.generic_params),
@ -70,7 +70,7 @@ module.exports = grammar({
// Enum definition
enum_definition: ($) =>
seq(
"@enum",
seq("@", "enum"),
optional($.attribute_list),
$.identifier,
optional($.generic_params),
@ -102,7 +102,7 @@ module.exports = grammar({
// Function definition
function_definition: ($) =>
seq("@fn", $.identifier, $.parameter_list, $.content_block),
seq(seq("@", "fn"), $.identifier, $.parameter_list, $.content_block),
parameter_list: ($) =>
seq(
@ -173,7 +173,7 @@ module.exports = grammar({
attribute_if_statement: ($) =>
seq(
"@if",
seq("@", "if"),
$.expression,
"{",
repeat($.attribute_or_control),
@ -183,7 +183,7 @@ module.exports = grammar({
attribute_for_loop: ($) =>
seq(
"@for",
seq("@", "for"),
$.simple_pattern,
"in",
$.expression,
@ -251,11 +251,11 @@ module.exports = grammar({
// Let binding: @let name = expression
let_statement: ($) =>
seq("@let", $.pattern, "=", $.expression),
seq(seq("@", "let"), $.pattern, "=", $.expression),
if_statement: ($) =>
seq(
"@if",
seq("@", "if"),
optional(seq("let", $.pattern, "=")),
$.expression,
$.content_block,
@ -276,7 +276,7 @@ module.exports = grammar({
for_loop: ($) =>
seq(
"@for",
seq("@", "for"),
optional(seq($.identifier, ":")),
$.simple_pattern,
"in",
@ -285,7 +285,7 @@ module.exports = grammar({
),
match_statement: ($) =>
seq("@match", $.expression, "{", repeat($.match_arm), "}"),
seq(seq("@", "match"), $.expression, "{", repeat($.match_arm), "}"),
match_arm: ($) =>
seq(
@ -297,10 +297,10 @@ module.exports = grammar({
),
break_statement: ($) =>
seq("@break", optional(seq(":", $.identifier)), optional(";")),
seq(seq("@", "break"), optional(seq(":", $.identifier)), optional(";")),
continue_statement: ($) =>
seq("@continue", optional(seq(":", $.identifier)), optional(";")),
seq(seq("@", "continue"), optional(seq(":", $.identifier)), optional(";")),
// Function tags
function_tag: ($) =>

View File

@ -7,21 +7,36 @@
; Raw blocks
(raw_block) @string.special
; Keywords
"@use" @keyword
"@import" @keyword
"@struct" @keyword
"@enum" @keyword
"@fn" @keyword
"@let" @keyword
"@if" @keyword
"@for" @keyword
"@match" @keyword
"@break" @keyword
"@continue" @keyword
"else" @keyword
"if" @keyword
"let" @keyword
; Keywords - only in proper syntactic contexts
(use_statement "@" @keyword)
(use_statement "use" @keyword)
(import_statement "@" @keyword)
(import_statement "import" @keyword)
(struct_definition "@" @keyword)
(struct_definition "struct" @keyword)
(enum_definition "@" @keyword)
(enum_definition "enum" @keyword)
(function_definition "@" @keyword)
(function_definition "fn" @keyword)
(let_statement "@" @keyword)
(let_statement "let" @keyword)
(if_statement "@" @keyword)
(if_statement "if" @keyword)
(for_loop "@" @keyword)
(for_loop "for" @keyword)
(match_statement "@" @keyword)
(match_statement "match" @keyword)
(break_statement "@" @keyword)
(break_statement "break" @keyword)
(continue_statement "@" @keyword)
(continue_statement "continue" @keyword)
(attribute_if_statement "@" @keyword)
(attribute_if_statement "if" @keyword)
(attribute_for_loop "@" @keyword)
(attribute_for_loop "for" @keyword)
(else_if_branch "else" @keyword)
(else_if_branch "if" @keyword)
(else_branch "else" @keyword)
"in" @keyword
"as" @keyword
"mut" @keyword

View File

@ -42,8 +42,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@use"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "use"
}
]
},
{
"type": "SYMBOL",
@ -104,8 +113,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@import"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "import"
}
]
},
{
"type": "CHOICE",
@ -138,8 +156,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@struct"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "struct"
}
]
},
{
"type": "CHOICE",
@ -231,8 +258,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@enum"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "enum"
}
]
},
{
"type": "CHOICE",
@ -470,8 +506,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@fn"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "fn"
}
]
},
{
"type": "SYMBOL",
@ -778,8 +823,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@if"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "if"
}
]
},
{
"type": "SYMBOL",
@ -838,8 +892,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@for"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "for"
}
]
},
{
"type": "SYMBOL",
@ -1156,8 +1219,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@let"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "let"
}
]
},
{
"type": "SYMBOL",
@ -1177,8 +1249,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@if"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "if"
}
]
},
{
"type": "CHOICE",
@ -1297,8 +1378,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@for"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "for"
}
]
},
{
"type": "CHOICE",
@ -1343,8 +1433,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@match"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "match"
}
]
},
{
"type": "SYMBOL",
@ -1421,8 +1520,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@break"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "break"
}
]
},
{
"type": "CHOICE",
@ -1463,8 +1571,17 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@continue"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "STRING",
"value": "continue"
}
]
},
{
"type": "CHOICE",