fix(grammar): make brace and keyword separately queryable

This commit is contained in:
2026-05-17 06:00:21 +02:00
parent 218d2869a6
commit 6f4a0df871
7 changed files with 716 additions and 522 deletions
+12 -6
View File
@@ -26,20 +26,27 @@ export default grammar({
content: _ => token(prec(-1, /([^{]|\{[^A-Za-z/])+/)), content: _ => token(prec(-1, /([^{]|\{[^A-Za-z/])+/)),
block_open: $ => seq( block_open: $ => seq(
$._block_open_start, "{",
$.block_keyword,
":",
$.block_name, $.block_name,
optional($.attributes), optional($.attributes),
"}", "}",
), ),
block_close: $ => seq( block_close: $ => seq(
$._block_close_start, "{",
"/",
$.block_keyword,
":",
$.block_name, $.block_name,
"}", "}",
), ),
lang_tag: $ => seq( lang_tag: $ => seq(
$._lang_start, "{",
$.lang_keyword,
":",
$.lang_text, $.lang_text,
"}", "}",
), ),
@@ -65,9 +72,8 @@ export default grammar({
prefix_argument: _ => /[A-Za-z][A-Za-z0-9 _-]*/, prefix_argument: _ => /[A-Za-z][A-Za-z0-9 _-]*/,
lang_text: _ => /[^}]+/, lang_text: _ => /[^}]+/,
_block_open_start: _ => /\{[Bb][Ll][Oo][Cc][Kk]:/, block_keyword: _ => token(prec(1, /[Bb][Ll][Oo][Cc][Kk]/)),
_block_close_start: _ => /\{\/[Bb][Ll][Oo][Cc][Kk]:/, lang_keyword: _ => token(prec(1, /[Ll][Aa][Nn][Gg]/)),
_lang_start: _ => /\{[Ll][Aa][Nn][Gg]:/,
_space: _ => /[ \t]+/, _space: _ => /[ \t]+/,
}, },
}); });
+5 -6
View File
@@ -1,16 +1,15 @@
; Punctuation ; Punctuation
"{" @punctuation.bracket "{" @punctuation.bracket
"}" @punctuation.bracket "}" @punctuation.bracket
; Keywords that introduce a tag form. The grammar exposes the opening
; sequence as a hidden node, so capture the parent and let the editor
; colour the leading {block:, {/block:, {lang: literally via the
; tokenizer. The colon delimiter inside a variable_prefix tag is captured
; below.
":" @punctuation.delimiter ":" @punctuation.delimiter
"-" @punctuation.delimiter "-" @punctuation.delimiter
"/" @punctuation.delimiter
"=" @operator "=" @operator
; Block and lang keywords inside their tags: the literal "block" / "lang".
(block_keyword) @keyword.directive
(lang_keyword) @keyword.directive
; Block names. Known data-block names get @function.builtin; If/IfNot ; Block names. Known data-block names get @function.builtin; If/IfNot
; toggles get @keyword.conditional (theme authors may define arbitrary ; toggles get @keyword.conditional (theme authors may define arbitrary
; If* / IfNot* names via <meta name="if:..."> so we match by prefix). ; If* / IfNot* names via <meta name="if:..."> so we match by prefix).
+49 -11
View File
@@ -48,9 +48,17 @@
"block_open": { "block_open": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "STRING",
"value": "{"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_block_open_start" "name": "block_keyword"
},
{
"type": "STRING",
"value": ":"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@@ -77,9 +85,21 @@
"block_close": { "block_close": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "STRING",
"value": "/"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_block_close_start" "name": "block_keyword"
},
{
"type": "STRING",
"value": ":"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@@ -94,9 +114,17 @@
"lang_tag": { "lang_tag": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "STRING",
"value": "{"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_lang_start" "name": "lang_keyword"
},
{
"type": "STRING",
"value": ":"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@@ -250,17 +278,27 @@
"type": "PATTERN", "type": "PATTERN",
"value": "[^}]+" "value": "[^}]+"
}, },
"_block_open_start": { "block_keyword": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN", "type": "PATTERN",
"value": "\\{[Bb][Ll][Oo][Cc][Kk]:" "value": "[Bb][Ll][Oo][Cc][Kk]"
}
}
}, },
"_block_close_start": { "lang_keyword": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN", "type": "PATTERN",
"value": "\\{\\/[Bb][Ll][Oo][Cc][Kk]:" "value": "[Ll][Aa][Nn][Gg]"
}, }
"_lang_start": { }
"type": "PATTERN",
"value": "\\{[Ll][Aa][Nn][Gg]:"
}, },
"_space": { "_space": {
"type": "PATTERN", "type": "PATTERN",
+26 -2
View File
@@ -38,9 +38,13 @@
"named": true, "named": true,
"fields": {}, "fields": {},
"children": { "children": {
"multiple": false, "multiple": true,
"required": true, "required": true,
"types": [ "types": [
{
"type": "block_keyword",
"named": true
},
{ {
"type": "block_name", "type": "block_name",
"named": true "named": true
@@ -60,6 +64,10 @@
"type": "attributes", "type": "attributes",
"named": true "named": true
}, },
{
"type": "block_keyword",
"named": true
},
{ {
"type": "block_name", "type": "block_name",
"named": true "named": true
@@ -72,9 +80,13 @@
"named": true, "named": true,
"fields": {}, "fields": {},
"children": { "children": {
"multiple": false, "multiple": true,
"required": true, "required": true,
"types": [ "types": [
{
"type": "lang_keyword",
"named": true
},
{ {
"type": "lang_text", "type": "lang_text",
"named": true "named": true
@@ -150,6 +162,10 @@
"type": "-", "type": "-",
"named": false "named": false
}, },
{
"type": "/",
"named": false
},
{ {
"type": ":", "type": ":",
"named": false "named": false
@@ -166,6 +182,10 @@
"type": "attribute_value", "type": "attribute_value",
"named": true "named": true
}, },
{
"type": "block_keyword",
"named": true
},
{ {
"type": "block_name", "type": "block_name",
"named": true "named": true
@@ -186,6 +206,10 @@
"type": "image", "type": "image",
"named": false "named": false
}, },
{
"type": "lang_keyword",
"named": true
},
{ {
"type": "lang_text", "type": "lang_text",
"named": true "named": true
Generated
+602 -495
View File
File diff suppressed because it is too large Load Diff
+18
View File
@@ -9,8 +9,10 @@ Simple block
(template (template
(content) (content)
(block_open (block_open
(block_keyword)
(block_name)) (block_name))
(block_close (block_close
(block_keyword)
(block_name)) (block_name))
(content)) (content))
@@ -25,12 +27,14 @@ Block with content
(template (template
(content) (content)
(block_open (block_open
(block_keyword)
(block_name)) (block_name))
(content) (content)
(variable (variable
(variable_name)) (variable_name))
(content) (content)
(block_close (block_close
(block_keyword)
(block_name)) (block_name))
(content)) (content))
@@ -45,14 +49,18 @@ Nested blocks
(template (template
(content) (content)
(block_open (block_open
(block_keyword)
(block_name)) (block_name))
(block_open (block_open
(block_keyword)
(block_name)) (block_name))
(variable (variable
(variable_name)) (variable_name))
(block_close (block_close
(block_keyword)
(block_name)) (block_name))
(block_close (block_close
(block_keyword)
(block_name)) (block_name))
(content)) (content))
@@ -67,6 +75,7 @@ Block with one attribute
(template (template
(content) (content)
(block_open (block_open
(block_keyword)
(block_name) (block_name)
(attributes (attributes
(attribute (attribute
@@ -75,6 +84,7 @@ Block with one attribute
(variable (variable
(variable_name)) (variable_name))
(block_close (block_close
(block_keyword)
(block_name)) (block_name))
(content)) (content))
@@ -89,6 +99,7 @@ Block with multiple attributes
(template (template
(content) (content)
(block_open (block_open
(block_keyword)
(block_name) (block_name)
(attributes (attributes
(attribute (attribute
@@ -98,6 +109,7 @@ Block with multiple attributes
(attribute_name) (attribute_name)
(attribute_value)))) (attribute_value))))
(block_close (block_close
(block_keyword)
(block_name)) (block_name))
(content)) (content))
@@ -112,8 +124,10 @@ Case insensitive block keyword
(template (template
(content) (content)
(block_open (block_open
(block_keyword)
(block_name)) (block_name))
(block_close (block_close
(block_keyword)
(block_name)) (block_name))
(content)) (content))
@@ -128,12 +142,16 @@ Conditional block
(template (template
(content) (content)
(block_open (block_open
(block_keyword)
(block_name)) (block_name))
(block_open (block_open
(block_keyword)
(block_name)) (block_name))
(content) (content)
(block_close (block_close
(block_keyword)
(block_name)) (block_name))
(block_close (block_close
(block_keyword)
(block_name)) (block_name))
(content)) (content))
+2
View File
@@ -9,6 +9,7 @@ Simple lang tag
(template (template
(content) (content)
(lang_tag (lang_tag
(lang_keyword)
(lang_text)) (lang_text))
(content)) (content))
@@ -23,5 +24,6 @@ Lang tag with spaces and punctuation
(template (template
(content) (content)
(lang_tag (lang_tag
(lang_keyword)
(lang_text)) (lang_text))
(content)) (content))