diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-24 22:24:29 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-24 22:24:29 -0300 |
| commit | 89b3e7365bac28c4cd7d52d40c1786c01db5af52 (patch) | |
| tree | dcc65675b340f4ba93bfcf8d4edd9c7a4d16dc6c | |
| parent | b44a129a8854483dbe45ce2bcfa09f33ccbd6e8c (diff) | |
| download | pardes-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]>
| -rw-r--r-- | core/body_layer.zig | 263 | ||||
| -rw-r--r-- | core/jj.zig | 8 | ||||
| -rw-r--r-- | core/normal.zig | 4 | ||||
| -rw-r--r-- | core/syntax.zig | 5 |
4 files changed, 274 insertions, 6 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()); +} diff --git a/core/jj.zig b/core/jj.zig index c1d07662..7f1e5623 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -2030,14 +2030,15 @@ fn ownerOf(ch: *const Changes, f: *const File.State, row: usize) PinIndex { /// The line `]g` (`forward`) or `[g` goes to from buffer line `from` of /// the pane: the next or the previous change's first line (the first of a /// run of lines one pin wrote or took away: a ghost line is a line); null -/// with none shown, or none that way. -pub fn nextChange(p: *Pardes, pane: *const Pane, from: usize, forward: bool) ?usize { +/// with none shown, or none that way, or none in the `most` lines looked +/// at (a body's painted targets look at the rows shown alone). +pub fn nextChange(p: *Pardes, pane: *const Pane, from: usize, forward: bool, most: usize) ?usize { if (pane.layers.lanes == 0) return null; const f = if (pane.file) |*file| file else return null; const ch = answered(p, f.layer.owner) orelse return null; const n = File.lineCount(f.content); var row = from; - while (true) { + for (0..most) |_| { if (forward) { row += 1; if (row >= n) return null; @@ -2048,6 +2049,7 @@ pub fn nextChange(p: *Pardes, pane: *const Pane, from: usize, forward: bool) ?us const owner = ownerOf(ch, f, row); if (owner != .none and (row == 0 or ownerOf(ch, f, row - 1) != owner)) return row; } + return null; } /// A Look (B3) at column `col` of body row `vr` of pane `id` on the id of diff --git a/core/normal.zig b/core/normal.zig index 2ec8a4da..ea2bbc31 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -249,7 +249,7 @@ fn objectJump(e: Editor, t: *Text, text: []const u8, range: modal.Selection, obj const tree = nodeTree(e, t, text) orelse return; var last = range; for (0..count) |_| { - const s = syntax.objectNext(tree.path, tree.root, tree.text, object, forward, tree.in(modal.selectionCursor(text, last))) orelse break; + const s = syntax.objectNext(tree.path, tree.root, tree.text, object, forward, tree.in(modal.selectionCursor(text, last)), null) orelse break; const next: modal.Selection = .{ .anchor = tree.out(s.from, false), .head = tree.out(s.to, s.to > s.from) }; if (std.meta.eql(next, last)) break; last = next; @@ -1317,7 +1317,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! .view_scroll_down => if (t.what == .body) return t.scrollViewMove(p.settings.tab, text, range, 1), .view_scroll_up => if (t.what == .body) return t.scrollViewMove(p.settings.tab, text, range, -1), // The next and the previous of the changes this view shows (jj.zig). - .goto_change => if (@import("jj.zig").nextChange(p, pane, @intCast(@max(0, t.cur_row)), a.forward)) |line| + .goto_change => if (@import("jj.zig").nextChange(p, pane, @intCast(@max(0, t.cur_row)), a.forward, std.math.maxInt(usize))) |line| return pointMove(p.settings.tab, t, text, range, modal.lineStartOffset(text, line)), .goto_diagnostic, .goto_diagnostic_end => { // Once a Diagnostics query fed them, the diagnostics are this diff --git a/core/syntax.zig b/core/syntax.zig index 08ea1fb4..a388573b 100644 --- a/core/syntax.zig +++ b/core/syntax.zig @@ -566,7 +566,7 @@ pub fn objectAt(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object /// `]f`/`[f` and kin: the next object starting after byte `at`, or the /// previous one ending before it (helix goto_treesitter_object), from the /// first of `.movement`, `.around`, `.inside` the query names. -pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object: Object, forward: bool, at: usize) ?Span { +pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object: Object, forward: bool, at: usize, within: ?[2]usize) ?Span { const q = objectQuery(path) orelse return null; var buf: [32]u8 = undefined; const capture = for ([_][]const u8{ "movement", "around", "inside" }) |kind| { @@ -576,6 +576,9 @@ pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, obje const tree: *const ts.Tree = @ptrCast(@alignCast(tree_ptr)); const cursor = ts.QueryCursor.create(); defer cursor.destroy(); + // Only the objects these bytes hold (a body's rows shown): its painted + // targets (body_layer.paintTargets). + if (within) |w| cursor.setByteRange(@intCast(w[0]), @intCast(w[1])) catch return null; cursor.exec(q, tree.rootNode()); var best: ?Span = null; while (cursor.nextMatch()) |match| { |
