From 7b41b89dfb89e1a670baa50e14a892a9de0fa710 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:54:09 -0300 Subject: Text, normal: verticalMove and visualMove are one verticalMove(width): width 0 (j/k, Ctrl-d/u) moves by lines in one step, a wrap width (gj/gk) row by row, and the rows are read through the file's line index Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Text.zig | 51 +++++++++++++-------------------------------------- 1 file changed, 13 insertions(+), 38 deletions(-) (limited to 'src/Text.zig') diff --git a/src/Text.zig b/src/Text.zig index 7801d364..594307a8 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -531,43 +531,15 @@ fn filePane(t: *Text, text: []const u8) ?*const Pane { return if (f.content.ptr == text.ptr and f.content.len == text.len) fp else null; } -/// j/k and friends: sticky goal column, clamped onto short lines' newline -pub fn verticalMove(t: *Text, text: []const u8, range: modal.Selection, down: bool, cnt: usize) void { - const cur = modal.selectionCursor(text, range); - const pos = File.textPosition(t, text, cur); - const shown = filePane(t, text); - const goal: usize = if (t.sticky_col >= 0) - @intCast(t.sticky_col) - else if (shown) |fp| - @intCast(@max(0, File.displayOffset(fp, @intCast(pos.row), 0, @intCast(pos.col)))) - else - File.rawDisplayCol(File.textLine(t, text, pos.row), pos.col); - const last_row = File.textLineCount(t, text) - 1; - const nline = if (down) @min(pos.row + @max(1, cnt), last_row) else pos.row -| @max(1, cnt); - const target_col: usize = if (shown) |fp| - @intCast(File.byteAtRowDisplay(fp, @intCast(nline), 0, @intCast(@min(goal, std.math.maxInt(i32))))) - else - File.rawAtDisplay(File.textLine(t, text, nline), goal); - const target = File.textOffset(t, text, .{ .row = nline, .col = target_col }); - // extend mode never walks onto the empty trailing line (helix) - if (t.select and target == text.len and text.len > 0 and text[text.len - 1] == '\n') return; - setRange(t, text, 0, modal.moveSelectionCursor(text, range, target, t.select), false); - t.sticky_col = @intCast(goal); -} - -pub fn visualMove( - t: *Text, - text: []const u8, - range: modal.Selection, - down: bool, - cnt: usize, - width: usize, -) void { +/// j/k (`width` 0: a line is one row) and gj/gk (the rows a body `width` +/// cells wide wraps a line into): sticky goal column, clamped onto short +/// lines' newline. +pub fn verticalMove(t: *Text, text: []const u8, range: modal.Selection, down: bool, cnt: usize, width: usize) void { const cur = modal.selectionCursor(text, range); const pos = File.textPosition(t, text, cur); const last_row = File.textLineCount(t, text) - 1; var row = pos.row; - var line = modal.lineSlice(text, row); + var line = File.textLine(t, text, row); var vrow = File.visualRow(line, pos.col, width); const shown = filePane(t, text); const goal: usize = if (t.sticky_col >= 0) @@ -576,8 +548,11 @@ pub fn visualMove( @intCast(@max(0, File.displayOffset(fp, @intCast(row), @intCast(vrow.start), @intCast(pos.col)))) else File.rawDisplayCol(line[vrow.start..vrow.end], pos.col -| vrow.start); - var steps = @max(1, cnt); - while (steps > 0) : (steps -= 1) { + if (width == 0) { + row = if (down) @min(pos.row + @max(1, cnt), last_row) else pos.row -| @max(1, cnt); + line = File.textLine(t, text, row); + vrow = .{ .start = 0, .end = line.len }; + } else for (0..@max(1, cnt)) |_| { if (down) { if (vrow.end < line.len) { vrow = File.visualRow(line, vrow.end, width); @@ -585,7 +560,7 @@ pub fn visualMove( } if (row == last_row) break; row += 1; - line = modal.lineSlice(text, row); + line = File.textLine(t, text, row); vrow = File.visualRow(line, 0, width); } else { if (vrow.start > 0) { @@ -594,7 +569,7 @@ pub fn visualMove( } if (row == 0) break; row -= 1; - line = modal.lineSlice(text, row); + line = File.textLine(t, text, row); vrow = File.visualRow(line, line.len, width); } } @@ -620,7 +595,7 @@ pub fn halfPageMove(t: *Text, text: []const u8, range: modal.Selection, down: bo 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(t, text, range, down, @intCast(half)); + verticalMove(t, text, range, down, @intCast(half), 0); } /// helix `scroll` without cursor sync (Ctrl-f/b, PgUp/PgDn, zj/zk): shift -- cgit v1.3