summaryrefslogtreecommitdiff
path: root/core/body_layer.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-24 22:24:29 -0300
committerGabriel Schneider <[email protected]>2026-11-24 22:24:29 -0300
commit89b3e7365bac28c4cd7d52d40c1786c01db5af52 (patch)
treedcc65675b340f4ba93bfcf8d4edd9c7a4d16dc6c /core/body_layer.zig
parentb44a129a8854483dbe45ce2bcfa09f33ccbd6e8c (diff)
downloadpardes-89b3e7365bac28c4cd7d52d40c1786c01db5af52.tar.gz
pardes-89b3e7365bac28c4cd7d52d40c1786c01db5af52.zip
body_layer: a motion root's level paints its targets in the gutter while it shows: `g` its first and last lines, the window's top and bottom and the last edit (`g e t b .`); `]` the next and `[` the previous function, paragraph, test, comment, change, diagnostic, type and argument (`f p T c g d t a`); browsing `m` unrolled on `a` or `i` tints the brackets' region around the caret (pick-ui SPEC §4.4, walk UI line F item 2): the keys are the level's own (bindings.level, each leaf's role), each target looked for in the rows shown alone and a target off them not painted (the tag's arrows of §15 not built): the tree objects through syntax.objectNext given the shown bytes (QueryCursor.setByteRange; the motion passes null), `]g` through jj.nextChange bounded to the shown lines (`most`; the motion unbounded), `]d` a binary search of the fed diagnostics, `]p` the shown lines, `g t`/`g b` normal.gotoWindow's rows; drawn into the body as the `gw` labels are (R3), never written; at rest one test of the view's tag state (TagState.hostOf); the tree is the one the motion reads (normal.fileTree, made once a revision), not painted while the Changes layer's lines are in the buffer; TEST: body_layer.zig "a motion root's level paints its targets in the gutter ..." (`]` p, `g` g e, `m` on i tinted, rest as before); core-test 1025/1192 (167 skipped), 0 failed, load 9
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'core/body_layer.zig')
-rw-r--r--core/body_layer.zig263
1 files changed, 263 insertions, 0 deletions
diff --git a/core/body_layer.zig b/core/body_layer.zig
index 9c4a4c73..13fc7a85 100644
--- a/core/body_layer.zig
+++ b/core/body_layer.zig
@@ -25,6 +25,9 @@ const tagline = @import("tagline.zig");
const Text = panes.Text;
const Layer = @import("Layer.zig");
+const TagState = @import("TagState.zig");
+const bindings = @import("bindings.zig");
+const File = panes.File;
// ---- drawing a pane body into the surface ----
@@ -468,6 +471,8 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane
}
}
+ if (pane.file != null) paintTargets(p, s, arena, pane, r, tx, tw, body_y, body_h);
+
// `gw`'s labels, two letters over the start of each word
if (pane.jump.items.len > 0) labels: {
const flat = edit.flatSurface(p.editor(), &pane.body) catch break :labels;
@@ -548,6 +553,207 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane
};
}
+/// Painted targets (SPEC §4.4), while a motion root's level shows in this
+/// body's tag: each of its keys at the line it would go to, in the gutter
+/// (`g e t b .` for `g`; for `]` the next and for `[` the previous
+/// function, paragraph, test, comment, change, diagnostic, type, argument:
+/// `f p T c g d t a`); browsing `m a` or `m i` unrolled, the brackets'
+/// region around the caret tinted. Only the rows shown are looked at: a
+/// target off them is not painted (the tag's arrows, SPEC §15, are not
+/// built). At rest it costs one test of the view's tag state.
+fn paintTargets(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, tx: u16, tw: u16, body_y: u16, body_h: u16) void {
+ const host = TagState.hostOf(p) orelse return;
+ if (p.panes[host] != pane) return;
+ const v = p.view();
+ const cur = TagState.current(p, v) orelse return;
+ if (!TagState.shown(p, v)) return;
+ const f = &pane.file.?;
+ if (f.line_starts.len == 0) return;
+ const tab = p.settings.tab;
+ const held = pane.context_rows;
+ if (body_h <= held) return;
+ // The lines the rows show, past the context rows held at the top.
+ const lo: usize = @intCast(@max(0, pane.wrapAt(tab, held).line));
+ var hi = lo;
+ for (held..body_h) |row| {
+ const line = pane.wrapAt(tab, @intCast(row)).line;
+ if (line >= 0 and line < f.line_starts.len) hi = @max(hi, @as(usize, @intCast(line)));
+ }
+ var children: [bindings.max_children]bindings.Child = undefined;
+ const level = bindings.level(p, cur.root, cur.path, &children);
+ if (cur.root == .match) {
+ const st = &v.tag_state;
+ if (!st.unrolled) return;
+ const on = for (level) |c| {
+ if (c.key == st.hi) break c;
+ } else return;
+ const leaf = (on.node orelse return).leaf orelse return;
+ if (leaf != .role or (leaf.role != .match_inside and leaf.role != .match_around)) return;
+ const role = leaf.role;
+ const caret = lineStart(f, @intCast(@max(0, pane.body.cur_row))) + @as(usize, @intCast(@max(0, pane.body.cur_col)));
+ const span = enclosing(f.content, @min(caret, f.content.len), lineStart(f, lo), lineStart(f, hi + 1)) orelse return;
+ const from = if (role == .match_inside) span[0] + 1 else span[0];
+ const to = if (role == .match_inside) span[1] else span[1] + 1;
+ const first = lineOf(f, from);
+ const last = lineOf(f, to -| 1);
+ const rows = arena.alloc(Pane.PointerRow, last - first + 1) catch return;
+ for (rows, first..) |*row, line| row.* = .{
+ .row = @intCast(line),
+ .lo = if (line == first) from - lineStart(f, line) else 0,
+ .hi = if (line == last) to - lineStart(f, line) else File.sourceLine(pane, @intCast(line)).len,
+ .to_edge = line != last,
+ };
+ const th = p.theme();
+ const page = th.bg orelse th.tag_bg;
+ paintSourceSelection(p, s, pane, r, rows, colors.mix(page, colors.mix(page, th.sel_bg)), null);
+ return;
+ }
+ if (cur.root != .goto and cur.root != .next and cur.root != .prev) return;
+ const th = p.theme();
+ const style: CellStyle = .{ .fg = .{ .rgb = p.chromeTheme().box }, .bg = if (th.bg) |c| .{ .rgb = c } else .default, .bold = true };
+ // The rows painted so far: a second key on a row goes after the first.
+ var painted: [bindings.max_children]u16 = undefined;
+ var n: usize = 0;
+ for (level) |c| {
+ const leaf = (c.node orelse continue).leaf orelse continue;
+ if (leaf != .role) continue;
+ const line = targetLine(p, pane, f, leaf.role, cur.root != .prev, lo, hi) orelse continue;
+ if (line < lo or line > hi) continue;
+ var row: u16 = held;
+ while (row < body_h) : (row += 1) {
+ const w = pane.wrapAt(tab, row);
+ if (w.line == line and w.at == 0) break;
+ } else continue;
+ const x: u16 = @intCast(std.mem.count(u16, painted[0..n], &.{row}));
+ if (x >= tw) continue;
+ s.set(tx + x, body_y + row, bindings.keyName(c.key), style);
+ painted[n] = row;
+ n += 1;
+ }
+}
+
+/// The line a motion role would go to from the caret of `pane`, looked for
+/// in lines `lo` to `hi` (the rows shown) alone; null for none there, or a
+/// role that is no line's.
+fn targetLine(p: *Pardes, pane: *Pane, f: *panes.File.State, role: modal.Normal.Role, forward: bool, lo: usize, hi: usize) ?usize {
+ const tab = p.settings.tab;
+ const caret: usize = @intCast(@max(0, pane.body.cur_row));
+ const nlines = f.line_starts.len;
+ switch (role) {
+ .goto_file_start => return 0,
+ .goto_last_line => return if (nlines > 1 and f.line_starts[nlines - 1] == f.content.len) nlines - 2 else nlines - 1,
+ .goto_last_modification => return if (pane.body.last_edit) |at| @intCast(@max(0, at.row)) else null,
+ // helix's goto_window, as normal.gotoWindow counts its rows.
+ .goto_view_top, .goto_view_bottom => {
+ const shown: i32 = File.shownRows(tab, pane);
+ const held: i32 = pane.context_rows;
+ var last: i32 = shown - 1;
+ while (last > 0 and pane.wrapAt(tab, held + last).line >= @as(i32, @intCast(nlines))) last -= 1;
+ const margin: i32 = @min(config.scroll_off, @divTrunc(shown - 1, 2));
+ const vr = std.math.clamp(@min(@max(if (role == .goto_view_top) margin else last - margin, margin), last - margin), 0, last);
+ const line = pane.wrapAt(tab, held + vr).line;
+ return if (line < 0) null else @intCast(line);
+ },
+ // helix's paragraph: past the lines with text, then past the blank
+ // ones (modal.moveParagraph).
+ .goto_paragraph => {
+ var l = caret;
+ if (forward) {
+ while (l <= hi and !blank(pane, l)) l += 1;
+ while (l <= hi and blank(pane, l)) l += 1;
+ return if (l <= hi and l != caret) l else null;
+ }
+ while (l > lo and blank(pane, l - 1)) l -= 1;
+ while (l > lo and !blank(pane, l - 1)) l -= 1;
+ return if (l == caret or (l == lo and lo > 0)) null else l;
+ },
+ .goto_change => return @import("jj.zig").nextChange(p, pane, caret, forward, hi - lo + 1),
+ // Once a Diagnostics list fed them, the pane's places (normal.zig's
+ // `]d`): the first after the caret's line, or the last before it.
+ .goto_diagnostic => {
+ const set = &pane.places;
+ const fed = set.started and set.provenance == .list and if (p.paneBySerial(set.from)) |qid|
+ (if (p.panes[qid].?.query) |q| q.source == .diagnostics else false)
+ else
+ false;
+ if (!fed) return null;
+ const hits = set.hits.items;
+ const edge = if (forward) lineStart(f, caret + 1) else lineStart(f, caret);
+ var a: usize = 0;
+ var b: usize = hits.len;
+ while (a < b) {
+ const m = (a + b) / 2;
+ if (hits[m].start < edge) a = m + 1 else b = m;
+ }
+ if (forward) return if (a < hits.len) lineOf(f, hits[a].start) else null;
+ return if (a > 0) lineOf(f, hits[a - 1].start) else null;
+ },
+ inline .goto_function, .goto_class, .goto_parameter, .goto_comment, .goto_test, .goto_entry, .goto_xml_element => |r| {
+ // The tree as the motion reads it, made once a revision; with
+ // the Changes layer's lines in the buffer its offsets are not the
+ // buffer's: not painted then.
+ if (f.layer.lines.len > 0) return null;
+ const tree = normal.fileTree(p, f) orelse return null;
+ const object = @field(modal.Normal.Object, @tagName(r)["goto_".len..]);
+ const at = lineStart(f, caret) + @as(usize, @intCast(@max(0, pane.body.cur_col)));
+ const span = @import("syntax.zig").objectNext(f.path, tree, f.content, object, forward, @min(at, f.content.len), .{ lineStart(f, lo), lineStart(f, hi + 1) }) orelse return null;
+ return lineOf(f, span.from);
+ },
+ else => return null,
+ }
+}
+
+fn blank(pane: *const Pane, line: usize) bool {
+ return std.mem.trim(u8, File.sourceLine(pane, @intCast(line)), " \t\r").len == 0;
+}
+
+/// Where line `line` starts in a file's buffer; past its last, its end.
+fn lineStart(f: *const panes.File.State, line: usize) usize {
+ return if (line < f.line_starts.len) f.line_starts[line] else f.content.len;
+}
+
+/// The line holding byte `off` of a file's buffer.
+fn lineOf(f: *const panes.File.State, off: usize) usize {
+ var a: usize = 0;
+ var b = f.line_starts.len;
+ while (a + 1 < b) {
+ const m = (a + b) / 2;
+ if (f.line_starts[m] <= off) a = m else b = m;
+ }
+ return a;
+}
+
+/// The innermost bracket pair around byte `at` of `text`, both in bytes
+/// `lo` to `hi` (the rows shown): its opening and its closing byte.
+fn enclosing(text: []const u8, at: usize, lo: usize, hi: usize) ?[2]usize {
+ var depth: usize = 0;
+ var i = at;
+ const open = while (i > lo) {
+ i -= 1;
+ switch (text[i]) {
+ ')', ']', '}' => depth += 1,
+ '(', '[', '{' => if (depth == 0) break i else {
+ depth -= 1;
+ },
+ else => {},
+ }
+ } else return null;
+ const close: u8 = switch (text[open]) {
+ '(' => ')',
+ '[' => ']',
+ else => '}',
+ };
+ depth = 0;
+ i = at;
+ while (i < @min(hi, text.len)) : (i += 1) {
+ if (text[i] == text[open]) depth += 1 else if (text[i] == close) {
+ if (depth == 0) return .{ open, i };
+ depth -= 1;
+ }
+ }
+ return null;
+}
+
pub fn bodyText(tab: u16, arena: std.mem.Allocator, pane: *Pane) ![]const u8 {
if (pane.addon != null) {
const buf = try arena.alloc(u8, pane.rows -| 1);
@@ -656,3 +862,60 @@ fn fillBody(dst: ?[]u8, pane: *Pane, viewport: []const []const u8) usize {
}
return written;
}
+
+test "a motion root's level paints its targets in the gutter: `]` the next paragraph's p, `g` g and e; `m` browsed on `i` tints the braces' inside; nothing at rest" {
+ const Key = pardes.Key;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 20 });
+ defer p.deinit();
+ const pane = try p.setTestFile("fn a() {\n one;\n}\n\nfn b() {\n two;\n}\n");
+ const id = p.view().active;
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ const a = frame.allocator();
+ p.clock.now_ns = 5 * std.time.ns_per_s;
+ p.clock.stepped_ns = p.clock.now_ns;
+ pane.focus = .body;
+ pane.body.cur_row = 1;
+ pane.body.cur_col = 4;
+ p.sync();
+ var s = try p.render(a);
+ const body = s.region(.body, id).?.rect;
+ const gutter = body.x;
+ const at_rest = [_][]const u8{ s.at(gutter, body.y).grapheme(), s.at(gutter, body.y + 4).grapheme(), s.at(gutter, body.y + 6).grapheme() };
+ for (at_rest) |g| try std.testing.expect(!std.mem.eql(u8, g, "p") and !std.mem.eql(u8, g, "g") and !std.mem.eql(u8, g, "e"));
+ const plain_bg = s.at(gutter + panes.File.gutterWidth(pane) + 4, body.y + 1).style.bg;
+ // `]`: nothing until its level shows; then `p` on the next paragraph.
+ p.update(.{ .key = .{ .cp = ']' } });
+ p.advance(p.clock.now_ns);
+ _ = frame.reset(.retain_capacity);
+ s = try p.render(a);
+ try std.testing.expect(!std.mem.eql(u8, s.at(gutter, body.y + 4).grapheme(), "p"));
+ p.clock.now_ns = p.view().tag_state.due_ns;
+ _ = frame.reset(.retain_capacity);
+ s = try p.render(a);
+ try std.testing.expectEqualStrings("p", s.at(gutter, body.y + 4).grapheme());
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ // `g`: its first and last lines.
+ p.update(.{ .key = .{ .cp = 'g' } });
+ p.advance(p.clock.now_ns);
+ p.clock.now_ns = p.view().tag_state.due_ns;
+ _ = frame.reset(.retain_capacity);
+ s = try p.render(a);
+ try std.testing.expectEqualStrings("g", s.at(gutter, body.y).grapheme());
+ try std.testing.expectEqualStrings("e", s.at(gutter, body.y + 6).grapheme());
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ // `m` unrolled, its cursor on `i`: the inside of the braces around the
+ // caret tinted.
+ p.update(.{ .key = .{ .cp = 'm' } });
+ p.update(.{ .key = .{ .cp = Key.down } });
+ p.view().tag_state.hi = bindings.nodeAt(.match, "i").?.key;
+ _ = frame.reset(.retain_capacity);
+ s = try p.render(a);
+ const lit = s.at(gutter + panes.File.gutterWidth(pane) + 4, body.y + 1).style.bg;
+ try std.testing.expect(!std.meta.eql(plain_bg, lit));
+ // At rest again: the gutter as it was.
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ _ = frame.reset(.retain_capacity);
+ s = try p.render(a);
+ try std.testing.expectEqualStrings(at_rest[1], s.at(gutter, body.y + 4).grapheme());
+}