Add document and half-page motions

This commit is contained in:
slhx agent
2026-06-21 21:03:10 +02:00
parent 2277ed281d
commit c98c530c44
8 changed files with 99 additions and 2 deletions
+38
View File
@@ -42,6 +42,8 @@ pub const Command = union(enum) {
replace_char: []const u8,
move_line_start,
move_line_end,
move_document_start,
move_document_end,
};
pub const Snapshot = struct {
@@ -104,6 +106,8 @@ pub const Buffer = struct {
.replace_char => |text| try self.replaceChar(text),
.move_line_start => self.moveLineStart(),
.move_line_end => self.moveLineEnd(),
.move_document_start => self.moveDocumentStart(),
.move_document_end => self.moveDocumentEnd(),
}
}
@@ -263,6 +267,24 @@ pub const Buffer = struct {
self.refreshCellResetPreferred();
}
pub fn moveDocumentStart(self: *Buffer) void {
self.cursor.byte = 0;
self.refreshCellResetPreferred();
}
pub fn moveDocumentEnd(self: *Buffer) void {
if (self.bytes.items.len == 0) {
self.cursor.byte = 0;
self.refreshCellResetPreferred();
return;
}
var at = self.bytes.items.len;
if (at > 0 and self.bytes.items[at - 1] == '\n') at -= 1;
while (at > 0 and self.bytes.items[at - 1] != '\n') at -= 1;
self.cursor.byte = at;
self.refreshCellResetPreferred();
}
const LineRange = struct { start: usize, end: usize };
fn currentLineRange(self: *const Buffer, include_newline: bool) LineRange {
@@ -757,3 +779,19 @@ test "regular: line object selection includes the newline byte" {
const snap = try session.snapshot();
try std.testing.expectEqualStrings("abc\n", snap.bytes[snap.selection.?.anchor..snap.selection.?.cursor]);
}
test "regular: document motions jump to top and bottom line" {
var session = Session.init(std.testing.allocator);
defer session.deinit();
try session.openFixtureAt("one\ntwo\nthree\n", 4);
try session.dispatch(.move_document_end);
var snap = try session.snapshot();
try std.testing.expectEqual(@as(usize, 8), snap.cursor_byte);
try std.testing.expectEqual(@as(usize, 0), snap.cursor_cell);
try session.dispatch(.move_document_start);
snap = try session.snapshot();
try std.testing.expectEqual(@as(usize, 0), snap.cursor_byte);
try std.testing.expectEqual(@as(usize, 0), snap.cursor_cell);
}