From defafb5ccce2ae30d3e16cc8fb57fb73485e03f4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 18:26:46 -0300 Subject: scroll: what still moved by lines now counts rows where a wrapped line made it wrong (P62 round 3, item 3): gt/gb land on the row `scroll_off` rows in from the body's top or its last row of text, a wrapped line's later row as any, the cursor on the first byte that row shows (helix's goto_window), where they went `scroll_off` LINES down from the top line (inside a wrapped line: a line far below, the view moved); gw labels the words of the bytes the body shows (`File.shownBytes`: its first row's first to its last row's last), none on a wrapped line's rows above the view's top (a word the first row starts inside of begins above it: its label would not show), where it labelled the words of `rows` lines from the top line's start; a new pane goes below the rows a pane's text takes (layout.zig `blankRows`: the rows from the view's top to the text's end, `File.rowsTo`, a wrapped line's each), where a pane of a few long lines counted a row a line and had the new pane laid over its text; a Look opening a file in a pane of its own puts its line half the body's rows down, a wrapped line's rows above it counted (look.zig, `walkRows`), where it went half the body's LINES up; TESTS FIRST, each failing on item 1's tree (`qwssppvo`) and passing here: core/normal.zig "gt and gb go to the row scroll_off rows in from the body's top and from its bottom, ..." (the view moved, row 4 to 5), "gw labels the words the body shows and no others: ..." (a label above the top), core/layout.zig "a new pane goes below the rows a pane's text takes, ..." (the pane kept fewer rows than its text), core/look.zig "a Look that opens a file in a pane of its own puts its line half the body's rows down, ..." (24 rows above it, not 17); STILL LINES, each right there: Places' first range (its search pieces are whole lines, so a match is never cut at a piece's edge, and `scroll + rows + 1` lines hold every row shown, each line at least one), the frames' `view` (a statistic's label: no decision reads it), the rail's thumb (rows would wrap the whole text at each width), the syntax window (the rows shown lie inside the lines `scroll .. scroll + rows`) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/normal.zig | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 4 deletions(-) (limited to 'core/normal.zig') diff --git a/core/normal.zig b/core/normal.zig index 61f8f26b..fe0c729a 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -339,11 +339,22 @@ fn startJump(p: *Pardes, pane: *Pane, t: *Text) edit.Error!void { const text = try edit.flatSurface(p.editor(), t); const cursor = modal.selectionCursor(text, t.primaryRange(text, 0)); const top: usize = @intCast(@max(0, pane.scroll())); - const start = @min(modal.lineStartOffset(text, top), text.len); - const end = @min(modal.lineStartOffset(text, top + pane.rows), text.len); + // A file's words the body shows, its rows' bytes; a terminal's rows + // are its lines. + const shown = if (pane.file != null) File.shownBytes(p.settings.tab, pane) else File.Bytes{ .start = modal.lineStartOffset(text, top), .end = modal.lineStartOffset(text, top + pane.rows) }; + const start = @min(shown.start, text.len); + const end = @min(shown.end, text.len); const letters = config.jump_label_alphabet.len; try pane.jump.resize(p.gpa, letters * letters); - pane.jump.shrinkRetainingCapacity(modal.jumpWords(text, cursor, start, end, pane.jump.items)); + const n = modal.jumpWords(text, cursor, start, end, pane.jump.items); + // A word the body's first row starts inside of (a wrapped line's row + // cut mid-word) begins above it: no label it would not show. + var kept: usize = 0; + for (pane.jump.items[0..n]) |r| if (@min(r.anchor, r.head) >= start) { + pane.jump.items[kept] = r; + kept += 1; + }; + pane.jump.shrinkRetainingCapacity(kept); pane.jump_first = null; } @@ -978,11 +989,28 @@ fn gotoWindow(tab: u16, t: *Text, text: []const u8, range: modal.Selection, whic // a view to move through is a body's; a tag or an answer is all shown if (t.what != .body) return; const w = t.pane() orelse return; + const n: i32 = @intCast(cnt - 1); + if (w.file != null) { + // In rows, as helix's goto_window: the row `margin + n` in from the + // body's top or its last row of text, inside the margins, the + // cursor on the first byte it shows (a wrapped line's later row). + const held: i32 = w.context_rows; + const shown: i32 = File.shownRows(tab, w); + const lines = File.textLineCount(t, text); + var last: i32 = shown - 1; + while (last > 0 and w.wrapAt(tab, held + last).line >= lines) last -= 1; + const margin: i32 = @min(config.scroll_off, @divTrunc(shown - 1, 2)); + const vr = std.math.clamp(@min(@max(switch (which) { + .top => margin + n, + .bottom => last - (margin + n), + }, margin), last - margin), 0, last); + const at = w.wrapAt(tab, held + vr); + return pointMove(tab, t, text, range, File.textLineStart(t, text, @intCast(at.line)) + if (w.body_wrapped) @as(usize, @intCast(at.at)) else 0); + } const margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, w.rows) - 1, 2)); const top = w.scroll(); const last_row: i32 = @intCast(File.textLineCount(t, text) - 1); const last_vis: i32 = @min(@as(i32, w.rows) - 1, last_row - top); - const n: i32 = @intCast(cnt - 1); var vline: i32 = switch (which) { .top => top + margin + n, .bottom => top + last_vis - (margin + n), @@ -1461,6 +1489,71 @@ test "gj/gk step the wrapped rows a body draws while j/k keep the file's lines" try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); } +/// 10 short lines, line 10 of 200 numbered words (17 rows or more at 60 +/// columns), 40 short lines after it; the view's top four rows into line +/// 10, the cursor on a row the body shows. +fn wrappedView(p: *Pardes) !*Pane { + p.settings.wrap = true; + var text: std.ArrayList(u8) = .empty; + defer text.deinit(p.gpa); + for (0..51) |i| if (i == 10) { + for (0..200) |w| try text.print(p.gpa, "w{d:0>3} ", .{w}); + try text.append(p.gpa, '\n'); + } else try text.print(p.gpa, "line {d:0>2}\n", .{i}); + const pane = try p.setTestFile(text.items); + File.setTop(pane, .{ .line = 10, .row = 4 }); + p.sync(); + const at = pane.wrapAt(p.settings.tab, pane.context_rows + 6); + pane.body.cur_row = at.line; + pane.body.cur_col = at.at; + return pane; +} + +test "gt and gb go to the row scroll_off rows in from the body's top and from its bottom, a wrapped line's later row as any, the cursor on its first byte; the view stays" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 24 }); + defer p.deinit(); + const tab = p.settings.tab; + const pane = try wrappedView(p); + const top = File.top(tab, pane); + try std.testing.expectEqual(@as(u32, 4), top.row); + for ("tb") |k| { + p.update(.{ .key = .{ .cp = 'g', .text = "g" } }); + p.update(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }); + try std.testing.expectEqual(top, File.top(tab, pane)); + const row: i32 = if (k == 't') config.scroll_off else File.shownRows(tab, pane) - 1 - config.scroll_off; + const want = pane.wrapAt(tab, pane.context_rows + row); + try std.testing.expectEqual(want.line, pane.body.cur_row); + try std.testing.expectEqual(want.at, pane.body.cur_col); + } +} + +test "gw labels the words the body shows and no others: none on a wrapped line's rows above the view's top" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 24 }); + defer p.deinit(); + const tab = p.settings.tab; + const pane = try wrappedView(p); + p.update(.{ .key = .{ .cp = 'g', .text = "g" } }); + p.update(.{ .key = .{ .cp = 'w', .text = "w" } }); + try std.testing.expect(pane.jump.items.len > 20); + const f = &pane.file.?; + const shown = File.shownRows(tab, pane); + // Rows with a label: the body's first and its last among them. + var labelled: [2]bool = .{ false, false }; + for (pane.jump.items) |r| { + const at = @min(r.anchor, r.head); + const line = std.mem.count(u8, f.content[0..at], "\n"); + const col = at - File.lineStart(p.gpa, f, line); + // The row that shows its first byte is in the body, at or after + // that row's start (no row above the top). + const row = pane.wrapRow(tab, @intCast(line), @intCast(col)); + try std.testing.expect(row.row >= pane.context_rows and row.row < pane.context_rows + shown); + try std.testing.expect(col >= row.at); + if (row.row == pane.context_rows) labelled[0] = true; + if (row.row == pane.context_rows + shown - 1) labelled[1] = true; + } + try std.testing.expectEqual([2]bool{ true, true }, labelled); +} + test "flat text movement and selection replay need no scratch rows" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); -- cgit v1.3