summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig637
1 files changed, 318 insertions, 319 deletions
diff --git a/src/File.zig b/src/File.zig
index 553f34e4..d5438704 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -233,15 +233,6 @@ pub fn graphemeDisplayWidth(grapheme: []const u8) usize {
return @max(1, width);
}
-test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width" {
- try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xeee\xcc\x81"));
- try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\xee\xffe"));
- try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\u{4e16}\x80"));
- // The whole path the 9P fuzzer took: a body holding it, fitted to a width.
- const text = "\xeee\xcc\x81";
- try std.testing.expectEqual(@as(usize, 4), fitEnd(text, 0, 10));
-}
-
pub fn displayWidth(text: []const u8) usize {
var width: usize = 0;
var at: usize = 0;
@@ -323,36 +314,6 @@ pub fn sourceLine(pane: *const Pane, row: i32) []const u8 {
return f.content[start..end];
}
-test "indexed source rows match uncached scans across content changes" {
- const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
- defer p.deinit();
- const pane = try p.setTestFile("");
- const file = &pane.file.?;
- for ([_][]const u8{ "", "one", "one\n", "a\r\nλ界\nlast", "\n\n", "\tλ e\u{301}\n\r\n" }) |content| {
- setContent(p, file, try p.gpa.dupe(u8, content));
- // An edit carries a built index forward; it must equal a fresh one.
- if (file.line_starts.len > 0) {
- const carried = file.line_starts;
- const room = indexRoom(file);
- defer p.gpa.free(room);
- file.line_starts = &.{};
- try std.testing.expectEqualSlices(usize, try lineIndex(p.gpa, file), carried);
- p.gpa.free(file.line_starts);
- file.line_starts = &.{};
- }
- const rows = lineCount(content) + 2;
- for ([_]bool{ false, true }) |indexed| {
- if (indexed) _ = try lineIndex(p.gpa, file);
- try std.testing.expectEqualStrings("", sourceLine(pane, -1));
- for (0..rows) |row| try std.testing.expectEqualStrings(
- modal.lineSlice(content, row),
- sourceLine(pane, @intCast(row)),
- );
- if (!indexed) try std.testing.expectEqual(@as(usize, 0), file.line_starts.len);
- }
- }
-}
-
/// The pads `row` is laid out with in this pane: a row of a markdown
/// table (Tables.zig) of its text as it is now, when the table fits the
/// width a wrapping body has (a wider one is shown as its source, wrapped);
@@ -430,82 +391,6 @@ pub fn renderedLineDisplayCol(pane: *const Pane, row: i32, line_text: []const u8
return prefix +| rawDisplayCol(line_text[prefix..], byte_col - prefix);
}
-test "the ASCII arm of graphemeDisplayWidth matches the gwidth it skips" {
- const ref = struct {
- fn width(grapheme: []const u8) usize {
- if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now;
- return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode)));
- }
- }.width;
-
- var one: [1]u8 = undefined;
- var b: u8 = 0;
- while (b < 0x80) : (b += 1) {
- one[0] = b;
- try std.testing.expectEqual(ref(one[0..1]), graphemeDisplayWidth(one[0..1]));
- }
- for ([_][]const u8{
- "e\u{301}", "a\u{903}", "1\u{fe0f}\u{20e3}", "\u{4e16}",
- "\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff",
- }) |g| try std.testing.expectEqual(ref(g), graphemeDisplayWidth(g));
- // Two invalid bytes are two cells, as surface.zig draws them, where
- // vaxis's own count gives the pair one.
- try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xe4\xb8"));
-}
-
-test "the ASCII run in fitEnd survives an exhaustive byte sweep" {
- const reference = struct {
- fn fitEnd(text: []const u8, start: usize, width: usize) usize {
- var end = start;
- var used: usize = 0;
- while (end < text.len) {
- const next_end = modal.nextGrapheme(text, end);
- const next_used = used +| graphemeDisplayWidth(text[end..next_end]);
- if (next_used > width) return if (end == start) next_end else end;
- used = next_used;
- end = next_end;
- }
- return end;
- }
- }.fitEnd;
-
- const neighbours = [_][]const u8{
- "", "z", "\u{301}", "\u{200d}\u{1f680}",
- "\u{903}", "\u{fe0f}", "\u{4e16}", "\u{1f642}",
- "\u{1f1e6}\u{1f1e7}", "\xff", "\xe4\xb8", "\xe4\x28\xb8",
- };
- var buf: [16]u8 = undefined;
- // The same pair again behind an ASCII prefix, so a break can land exactly at the run boundary
- // as well as before it and inside the multi-byte cluster that follows it.
- var prefixed: [18]u8 = undefined;
- prefixed[0] = 'a';
- prefixed[1] = 'b';
- var b: u8 = 0;
- while (b < 0x80) : (b += 1) {
- buf[0] = b;
- for (neighbours) |tail| {
- @memcpy(buf[1..][0..tail.len], tail);
- const pair = buf[0 .. 1 + tail.len];
- @memcpy(prefixed[2..][0..pair.len], pair);
- for ([_][]const u8{ pair, prefixed[0 .. 2 + pair.len] }) |text| {
- var width: usize = 0;
- while (width <= text.len + 3) : (width += 1) {
- var start: usize = 0;
- while (start <= text.len) : (start += 1) {
- std.testing.expectEqual(
- reference(text, start, width),
- fitEnd(text, start, width),
- ) catch |e| {
- std.debug.print("fitEnd({any}, {d}, {d})\n", .{ text, start, width });
- return e;
- };
- }
- }
- }
- }
- }
-}
-
fn fitEnd(text: []const u8, start: usize, width: usize) usize {
var end = start;
var used: usize = 0;
@@ -526,52 +411,6 @@ fn fitEnd(text: []const u8, start: usize, width: usize) usize {
return end;
}
-test "the ASCII run in fitEnd cuts where the grapheme walk would" {
- const reference = struct {
- fn fitEnd(text: []const u8, start: usize, width: usize) usize {
- var end = start;
- var used: usize = 0;
- while (end < text.len) {
- const next_end = modal.nextGrapheme(text, end);
- const next_used = used +| graphemeDisplayWidth(text[end..next_end]);
- if (next_used > width) return if (end == start) next_end else end;
- used = next_used;
- end = next_end;
- }
- return end;
- }
- }.fitEnd;
-
- const cases = [_][]const u8{
- "",
- "hello world",
- // the fast path must hand over at the first non-ASCII byte, mid-run
- "abc\u{00e9}def",
- // a wide glyph is two columns, so a width boundary can land inside it
- "ab\u{4e16}\u{754c}cd",
- // a cluster the fast path must not split
- "a\u{0301}bc",
- // tabs and controls are excluded from the fast path by the range test
- "ab\tcd",
- "ab\rcd",
- // an ASCII byte followed by a continuation byte is NOT its own cluster
- "e\u{0301}x",
- "\u{1f1e6}\u{1f1e7}ok",
- };
- for (cases) |text| {
- var width: usize = 0;
- while (width <= text.len + 3) : (width += 1) {
- var start: usize = 0;
- while (start <= text.len) : (start += 1) {
- try std.testing.expectEqual(
- reference(text, start, width),
- fitEnd(text, start, width),
- );
- }
- }
- }
-}
-
pub const lineCount = modal.cursorLineCount;
// Content changes invalidate this lazily rebuilt byte-offset index.
@@ -668,44 +507,6 @@ pub fn shiftStarts(room: []usize, len: usize, n: usize, off: usize, cut: usize,
}
}
-test "a line index shifted in place equals a rebuilt one, and the edit turned round gives the one before back" {
- const gpa = std.testing.allocator;
- var prng = std.Random.DefaultPrng.init(11);
- const r = prng.random();
- var text: std.ArrayList(u8) = .empty;
- defer text.deinit(gpa);
- for (0..300) |_| try text.append(gpa, "ab\n"[r.uintLessThan(usize, 3)]);
- var f: State = .{ .path = &.{}, .content = text.items, .history = undefined };
- _ = try lineIndex(gpa, &f);
- defer gpa.free(indexRoom(&f));
- for (0..2000) |round| {
- const at = r.uintAtMost(usize, text.items.len);
- const del = r.uintAtMost(usize, @min(12, text.items.len - at));
- var ins: [12]u8 = undefined;
- const ins_len = r.uintAtMost(usize, 12);
- for (ins[0..ins_len]) |*c| c.* = "ab\n"[r.uintLessThan(usize, 3)];
- const before = try gpa.dupe(usize, f.line_starts);
- defer gpa.free(before);
- const cut = try gpa.dupe(u8, text.items[at..][0..del]);
- defer gpa.free(cut);
- f.content = text.items;
- shiftIndex(gpa, &f, at, del, ins[0..ins_len]);
- try text.replaceRange(gpa, at, del, ins[0..ins_len]);
- var fresh: State = .{ .path = &.{}, .content = text.items, .history = undefined };
- const want = try lineIndex(gpa, &fresh);
- defer gpa.free(want);
- try std.testing.expectEqualSlices(usize, want, f.line_starts);
- // Every tenth turned round in a copy, as a rollback takes it back.
- if (round % 10 == 0) {
- const room = try gpa.alloc(usize, indexRoom(&f).len);
- defer gpa.free(room);
- @memcpy(room[0..f.line_starts.len], f.line_starts);
- shiftStarts(room, f.line_starts.len, before.len, at, ins_len, cut);
- try std.testing.expectEqualSlices(usize, before, room[0..before.len]);
- }
- }
-}
-
/// line count, O(1) once the index is warm
pub fn nlines(gpa: std.mem.Allocator, f: *State) usize {
const idx = lineIndex(gpa, f) catch return lineCount(f.content);
@@ -760,28 +561,6 @@ pub fn textLine(t: *Text, text: []const u8, row: usize) []const u8 {
return text[start..end];
}
-test "indexed text rows preserve fragment and allocation failure semantics" {
- var allocator = std.testing.FailingAllocator.init(std.testing.allocator, .{});
- const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true });
- defer p.deinit();
- const pane = try p.setTestFile("first\n\tλ界\n\nlast\n");
- const text = pane.file.?.content;
- allocator.fail_index = allocator.alloc_index;
- for (0..lineCount(text) + 2) |row| try std.testing.expectEqualStrings(modal.lineSlice(text, row), textLine(&pane.body, text, row));
- try std.testing.expectEqual(@as(usize, 0), pane.file.?.line_starts.len);
- allocator.fail_index = std.math.maxInt(usize);
- _ = try lineIndex(p.gpa, &pane.file.?);
- allocator.fail_index = allocator.alloc_index;
- const allocations = allocator.allocations;
- for ([_][]const u8{ text, text[2..], text[0..8], "other\nrows\n\n", "" }) |fragment| {
- for (0..lineCount(fragment) + 2) |row| try std.testing.expectEqualStrings(
- modal.lineSlice(fragment, row),
- textLine(&pane.body, fragment, row),
- );
- }
- try std.testing.expectEqual(allocations, allocator.allocations);
-}
-
pub fn textLineCount(t: *Text, text: []const u8) usize {
const index = contentIndex(t, text) orelse return modal.cursorLineCount(text);
return index.len;
@@ -1131,29 +910,6 @@ pub fn spanOf(old: []const u8, new: []const u8) Span {
return .{ .head = head, .tail = tail };
}
-test "spanOf by blocks finds the byte loop's ends" {
- var prng: std.Random.DefaultPrng = .init(5);
- const random = prng.random();
- var a: [160]u8 = undefined;
- var b: [160]u8 = undefined;
- for (0..4000) |_| {
- const la = random.uintLessThan(usize, a.len);
- const lb = random.uintLessThan(usize, b.len);
- for (a[0..la]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)];
- const keep = @min(la, lb);
- @memcpy(b[0..keep], a[0..keep]);
- for (b[keep..lb]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)];
- if (lb > 0 and random.boolean()) b[random.uintLessThan(usize, lb)] ^= 1;
- const old = a[0..la];
- const new = b[0..lb];
- const n = @min(old.len, new.len);
- const head = std.mem.indexOfDiff(u8, old[0..n], new[0..n]) orelse n;
- var tail: usize = 0;
- while (tail < n - head and old[old.len - 1 - tail] == new[new.len - 1 - tail]) tail += 1;
- try std.testing.expectEqual(Span{ .head = head, .tail = tail }, spanOf(old, new));
- }
-}
-
fn positionIn(gpa: std.mem.Allocator, f: *State, at: usize) struct { row: usize, col: usize } {
const index = lineIndex(gpa, f) catch {
const c = modal.positionAt(f.content, at);
@@ -1475,27 +1231,6 @@ pub fn looksLikeDiff(head: []const u8) bool {
return diff.looksLikeDiff(lines[0..n]);
}
-test "a +Pager pane holding a diff is drawn and looked at as one; holding other text, as text" {
- const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
- defer p.deinit();
- const pane = try p.setTestFile("");
- const f = &pane.file.?;
- p.gpa.free(f.path);
- f.path = try p.gpa.dupe(u8, "/tmp/+Pager");
- f.pager = true;
- setContent(p, f, try p.gpa.dupe(u8, "commit abc\n\ndiff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a\n+b\n"));
- try std.testing.expect(pane.colorAlgo() == .diff);
- try std.testing.expect(pardes.look.diffLines(p, pane) != null);
- setContent(p, f, try p.gpa.dupe(u8, "commit abc\n\n a plain log\n"));
- try std.testing.expect(pane.colorAlgo() != .diff);
-}
-
-test "a pager's text is a diff when it holds a diff header or a unified hunk" {
- try std.testing.expect(looksLikeDiff("commit abc\nAuthor: x\n\n msg\n\ndiff --git a/f b/f\n"));
- try std.testing.expect(looksLikeDiff("--- a/f\n+++ b/f\n@@ -1 +1 @@\n-a\n+b\n"));
- try std.testing.expect(!looksLikeDiff("commit abc\nAuthor: x\n\n plain log\n"));
-}
-
pub fn restoreSnap(pane: *Pane, f: *State, snap: Text.Place) void {
const n = nlines(pane.gpa, f);
const row: usize = @min(@as(usize, @intCast(@max(0, snap.cur_row))), n - 1);
@@ -1830,52 +1565,6 @@ fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void {
Stream.paintAhead(p, pane.serial, f.revision, f.path, f.content, start, end);
}
-test "a long line is painted only around the caret" {
- if (!syntax.enabled) return;
- const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
- defer p.deinit();
- const line = try std.testing.allocator.alloc(u8, 600 << 10);
- defer std.testing.allocator.free(line);
- for (line, 0..) |*c, i| c.* = if (i % 2 == 0) '1' else ',';
- const pane = try p.setTestFile(line);
- p.gpa.free(pane.file.?.path);
- pane.file.?.path = try p.gpa.dupe(u8, "/tmp/long.json");
- pane.body.cur_col = 300 << 10;
- var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
- defer arena.deinit();
- _ = try p.render(arena.allocator());
- const f = &pane.file.?;
- try std.testing.expect(f.highlights.len == long_paint);
- try std.testing.expect(f.highlight_start <= 300 << 10 and f.highlight_start + f.highlights.len >= 300 << 10);
-}
-
-test "a caret deep in a long line scrolls the view sideways to it, measured from the view's edge" {
- const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
- defer p.deinit();
- p.settings.wrap = false;
- const line = try std.testing.allocator.alloc(u8, 500 << 10);
- defer std.testing.allocator.free(line);
- @memset(line, 'a');
- const pane = try p.setTestFile(line);
- var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
- defer arena.deinit();
- _ = try p.render(arena.allocator());
- // Right, far: the caret ends a margin in from the right edge.
- pane.body.cur_col = 400_000;
- pane.ensureCursorVisible();
- const w: i32 = @as(i32, pane.cols) - @as(i32, gutterWidth(pane));
- const hmargin: i32 = @min(config.scroll_off, @divTrunc(w - 1, 2));
- try std.testing.expectEqual(@as(i32, 400_000 - (w - 1 - hmargin)), pane.hscroll);
- // Back left of the view: a margin in from the left edge.
- pane.body.cur_col = 100_000;
- pane.ensureCursorVisible();
- try std.testing.expectEqual(@as(i32, 100_000 - hmargin), pane.hscroll);
- // At the start: none.
- pane.body.cur_col = 1;
- pane.ensureCursorVisible();
- try std.testing.expectEqual(@as(i32, 0), pane.hscroll);
-}
-
pub fn supportsContext(pane: *const Pane) bool {
const file = pane.file orelse return false;
return file.output == null and file.mini == null and syntax.supportsPath(file.path);
@@ -1973,14 +1662,13 @@ fn mapRows(pane: *Pane, f: *State) usize {
fn geometryKey(pane: *const Pane, f: *const State) u64 {
const t = &f.tables;
const facts = .{
- f.revision, f.content.len, f.scroll, pane.hscroll,
- pane.rows, pane.cols, pane.tag_rows, pane.collapsed,
- pane.query != null, f.pager, f.mini != null, f.output != null,
- if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context_revision orelse 0,
- f.context_declarations.len,
- f.line_starts.len, t.revision orelse 0, t.list.items.len, if (t.pending) |r| r.at else std.math.maxInt(usize),
- pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h,
- pane.view.rect_h,
+ f.revision, f.content.len, f.scroll, pane.hscroll,
+ pane.rows, pane.cols, pane.tag_rows, pane.collapsed,
+ pane.query != null, f.pager, f.mini != null, f.output != null,
+ if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context_revision orelse 0,
+ f.context_declarations.len, f.line_starts.len, t.revision orelse 0, t.list.items.len,
+ if (t.pending) |r| r.at else std.math.maxInt(usize), pane.view.wrap, pane.view.layered, pane.view.body_h,
+ pane.view.tagline_h, pane.view.rect_h,
};
// One hash of them laid out as words: every reader of the map asks for
// this key, a hundred times a frame.
@@ -2557,6 +2245,317 @@ pub fn paintWordSelection(
}
}
+test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width" {
+ try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xeee\xcc\x81"));
+ try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\xee\xffe"));
+ try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\u{4e16}\x80"));
+ // The whole path the 9P fuzzer took: a body holding it, fitted to a width.
+ const text = "\xeee\xcc\x81";
+ try std.testing.expectEqual(@as(usize, 4), fitEnd(text, 0, 10));
+}
+
+test "indexed source rows match uncached scans across content changes" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
+ defer p.deinit();
+ const pane = try p.setTestFile("");
+ const file = &pane.file.?;
+ for ([_][]const u8{ "", "one", "one\n", "a\r\nλ界\nlast", "\n\n", "\tλ e\u{301}\n\r\n" }) |content| {
+ setContent(p, file, try p.gpa.dupe(u8, content));
+ // An edit carries a built index forward; it must equal a fresh one.
+ if (file.line_starts.len > 0) {
+ const carried = file.line_starts;
+ const room = indexRoom(file);
+ defer p.gpa.free(room);
+ file.line_starts = &.{};
+ try std.testing.expectEqualSlices(usize, try lineIndex(p.gpa, file), carried);
+ p.gpa.free(file.line_starts);
+ file.line_starts = &.{};
+ }
+ const rows = lineCount(content) + 2;
+ for ([_]bool{ false, true }) |indexed| {
+ if (indexed) _ = try lineIndex(p.gpa, file);
+ try std.testing.expectEqualStrings("", sourceLine(pane, -1));
+ for (0..rows) |row| try std.testing.expectEqualStrings(
+ modal.lineSlice(content, row),
+ sourceLine(pane, @intCast(row)),
+ );
+ if (!indexed) try std.testing.expectEqual(@as(usize, 0), file.line_starts.len);
+ }
+ }
+}
+
+test "the ASCII arm of graphemeDisplayWidth matches the gwidth it skips" {
+ const ref = struct {
+ fn width(grapheme: []const u8) usize {
+ if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now;
+ return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode)));
+ }
+ }.width;
+
+ var one: [1]u8 = undefined;
+ var b: u8 = 0;
+ while (b < 0x80) : (b += 1) {
+ one[0] = b;
+ try std.testing.expectEqual(ref(one[0..1]), graphemeDisplayWidth(one[0..1]));
+ }
+ for ([_][]const u8{
+ "e\u{301}", "a\u{903}", "1\u{fe0f}\u{20e3}", "\u{4e16}",
+ "\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff",
+ }) |g| try std.testing.expectEqual(ref(g), graphemeDisplayWidth(g));
+ // Two invalid bytes are two cells, as surface.zig draws them, where
+ // vaxis's own count gives the pair one.
+ try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xe4\xb8"));
+}
+
+test "the ASCII run in fitEnd survives an exhaustive byte sweep" {
+ const reference = struct {
+ fn fitEnd(text: []const u8, start: usize, width: usize) usize {
+ var end = start;
+ var used: usize = 0;
+ while (end < text.len) {
+ const next_end = modal.nextGrapheme(text, end);
+ const next_used = used +| graphemeDisplayWidth(text[end..next_end]);
+ if (next_used > width) return if (end == start) next_end else end;
+ used = next_used;
+ end = next_end;
+ }
+ return end;
+ }
+ }.fitEnd;
+
+ const neighbours = [_][]const u8{
+ "", "z", "\u{301}", "\u{200d}\u{1f680}",
+ "\u{903}", "\u{fe0f}", "\u{4e16}", "\u{1f642}",
+ "\u{1f1e6}\u{1f1e7}", "\xff", "\xe4\xb8", "\xe4\x28\xb8",
+ };
+ var buf: [16]u8 = undefined;
+ // The same pair again behind an ASCII prefix, so a break can land exactly at the run boundary
+ // as well as before it and inside the multi-byte cluster that follows it.
+ var prefixed: [18]u8 = undefined;
+ prefixed[0] = 'a';
+ prefixed[1] = 'b';
+ var b: u8 = 0;
+ while (b < 0x80) : (b += 1) {
+ buf[0] = b;
+ for (neighbours) |tail| {
+ @memcpy(buf[1..][0..tail.len], tail);
+ const pair = buf[0 .. 1 + tail.len];
+ @memcpy(prefixed[2..][0..pair.len], pair);
+ for ([_][]const u8{ pair, prefixed[0 .. 2 + pair.len] }) |text| {
+ var width: usize = 0;
+ while (width <= text.len + 3) : (width += 1) {
+ var start: usize = 0;
+ while (start <= text.len) : (start += 1) {
+ std.testing.expectEqual(
+ reference(text, start, width),
+ fitEnd(text, start, width),
+ ) catch |e| {
+ std.debug.print("fitEnd({any}, {d}, {d})\n", .{ text, start, width });
+ return e;
+ };
+ }
+ }
+ }
+ }
+ }
+}
+
+test "the ASCII run in fitEnd cuts where the grapheme walk would" {
+ const reference = struct {
+ fn fitEnd(text: []const u8, start: usize, width: usize) usize {
+ var end = start;
+ var used: usize = 0;
+ while (end < text.len) {
+ const next_end = modal.nextGrapheme(text, end);
+ const next_used = used +| graphemeDisplayWidth(text[end..next_end]);
+ if (next_used > width) return if (end == start) next_end else end;
+ used = next_used;
+ end = next_end;
+ }
+ return end;
+ }
+ }.fitEnd;
+
+ const cases = [_][]const u8{
+ "",
+ "hello world",
+ // the fast path must hand over at the first non-ASCII byte, mid-run
+ "abc\u{00e9}def",
+ // a wide glyph is two columns, so a width boundary can land inside it
+ "ab\u{4e16}\u{754c}cd",
+ // a cluster the fast path must not split
+ "a\u{0301}bc",
+ // tabs and controls are excluded from the fast path by the range test
+ "ab\tcd",
+ "ab\rcd",
+ // an ASCII byte followed by a continuation byte is NOT its own cluster
+ "e\u{0301}x",
+ "\u{1f1e6}\u{1f1e7}ok",
+ };
+ for (cases) |text| {
+ var width: usize = 0;
+ while (width <= text.len + 3) : (width += 1) {
+ var start: usize = 0;
+ while (start <= text.len) : (start += 1) {
+ try std.testing.expectEqual(
+ reference(text, start, width),
+ fitEnd(text, start, width),
+ );
+ }
+ }
+ }
+}
+
+test "a line index shifted in place equals a rebuilt one, and the edit turned round gives the one before back" {
+ const gpa = std.testing.allocator;
+ var prng = std.Random.DefaultPrng.init(11);
+ const r = prng.random();
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(gpa);
+ for (0..300) |_| try text.append(gpa, "ab\n"[r.uintLessThan(usize, 3)]);
+ var f: State = .{ .path = &.{}, .content = text.items, .history = undefined };
+ _ = try lineIndex(gpa, &f);
+ defer gpa.free(indexRoom(&f));
+ for (0..2000) |round| {
+ const at = r.uintAtMost(usize, text.items.len);
+ const del = r.uintAtMost(usize, @min(12, text.items.len - at));
+ var ins: [12]u8 = undefined;
+ const ins_len = r.uintAtMost(usize, 12);
+ for (ins[0..ins_len]) |*c| c.* = "ab\n"[r.uintLessThan(usize, 3)];
+ const before = try gpa.dupe(usize, f.line_starts);
+ defer gpa.free(before);
+ const cut = try gpa.dupe(u8, text.items[at..][0..del]);
+ defer gpa.free(cut);
+ f.content = text.items;
+ shiftIndex(gpa, &f, at, del, ins[0..ins_len]);
+ try text.replaceRange(gpa, at, del, ins[0..ins_len]);
+ var fresh: State = .{ .path = &.{}, .content = text.items, .history = undefined };
+ const want = try lineIndex(gpa, &fresh);
+ defer gpa.free(want);
+ try std.testing.expectEqualSlices(usize, want, f.line_starts);
+ // Every tenth turned round in a copy, as a rollback takes it back.
+ if (round % 10 == 0) {
+ const room = try gpa.alloc(usize, indexRoom(&f).len);
+ defer gpa.free(room);
+ @memcpy(room[0..f.line_starts.len], f.line_starts);
+ shiftStarts(room, f.line_starts.len, before.len, at, ins_len, cut);
+ try std.testing.expectEqualSlices(usize, before, room[0..before.len]);
+ }
+ }
+}
+
+test "indexed text rows preserve fragment and allocation failure semantics" {
+ var allocator = std.testing.FailingAllocator.init(std.testing.allocator, .{});
+ const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true });
+ defer p.deinit();
+ const pane = try p.setTestFile("first\n\tλ界\n\nlast\n");
+ const text = pane.file.?.content;
+ allocator.fail_index = allocator.alloc_index;
+ for (0..lineCount(text) + 2) |row| try std.testing.expectEqualStrings(modal.lineSlice(text, row), textLine(&pane.body, text, row));
+ try std.testing.expectEqual(@as(usize, 0), pane.file.?.line_starts.len);
+ allocator.fail_index = std.math.maxInt(usize);
+ _ = try lineIndex(p.gpa, &pane.file.?);
+ allocator.fail_index = allocator.alloc_index;
+ const allocations = allocator.allocations;
+ for ([_][]const u8{ text, text[2..], text[0..8], "other\nrows\n\n", "" }) |fragment| {
+ for (0..lineCount(fragment) + 2) |row| try std.testing.expectEqualStrings(
+ modal.lineSlice(fragment, row),
+ textLine(&pane.body, fragment, row),
+ );
+ }
+ try std.testing.expectEqual(allocations, allocator.allocations);
+}
+
+test "spanOf by blocks finds the byte loop's ends" {
+ var prng: std.Random.DefaultPrng = .init(5);
+ const random = prng.random();
+ var a: [160]u8 = undefined;
+ var b: [160]u8 = undefined;
+ for (0..4000) |_| {
+ const la = random.uintLessThan(usize, a.len);
+ const lb = random.uintLessThan(usize, b.len);
+ for (a[0..la]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)];
+ const keep = @min(la, lb);
+ @memcpy(b[0..keep], a[0..keep]);
+ for (b[keep..lb]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)];
+ if (lb > 0 and random.boolean()) b[random.uintLessThan(usize, lb)] ^= 1;
+ const old = a[0..la];
+ const new = b[0..lb];
+ const n = @min(old.len, new.len);
+ const head = std.mem.indexOfDiff(u8, old[0..n], new[0..n]) orelse n;
+ var tail: usize = 0;
+ while (tail < n - head and old[old.len - 1 - tail] == new[new.len - 1 - tail]) tail += 1;
+ try std.testing.expectEqual(Span{ .head = head, .tail = tail }, spanOf(old, new));
+ }
+}
+
+test "a +Pager pane holding a diff is drawn and looked at as one; holding other text, as text" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
+ defer p.deinit();
+ const pane = try p.setTestFile("");
+ const f = &pane.file.?;
+ p.gpa.free(f.path);
+ f.path = try p.gpa.dupe(u8, "/tmp/+Pager");
+ f.pager = true;
+ setContent(p, f, try p.gpa.dupe(u8, "commit abc\n\ndiff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a\n+b\n"));
+ try std.testing.expect(pane.colorAlgo() == .diff);
+ try std.testing.expect(pardes.look.diffLines(p, pane) != null);
+ setContent(p, f, try p.gpa.dupe(u8, "commit abc\n\n a plain log\n"));
+ try std.testing.expect(pane.colorAlgo() != .diff);
+}
+
+test "a pager's text is a diff when it holds a diff header or a unified hunk" {
+ try std.testing.expect(looksLikeDiff("commit abc\nAuthor: x\n\n msg\n\ndiff --git a/f b/f\n"));
+ try std.testing.expect(looksLikeDiff("--- a/f\n+++ b/f\n@@ -1 +1 @@\n-a\n+b\n"));
+ try std.testing.expect(!looksLikeDiff("commit abc\nAuthor: x\n\n plain log\n"));
+}
+
+test "a long line is painted only around the caret" {
+ if (!syntax.enabled) return;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ const line = try std.testing.allocator.alloc(u8, 600 << 10);
+ defer std.testing.allocator.free(line);
+ for (line, 0..) |*c, i| c.* = if (i % 2 == 0) '1' else ',';
+ const pane = try p.setTestFile(line);
+ p.gpa.free(pane.file.?.path);
+ pane.file.?.path = try p.gpa.dupe(u8, "/tmp/long.json");
+ pane.body.cur_col = 300 << 10;
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ _ = try p.render(arena.allocator());
+ const f = &pane.file.?;
+ try std.testing.expect(f.highlights.len == long_paint);
+ try std.testing.expect(f.highlight_start <= 300 << 10 and f.highlight_start + f.highlights.len >= 300 << 10);
+}
+
+test "a caret deep in a long line scrolls the view sideways to it, measured from the view's edge" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ p.settings.wrap = false;
+ const line = try std.testing.allocator.alloc(u8, 500 << 10);
+ defer std.testing.allocator.free(line);
+ @memset(line, 'a');
+ const pane = try p.setTestFile(line);
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ _ = try p.render(arena.allocator());
+ // Right, far: the caret ends a margin in from the right edge.
+ pane.body.cur_col = 400_000;
+ pane.ensureCursorVisible();
+ const w: i32 = @as(i32, pane.cols) - @as(i32, gutterWidth(pane));
+ const hmargin: i32 = @min(config.scroll_off, @divTrunc(w - 1, 2));
+ try std.testing.expectEqual(@as(i32, 400_000 - (w - 1 - hmargin)), pane.hscroll);
+ // Back left of the view: a margin in from the left edge.
+ pane.body.cur_col = 100_000;
+ pane.ensureCursorVisible();
+ try std.testing.expectEqual(@as(i32, 100_000 - hmargin), pane.hscroll);
+ // At the start: none.
+ pane.body.cur_col = 1;
+ pane.ensureCursorVisible();
+ try std.testing.expectEqual(@as(i32, 0), pane.hscroll);
+}
+
test "undo and redo walk back and forth through every step of random edits, joined entries and all" {
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });