refactor: use an enum to select select_line_at_cursor mode

This commit is contained in:
CJ van den Berg 2025-10-30 09:38:28 +01:00
parent 23ea7333a7
commit 62873353b8

View file

@ -2406,7 +2406,7 @@ pub const Editor = struct {
primary.disable_selection(root, self.metrics); primary.disable_selection(root, self.metrics);
self.selection_mode = .line; self.selection_mode = .line;
primary.cursor.move_abs(root, &self.view, @intCast(y), @intCast(x), self.metrics) catch return; primary.cursor.move_abs(root, &self.view, @intCast(y), @intCast(x), self.metrics) catch return;
try self.select_line_at_cursor(root, primary, false); try self.select_line_at_cursor(root, primary, .exclude_eol);
self.selection_drag_initial = primary.selection; self.selection_drag_initial = primary.selection;
self.clamp_mouse(); self.clamp_mouse();
} }
@ -3034,7 +3034,7 @@ pub const Editor = struct {
pub fn delete_line(self: *Self, _: Context) Result { pub fn delete_line(self: *Self, _: Context) Result {
const b = try self.buf_for_update(); const b = try self.buf_for_update();
const primary = self.get_primary(); const primary = self.get_primary();
try self.select_line_at_cursor(b.root, primary, true); try self.select_line_at_cursor(b.root, primary, .include_eol);
const root = try self.delete_selection(b.root, primary, b.allocator); const root = try self.delete_selection(b.root, primary, b.allocator);
try self.update_buf(root); try self.update_buf(root);
self.clamp(); self.clamp();
@ -4112,13 +4112,14 @@ pub const Editor = struct {
return sel; return sel;
} }
fn select_line_at_cursor(self: *Self, root: Buffer.Root, cursel: *CurSel, include_newline: bool) !void { fn select_line_at_cursor(self: *Self, root: Buffer.Root, cursel: *CurSel, mode: enum { include_eol, exclude_eol }) !void {
const sel = try cursel.enable_selection(root, self.metrics); const sel = try cursel.enable_selection(root, self.metrics);
sel.normalize(); sel.normalize();
try move_cursor_begin(root, &sel.begin, self.metrics); try move_cursor_begin(root, &sel.begin, self.metrics);
move_cursor_end(root, &sel.end, self.metrics) catch {}; move_cursor_end(root, &sel.end, self.metrics) catch {};
if (include_newline) { switch (mode) {
move_cursor_right(root, &sel.end, self.metrics) catch {}; // catch{} because may be impossible to get eol (e.g., we're at eof) .include_eol => move_cursor_right(root, &sel.end, self.metrics) catch {}, // catch{} because may be impossible to get eol (e.g., we're at eof)
else => {},
} }
cursel.cursor = sel.end; cursel.cursor = sel.end;
} }