feat: The queries for HTML and CSS are now baked in to the thor binary.

This commit is contained in:
Spencer Brower
2026-07-24 16:50:33 -04:00
parent 1992349520
commit 4f0b5c929d
6 changed files with 141 additions and 87 deletions
+76
View File
@@ -0,0 +1,76 @@
(comment) @comment
(tag_name) @tag
(nesting_selector) @tag
(universal_selector) @tag
"~" @operator
">" @operator
"+" @operator
"-" @operator
"*" @operator
"/" @operator
"=" @operator
"^=" @operator
"|=" @operator
"~=" @operator
"$=" @operator
"*=" @operator
"and" @operator
"or" @operator
"not" @operator
"only" @operator
(attribute_selector (plain_value) @string)
((property_name) @variable
(#match? @variable "^--"))
((plain_value) @variable
(#match? @variable "^--"))
(class_name) @property
(id_name) @property
(namespace_name) @property
(property_name) @property
(feature_name) @property
(pseudo_element_selector (tag_name) @attribute)
(pseudo_class_selector (class_name) @attribute)
(attribute_name) @attribute
(function_name) @function
"@media" @keyword
"@import" @keyword
"@charset" @keyword
"@namespace" @keyword
"@supports" @keyword
"@keyframes" @keyword
(at_keyword) @keyword
(to) @keyword
(from) @keyword
(important) @keyword
(string_value) @string
(color_value) @string.special
(integer_value) @number
(float_value) @number
(unit) @type
[
"#"
","
"."
":"
"::"
";"
] @punctuation.delimiter
[
"{"
")"
"("
"}"
] @punctuation.bracket
+13
View File
@@ -0,0 +1,13 @@
(tag_name) @tag
(erroneous_end_tag_name) @tag.error
(doctype) @constant
(attribute_name) @attribute
(attribute_value) @string
(comment) @comment
[
"<"
">"
"</"
"/>"
] @punctuation.bracket
+7
View File
@@ -0,0 +1,7 @@
((script_element
(raw_text) @injection.content)
(#set! injection.language "javascript"))
((style_element
(raw_text) @injection.content)
(#set! injection.language "css"))
+31 -13
View File
@@ -9,6 +9,9 @@ import "core:strings"
grammar_dir: string
query_dir: string
HTML_HIGHLIGHTS :: #load(#directory + "queries/html/highlights.scm", string)
CSS_HIGHLIGHTS :: #load(#directory + "queries/css/highlights.scm", string)
Language :: distinct rawptr
Parser :: distinct rawptr
Tree :: distinct rawptr
@@ -137,6 +140,30 @@ builtin_language :: proc(lang: string) -> (language: Language, ok: bool) {
return
}
// load_query returns the highlight query source for a language. Builtin
// languages (html/css) are baked into the binary via `#load`; all others are
// read from the runtime `query_dir`. `path` is the on-disk location for
// diagnostics ("(builtin)" for embedded queries). Mirrors `ensure_parser`.
load_query :: proc(lang: string) -> (src: string, path: string, ok: bool) {
switch lang {
case "html":
return HTML_HIGHLIGHTS, "(builtin)", true
case "css":
return CSS_HIGHLIGHTS, "(builtin)", true
}
if query_dir == "" {
log.warnf("treesitter: no query path set, skipping %s", lang)
return "", "", false
}
path = fmt.tprintf("%s/%s/highlights.scm", query_dir, lang)
raw, err := os.read_entire_file_from_path(path, context.allocator)
if err != nil {
log.warnf("treesitter: cannot load query %s", path)
return "", "", false
}
return string(raw), path, true
}
ensure_parser :: proc(lang: string) -> ^Grammar_Cache {
if grammar_cache == nil {
grammar_cache = make(map[string]^Grammar_Cache)
@@ -208,28 +235,19 @@ load_grammar :: proc(lang: string) -> ^Grammar_Cache {
return nil
}
if query_dir == "" {
log.warnf("treesitter: no query path set, skipping %s", lang)
query_src, query_path, ok := load_query(lang)
if !ok {
gc.query_failed = true
return nil
}
query_path := fmt.tprintf("%s/%s/highlights.scm", query_dir, lang)
query_src, err := os.read_entire_file_from_path(query_path, context.allocator)
if err != nil {
log.warnf("treesitter: cannot load query %s", query_path)
gc.query_failed = true
return nil
}
query_str := string(query_src)
query_c := strings.clone_to_cstring(query_str)
query_c := strings.clone_to_cstring(query_src)
defer delete(query_c)
err_offset: u32
err_type: Query_Error
query := query_new(gc.language, query_c, u32(len(query_src)), &err_offset, &err_type)
if query == nil {
tok := extract_query_token(query_src, err_offset)
tok := extract_query_token(transmute([]byte)query_src, err_offset)
cause := fmt.tprintf("query error at byte %d (type %v)", err_offset, err_type)
#partial switch err_type {
case .NodeType: