summaryrefslogtreecommitdiff
path: root/core/body_layer.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/body_layer.zig')
-rw-r--r--core/body_layer.zig367
1 files changed, 365 insertions, 2 deletions
diff --git a/core/body_layer.zig b/core/body_layer.zig
index ef3401ed..a182f218 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 ----
@@ -347,6 +350,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane
panes.Tables.paint(p, s, pane, r, tx, tw, body_h);
drawWrapMarkers(p, s, pane, r, tx, tw, body_h, pane_bg);
pane.places.paint(p, s, pane, r, body_h);
+ pardes.Prompt.paintGutter(p, s, pane, r, body_h);
panes.Query.paint(p, s, pane, r, body_h);
} else if (pane.isTerminal()) {
ansi.recolor(p, s, pane, r, tx, tw, body_h, body);
@@ -379,7 +383,10 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane
if (b == pardes.sel_slot and @min(sl.r0, sl.r1) < pane.tag_rows) continue;
paintPointerSelection(p, s, pane, r, sl, pane.tag_rows, sel_btn[b], buttonInks(th)[b]);
}
- const preview = normal.selRegexArmed(pane) != null;
+ // A selecting prompt's live result (Prompt.zig), dimmed while its
+ // field is no regex.
+ const selecting = pardes.Prompt.selecting(p, pane);
+ const preview = normal.selRegexArmed(pane) != null or selecting != null;
const show_prim = (pane.body.mode == .normal and pane.body.vsel.active) or preview or
(modal_hover and pane.body.vsel.active);
const show_extra = pane.body.mode != .tty and pane.body.nsel > 0;
@@ -397,7 +404,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane
} else pane.body.sels[si];
const bnd = edit.cellBounds(sr);
const hover_only = primary and modal_hover and pane.body.mode != .normal and !preview;
- const bg = if (hover_only)
+ const bg = if (hover_only or (selecting != null and selecting.?.dim))
colors.mix(page_rgb, colors.mix(page_rgb, th.sel_bg))
else if (primary)
th.sel_bg
@@ -468,7 +475,14 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane
}
}
+ // What the body would do, drawn and never written (ruling R3): a motion
+ // root's targets in the gutter first, so the ghost rows below push them
+ // with their lines; then a message's words and detail (Messages), a
+ // hover's lines (SPEC §3.5), a rename's preview.
+ if (pane.file != null) paintTargets(p, s, arena, pane, r, tx, tw, body_y, body_h);
if (pane.file != null and p.messages.live_len > 0) messageGhosts(p, s, pane, tx, tw, body_y, body_h);
+ if (p.lsp_ask.hover != null) hoverGhosts(p, s, pane, tx, tw, body_y, body_h);
+ if (p.view().tag_state.kind == .rename) renameGhosts(p, s, pane, tx, tw, body_y, body_h);
// `gw`'s labels, two letters over the start of each word
if (pane.jump.items.len > 0) labels: {
@@ -540,6 +554,9 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane
// A replica's predicted echo, over what its authority's screen shows.
if (p.echo) |e| if (pane.isTerminal()) e.paint(p, s, pane, id, tx, tw, body_y, body_h, active);
+ // A pipe prompt's live run, as ghost lines over what is painted
+ // (Prompt.zig).
+ if (pane.file != null) try pardes.Prompt.ghosts(p, s, arena, pane, r, body_h);
if (rail) |bar| if (bar.rect.h > 0) {
// The rail covers the notice bands too: nothing else paints those
// two gutter columns, so a gap there keeps the frame-wide border
@@ -633,6 +650,295 @@ pub fn wordAt(p: *Pardes, arena: std.mem.Allocator, id: usize, row: usize, col:
return text.items[b.lo..b.hi];
}
+/// 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 and diagnostic: `f p T c g
+/// d`, the spec's, each tree object a query of the rows shown); 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;
+ }
+}
+
+/// A hover's answer as ghost lines under the line it was asked at (SPEC
+/// §3.5, R3): the rows below move down by its lines (those past the body's
+/// foot not shown meanwhile), the buffer untouched. Bounded by the rows
+/// shown and the hover's `Ask.hover_max` bytes.
+fn hoverGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: u16, body_h: u16) void {
+ const h = &p.lsp_ask.hover.?;
+ if (h.serial != pane.serial) return;
+ const f = if (pane.file) |*file| file else return;
+ if (f.revision != h.revision) return;
+ const tab = p.settings.tab;
+ // The last row the line is shown on.
+ var under: ?u16 = null;
+ var row: u16 = pane.context_rows;
+ while (row < body_h) : (row += 1) {
+ if (pane.wrapAt(tab, row).line == h.row) under = row else if (under != null) break;
+ }
+ const at = under orelse return;
+ const text = std.mem.trimEnd(u8, h.text.slice(), "\n");
+ const k: u16 = @intCast(@min(std.mem.count(u8, text, "\n") + 1, body_h - at - 1));
+ if (k == 0) return;
+ var dst: u16 = body_h - 1;
+ while (dst >= at + 1 + k) : (dst -= 1) {
+ const to = @as(usize, body_y + dst) * s.cols + tx;
+ const from = @as(usize, body_y + dst - k) * s.cols + tx;
+ @memcpy(s.cells[to..][0..tw], s.cells[from..][0..tw]);
+ }
+ const th = p.theme();
+ const style: CellStyle = .{ .fg = .{ .rgb = th.tag_fg }, .bg = .{ .rgb = th.tag_bg } };
+ const gutter: u16 = File.gutterWidth(pane);
+ var lines = std.mem.splitScalar(u8, text, '\n');
+ for (0..k) |i| {
+ const y = body_y + at + 1 + @as(u16, @intCast(i));
+ s.fill(tx, y, tw, 1, .{ .bg = .{ .rgb = th.tag_bg } });
+ if (gutter + 2 < tw) _ = s.print(tx + gutter + 2, y, tw - gutter - 2, lines.next() orelse "", style);
+ }
+}
+
+/// A Rename typed in this body's tag, shown in its rows (SPEC §3.5, R3):
+/// each whole-word place of the name under the caret in the rows shown
+/// struck, the name typed after it, the rest of its row pushed right.
+/// Never written; bounded by the rows shown.
+fn renameGhosts(p: *Pardes, s: *Surface, pane: *Pane, tx: u16, tw: u16, body_y: u16, body_h: u16) void {
+ const host = TagState.hostOf(p) orelse return;
+ if (p.panes[host] != pane or pane.file == null) return;
+ const old = @import("lsp/Ask.zig").renamedWord(pane) orelse return;
+ const new = p.view().tag_state.field.text();
+ const tab = p.settings.tab;
+ const chrome = p.chromeTheme();
+ const th = p.theme();
+ const added: CellStyle = .{ .fg = .{ .rgb = chrome.diagnostic_hint }, .bg = if (th.bg) |c| .{ .rgb = c } else .default, .bold = true };
+ const wide: u16 = @intCast(@min(File.displayWidth(tab, new), tw));
+ const gutter: i32 = File.gutterWidth(pane);
+ var row: u16 = pane.context_rows;
+ while (row < body_h) : (row += 1) {
+ const w = pane.wrapAt(tab, row);
+ if (w.line < 0) continue;
+ const line = File.sourceLine(pane, w.line);
+ const from: usize = @min(line.len, @as(usize, @intCast(@max(0, w.at))));
+ const next = pane.wrapAt(tab, row + 1);
+ const to: usize = if (next.line == w.line) @min(line.len, @as(usize, @intCast(@max(0, next.at)))) else line.len;
+ // Right to left, so a push leaves the places before it where they are.
+ var end = to;
+ while (std.mem.lastIndexOf(u8, line[from..end], old)) |k| {
+ const at = from + k;
+ end = at;
+ const before = at > 0 and (std.ascii.isAlphanumeric(line[at - 1]) or line[at - 1] == '_');
+ const after = at + old.len < line.len and (std.ascii.isAlphanumeric(line[at + old.len]) or line[at + old.len] == '_');
+ if (before or after) continue;
+ const c0 = gutter + w.pad + File.displayOffset(tab, pane, w.line, w.at, @intCast(at));
+ const c1 = gutter + w.pad + File.displayOffset(tab, pane, w.line, w.at, @intCast(at + old.len));
+ if (c0 < gutter or c1 > tw) continue;
+ const y = body_y + row;
+ var c: u16 = @intCast(c0);
+ while (c < c1) : (c += 1) {
+ const cell = s.at(tx + c, y);
+ cell.style.strikethrough = true;
+ cell.style.fg = .{ .rgb = chrome.diagnostic_error };
+ }
+ const x: u16 = @intCast(c1);
+ if (x + wide < tw) {
+ const cells = s.cells[@as(usize, y) * s.cols + tx ..][0..tw];
+ std.mem.copyBackwards(Cell, cells[x + wide ..], cells[x .. tw - wide]);
+ }
+ _ = s.print(tx + x, y, tw - x, new, added);
+ }
+ }
+}
+
+/// 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_comment, .goto_test => |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);
@@ -848,3 +1154,60 @@ test "a click under a message's detail lands on the row drawn there, a click on
try std.testing.expect(p.settings.wrap != wrapped);
try std.testing.expectEqualStrings("one\ntwo\nthree\nfour\nfive\nsix\n", pane.file.?.content);
}
+
+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());
+}