refactor: use Cursor.from_pos in PosToWidthCache.from_pos

This commit is contained in:
CJ van den Berg 2025-11-25 14:45:36 +01:00
parent ccc92f3ea6
commit bd976e0894
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -1442,14 +1442,13 @@ pub const Editor = struct {
pos_cache: PosToWidthCache, pos_cache: PosToWidthCache,
last_begin: Cursor = Cursor.invalid(), last_begin: Cursor = Cursor.invalid(),
fn cb(ctx: *@This(), range: syntax.Range, scope: []const u8, id: u32, idx: usize, _: *const syntax.Node) error{Stop}!void { fn cb(ctx: *@This(), range: syntax.Range, scope: []const u8, id: u32, idx: usize, _: *const syntax.Node) error{Stop}!void {
const sel_ = ctx.pos_cache.range_to_selection(range, ctx.root, ctx.self.metrics); var sel = ctx.pos_cache.from_pos(range, ctx.root, ctx.self.metrics);
if (idx > 0) return; if (idx > 0) return;
if (sel_.begin.eql(ctx.last_begin)) return; if (sel.begin.eql(ctx.last_begin)) return;
ctx.last_begin = sel_.begin; ctx.last_begin = sel.begin;
const style_ = style_cache_lookup(ctx.theme, ctx.cache, scope, id); const style_ = style_cache_lookup(ctx.theme, ctx.cache, scope, id);
const style = if (style_) |sty| sty.style else return; const style = if (style_) |sty| sty.style else return;
var sel = sel_;
if (sel.end.row < ctx.self.view.row) return; if (sel.end.row < ctx.self.view.row) return;
if (sel.begin.row > ctx.self.view.row + ctx.self.view.rows) return; if (sel.begin.row > ctx.self.view.row + ctx.self.view.rows) return;
@ -6682,24 +6681,25 @@ pub const PosToWidthCache = struct {
self.cache.deinit(self.allocator); self.cache.deinit(self.allocator);
} }
pub fn range_to_selection(self: *Self, range: syntax.Range, root: Buffer.Root, metrics: Buffer.Metrics) Selection { pub fn from_pos(self: *Self, range: syntax.Range, root: Buffer.Root, metrics: Buffer.Metrics) Selection {
var sel = Selection.from_range_raw(range); var sel = Selection.from_range_raw(range);
if (root != self.cached_root or self.cached_line != sel.begin.row) { if (root != self.cached_root or self.cached_line != sel.begin.row) {
self.cache.clearRetainingCapacity(); self.cache.clearRetainingCapacity();
self.cached_line = sel.begin.row; self.cached_line = sel.begin.row;
self.cached_root = root; self.cached_root = root;
root.get_line_width_map(self.cached_line, &self.cache, self.allocator, metrics) catch return sel; root.get_line_width_map(self.cached_line, &self.cache, self.allocator, metrics) catch
return sel.from_pos(root, metrics);
} }
sel.begin.col = if (sel.begin.col < self.cache.items.len) sel.begin.col = if (sel.begin.col < self.cache.items.len)
self.cache.items[sel.begin.col] self.cache.items[sel.begin.col]
else else
sel.begin.col; sel.begin.from_pos(root, metrics).col;
sel.end.col = if (sel.end.row == sel.end.row and sel.end.col < self.cache.items.len) sel.end.col = if (sel.end.row == sel.end.row and sel.end.col < self.cache.items.len)
self.cache.items[sel.end.col] self.cache.items[sel.end.col]
else else
root.pos_to_width(sel.end.row, sel.end.col, metrics) catch sel.end.col; sel.end.from_pos(root, metrics).col;
return sel; return sel;
} }