@@ -0,0 +1,137 @@
1+const std = @import("std");
2+const common = @import("../tools/common.zig");
3+const theme = @import("theme.zig");
4+const highlight = @import("highlight.zig");
5+const render = @import("render.zig");
6+
7+const BodyLine = render.BodyLine;
8+
9+fn diffHunk() theme.Style {
10+ return .{ .fg = theme.current.prompt };
11+}
12+
13+const DiffKind = enum { hunk, add, del, ctx, note };
14+
15+fn diffKindOf(kind: []const u8) ?DiffKind {
16+ if (std.mem.eql(u8, kind, "location")) return .hunk;
17+ if (std.mem.eql(u8, kind, "addition")) return .add;
18+ if (std.mem.eql(u8, kind, "deletion")) return .del;
19+ if (std.mem.eql(u8, kind, "context")) return .ctx;
20+ if (highlight.isDiffLineKind(kind)) return .note;
21+ return null;
22+}
23+
24+fn prefixKind(line: []const u8) DiffKind {
25+ if (std.mem.startsWith(u8, line, "@@")) return .hunk;
26+ if (std.mem.startsWith(u8, line, "+")) return .add;
27+ if (std.mem.startsWith(u8, line, "-")) return .del;
28+ if (std.mem.startsWith(u8, line, " ")) return .ctx;
29+ return .note;
30+}
31+
32+fn langFor(path: []const u8) []const u8 {
33+ const ext = std.fs.path.extension(path);
34+ const named = [_]struct { ext: []const u8, lang: []const u8 }{
35+ .{ .ext = ".js", .lang = "js" },
36+ .{ .ext = ".jsx", .lang = "jsx" },
37+ .{ .ext = ".mjs", .lang = "js" },
38+ .{ .ext = ".cjs", .lang = "js" },
39+ .{ .ext = ".ts", .lang = "ts" },
40+ .{ .ext = ".tsx", .lang = "tsx" },
41+ .{ .ext = ".py", .lang = "py" },
42+ .{ .ext = ".go", .lang = "go" },
43+ .{ .ext = ".zig", .lang = "zig" },
44+ };
45+ for (named) |entry| {
46+ if (std.ascii.eqlIgnoreCase(ext, entry.ext)) return entry.lang;
47+ }
48+ return "";
49+}
50+
51+fn rebaseSpans(a: std.mem.Allocator, spans: []const highlight.Span, lo: u32, hi: u32) []const highlight.Span {
52+ var out: std.ArrayList(highlight.Span) = .empty;
53+ for (spans) |s| {
54+ const start = @max(s.start, lo);
55+ const end = @min(s.end, hi);
56+ if (end <= start) continue;
57+ out.append(a, .{ .start = start - lo, .end = end - lo, .style = s.style }) catch {};
58+ }
59+ return out.items;
60+}
61+
62+pub fn diffLines(a: std.mem.Allocator, path: []const u8, lines: []const []const u8) []const BodyLine {
63+ var joined: std.ArrayList(u8) = .empty;
64+ var starts = a.alloc(u32, lines.len) catch return &.{};
65+ for (lines, 0..) |l, i| {
66+ if (i > 0) joined.append(a, '\n') catch {};
67+ starts[i] = @intCast(joined.items.len);
68+ joined.appendSlice(a, l) catch {};
69+ }
70+ const nodes = highlight.diffSpans(a, joined.items);
71+ std.mem.sort(highlight.NodeSpan, nodes, {}, struct {
72+ fn lt(_: void, x: highlight.NodeSpan, y: highlight.NodeSpan) bool {
73+ return x.start < y.start;
74+ }
75+ }.lt);
76+
77+ var code: std.ArrayList(u8) = .empty;
78+ var kinds = a.alloc(DiffKind, lines.len) catch return &.{};
79+ var los = a.alloc(u32, lines.len) catch return &.{};
80+ var his = a.alloc(u32, lines.len) catch return &.{};
81+ var cursor: usize = 0;
82+ for (lines, 0..) |l, i| {
83+ while (cursor < nodes.len and nodes[cursor].end <= starts[i]) cursor += 1;
84+ const parsed: ?DiffKind = if (cursor < nodes.len and nodes[cursor].start == starts[i])
85+ diffKindOf(nodes[cursor].kind)
86+ else
87+ null;
88+ const kind = parsed orelse prefixKind(l);
89+ kinds[i] = kind;
90+ los[i] = 0;
91+ his[i] = 0;
92+ if (kind != .add and kind != .del and kind != .ctx) continue;
93+ const content = l[1..];
94+ los[i] = @intCast(code.items.len);
95+ code.appendSlice(a, content) catch {};
96+ code.append(a, '\n') catch {};
97+ his[i] = @intCast(code.items.len - 1);
98+ }
99+
100+ const spans = highlight.spansFor(a, langFor(path), code.items);
101+
102+ var out: std.ArrayList(BodyLine) = .empty;
103+ for (lines, 0..) |l, i| {
104+ switch (kinds[i]) {
105+ .hunk => out.append(a, .{ .text = l, .style = diffHunk() }) catch {},
106+ .note => out.append(a, .{ .text = l, .style = .{ .fg = theme.current.comment } }) catch {},
107+ .ctx => out.append(a, .{ .text = l, .style = .{ .fg = theme.current.comment } }) catch {},
108+ .add, .del => {
109+ const bg = if (kinds[i] == .add) theme.current.diff_add else theme.current.diff_delete;
110+ const content = l[1..];
111+ const painted = highlight.paint(a, content, rebaseSpans(a, spans, los[i], his[i]), .{ .bg = bg });
112+ out.append(a, .{
113+ .text = std.fmt.allocPrint(a, "{c}{s}", .{ l[0], painted }) catch l,
114+ .bg = bg,
115+ }) catch {};
116+ },
117+ }
118+ }
119+ return out.items;
120+}
121+
122+pub fn diffRows(a: std.mem.Allocator, diffs: []const common.FileDiff) []const BodyLine {
123+ var out: std.ArrayList(BodyLine) = .empty;
124+ for (diffs) |d| {
125+ out.append(a, .{ .text = d.path, .style = .{ .fg = theme.current.prompt } }) catch {};
126+ if (d.patch) |patch| {
127+ const trimmed = std.mem.trimEnd(u8, patch, " \t\r\n");
128+ var it = std.mem.splitScalar(u8, trimmed, '\n');
129+ var body: std.ArrayList([]const u8) = .empty;
130+ while (it.next()) |l| body.append(a, l) catch {};
131+ out.appendSlice(a, diffLines(a, d.path, body.items)) catch {};
132+ } else if (d.note) |note| {
133+ out.append(a, .{ .text = note, .style = .{ .fg = theme.current.comment } }) catch {};
134+ }
135+ }
136+ return out.items;
Cut short: this commit's diff is over 1000 lines.