@@ -11,6 +11,8 @@ extern fn tree_sitter_markdown_inline() callconv(.c) *const ts.Language;
1111 extern fn tree_sitter_python() callconv(.c) *const ts.Language;
1212 extern fn tree_sitter_go() callconv(.c) *const ts.Language;
1313 extern fn tree_sitter_zig() callconv(.c) *const ts.Language;
14+extern fn tree_sitter_bash() callconv(.c) *const ts.Language;
15+extern fn tree_sitter_diff() callconv(.c) *const ts.Language;
1416
1517 const JS_QUERIES = @embedFile("js_highlights");
1618 const TS_QUERIES = @embedFile("ts_highlights");
@@ -20,6 +22,8 @@ const MD_INLINE_QUERIES = @embedFile("md_inline_highlights");
2022 const PY_QUERIES = @embedFile("py_highlights");
2123 const GO_QUERIES = @embedFile("go_highlights");
2224 const ZIG_QUERIES = @embedFile("zig_highlights");
25+const BASH_QUERIES = @embedFile("bash_highlights");
26+const DIFF_QUERIES = @embedFile("diff_highlights");
2327
2428 const Syntax = struct {
2529 parser: *ts.Parser,
@@ -56,6 +60,8 @@ const Grammars = struct {
5660 python: Syntax,
5761 go: Syntax,
5862 zig: Syntax,
63+ bash: Syntax,
64+ diff: Syntax,
5965
6066 fn init(a: std.mem.Allocator) Grammars {
6167 const js = tree_sitter_javascript();
@@ -66,6 +72,8 @@ const Grammars = struct {
6672 const py = tree_sitter_python();
6773 const go_lang = tree_sitter_go();
6874 const zig_lang = tree_sitter_zig();
75+ const bash_lang = tree_sitter_bash();
76+ const diff_lang = tree_sitter_diff();
6977 return .{
7078 .javascript = syntax(a, js, &.{JS_QUERIES}) orelse unreachable,
7179 .typescript = syntax(a, ts_lang, &.{ JS_QUERIES, TS_QUERIES }) orelse unreachable,
@@ -75,6 +83,8 @@ const Grammars = struct {
7583 .python = syntax(a, py, &.{PY_QUERIES}) orelse unreachable,
7684 .go = syntax(a, go_lang, &.{GO_QUERIES}) orelse unreachable,
7785 .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,
7888 };
7989 }
8090 };
@@ -84,7 +94,7 @@ fn getGrammars(a: std.mem.Allocator) *Grammars {
8494 return &grammars.?;
8595 }
8696
87-const Span = struct {
97+pub const Span = struct {
8898 start: u32,
8999 end: u32,
90100 style: theme.Style,
@@ -232,7 +242,7 @@ fn eventLess(_: void, x: Event, y: Event) bool {
232242 return x.order < y.order;
233243 }
234244
235-fn paint(a: std.mem.Allocator, text: []const u8, spans: []const Span) []u8 {
245+fn paint(a: std.mem.Allocator, text: []const u8, spans: []const Span, base: theme.Style) []u8 {
236246 var events: std.ArrayList(Event) = .empty;
237247 for (spans, 0..) |span, i| {
238248 if (span.end <= span.start) continue;
@@ -247,7 +257,7 @@ fn paint(a: std.mem.Allocator, text: []const u8, spans: []const Span) []u8 {
247257 var at: u32 = 0;
248258
249259 for (events.items) |event| {
250- emit(a, &out, &active, spans, text, event.at, &at, &plain);
260+ emit(a, &out, &active, spans, text, event.at, &at, &plain, base);
251261 if (event.open) {
252262 active.append(a, event.span) catch {};
253263 } else {
@@ -259,33 +269,113 @@ fn paint(a: std.mem.Allocator, text: []const u8, spans: []const Span) []u8 {
259269 }
260270 }
261271 }
262- emit(a, &out, &active, spans, text, @intCast(text.len), &at, &plain);
263- if (!plain) out.appendSlice(a, theme.SGR_PLAIN) catch {};
272+ emit(a, &out, &active, spans, text, @intCast(text.len), &at, &plain, base);
273+ if (!plain) out.appendSlice(a, theme.sgrOn(a, .{}, base)) catch {};
264274 return out.items;
265275 }
266276
267-fn emit(a: std.mem.Allocator, out: *std.ArrayList(u8), active: *std.ArrayList(usize), spans: []const Span, text: []const u8, end: u32, at: *u32, plain: *bool) void {
277+fn emit(a: std.mem.Allocator, out: *std.ArrayList(u8), active: *std.ArrayList(usize), spans: []const Span, text: []const u8, end: u32, at: *u32, plain: *bool, base: theme.Style) void {
268278 if (end <= at.*) return;
279+ const reset = theme.sgrOn(a, .{}, base);
269280 const style: ?theme.Style = if (active.items.len == 0) null else spans[active.items[active.items.len - 1]].style;
270281 if (style) |s| {
271- out.appendSlice(a, theme.sgr(a, s)) catch {};
282+ const sgr = theme.sgrOn(a, s, base);
283+ out.appendSlice(a, sgr) catch {};
272284 plain.* = false;
273- } else {
274- if (!plain.*) out.appendSlice(a, theme.SGR_PLAIN) catch {};
275- plain.* = true;
276- }
277- const chunk = text[at.*..end];
278- if (style) |s| {
285+ const chunk = text[at.*..end];
279286 for (chunk) |c| {
280287 out.append(a, c) catch {};
281- if (c == '\n') out.appendSlice(a, theme.sgr(a, s)) catch {};
288+ if (c == '\n') out.appendSlice(a, sgr) catch {};
282289 }
283290 } else {
284- out.appendSlice(a, chunk) catch {};
291+ if (!plain.*) out.appendSlice(a, reset) catch {};
292+ plain.* = true;
293+ out.appendSlice(a, text[at.*..end]) catch {};
285294 }
286295 at.* = end;
287296 }
288297
298+pub fn paintOn(a: std.mem.Allocator, text: []const u8, spans: []const Span, base: theme.Style) []u8 {
299+ return paint(a, text, spans, base);
300+}
301+
302+fn syntaxFor(g: *Grammars, lang: []const u8) ?*Syntax {
303+ if (std.mem.eql(u8, lang, "js") or std.mem.eql(u8, lang, "javascript") or std.mem.eql(u8, lang, "jsx")) return &g.javascript;
304+ if (std.mem.eql(u8, lang, "ts") or std.mem.eql(u8, lang, "typescript")) return &g.typescript;
305+ if (std.mem.eql(u8, lang, "tsx")) return &g.tsx;
306+ if (std.mem.eql(u8, lang, "py") or std.mem.eql(u8, lang, "python")) return &g.python;
307+ if (std.mem.eql(u8, lang, "go") or std.mem.eql(u8, lang, "golang")) return &g.go;
308+ if (std.mem.eql(u8, lang, "zig")) return &g.zig;
309+ if (std.mem.eql(u8, lang, "bash") or std.mem.eql(u8, lang, "sh") or std.mem.eql(u8, lang, "shell")) return &g.bash;
310+ return null;
311+}
312+
313+fn infoLang(info: []const u8) []const u8 {
314+ const trimmed = std.mem.trim(u8, info, " \t\r\n");
315+ var end: usize = 0;
316+ while (end < trimmed.len and trimmed[end] != ' ' and trimmed[end] != '\t') end += 1;
317+ return trimmed[0..end];
318+}
319+
320+pub fn spansFor(a: std.mem.Allocator, info: []const u8, text: []const u8) []const Span {
321+ const g = getGrammars(a);
322+ const lang = std.ascii.allocLowerString(a, infoLang(info)) catch infoLang(info);
323+ const s = syntaxFor(g, lang) orelse return &.{};
324+ const u = utf16(a, text) orelse return &.{};
325+ const root = parse(s.parser, u) orelse return &.{};
326+ defer root.destroy();
327+ return spansOf(a, s, root.rootNode(), u, text, 0);
328+}
329+
330+pub fn highlightOn(a: std.mem.Allocator, info: []const u8, text: []const u8, base: theme.Style) []const u8 {
331+ return paint(a, text, spansFor(a, info, text), base);
332+}
333+
334+pub const NodeSpan = struct { start: u32, end: u32, kind: []const u8 };
335+
336+const diff_line_kinds = [_][]const u8{
337+ "file_change", "binary_change", "index", "similarity", "dissimilarity",
338+ "old_file", "new_file", "location", "addition", "deletion",
339+ "change", "context", "comment", "special", "unrecognized",
340+};
341+
342+fn isDiffLineKind(kind: []const u8) bool {
343+ for (diff_line_kinds) |k| {
344+ if (std.mem.eql(u8, kind, k)) return true;
345+ }
346+ return false;
347+}
348+
349+fn collectDiffNodes(a: std.mem.Allocator, node: ts.Node, u: Utf16, out: *std.ArrayList(NodeSpan)) void {
350+ if (isDiffLineKind(node.kind())) {
351+ out.append(a, .{
352+ .start = byteOf(u, node.startByte() / 2),
353+ .end = byteOf(u, node.endByte() / 2),
354+ .kind = node.kind(),
355+ }) catch {};
356+ return;
357+ }
358+ var i: u32 = 0;
359+ while (i < node.childCount()) : (i += 1) {
360+ const child = node.child(i) orelse continue;
361+ collectDiffNodes(a, child, u, out);
362+ }
363+}
364+
365+pub fn diffSpans(a: std.mem.Allocator, text: []const u8) []NodeSpan {
366+ const g = getGrammars(a);
367+ const u = utf16(a, text) orelse return &.{};
368+ const root = parse(g.diff.parser, u) orelse return &.{};
369+ defer root.destroy();
370+ var out: std.ArrayList(NodeSpan) = .empty;
371+ collectDiffNodes(a, root.rootNode(), u, &out);
372+ return out.items;
373+}
374+
375+pub fn highlightCode(a: std.mem.Allocator, info: []const u8, text: []const u8) []const u8 {
376+ return highlightOn(a, info, text, .{});
377+}
378+
289379 pub fn highlightMarkdown(a: std.mem.Allocator, text: []const u8) []const u8 {
290380 const g = getGrammars(a);
291381 const u = utf16(a, text) orelse return text;
@@ -334,7 +424,7 @@ pub fn highlightMarkdown(a: std.mem.Allocator, text: []const u8) []const u8 {
334424 const idx = @min(level, theme.current.headings.len) - 1;
335425 spans = std.ArrayList(Span).fromOwnedSlice(recolor(a, spans.items, byteOf(u, heading.startByte() / 2), byteOf(u, heading.endByte() / 2), theme.current.headings[idx]));
336426 }
337- return paint(a, text, spans.items);
427+ return paint(a, text, spans.items, .{});
338428 }
339429
340430 const Align = enum { none, left, center, right };
@@ -478,24 +568,3 @@ fn byteOf(u: Utf16, unit: u32) u32 {
478568 fn parse(parser: *ts.Parser, u: Utf16) ?*ts.Tree {
479569 return parser.parseStringEncoding(std.mem.sliceAsBytes(u.units), null, .utf16le);
480570 }
481-
482-pub fn highlightCode(a: std.mem.Allocator, info: []const u8, text: []const u8) []const u8 {
483- const trimmed = std.mem.trim(u8, info, " \t\r\n");
484- var end: usize = 0;
485- while (end < trimmed.len and trimmed[end] != ' ' and trimmed[end] != '\t') end += 1;
486- const lang = std.ascii.allocLowerString(a, trimmed[0..end]) catch trimmed[0..end];
487- const g = getGrammars(a);
488- const u = utf16(a, text) orelse return text;
489- const syn: ?*Syntax =
490- if (std.mem.eql(u8, lang, "js") or std.mem.eql(u8, lang, "javascript") or std.mem.eql(u8, lang, "jsx")) &g.javascript
491- else if (std.mem.eql(u8, lang, "ts") or std.mem.eql(u8, lang, "typescript")) &g.typescript
492- else if (std.mem.eql(u8, lang, "tsx")) &g.tsx
493- else if (std.mem.eql(u8, lang, "py") or std.mem.eql(u8, lang, "python")) &g.python
494- else if (std.mem.eql(u8, lang, "go") or std.mem.eql(u8, lang, "golang")) &g.go
495- else if (std.mem.eql(u8, lang, "zig")) &g.zig
496- else null;
497- const s = syn orelse return text;
498- const root = parse(s.parser, u) orelse return text;
499- defer root.destroy();
500- return paint(a, text, spansOf(a, s, root.rootNode(), u, text, 0));
501-}