diff options
Diffstat (limited to 'core/Band.zig')
| -rw-r--r-- | core/Band.zig | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/core/Band.zig b/core/Band.zig index 9fa204ee..03cdbbda 100644 --- a/core/Band.zig +++ b/core/Band.zig @@ -328,24 +328,31 @@ fn findSpines(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id } /// `rows` wrapped at `width` display columns, word by word, `height` rows -/// at most: a doc's paragraphs. +/// at most: a doc's paragraphs, a line each in its text (builtins.Doc), +/// each starting a row (a newline drawn in a row was a `�`). fn wrap(arena: std.mem.Allocator, tab: u16, rows: []const Row, width: u16, max: u16) ![]const Row { var out: std.ArrayList(Row) = .empty; - for (rows) |row| { - if (File.displayWidth(tab, row.text) <= width) { - try out.append(arena, row); - } else { - var rest = row.text; - while (rest.len > 0 and out.items.len < max) { - var cut = File.byteAtDisplay(tab, rest, width); - if (cut < rest.len) { - if (std.mem.lastIndexOfScalar(u8, rest[0..cut], ' ')) |space| { - if (space > 0) cut = space; + for (rows) |whole| { + var paragraphs = std.mem.splitScalar(u8, whole.text, '\n'); + var row = whole; + while (paragraphs.next()) |text| : (row = .{ .text = "" }) { + if (out.items.len >= max) break; + row.text = text; + if (File.displayWidth(tab, row.text) <= width) { + try out.append(arena, row); + } else { + var rest = row.text; + while (rest.len > 0 and out.items.len < max) { + var cut = File.byteAtDisplay(tab, rest, width); + if (cut < rest.len) { + if (std.mem.lastIndexOfScalar(u8, rest[0..cut], ' ')) |space| { + if (space > 0) cut = space; + } } + if (cut == 0) cut = modal.nextGrapheme(rest, 0); + try out.append(arena, .{ .text = std.mem.trimEnd(u8, rest[0..cut], " ") }); + rest = std.mem.trimStart(u8, rest[cut..], " "); } - if (cut == 0) cut = modal.nextGrapheme(rest, 0); - try out.append(arena, .{ .text = std.mem.trimEnd(u8, rest[0..cut], " ") }); - rest = std.mem.trimStart(u8, rest[cut..], " "); } } if (out.items.len >= max) break; @@ -540,3 +547,23 @@ test "the band's words are read-only text: B2 runs one, the tag back at rest" { try std.testing.expect(p.settings.wrap != wrapped); try std.testing.expect(p.view().prefix == .none and !p.view().tag_state.unrolled); } + +test "a doc's paragraphs are rows of their own: no newline in a band's row" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + _ = try p.setTestFile("one\n"); + const id = p.view().active; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + // SPC b b is Panes, whose documentation has three paragraphs. + for ([_]u21{ ' ', 'b', Key.down }) |c| p.update(.{ .key = .{ .cp = c } }); + const band = (try of(p, a, p.panes[id].?, id)).?; + var docs: usize = 0; + for (band.steps[0..band.nsteps]) |step| for (step.rows) |row| { + try std.testing.expect(std.mem.indexOfScalar(u8, row.text, '\n') == null); + if (step.doc) docs += 1; + }; + try std.testing.expect(docs > 2); +} |
