summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/File.zig2
-rw-r--r--src/Tables.zig41
2 files changed, 36 insertions, 7 deletions
diff --git a/src/File.zig b/src/File.zig
index 5db04443..cf44c69a 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -588,7 +588,7 @@ test "indexed source rows match uncached scans across content changes" {
/// through here, so the drawn row, the caret and a click cannot disagree.
pub fn rowPads(pane: *const Pane, row: i32, out: *[Tables.max_pads]Tables.Pad) []const Tables.Pad {
const table = rowTable(pane, row) orelse return &.{};
- return pane.file.?.tables.padsOf(table, sourceLine(pane, row), out);
+ return pane.file.?.tables.padsOf(table, @intCast(row), sourceLine(pane, row), out);
}
/// The table `row` is laid out in, in this pane (`rowPads`).
diff --git a/src/Tables.zig b/src/Tables.zig
index 1dd296f7..2e65a716 100644
--- a/src/Tables.zig
+++ b/src/Tables.zig
@@ -594,7 +594,7 @@ fn add(t: *Tables, gpa: std.mem.Allocator, content: []const u8, header: Line, de
var buf: [max_pads]Pad = undefined;
while (row) |r| : (row = if (r.n == last.n) null else nextLine(content, r)) {
var width = widthOf(r.text);
- for (t.padsOf(&table, r.text, &buf)) |pad| width += pad.cells;
+ for (t.padsOf(&table, r.n, r.text, &buf)) |pad| width += pad.cells;
table.width = @max(table.width, @as(u32, @intCast(@min(width, std.math.maxInt(u32)))));
}
try t.list.append(gpa, table);
@@ -604,7 +604,7 @@ fn add(t: *Tables, gpa: std.mem.Allocator, content: []const u8, header: Line, de
/// The pads of one of `table`'s rows, by byte, in order. No pad is ever at
/// byte 0: a row is drawn from its first byte, so there is no cell before
/// it to give.
-pub fn padsOf(t: *const Tables, table: *const Table, text: []const u8, out: *[max_pads]Pad) []Pad {
+pub fn padsOf(t: *const Tables, table: *const Table, line: usize, text: []const u8, out: *[max_pads]Pad) []Pad {
const pipes = Pipes.of(text);
var n: usize = 0;
if (pipes.leading and pipes.at[0] > 0) {
@@ -625,6 +625,17 @@ pub fn padsOf(t: *const Tables, table: *const Table, text: []const u8, out: *[ma
// The first cell with no leading pipe starts at byte 0: it pads at
// its end, whatever its alignment.
const how: Align = if (from == 0) .left else t.aligns.items[table.col0 + k];
+ // The delimiter row's cells keep their colons at their edges: the
+ // rule is drawn long between them (a pad before a closing `:`).
+ if (line == table.first + 1) {
+ var last = stop;
+ while (last > from and (text[last - 1] == ' ' or text[last - 1] == '\t')) last -= 1;
+ const at = if (last > from and text[last - 1] == ':') last - 1 else stop;
+ if (at == 0) continue;
+ out[n] = .{ .at = @intCast(at), .cells = missing };
+ n += 1;
+ continue;
+ }
const before: u16 = switch (how) {
.left => 0,
.right => missing,
@@ -717,7 +728,7 @@ pub fn paint(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw:
const table = File.rowTable(pane, w.line) orelse continue;
const text = File.sourceLine(pane, w.line);
var buf: [max_pads]Pad = undefined;
- const pads = f.tables.padsOf(table, text, &buf);
+ const pads = f.tables.padsOf(table, @intCast(w.line), text, &buf);
const pipes = Pipes.of(text);
const delimiter = w.line == table.first + 1;
const header = w.line == table.first;
@@ -818,11 +829,11 @@ test "tables: a pipe table's columns, alignments and pads, by line" {
const fig = "| fig || |";
try testing.expectEqualSlices(Pad, &.{
.{ .at = 6, .cells = 2 }, .{ .at = 7, .cells = 5 }, .{ .at = 8, .cells = 8 }, .{ .at = 9, .cells = 8 },
- }, t.padsOf(&table, fig, &buf));
+ }, t.padsOf(&table, 6, fig, &buf));
// `| apple | 3 | red |`: right-aligned 3 pads before its first byte.
try testing.expectEqualSlices(Pad, &.{
.{ .at = 9, .cells = 2 }, .{ .at = 13, .cells = 6 }, .{ .at = 18, .cells = 6 },
- }, t.padsOf(&table, "| apple | 3 | red |", &buf));
+ }, t.padsOf(&table, 4, "| apple | 3 | red |", &buf));
// Every row laid out is one width: the table's.
try testing.expectEqual(@as(u32, 1 + 7 + 1 + 5 + 1 + 17 + 1), table.width);
// Not tables: a fence's rows, counts that differ, a delimiter alone.
@@ -847,7 +858,7 @@ test "tables: every byte's cell maps back to it, and every pad cell to the byte
while (lines.next()) |text| : (n += 1) {
if (n < table.first or n >= table.end) continue;
var buf: [max_pads]Pad = undefined;
- const pads = t.padsOf(table, text, &buf);
+ const pads = t.padsOf(table, n, text, &buf);
var col: usize = 0;
for (text, 0..) |_, b| {
// The pad before b: each of its cells is b's.
@@ -1054,6 +1065,24 @@ test "tables: a big text's tables are read a frame's budget at a time from the t
}
}
+test "tables: a table wider than a wrapping pane is shown as its source, laid out again with Wrap off" {
+ const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 34, .rows = 24 });
+ defer p.deinit();
+ var frame = std.heap.ArenaAllocator.init(testing.allocator);
+ defer frame.deinit();
+ const pane = try testPane(p, sample, frame.allocator());
+ try testing.expect(pane.body_wrapped);
+ try testing.expect(File.wrapWidth(pane, true) < 34);
+ try testing.expect(File.rowTable(pane, 2) == null);
+ // Source: a pipe is a pipe, at its source column.
+ try testing.expectEqual(@as(i32, 7), File.displayOffset(pane, 5, 0, 7));
+ try testing.expect(p.executeBuiltinLine(0, "Wrap off"));
+ _ = frame.reset(.retain_capacity);
+ _ = try p.render(frame.allocator());
+ try testing.expect(!pane.body_wrapped);
+ try testing.expect(File.rowTable(pane, 2) != null);
+}
+
test "zz tables timings on a big markdown file (TABLES_TIMES=<path>)" {
const path_z = std.c.getenv("TABLES_TIMES") orelse return error.SkipZigTest;
const path = std.mem.span(path_z);