refactor: simplify code by using Selection.from_pos

This commit is contained in:
CJ van den Berg 2025-10-02 16:25:48 +02:00
parent 10bd270d89
commit 390fbd51ff
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -179,16 +179,10 @@ pub const CurSel = struct {
} }
fn selection_from_range(range: syntax.Range, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection { fn selection_from_range(range: syntax.Range, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection {
return .{ return Selection.from_pos(.{
.begin = .{ .begin = .{ .row = range.start_point.row, .col = range.start_point.column },
.row = range.start_point.row, .end = .{ .row = range.end_point.row, .col = range.end_point.column },
.col = try root.pos_to_width(range.start_point.row, range.start_point.column, metrics), }, root, metrics);
},
.end = .{
.row = range.end_point.row,
.col = try root.pos_to_width(range.end_point.row, range.end_point.column, metrics),
},
};
} }
pub fn selection_from_node(node: syntax.Node, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection { pub fn selection_from_node(node: syntax.Node, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection {
@ -5446,10 +5440,8 @@ pub const Editor = struct {
if (pos_type == .byte) { if (pos_type == .byte) {
column = root.pos_to_width(line - 1, column - 1, self.metrics) catch return; column = root.pos_to_width(line - 1, column - 1, self.metrics) catch return;
column += 1; column += 1;
if (have_sel) { if (have_sel)
sel.begin.col = root.pos_to_width(sel.begin.row, sel.begin.col, self.metrics) catch return; sel = sel.from_pos(root, self.metrics) catch return;
sel.end.col = root.pos_to_width(sel.end.row, sel.end.col, self.metrics) catch return;
}
// self.logger.print("goto_byte_pos: l:{d} c:{d} {any} {}", .{ line, column, sel, pos_type }); // self.logger.print("goto_byte_pos: l:{d} c:{d} {any} {}", .{ line, column, sel, pos_type });
} }
const primary = self.get_primary(); const primary = self.get_primary();
@ -5549,16 +5541,7 @@ pub const Editor = struct {
.push => try self.push_cursor(), .push => try self.push_cursor(),
} }
const root = self.buf_root() catch return; const root = self.buf_root() catch return;
const sel: Selection = .{ const sel = try sel_.from_pos(root, self.metrics);
.begin = .{
.row = sel_.begin.row,
.col = try root.pos_to_width(sel_.begin.row, sel_.begin.col, self.metrics),
},
.end = .{
.row = sel_.end.row,
.col = try root.pos_to_width(sel_.end.row, sel_.end.col, self.metrics),
},
};
const primary = self.get_primary(); const primary = self.get_primary();
primary.selection = sel; primary.selection = sel;
primary.cursor = sel.end; primary.cursor = sel.end;
@ -5599,18 +5582,16 @@ pub const Editor = struct {
} }
first = false; first = false;
} }
const begin_col = try root.pos_to_width(begin_row, begin_col_pos, self.metrics);
const end_col = try root.pos_to_width(end_row, end_col_pos, self.metrics);
last_begin_row = begin_row; last_begin_row = begin_row;
last_begin_col_pos = begin_col_pos; last_begin_col_pos = begin_col_pos;
last_end_row = end_row; last_end_row = end_row;
last_end_col_pos = end_col_pos; last_end_col_pos = end_col_pos;
const sel: Selection = .{ const sel = try Selection.from_pos(.{
.begin = .{ .row = begin_row, .col = begin_col }, .begin = .{ .row = begin_row, .col = begin_col_pos },
.end = .{ .row = end_row, .col = end_col }, .end = .{ .row = end_row, .col = end_col_pos },
}; }, root, self.metrics);
const primary = self.get_primary(); const primary = self.get_primary();
primary.selection = sel; primary.selection = sel;
primary.cursor = sel.end; primary.cursor = sel.end;
@ -5687,23 +5668,13 @@ pub const Editor = struct {
if (!std.mem.eql(u8, file_path, self.file_path orelse return)) return; if (!std.mem.eql(u8, file_path, self.file_path orelse return)) return;
const root = self.buf_root() catch return; const root = self.buf_root() catch return;
const sel: Selection = .{
.begin = .{
.row = sel_.begin.row,
.col = root.pos_to_width(sel_.begin.row, sel_.begin.col, self.metrics) catch return,
},
.end = .{
.row = sel_.end.row,
.col = root.pos_to_width(sel_.end.row, sel_.end.col, self.metrics) catch return,
},
};
(try self.diagnostics.addOne(self.allocator)).* = .{ (try self.diagnostics.addOne(self.allocator)).* = .{
.source = try self.allocator.dupe(u8, source), .source = try self.allocator.dupe(u8, source),
.code = try self.allocator.dupe(u8, code), .code = try self.allocator.dupe(u8, code),
.message = try self.allocator.dupe(u8, message), .message = try self.allocator.dupe(u8, message),
.severity = severity, .severity = severity,
.sel = sel, .sel = sel_.from_pos(root, self.metrics) catch return,
}; };
switch (Diagnostic.to_severity(severity)) { switch (Diagnostic.to_severity(severity)) {