diff options
Diffstat (limited to 'core/normal.zig')
| -rw-r--r-- | core/normal.zig | 185 |
1 files changed, 171 insertions, 14 deletions
diff --git a/core/normal.zig b/core/normal.zig index 89fd72f6..1f7ce1a7 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; } @@ -946,14 +957,31 @@ fn verticalMove(tab: u16, t: *Text, text: []const u8, range: modal.Selection, do t.sticky_col = @intCast(goal); } -/// Ctrl-d/u: scroll half a page AND move the cursor by the same rows +/// Ctrl-d/u: the view half the body's rows on, and the cursor where it +/// stood on the screen (inside the margins): on the line whose first row is +/// now there, the next one the way it went past a wrapped line's later +/// row, and the rows the view could not move (the text's end) further. +/// None in the body (inside a line taller than it): it stays, off the +/// view, and the view where it went. A terminal's rows are its lines: both +/// move by them. fn halfPageMove(tab: u16, t: *Text, text: []const u8, range: modal.Selection, down: bool) void { // 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 half: i32 = @max(1, @divTrunc(@as(i32, w.rows), 2)); - w.scrollBy(if (down) half else -half); - verticalMove(tab, t, text, range, down, @intCast(half), 0); + if (w.file == null) { + w.scrollBy(tab, if (down) half else -half); + return verticalMove(tab, t, text, range, down, @intCast(half), 0); + } + const body: i64 = File.shownRows(tab, w); + const margin = @min(config.scroll_off, @divTrunc(body - 1, 2)); + const from = File.top(tab, w); + const line: usize = @intCast(@max(0, t.cur_row)); + const on: i64 = if (File.rowsTo(tab, w, from, line, @intCast(body))) |d| d + File.rowOf(tab, w, line, @intCast(@max(0, t.cur_col))) else if (line < from.line) 0 else body - 1; + const moved = File.scrollRows(tab, w, if (down) half else -half); + const to = File.textRowLine(tab, w, on + (if (down) half else -half) - moved, margin, if (down) .down else .up) orelse return; + if (to == t.cur_row) return; + verticalMove(tab, t, text, range, to > t.cur_row, @abs(to - t.cur_row), 0); } /// gt/gb: view-relative rows, col 0, scrolloff clamped (helix goto_window) @@ -961,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 shown: i32 = File.shownRows(tab, w); + const held: i32 = w.context_rows; + 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), @@ -1235,13 +1280,28 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! // helix align_view: the cursor's row goes `above` rows down the // view, the middle of an odd one and the upper middle of an even one .view_top, .view_center, .view_bottom => if (t.what == .body) { - const above: i32 = switch (a.role) { - .view_top => 0, - .view_center => @divTrunc(@as(i32, pane.rows) - 1, 2), - else => @as(i32, pane.rows) - 1, - }; - pane.scrollBy(t.cur_row - (pane.scroll() + above)); - pane.ensureCursorVisible(p.settings.tab); + // In rows: zt shows the cursor's line from its first row, a + // line taller than the body from the cursor's. + const tab = p.settings.tab; + if (pane.file == null) { + const above: i32 = switch (a.role) { + .view_top => 0, + .view_center => @divTrunc(@as(i32, pane.rows) - 1, 2), + else => @as(i32, pane.rows) - 1, + }; + pane.scrollBy(tab, t.cur_row - (pane.scroll() + above)); + } else { + const body: i64 = File.shownRows(tab, pane); + const line: usize = @intCast(@max(0, t.cur_row)); + const row = File.rowOf(tab, pane, line, @intCast(@max(0, t.cur_col))); + const at: File.Top = .{ .line = line, .row = if (a.role == .view_top and row < body) 0 else row }; + File.setTop(pane, File.walkRows(tab, pane, at, switch (a.role) { + .view_top => 0, + .view_center => -@divTrunc(body - 1, 2), + else => -(body - 1), + }).to); + } + pane.ensureCursorVisible(tab); }, // helix align_view_middle: the cursor's column to the middle of an // unwrapped view @@ -1429,6 +1489,103 @@ 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 "gt after a step that moved the view off the place its geometry was made for: the row scroll_off in from the body's top under the context rows the view has now, as a core loaded from the session goes" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer p.deinit(); + const pane = try p.setTestFile("pub const Box = struct {\n pub fn run() void {\n" ++ (std.mem.asBytes(&@as([30][21]u8, @splat(" // body line\n".*)))) ++ " }\n};\n"); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/test.zig"); + if (!File.supportsContext(pane)) return error.SkipZigTest; + pane.file.?.tree_context = true; + // The view inside the function, its two headers held over the body, + // drawn. + File.setTop(pane, .{ .line = 3 }); + p.sync(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try p.render(arena.allocator()); + try std.testing.expectEqual(@as(u16, 2), pane.context_rows); + // Two notches up: the top on the function's header, one header held + // (the geometry the second notch read was the one before it). + const rect = p.view().rects[0]; + for (0..2) |_| p.update(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = rect.x + 10, .row = rect.y + 5 } }); + try std.testing.expectEqual(@as(usize, 1), pane.file.?.scroll); + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }, bytes, null); + defer loaded.deinit(); + for ([_]*Pardes{ p, loaded }) |core| for ("gt") |k| core.update(.{ .key = .{ .cp = k, .text = &[_]u8{k} } }); + try std.testing.expectEqual(loaded.panes[0].?.body.cur_row, pane.body.cur_row); + try std.testing.expectEqual(@as(u16, 1), pane.context_rows); + try std.testing.expectEqual(pane.wrapAt(p.settings.tab, 1 + config.scroll_off).line, pane.body.cur_row); +} + +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 }); |
