@@ -51,46 +51,35 @@ fn syntax(a: std.mem.Allocator, language: *const ts.Language, sources: []const [
5151
5252 var grammars: ?Grammars = null;
5353
54+const Lang = enum { javascript, typescript, tsx, markdown, markdown_inline, python, go, zig, bash, diff };
55+
56+fn load(a: std.mem.Allocator, lang: Lang) Syntax {
57+ return switch (lang) {
58+ .javascript => syntax(a, tree_sitter_javascript(), &.{JS_QUERIES}) orelse unreachable,
59+ .typescript => syntax(a, tree_sitter_typescript(), &.{ JS_QUERIES, TS_QUERIES }) orelse unreachable,
60+ .tsx => syntax(a, tree_sitter_tsx(), &.{ JS_QUERIES, TS_QUERIES }) orelse unreachable,
61+ .markdown => syntax(a, tree_sitter_markdown(), &.{ MD_QUERIES, MD_TABLES }) orelse unreachable,
62+ .markdown_inline => syntax(a, tree_sitter_markdown_inline(), &.{MD_INLINE_QUERIES}) orelse unreachable,
63+ .python => syntax(a, tree_sitter_python(), &.{PY_QUERIES}) orelse unreachable,
64+ .go => syntax(a, tree_sitter_go(), &.{GO_QUERIES}) orelse unreachable,
65+ .zig => syntax(a, tree_sitter_zig(), &.{ZIG_QUERIES}) orelse unreachable,
66+ .bash => syntax(a, tree_sitter_bash(), &.{BASH_QUERIES}) orelse unreachable,
67+ .diff => syntax(a, tree_sitter_diff(), &.{DIFF_QUERIES}) orelse unreachable,
68+ };
69+}
70+
5471 const Grammars = struct {
55- javascript: Syntax,
56- typescript: Syntax,
57- tsx: Syntax,
58- markdown: Syntax,
59- markdown_inline: Syntax,
60- python: Syntax,
61- go: Syntax,
62- zig: Syntax,
63- bash: Syntax,
64- diff: Syntax,
65-
66- fn init(a: std.mem.Allocator) Grammars {
67- const js = tree_sitter_javascript();
68- const ts_lang = tree_sitter_typescript();
69- const tsx_lang = tree_sitter_tsx();
70- const md = tree_sitter_markdown();
71- const md_inline = tree_sitter_markdown_inline();
72- const py = tree_sitter_python();
73- const go_lang = tree_sitter_go();
74- const zig_lang = tree_sitter_zig();
75- const bash_lang = tree_sitter_bash();
76- const diff_lang = tree_sitter_diff();
77- return .{
78- .javascript = syntax(a, js, &.{JS_QUERIES}) orelse unreachable,
79- .typescript = syntax(a, ts_lang, &.{ JS_QUERIES, TS_QUERIES }) orelse unreachable,
80- .tsx = syntax(a, tsx_lang, &.{ JS_QUERIES, TS_QUERIES }) orelse unreachable,
81- .markdown = syntax(a, md, &.{ MD_QUERIES, MD_TABLES }) orelse unreachable,
82- .markdown_inline = syntax(a, md_inline, &.{MD_INLINE_QUERIES}) orelse unreachable,
83- .python = syntax(a, py, &.{PY_QUERIES}) orelse unreachable,
84- .go = syntax(a, go_lang, &.{GO_QUERIES}) orelse unreachable,
85- .zig = syntax(a, zig_lang, &.{ZIG_QUERIES}) orelse unreachable,
86- .bash = syntax(a, bash_lang, &.{BASH_QUERIES}) orelse unreachable,
87- .diff = syntax(a, diff_lang, &.{DIFF_QUERIES}) orelse unreachable,
88- };
72+ slots: [@typeInfo(Lang).@"enum".fields.len]?Syntax = @splat(null),
73+
74+ fn get(self: *Grammars, a: std.mem.Allocator, lang: Lang) *Syntax {
75+ const slot = &self.slots[@intFromEnum(lang)];
76+ if (slot.* == null) slot.* = load(a, lang);
77+ return &slot.*.?;
8978 }
9079 };
9180
92-fn getGrammars(a: std.mem.Allocator) *Grammars {
93- if (grammars == null) grammars = Grammars.init(a);
81+fn getGrammars() *Grammars {
82+ if (grammars == null) grammars = .{};
9483 return &grammars.?;
9584 }
9685
@@ -295,17 +284,21 @@ fn emit(a: std.mem.Allocator, out: *std.ArrayList(u8), active: *std.ArrayList(us
295284 at.* = end;
296285 }
297286
298-fn syntaxFor(g: *Grammars, lang: []const u8) ?*Syntax {
299- if (std.mem.eql(u8, lang, "js") or std.mem.eql(u8, lang, "javascript") or std.mem.eql(u8, lang, "jsx")) return &g.javascript;
300- if (std.mem.eql(u8, lang, "ts") or std.mem.eql(u8, lang, "typescript")) return &g.typescript;
301- if (std.mem.eql(u8, lang, "tsx")) return &g.tsx;
302- if (std.mem.eql(u8, lang, "py") or std.mem.eql(u8, lang, "python")) return &g.python;
303- if (std.mem.eql(u8, lang, "go") or std.mem.eql(u8, lang, "golang")) return &g.go;
304- if (std.mem.eql(u8, lang, "zig")) return &g.zig;
305- if (std.mem.eql(u8, lang, "bash") or std.mem.eql(u8, lang, "sh") or std.mem.eql(u8, lang, "shell")) return &g.bash;
287+fn langFor(lang: []const u8) ?Lang {
288+ if (std.mem.eql(u8, lang, "js") or std.mem.eql(u8, lang, "javascript") or std.mem.eql(u8, lang, "jsx")) return .javascript;
289+ if (std.mem.eql(u8, lang, "ts") or std.mem.eql(u8, lang, "typescript")) return .typescript;
290+ if (std.mem.eql(u8, lang, "tsx")) return .tsx;
291+ if (std.mem.eql(u8, lang, "py") or std.mem.eql(u8, lang, "python")) return .python;
292+ if (std.mem.eql(u8, lang, "go") or std.mem.eql(u8, lang, "golang")) return .go;
293+ if (std.mem.eql(u8, lang, "zig")) return .zig;
294+ if (std.mem.eql(u8, lang, "bash") or std.mem.eql(u8, lang, "sh") or std.mem.eql(u8, lang, "shell")) return .bash;
306295 return null;
307296 }
308297
298+fn syntaxFor(g: *Grammars, a: std.mem.Allocator, lang: []const u8) ?*Syntax {
299+ return g.get(a, langFor(lang) orelse return null);
300+}
301+
309302 fn infoLang(info: []const u8) []const u8 {
310303 const trimmed = std.mem.trim(u8, info, " \t\r\n");
311304 var end: usize = 0;
@@ -314,9 +307,9 @@ fn infoLang(info: []const u8) []const u8 {
314307 }
315308
316309 pub fn spansFor(a: std.mem.Allocator, info: []const u8, text: []const u8) []const Span {
317- const g = getGrammars(a);
310+ const g = getGrammars();
318311 const lang = std.ascii.allocLowerString(a, infoLang(info)) catch infoLang(info);
319- const s = syntaxFor(g, lang) orelse return &.{};
312+ const s = syntaxFor(g, a, lang) orelse return &.{};
320313 const u = utf16(a, text) orelse return &.{};
321314 const root = parse(s.parser, u) orelse return &.{};
322315 defer root.destroy();
@@ -359,9 +352,9 @@ fn collectDiffNodes(a: std.mem.Allocator, node: ts.Node, u: Utf16, out: *std.Arr
359352 }
360353
361354 pub fn diffSpans(a: std.mem.Allocator, text: []const u8) []NodeSpan {
362- const g = getGrammars(a);
355+ const g = getGrammars();
363356 const u = utf16(a, text) orelse return &.{};
364- const root = parse(g.diff.parser, u) orelse return &.{};
357+ const root = parse(g.get(a, .diff).parser, u) orelse return &.{};
365358 defer root.destroy();
366359 var out: std.ArrayList(NodeSpan) = .empty;
367360 collectDiffNodes(a, root.rootNode(), u, &out);
@@ -373,12 +366,12 @@ pub fn highlightCode(a: std.mem.Allocator, info: []const u8, text: []const u8) [
373366 }
374367
375368 pub fn highlightMarkdown(a: std.mem.Allocator, text: []const u8) []const u8 {
376- const g = getGrammars(a);
369+ const g = getGrammars();
377370 const u = utf16(a, text) orelse return text;
378- const root = parse(g.markdown.parser, u) orelse return text;
371+ const root = parse(g.get(a, .markdown).parser, u) orelse return text;
379372 defer root.destroy();
380373 var spans: std.ArrayList(Span) = .empty;
381- spans.appendSlice(a, spansOf(a, &g.markdown, root.rootNode(), u, text, 0)) catch {};
374+ spans.appendSlice(a, spansOf(a, g.get(a, .markdown), root.rootNode(), u, text, 0)) catch {};
382375
383376 var inline_nodes: std.ArrayList(ts.Node) = .empty;
384377 collectByKind(a, root.rootNode(), &.{ "inline", "pipe_table_cell" }, &inline_nodes);
@@ -386,9 +379,9 @@ pub fn highlightMarkdown(a: std.mem.Allocator, text: []const u8) []const u8 {
386379 const start = byteOf(u, node.startByte() / 2);
387380 const inline_text = text[start..byteOf(u, node.endByte() / 2)];
388381 const iu = utf16(a, inline_text) orelse continue;
389- const inline_root = parse(g.markdown_inline.parser, iu) orelse continue;
382+ const inline_root = parse(g.get(a, .markdown_inline).parser, iu) orelse continue;
390383 defer inline_root.destroy();
391- spans.appendSlice(a, spansOf(a, &g.markdown_inline, inline_root.rootNode(), iu, inline_text, start)) catch {};
384+ spans.appendSlice(a, spansOf(a, g.get(a, .markdown_inline), inline_root.rootNode(), iu, inline_text, start)) catch {};
392385 var code_spans: std.ArrayList(ts.Node) = .empty;
393386 collectByKind(a, inline_root.rootNode(), &.{"code_span"}, &code_spans);
394387 for (code_spans.items) |code| spans = std.ArrayList(Span).fromOwnedSlice(recolor(
@@ -506,7 +499,7 @@ fn formatTable(a: std.mem.Allocator, text: []const u8, u: Utf16, table: ts.Node)
506499
507500 pub fn formatTables(a: std.mem.Allocator, text: []const u8) []const u8 {
508501 const u = utf16(a, text) orelse return text;
509- const root = parse(getGrammars(a).markdown.parser, u) orelse return text;
502+ const root = parse(getGrammars().get(a, .markdown).parser, u) orelse return text;
510503 defer root.destroy();
511504 var tables: std.ArrayList(ts.Node) = .empty;
512505 collectByKind(a, root.rootNode(), &.{"pipe_table"}, &tables);