diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/hunks.zig | 300 | ||||
| -rw-r--r-- | core/lib_syntax.zig | 17 |
2 files changed, 225 insertions, 92 deletions
diff --git a/core/hunks.zig b/core/hunks.zig index 837d2a52..98584980 100644 --- a/core/hunks.zig +++ b/core/hunks.zig @@ -5,6 +5,12 @@ //! green, coloured as their file, and the words a changed line changed on //! a tinted ground as delta emphasises them (its edits.rs and align.rs, here as `inferEdits` //! and `Alignment`). +//! +//! `format`, and what it takes and gives, names no pane and reads no file +//! but through its `Ctx`: `pardes-syntax` exports it (core/lib_syntax.zig) +//! for a program with no editor, a browser's reader, and with no colours +//! handed in it names no grammar either. The +Diff in the editor, the +//! file's second half, maps its rows onto a pane's (`paneResult`). const std = @import("std"); const diff = @import("diff.zig"); const syntax = @import("syntax.zig"); @@ -15,45 +21,132 @@ const Pardes = pardes.Pardes; const Pane = pardes.Pane; const Output = pardes.panes.Output; -const Row = locations.Row; - -/// What `format` takes of the editor it stacks for: its allocators, the -/// files a diff names read as the editor reads them (in pardes an open -/// pane's text, else the disk's, each read a site of the step's tape: -/// openDiff), their declarations, and one setting. +/// What `format` takes of the program it stacks for: an arena, the files a +/// diff names (in pardes an open pane's text, else the disk's, each read a +/// site of the step's tape: openDiff), their declarations, the diff's +/// colours, a text's width, one setting. A program with no files leaves +/// `source` to answer none: each hunk is numbered as the diff numbers it, +/// under git's own function line. pub const Ctx = struct { - /// The text and rows made, the caller's. - gpa: std.mem.Allocator, - /// All else, the call's. + /// What `format` makes and returns, the caller's to keep while it reads + /// the rows, and everything else of the call. arena: std.mem.Allocator, /// `path` as a look from `dir` finds it, its text the arena's (or the - /// editor's, good for the call); null when it is not there. - source: *const fn (ctx: *const Ctx, dir: []const u8, path: []const u8) ?Source, + /// caller's, good for the call); null when it is not there. + source: *const fn (ctx: *const Ctx, dir: []const u8, path: []const u8) ?Source = none, /// The declarations of `bytes`, the file at `path`, outermost first - /// (syntax.analyzeSource's), the arena's. - declarations: *const fn (ctx: *const Ctx, path: []const u8, bytes: []const u8) error{OutOfMemory}![]const syntax.ContextDeclaration, - /// The two above's own. - host: *anyopaque, + /// (`parsed`: syntax.analyzeSource's), the arena's; none by default. + declarations: *const fn (ctx: *const Ctx, path: []const u8, bytes: []const u8) error{OutOfMemory}![]const syntax.ContextDeclaration = noDeclarations, + /// A style byte for each byte of the diff, the arena's, its lines' + /// colours (`languages`: each hunk's as its file's language); null, the + /// grounds alone, and no grammar or tree-sitter in the program. + colors: ?*const fn (ctx: *const Ctx, content: []const u8) anyerror![]u8 = null, + /// The cells `text` takes, by which a removed and an added line are one + /// line changed or two (`max_line_distance`): its code points, or as + /// pardes's panes draw it. + width: *const fn (ctx: *const Ctx, text: []const u8) usize = codepoints, + /// The functions' own. + host: *anyopaque = undefined, /// A declaration above a hunk gets its `path:line` row too /// (LocationsConfig's tslocations). - tslocations: bool, - /// A tab's width in cells (the core's `settings.tab`). - tab: u16, + tslocations: bool = true, + + pub fn none(_: *const Ctx, _: []const u8, _: []const u8) ?Source { + return null; + } + + pub fn noDeclarations(_: *const Ctx, _: []const u8, _: []const u8) error{OutOfMemory}![]const syntax.ContextDeclaration { + return &.{}; + } + + /// The file parsed by its path's grammar, each call. + pub fn parsed(ctx: *const Ctx, path: []const u8, bytes: []const u8) error{OutOfMemory}![]const syntax.ContextDeclaration { + return (syntax.analyzeSource(ctx.arena, path, bytes, true, false) catch return &.{}).declarations; + } + + /// The diff coloured as the editor colours it (syntax.highlightDiff). + pub fn languages(ctx: *const Ctx, content: []const u8) anyerror![]u8 { + return syntax.highlightDiff(ctx.arena, content, 0, content.len); + } + + pub fn codepoints(_: *const Ctx, text: []const u8) usize { + var n: usize = 0; + for (text) |c| n += @intFromBool(c & 0xc0 != 0x80); + return n; + } }; /// A file's text, and its path as `dir` and the path named resolve. pub const Source = struct { path: []const u8, bytes: []const u8 }; -/// The rows `content`, a diff, stacks to, for a pane in `dir`, with each -/// row's metadata (Output.fillRows). Text and rows are `ctx.gpa`'s. -pub fn format(ctx: *const Ctx, dir: []const u8, content: []const u8) !locations.Result { +/// A diff stacked: its text, and a row for each of its lines (row i is +/// line i), the arena's; a row's path may be the diff's own bytes. +pub const Patch = struct { text: []const u8, rows: []const Row }; + +pub const Row = struct { + kind: Kind, + /// The file its section names, as `dir` finds it (the diff's name when + /// it is not found); "" for `other` and `meta`. + path: []const u8 = "", + /// The line of that file it points at, from 1 (0: none): where the file + /// has it now when `source` found it moved, else the diff's number. + line: usize = 0, + /// A hunk line's number in the old and in the new file, as the diff + /// numbers them; null on the side it is not on (an added line's old) + /// and on the rows that are not a hunk's lines. + old: ?usize = null, + new: ?usize = null, + /// Bytes into its text: where its `path:line` ends, and where what + /// follows the label starts (a hunk's stars). + location_end: usize = 0, + code_start: usize = 0, + /// A style byte for each byte of its text, or none: a `Syn` in the low + /// bits (`syntax.syn_bits`, with `colors`) and the grounds over it: + /// `ground_add` or `ground_del` on a changed line, `ground_line` on the + /// marker of one no other line changed, `ground_emph` on the words a + /// changed line changed. + styles: []const u8 = &.{}, + /// The diff's `\ No newline at end of file` follows this line: it has + /// no row of its own (delta shows it so, and the editor). + no_newline: bool = false, + + pub const Kind = enum { + /// What is said around the hunks: a mailed patch's headers, its + /// message and its diffstat, as written. + other, + /// A section's own words: a new file's mode, a rename, `Binary files + /// ... differ` (its `diff` and `index` lines say nothing the labels + /// do not, and its `---`/`+++` are the labels'). + meta, + /// A hunk's label, `path:line` where it changes something, a star + /// for each declaration it stands in. + hunk, + /// What the hunk stands in, above its label: a declaration's + /// `path:line` (`tslocations`) and its header's lines, from the file + /// `source` found and `declarations` read; or, without them, git's + /// own function line from the `@@`. + declaration, + /// A hunk's lines, behind their ` `, `+` or `-`. + context, + added, + removed, + }; +}; + +/// The rows `content`, a diff, stacks to, its files looked for from `dir`. +pub fn format(ctx: *const Ctx, dir: []const u8, content: []const u8) !Patch { const arena = ctx.arena; - const styles = try syntax.highlightDiff(arena, content, 0, content.len); - var out: Out = .{ .gpa = ctx.gpa, .text = .init(ctx.gpa) }; - errdefer out.deinit(); + const styles = if (ctx.colors) |colors| try colors(ctx, content) else plain: { + const none = try arena.alloc(u8, content.len); + @memset(none, 0); + break :plain none; + }; + var out: Out = .{ .text = .init(arena) }; var walk: diff.Walk = .{}; var hunk: Hunk = .{}; var source: SourceMemo = .{}; + // inferEdits' scratch, made once a call. + var scratch: []u8 = &.{}; var at: usize = 0; // The empty text after a last newline is no line. while (at < content.len) { @@ -73,9 +166,11 @@ pub fn format(ctx: *const Ctx, dir: []const u8, content: []const u8) !locations. }); }, // A `\ No newline` belongs to the line above, as delta shows it. - .no_newline => {}, + .no_newline => if (hunk.lines.items.len > 0) { + hunk.lines.items[hunk.lines.items.len - 1].no_newline = true; + }, else => { - try flush(ctx, &out, &hunk, &source, dir); + try flush(ctx, &out, &hunk, &source, &scratch, dir); switch (got.kind) { .hunk => hunk = .{ .path = walk.path(), @@ -86,22 +181,20 @@ pub fn format(ctx: *const Ctx, dir: []const u8, content: []const u8) !locations. }, // What is said around the hunks, a mailed patch's // headers, message and diffstat, as it was written. - .other => try out.row(line, "", .{ .kind = .context, .at = .{} }), + .other => try out.row(arena, line, .{ .kind = .other }), // A section's own words (a new file's mode, a rename, // `Binary files ... differ`), muted; its `diff` and // `index` lines say nothing the labels do not. .meta => if (!std.mem.startsWith(u8, line, "diff ") and !std.mem.startsWith(u8, line, "index ")) - try out.row(line, "", .{ .kind = .context, .declaration = true, .at = .{} }), + try out.row(arena, line, .{ .kind = .meta }), else => {}, } }, } at = nl + 1; } - try flush(ctx, &out, &hunk, &source, dir); - const rows = try out.rows.toOwnedSlice(ctx.gpa); - errdefer locations.freeRows(ctx.gpa, rows); - return .{ .content = try out.text.toOwnedSlice(), .rows = rows, .anchor = null }; + try flush(ctx, &out, &hunk, &source, &scratch, dir); + return .{ .text = out.text.written(), .rows = out.rows.items }; } const HunkLine = struct { @@ -115,6 +208,7 @@ const HunkLine = struct { /// Paired with a line it changed (inferEdits): its words show that; /// unpaired, the whole line was added or removed. paired: bool = false, + no_newline: bool = false, }; const Hunk = struct { @@ -134,37 +228,14 @@ fn gitContext(line: []const u8) []const u8 { } const Out = struct { - gpa: std.mem.Allocator, text: std.Io.Writer.Allocating, rows: std.ArrayList(Row) = .empty, - fn deinit(out: *Out) void { - out.text.deinit(); - locations.freeRowList(out.gpa, &out.rows); - } - - const Meta = struct { - kind: @FieldType(Row, "kind"), - declaration: bool = false, - at: @FieldType(Row, "at"), - location_end: usize = 0, - code_start: usize = 0, - colors: []const u8 = &.{}, - }; - - /// One row: its text, a newline, and its metadata. - fn row(out: *Out, text: []const u8, path: []const u8, meta: Meta) !void { + /// One row: its text, a newline, and what it is. + fn row(out: *Out, arena: std.mem.Allocator, text: []const u8, r: Row) !void { try out.text.writer.writeAll(text); try out.text.writer.writeByte('\n'); - try locations.appendRow(out.gpa, &out.rows, .{ - .kind = meta.kind, - .declaration = meta.declaration, - .path = @constCast(path), - .at = meta.at, - .location_end = meta.location_end, - .code_start = meta.code_start, - .colors = @constCast(meta.colors), - }); + try out.rows.append(arena, r); } }; @@ -276,7 +347,7 @@ fn heldAt(memo: *const SourceMemo, hunk: *const Hunk, comptime side: Side, start return .{ .held = held, .of = @max(of, 1) }; } -fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []const u8) !void { +fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, scratch: *[]u8, dir: []const u8) !void { defer hunk.* = .{}; if (hunk.path.len == 0 or hunk.lines.items.len == 0) return; const arena = ctx.arena; @@ -310,13 +381,12 @@ fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []cons depth += 1; var row = declaration.start_line; while (row <= declaration.header_end_line and row < start_row) : (row += 1) { - const at: @FieldType(Row, "at") = .{ .line = row + 1 }; if (row == declaration.start_line and ctx.tslocations) { const label = try std.fmt.allocPrint(arena, "{s}:{d}", .{ shown, row + 1 }); - try out.row(label, shown, .{ .kind = .context, .declaration = true, .at = at, .location_end = label.len, .code_start = label.len }); + try out.row(arena, label, .{ .kind = .declaration, .path = shown, .line = row + 1, .location_end = label.len, .code_start = label.len }); } // A blank where a hunk line has its marker: the code aligns. - try out.row(try std.fmt.allocPrint(arena, " {s}", .{memo.lines[row]}), shown, .{ .kind = .context, .declaration = true, .at = at }); + try out.row(arena, try std.fmt.allocPrint(arena, " {s}", .{memo.lines[row]}), .{ .kind = .declaration, .path = shown, .line = row + 1 }); } }; @@ -329,12 +399,12 @@ fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []cons try label.writer.writeByte(' '); try label.writer.splatByteAll('*', depth); } - try out.row(label.written(), shown, .{ .kind = .match, .at = .{ .line = line_no }, .location_end = location_end, .code_start = label.written().len }); + try out.row(arena, label.written(), .{ .kind = .hunk, .path = shown, .line = line_no, .location_end = location_end, .code_start = label.written().len }); // No file to read declarations from: git's own function line, muted // the same way. With one, git's guess (the last line before the hunk // that looked like a function) would only contradict it. if (side == .neither and hunk.header.len > 0) - try out.row(try std.fmt.allocPrint(arena, " {s}", .{hunk.header}), shown, .{ .kind = .context, .declaration = true, .at = .{ .line = line_no } }); + try out.row(arena, try std.fmt.allocPrint(arena, " {s}", .{hunk.header}), .{ .kind = .declaration, .path = shown, .line = line_no }); // Each run of removed lines and the added lines after it: the words // they changed. @@ -348,7 +418,7 @@ fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []cons while (i < lines.len and lines[i].kind == .removed) i += 1; const plus_start = i; while (i < lines.len and lines[i].kind == .added) i += 1; - try inferEdits(ctx.tab, arena, lines[minus_start..plus_start], lines[plus_start..i]); + try inferEdits(ctx, scratch, lines[minus_start..plus_start], lines[plus_start..i]); } // Each line behind its marker, `+`, `-` or a blank, as the diff @@ -368,7 +438,19 @@ fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []cons colors[0] = flag; @memcpy(colors[1..], line.styles); const on = moved(if (on_old) line.old else line.new, disk.shift); - try out.row(text, shown, .{ .kind = .preview, .at = .{ .line = @max(1, on) }, .colors = colors }); + try out.row(arena, text, .{ + .kind = switch (line.kind) { + .added => .added, + .removed => .removed, + else => .context, + }, + .path = shown, + .line = @max(1, on), + .old = if (line.kind == .added) null else line.old, + .new = if (line.kind == .removed) null else line.new, + .styles = colors, + .no_newline = line.no_newline, + }); } } @@ -385,18 +467,21 @@ const max_run = 256; const max_cells = 1 << 16; /// Pairs one hunk compares (each a table of up to `max_cells`): past it, the /// rest of its lines are shown unemphasised. And the scratch each pair is -/// aligned in, reused by the next: its table and the ranges it marks. +/// aligned in, reused by the next and by the next run's (one a `format`): +/// its table and the ranges it marks. const max_pairs = 4096; const pair_scratch_bytes = 8 << 20; /// Pair each removed line with the first added line after the last one /// paired that is near enough to it, in order, and emphasise in both the /// tokens one has and the other has not. -fn inferEdits(tab: u16, arena: std.mem.Allocator, minus: []HunkLine, plus: []HunkLine) !void { +fn inferEdits(ctx: *const Ctx, scratch_bytes: *[]u8, minus: []HunkLine, plus: []HunkLine) !void { if (minus.len == 0 or plus.len == 0 or minus.len > max_run or plus.len > max_run) return; + const arena = ctx.arena; const plus_tokens = try arena.alloc([]const []const u8, plus.len); for (plus, plus_tokens) |line, *tokens| tokens.* = try tokenize(arena, line.code); - var scratch: std.heap.FixedBufferAllocator = .init(try @import("memory.zig").unfilled(arena, pair_scratch_bytes)); + if (scratch_bytes.len == 0) scratch_bytes.* = try @import("memory.zig").unfilled(arena, pair_scratch_bytes); + var scratch: std.heap.FixedBufferAllocator = .init(scratch_bytes.*); var pairs: usize = 0; var plus_index: usize = 0; for (minus) |*m| { @@ -410,7 +495,7 @@ fn inferEdits(tab: u16, arena: std.mem.Allocator, minus: []HunkLine, plus: []Hun scratch.reset(); const pair = scratch.allocator(); var alignment = Alignment.init(pair, m_tokens, p_tokens) catch continue; - const marks = annotate(tab, pair, &alignment, m.code, plus[j].code) catch continue; + const marks = annotate(ctx, pair, &alignment, m.code, plus[j].code) catch continue; if (marks.distance > max_line_distance) continue; for (marks.minus) |r| for (m.styles[r[0]..r[1]]) |*b| { b.* |= syntax.ground_emph; @@ -536,7 +621,7 @@ const Marks = struct { /// what each deleted and inserted, and a run of blanks between two changes /// with them; the distance is the changed width over the whole width (a /// kept section counting on both lines). -fn annotate(tab: u16, arena: std.mem.Allocator, a: *const Alignment, minus_line: []const u8, plus_line: []const u8) !Marks { +fn annotate(ctx: *const Ctx, arena: std.mem.Allocator, a: *const Alignment, minus_line: []const u8, plus_line: []const u8) !Marks { var minus: std.ArrayList([2]usize) = .empty; var plus: std.ArrayList([2]usize) = .empty; var x_off: usize = 0; @@ -562,7 +647,7 @@ fn annotate(tab: u16, arena: std.mem.Allocator, a: *const Alignment, minus_line: switch (op) { .deletion => { const r = Take.section(a.x, &x_off, &m_byte, n, minus_line); - const w = trimmedWidth(tab, minus_line[r[0]..r[1]]); + const w = trimmedWidth(ctx, minus_line[r[0]..r[1]]); numer += w; denom += w; try minus.append(arena, r); @@ -571,7 +656,7 @@ fn annotate(tab: u16, arena: std.mem.Allocator, a: *const Alignment, minus_line: .noop => { const r = Take.section(a.x, &x_off, &m_byte, n, minus_line); const section = minus_line[r[0]..r[1]]; - denom += 2 * trimmedWidth(tab, section); + denom += 2 * trimmedWidth(ctx, section); const is_space = std.mem.trim(u8, section, " \t").len == 0; const coalesce = is_space and ((minus_prev == .deletion and plus_prev == .insertion and @@ -585,7 +670,7 @@ fn annotate(tab: u16, arena: std.mem.Allocator, a: *const Alignment, minus_line: }, .insertion => { const r = Take.section(a.y, &y_off, &p_byte, n, plus_line); - const w = trimmedWidth(tab, plus_line[r[0]..r[1]]); + const w = trimmedWidth(ctx, plus_line[r[0]..r[1]]); numer += w; denom += w; try plus.append(arena, r); @@ -597,8 +682,8 @@ fn annotate(tab: u16, arena: std.mem.Allocator, a: *const Alignment, minus_line: return .{ .minus = minus.items, .plus = plus.items, .distance = distance }; } -fn trimmedWidth(tab: u16, section: []const u8) usize { - return @import("File.zig").displayWidth(tab, std.mem.trim(u8, section, " \t")); +fn trimmedWidth(ctx: *const Ctx, section: []const u8) usize { + return ctx.width(ctx, std.mem.trim(u8, section, " \t")); } // ---- the +Diff in the editor ---- @@ -728,18 +813,22 @@ pub fn openDiff(p: *Pardes, id: usize, path: []const u8, content: []const u8) !u // The cache's may go at its next use: kept for the call. return ctx.arena.dupe(syntax.ContextDeclaration, hit.analysis.declarations); } + fn width(ctx: *const Ctx, text: []const u8) usize { + const pp: *Pardes = @ptrCast(@alignCast(ctx.host)); + return pardes.panes.File.displayWidth(pp.settings.tab, text); + } }; const ctx: Ctx = .{ - .gpa = p.gpa, .arena = p.scratch.allocator(), .source = Read.source, .declarations = Read.declarations, + .colors = Ctx.languages, + .width = Read.width, .host = p, .tslocations = p.locations_config.tslocations, - .tab = p.settings.tab, }; const root = sourceRoot(&ctx, dir, content); - const result = try format(&ctx, root, content); + const result = try paneResult(p.gpa, try format(&ctx, root, content)); errdefer { p.gpa.free(result.content); locations.freeRows(p.gpa, result.rows); @@ -756,6 +845,31 @@ pub fn openDiff(p: *Pardes, id: usize, path: []const u8, content: []const u8) !u return Output.fillRows(p, id, root, .{ .cmd = .Diff }, name, arg, result.content, result.rows, null); } +/// `patch`'s text and rows as a pane has them (Output.fillRows), `gpa`'s: a +/// hunk's label is a match n and N step to, what is around the hunks and +/// what they stand in is context (a section's words and a declaration +/// muted), a hunk's lines are previews of their file. +fn paneResult(gpa: std.mem.Allocator, patch: Patch) !locations.Result { + var rows: std.ArrayList(locations.Row) = .empty; + errdefer locations.freeRowList(gpa, &rows); + for (patch.rows) |r| try locations.appendRow(gpa, &rows, .{ + .kind = switch (r.kind) { + .hunk => .match, + .other, .meta, .declaration => .context, + .context, .added, .removed => .preview, + }, + .declaration = r.kind == .meta or r.kind == .declaration, + .path = @constCast(r.path), + .at = .{ .line = r.line }, + .location_end = r.location_end, + .code_start = r.code_start, + .colors = @constCast(r.styles), + }); + const content = try gpa.dupe(u8, patch.text); + errdefer gpa.free(content); + return .{ .content = content, .rows = try rows.toOwnedSlice(gpa), .anchor = null }; +} + /// Diff on a +Diff: the diff it stacks, as text. A pane holding it /// already (the newest: a +Pager folded under its +Diff unfolds) is /// shown; else the file is opened as text. @@ -796,7 +910,7 @@ const TestCtx = struct { fn init(t: *TestCtx) void { t.arena_state = .init(testing.allocator); - t.ctx = .{ .gpa = testing.allocator, .arena = t.arena_state.allocator(), .source = source, .declarations = declarations, .host = t, .tslocations = true, .tab = @import("config.zig").tab_width }; + t.ctx = .{ .arena = t.arena_state.allocator(), .source = source, .declarations = Ctx.parsed, .colors = Ctx.languages, .width = width }; syntax.start(testing.allocator); } @@ -811,8 +925,8 @@ const TestCtx = struct { return .{ .path = full, .bytes = bytes }; } - fn declarations(ctx: *const Ctx, path: []const u8, bytes: []const u8) error{OutOfMemory}![]const syntax.ContextDeclaration { - return (syntax.analyzeSource(ctx.arena, path, bytes, true, false) catch return &.{}).declarations; + fn width(_: *const Ctx, text: []const u8) usize { + return pardes.panes.File.displayWidth(@import("config.zig").tab_width, text); } }; @@ -822,15 +936,17 @@ fn emphasised(line: HunkLine) ![]const u8 { return out.toOwnedSlice(testing.allocator); } -fn expectEmphasis(tab: u16, minus: []const u8, plus: []const u8, want_minus: []const u8, want_plus: []const u8) !void { +fn expectEmphasis(minus: []const u8, plus: []const u8, want_minus: []const u8, want_plus: []const u8) !void { var arena_state: std.heap.ArenaAllocator = .init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); + const ctx: Ctx = .{ .arena = arena, .width = TestCtx.width }; + var scratch: []u8 = &.{}; var m = [_]HunkLine{.{ .kind = .removed, .code = minus, .styles = try arena.alloc(u8, minus.len), .old = 1, .new = 1 }}; var p = [_]HunkLine{.{ .kind = .added, .code = plus, .styles = try arena.alloc(u8, plus.len), .old = 2, .new = 1 }}; @memset(m[0].styles, 0); @memset(p[0].styles, 0); - try inferEdits(tab, arena, &m, &p); + try inferEdits(&ctx, &scratch, &m, &p); const got_minus = try emphasised(m[0]); defer testing.allocator.free(got_minus); const got_plus = try emphasised(p[0]); @@ -840,16 +956,16 @@ fn expectEmphasis(tab: u16, minus: []const u8, plus: []const u8, want_minus: []c } test "delta's emphasis: the word a line changed, in both lines" { - try expectEmphasis(@import("config.zig").tab_width, " let x = foo(1);", " let x = bar(1);", "............foo....", "............bar...."); - try expectEmphasis(@import("config.zig").tab_width, "a b c", "a x c", "..b..", "..x.."); + try expectEmphasis(" let x = foo(1);", " let x = bar(1);", "............foo....", "............bar...."); + try expectEmphasis("a b c", "a x c", "..b..", "..x.."); } test "delta's emphasis: lines too far apart are no change of one another" { - try expectEmphasis(@import("config.zig").tab_width, "completely different text", "nothing alike here at all", ".........................", "........................."); + try expectEmphasis("completely different text", "nothing alike here at all", ".........................", "........................."); } test "delta's emphasis: a word added keeps the rest plain" { - try expectEmphasis(@import("config.zig").tab_width, "fn run(a: u32)", "fn run(a: u32, b: u32)", "..............", "............., b: u32."); + try expectEmphasis("fn run(a: u32)", "fn run(a: u32, b: u32)", "..............", "............., b: u32."); } test "delta's alignment: kitten to sitting, as align.rs has it" { @@ -880,7 +996,7 @@ test "a diff stacks to a label a hunk and its lines, markers gone, tinted and st \\ tail \\ ; - const result = try format(&t.ctx, "/pardes-surely-missing-dir", patch); + const result = try paneResult(testing.allocator, try format(&t.ctx, "/pardes-surely-missing-dir", patch)); defer testing.allocator.free(result.content); defer locations.freeRows(testing.allocator, result.rows); try testing.expectEqualStrings("nowhere/x.txt:2\n section one\n keep\n-old value\n+new value\n tail\n", result.content); @@ -919,7 +1035,7 @@ test "a hunk the file has moved since is found where it went, inside its declara \\ b(); \\ ; - const result = try format(&t.ctx, dir, patch); + const result = try paneResult(testing.allocator, try format(&t.ctx, dir, patch)); defer testing.allocator.free(result.content); defer locations.freeRows(testing.allocator, result.rows); try testing.expect(std.mem.indexOf(u8, result.content, "a.zig:3\n const S = struct {\na.zig:5 *\n") != null); @@ -947,7 +1063,7 @@ test "a mailed patch keeps its headers, message and diffstat above its hunks" { \\+b \\ ; - const result = try format(&t.ctx, "/pardes-surely-missing-dir", patch); + const result = try paneResult(testing.allocator, try format(&t.ctx, "/pardes-surely-missing-dir", patch)); defer testing.allocator.free(result.content); defer locations.freeRows(testing.allocator, result.rows); try testing.expectEqualStrings("From 0123 Mon Sep 17 00:00:00 2001\nSubject: [PATCH] change x\n\nWhy it changed.\n---\n x.txt | 2 +-\n\nx.txt:1\n-a\n+b\n", result.content); @@ -1038,7 +1154,7 @@ test "a hunk edited since, beside siblings with its signature's tail, is found i \\ c \\ ; - const result = try format(&t.ctx, dir, patch); + const result = try paneResult(testing.allocator, try format(&t.ctx, dir, patch)); defer testing.allocator.free(result.content); defer locations.freeRows(testing.allocator, result.rows); try testing.expect(std.mem.indexOf(u8, result.content, "a.rs:12\n fn three(\na.rs:15 *\n") != null); diff --git a/core/lib_syntax.zig b/core/lib_syntax.zig index f4f85820..0bb3f510 100644 --- a/core/lib_syntax.zig +++ b/core/lib_syntax.zig @@ -13,6 +13,7 @@ const syntax = @import("syntax.zig"); const colors = @import("colors.zig"); const modal = @import("modal.zig"); +const hunks = @import("hunks.zig"); /// Every grammar pardes knows (`all`), by name or alias (`byName`) and by /// path (`byPath`). The build's `grammars` option says which of them are @@ -70,6 +71,22 @@ pub const ground_emph = syntax.ground_emph; pub const ground_line = syntax.ground_line; pub const Grounds = colors.Grounds; +/// A unified diff stacked as pardes's patch view (its +Diff) shows it: +/// `formatPatch` gives a `Patch`, its text and a `PatchRow` a line: a +/// hunk's `path:line`, the declarations it stands in (or git's function +/// line), its lines with both their numbers, each byte's style (a `Syn` +/// when `colors` are given, the grounds, the words a changed line changed). +/// What it takes of the program is a `PatchCtx`: an arena, and optionally +/// the files the diff names (`source`, a `PatchSource`), their declarations +/// (`PatchCtx.parsed`), the colours (`PatchCtx.languages`, highlightDiff's) +/// and a text's width; with none of them it names no grammar and no +/// tree-sitter. +pub const formatPatch = hunks.format; +pub const PatchCtx = hunks.Ctx; +pub const PatchSource = hunks.Source; +pub const Patch = hunks.Patch; +pub const PatchRow = hunks.Row; + /// The tree-sitter bindings a tree is read through: `tree.rootNode()`, and /// a node's `kind()`, `startByte()`, `endByte()`, `child(i)`, `parent()`. pub const ts = syntax.ts; |
