//! A file pane's text: its content and undo history, display columns, line //! index, syntax highlights and the body it fills. const panes = @import("panes.zig"); const std = @import("std"); const vaxis = @import("vaxis"); const builtin = @import("builtin"); const pardes = @import("pardes.zig"); const config = @import("config.zig"); const Pardes = pardes.Pardes; const Surface = pardes.Surface; const modal = @import("modal.zig"); const filesystem = @import("fs.zig"); const syntax = @import("syntax.zig"); const diff = @import("diff.zig"); const tracy = @import("tracy.zig"); const limits = @import("memory.zig").limits; const locations = @import("locations.zig"); const Colors = @import("colors.zig"); const Tables = @import("Tables.zig"); const Stream = @import("Stream.zig"); const Pane = panes.Pane; const Output = panes.Output; const Mini = panes.mini; const terminal = panes.terminal; const Text = panes.Text; const SYNTAX_CONTEXT_AFTER_ROWS: usize = 2; const long_paint: usize = 16 << 10; /// Where the pane is in its text and what the text is saved as now: an /// undo step's edge (Text.Place), kept once, never a copy of the text. fn placeOf(pane: *const Pane, f: *const State) Text.Place { return .{ .cur_row = pane.body.cur_row, .cur_col = pane.body.cur_col, .vsel = pane.body.vsel, .gen = f.history.gen, .clean = f.revision == f.saved_revision, .saved_hash = f.saved_hash, .saved_len = f.saved_len, }; } /// A rename (ninep/pane.zig nameBuffer): the name it ends, kept for an undo /// back across it; `old` is the History's from here (owned), freed if it /// cannot be kept. pub fn noteRenamed(gpa: std.mem.Allocator, history: *History, old: []u8) void { history.names.append(gpa, .{ .gen = history.gen, .path = old }) catch gpa.free(old); history.gen +%= 1; } /// After a step's text went in: if it was under another name, that name /// back, and the step's saved state with it (Get file, Edit's e), as acme's /// undo puts them back with the text. fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Text.Place) void { const history = f.history; if (snap.gen == history.gen) return; // The latest name kept for that generation. var at = history.names.items.len; const path = while (at > 0) { at -= 1; if (history.names.items[at].gen == snap.gen) break history.names.items[at].path; } else return; if (std.mem.eql(u8, path, f.path)) { history.gen = snap.gen; return; } const id = for (p.panes, 0..) |slot, i| { if (slot == pane) break i; } else return; // nameBuffer frees nothing of `path`: it keeps the name it ends. const back = p.gpa.dupe(u8, path) catch return; defer p.gpa.free(back); pardes.ctlfs.pane.nameBuffer(p, id, back, false) catch return; history.gen = snap.gen; f.saved_hash = snap.saved_hash; f.saved_len = snap.saved_len; f.saved_revision = if (snap.clean) f.revision else f.revision -% 1; f.disk_newer = null; f.disk_newer_warned = false; if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); } /// A file's undo, which its twins (Zerox) share: its edits as a journal /// (Text.Journal), and the names it had. pub const History = struct { journal: Text.Journal = .{}, /// The names the file had, each with the rename generation it ended /// (Text.Place.gen), and the generation now: no allocation a step, a /// name is kept once, at its rename. names: std.ArrayList(struct { gen: u32, path: []u8 }) = .empty, gen: u32 = 0, pub fn create(gpa: std.mem.Allocator) !*History { const history = try gpa.create(History); history.* = .{}; return history; } pub fn destroy(history: *History, gpa: std.mem.Allocator) void { history.journal.deinit(gpa); for (history.names.items) |name| gpa.free(name.path); history.names.deinit(gpa); gpa.destroy(history); } }; /// A file pane's backing: owned content, its derived caches, and undo history. pub const State = struct { /// What the replay digest leaves out (src/walk.zig): what is made again /// from the text whenever it is wanted. pub const replica = .{ .derived = .{ "cap", "line_starts", "line_cap", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" }, // Big texts, dumped and sent by their hash (src/walk.zig). The // text is written in place by `replace`, but never while a join // sends it (memory.zig `pin`): then an edit puts a new buffer in // its place. .blob = .{ "content", "location_rows" }, // Written in place in a replica's core too: its rollback copy keeps // none of their bytes, and takes back what a speculation wrote // (src/rollback.zig `Epoch.wrote`). .journal = .{ "content", "line_starts" }, }; path: []u8, content: []u8, /// The bytes `content`'s buffer has room for (0: as many as it holds): /// an edit that must grow it grows it by an eighth more, so the keys /// after it move no text but what follows them (`replace`). cap: usize = 0, revision: u32 = 0, /// Revision last known to match disk, after Save or an external reload. /// Equal means the screen matches disk. saved_revision: u32 = 0, /// The text at `saved_revision`, hashed as the first edit after it /// replaces it: an edit that brings the text back to it is clean again, /// as an undo to the save point is in acme. saved_hash: ?u64 = null, /// The length of the text `saved_hash` is of, when it is of one: an /// edit to another length cannot be back to it, and is not hashed. saved_len: ?usize = null, watch_after_save: bool = false, /// The file changed on disk under unsaved edits, which were kept (see /// `changed`): its hash, so the same change is said once, until a Save /// or get settles it. A Save warns once before overwriting it, as acme's /// Put does (exec.c:577, "modified since last read"). disk_newer: ?u64 = null, disk_newer_warned: bool = false, /// Its file was deleted on disk: said once (`deleted`) until it is /// written or found again. disk_gone: bool = false, /// A +Pager pane (`pardes -`): no line numbers, its program's colours /// through Filter until it is toggled off. pager: bool = false, /// A +Pager whose text is a diff (`git diff`, `show`, `log -p`): drawn /// and looked at as a diff (Pane.colorAlgo). pager_diff: bool = false, /// A +Pager's colours, as its program printed them (pager.zig): drawn /// over its text by recolorSyntax, never read over 9P. ponytail: any /// edit drops them (setContentSpan); shifting them through the splice, /// as pane.zig's shiftOne moves dot, would keep them. pager_spans: []pardes.pager.Span = &.{}, /// Seconds of the last content change, for the filesystem's stat; zero until edited. mtime: u32 = 0, /// Non-null for a generated output buffer rather than an on-disk file. output: ?Output.State = null, /// A directory pane's entries (Dir.zig), its output `.dir`. listing: ?panes.Dir.State = null, mini: ?Mini.State = null, scroll: usize = 0, /// `line_starts[i]` is line i's byte offset. Empty means not built yet. line_starts: []usize = &.{}, /// The room `line_starts` has, as `cap` is `content`'s. line_cap: usize = 0, /// One tree_sitter_gpa-owned syntax.Syn byte per highlighted source byte. highlights: []u8 = &.{}, location_rows: []locations.Row = &.{}, /// The last edit's splice: what the text at `revision` kept of the one /// before it at either end (`old_len` long). A pane's places shift by it /// rather than search the whole text again (Places.shift). splice: struct { revision: u32 = 0, head: usize = 0, tail: usize = 0, old_len: usize = 0 } = .{}, highlight_start: usize = 0, syntax_dirty: bool = true, tree_context: bool = false, context_declarations: []syntax.ContextDeclaration = &.{}, context_revision: ?u32 = null, /// The whole file's parse for the keys that walk its nodes (Alt-o and /// kin), made on the first of them after an edit and kept until the next /// one: `syntax.parseTree`'s, opaque here. node_tree: ?*anyopaque = null, node_tree_revision: u32 = 0, /// helix's object_selections: what Alt-o grew from, for Alt-i to go back /// to; gpa-owned ranges and the primary's index. node_history: std.ArrayList(NodeStep) = .empty, /// A markdown text's pipe tables, laid out in their rows (Tables.zig): /// found again on the first body drawn after an edit. tables: Tables = .{}, history: *History, /// Zerox's group: the panes on one buffer share `content`, `history` /// and (while it is the one index) `line_starts`, each with its own /// scroll, cursor and syntax; 0, none. See `zerox`. twin: u32 = 0, }; pub const NodeStep = struct { ranges: []modal.Selection, primary: usize }; pub fn graphemeDisplayWidth(grapheme: []const u8) usize { if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now; if (grapheme.len == 1 and grapheme[0] >= 0x20 and grapheme[0] < 0x7f) return 1; if (std.unicode.utf8ValidateSlice(grapheme)) return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode))); // vaxis's gwidth counts back a replacement rune's 3 bytes over the one // invalid byte it stood for (`\xee` before `e\u{301}` overflowed it): // each invalid byte is a cell, as surface.zig draws it, and each valid // run between is measured alone. var width: usize = 0; var run: usize = 0; var i: usize = 0; while (i < grapheme.len) { const n = std.unicode.utf8ByteSequenceLength(grapheme[i]) catch 0; if (n > 0 and i + n <= grapheme.len and std.unicode.utf8ValidateSlice(grapheme[i .. i + n])) { i += n; continue; } if (run < i) width += vaxis.gwidth.gwidth(grapheme[run..i], .unicode); width += 1; i += 1; run = i; } if (run < grapheme.len) width += vaxis.gwidth.gwidth(grapheme[run..], .unicode); return @max(1, width); } test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width" { try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xeee\xcc\x81")); try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\xee\xffe")); try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\u{4e16}\x80")); // The whole path the 9P fuzzer took: a body holding it, fitted to a width. const text = "\xeee\xcc\x81"; try std.testing.expectEqual(@as(usize, 4), fitEnd(text, 0, 10)); } pub fn displayWidth(text: []const u8) usize { var width: usize = 0; var at: usize = 0; while (at < text.len) { const end = modal.nextGrapheme(text, at); width +|= graphemeDisplayWidth(text[at..end]); at = end; } return width; } /// Source byte at a zero-based display column. Every cell occupied by a tab /// maps back to that one tab byte. pub fn byteAtDisplay(text: []const u8, display_col: usize) usize { var col: usize = 0; var at: usize = 0; while (at < text.len) { const end = modal.nextGrapheme(text, at); const next = col +| graphemeDisplayWidth(text[at..end]); if (display_col < next) return at; col = next; at = end; } return text.len; } /// File cursor columns may live past EOL. Tabs expand before that boundary; /// every virtual column after it remains one screen cell. pub fn rawDisplayCol(line_text: []const u8, raw_col: usize) usize { const bounded = modal.graphemeStart(line_text, @min(raw_col, line_text.len)); return displayWidth(line_text[0..bounded]) +| (raw_col -| line_text.len); } /// Walked only as far as the column: a row's last byte, a million bytes /// into a line, is found without measuring the rest of it. Only a line /// that ends before the column is measured whole (it is short). pub fn rawAtDisplay(line_text: []const u8, display_col: usize) usize { const at = byteAtDisplay(line_text, display_col); if (at < line_text.len) return at; return line_text.len +| (display_col -| displayWidth(line_text)); } pub fn byteAtDisplayFrom(line_text: []const u8, from_raw: usize, display_col: usize) usize { if (from_raw >= line_text.len) return from_raw +| display_col; const from = modal.graphemeStart(line_text, from_raw); return from +| rawAtDisplay(line_text[from..], display_col); } /// The display columns from `from_raw` to `to_raw` on a line. Widths add /// (a tab is `tab_width` wherever it stands), so only the text between /// the two is measured, never the line from its start: a selection a /// million bytes into a line draws as fast as one at its start. pub fn lineDisplayOffset(line_text: []const u8, from_raw: usize, to_raw: usize) i32 { const lo = @min(from_raw, to_raw); const hi = @max(from_raw, to_raw); const a = modal.graphemeStart(line_text, @min(lo, line_text.len)); const b = modal.graphemeStart(line_text, @min(hi, line_text.len)); const past = (hi -| line_text.len) - (lo -| line_text.len); const width: usize = displayWidth(line_text[a..b]) +| past; return if (to_raw >= from_raw) @intCast(width) else -@as(i32, @intCast(width)); } pub fn lineDisplayEndOffset(line_text: []const u8, from_raw: usize, at_raw: usize) i32 { const start = lineDisplayOffset(line_text, from_raw, at_raw); if (at_raw >= line_text.len) return start; const at = modal.graphemeStart(line_text, at_raw); const end = modal.nextGrapheme(line_text, at); return start + @as(i32, @intCast(graphemeDisplayWidth(line_text[at..end]))) - 1; } pub fn sourceLine(pane: *const Pane, row: i32) []const u8 { const f = if (pane.file) |*file| file else return ""; if (row < 0) return ""; const line: usize = @intCast(row); if (f.line_starts.len == 0) return modal.lineSlice(f.content, line); if (line >= f.line_starts.len) return ""; const start = f.line_starts[line]; const end = if (line + 1 < f.line_starts.len) f.line_starts[line + 1] - 1 else f.content.len; return f.content[start..end]; } test "indexed source rows match uncached scans across content changes" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); const pane = try p.setTestFile(""); const file = &pane.file.?; for ([_][]const u8{ "", "one", "one\n", "a\r\nλ界\nlast", "\n\n", "\tλ e\u{301}\n\r\n" }) |content| { setContent(p, file, try p.gpa.dupe(u8, content)); // An edit carries a built index forward; it must equal a fresh one. if (file.line_starts.len > 0) { const carried = file.line_starts; const room = indexRoom(file); defer p.gpa.free(room); file.line_starts = &.{}; try std.testing.expectEqualSlices(usize, try lineIndex(p.gpa, file), carried); p.gpa.free(file.line_starts); file.line_starts = &.{}; } const rows = lineCount(content) + 2; for ([_]bool{ false, true }) |indexed| { if (indexed) _ = try lineIndex(p.gpa, file); try std.testing.expectEqualStrings("", sourceLine(pane, -1)); for (0..rows) |row| try std.testing.expectEqualStrings( modal.lineSlice(content, row), sourceLine(pane, @intCast(row)), ); if (!indexed) try std.testing.expectEqual(@as(usize, 0), file.line_starts.len); } } } /// The pads `row` is laid out with in this pane: a row of a markdown /// table (Tables.zig) of its text as it is now, when the table fits the /// width a wrapping body has (a wider one is shown as its source, wrapped); /// else none. Every mapping between a file row's bytes and its cells goes /// through here, so the drawn row, the caret and a click cannot disagree. pub fn rowPads(pane: *const Pane, row: i32, out: *[Tables.max_pads]Tables.Pad) []const Tables.Pad { const table = rowTable(pane, row) orelse return &.{}; return pane.file.?.tables.padsOf(table, @intCast(row), sourceLine(pane, row), out); } /// The table `row` is laid out in, in this pane (`rowPads`). pub fn rowTable(pane: *const Pane, row: i32) ?*const Tables.Table { const f = if (pane.file) |*file| file else return null; if (row < 0 or f.tables.list.items.len == 0 or !f.tables.current(f)) return null; const table = f.tables.find(@intCast(row)) orelse return null; const width = wrapWidth(pane, pane.body_wrapped); if (width > 0 and table.width > width) return null; return table; } pub fn displayOffset(pane: *const Pane, row: i32, from_raw: i32, to_raw: i32) i32 { const line_text = sourceLine(pane, row); const from: usize = @intCast(@max(0, from_raw)); const to: usize = @intCast(@max(0, to_raw)); var buf: [Tables.max_pads]Tables.Pad = undefined; return lineDisplayOffset(line_text, from, to) + Tables.between(rowPads(pane, row, &buf), from, to); } pub fn displayEndOffset(pane: *const Pane, row: i32, from_raw: i32, at_raw: i32) i32 { const line_text = sourceLine(pane, row); const from: usize = @intCast(@max(0, from_raw)); const at: usize = @intCast(@max(0, at_raw)); var buf: [Tables.max_pads]Tables.Pad = undefined; return lineDisplayEndOffset(line_text, from, at) + Tables.between(rowPads(pane, row, &buf), from, at); } pub fn byteAtRowDisplay(pane: *const Pane, row: i32, from_raw: i32, display_col: i32) i32 { const line_text = sourceLine(pane, row); var buf: [Tables.max_pads]Tables.Pad = undefined; const pads = rowPads(pane, row, &buf); if (pads.len > 0) return @intCast(Tables.byteAt(line_text, pads, @intCast(@max(0, from_raw)), @intCast(@max(0, display_col)))); return @intCast(byteAtDisplayFrom(line_text, @intCast(@max(0, from_raw)), @intCast(@max(0, display_col)))); } pub fn gutterWidth(pane: *const Pane) u16 { if (pane.file == null) return 0; const file = &pane.file.?; // A +Pager, and a +Diff (hunks.zig), show no line numbers, as a // pager and delta show none. if (file.pager) return 0; if (file.output) |o| if (std.meta.eql(o.from, Output.Origin{ .cmd = .Diff })) return 0; // A query pane's rows carry their own line numbers (Query.zig). if (pane.query != null) return 0; var lines = if (file.line_starts.len > 0) file.line_starts.len else lineCount(file.content); var digits: u16 = 1; while (lines >= 10) : (lines /= 10) digits += 1; return @max(config.PREFIX_W, digits + 1); } /// Convert between rendered cells and UTF-8 byte columns. Tag rows always /// need grapheme conversion; file body rows additionally skip the gutter. pub fn renderedLineByteCol(pane: *const Pane, row: i32, line_text: []const u8, display_col: usize) usize { if (row < pane.tag_rows) return rawAtDisplay(line_text, display_col); if (pane.file == null) return rawAtDisplay(line_text, display_col); const prefix = @min(@as(usize, gutterWidth(pane)), line_text.len); if (display_col <= prefix) return display_col; return prefix +| rawAtDisplay(line_text[prefix..], display_col - prefix); } pub fn renderedLineDisplayCol(pane: *const Pane, row: i32, line_text: []const u8, byte_col: usize) usize { if (row < pane.tag_rows) return rawDisplayCol(line_text, byte_col); if (pane.file == null) return rawDisplayCol(line_text, byte_col); const prefix = @min(@as(usize, gutterWidth(pane)), line_text.len); if (byte_col <= prefix) return byte_col; return prefix +| rawDisplayCol(line_text[prefix..], byte_col - prefix); } test "the ASCII arm of graphemeDisplayWidth matches the gwidth it skips" { const ref = struct { fn width(grapheme: []const u8) usize { if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now; return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode))); } }.width; var one: [1]u8 = undefined; var b: u8 = 0; while (b < 0x80) : (b += 1) { one[0] = b; try std.testing.expectEqual(ref(one[0..1]), graphemeDisplayWidth(one[0..1])); } for ([_][]const u8{ "e\u{301}", "a\u{903}", "1\u{fe0f}\u{20e3}", "\u{4e16}", "\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff", }) |g| try std.testing.expectEqual(ref(g), graphemeDisplayWidth(g)); // Two invalid bytes are two cells, as surface.zig draws them, where // vaxis's own count gives the pair one. try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xe4\xb8")); } test "the ASCII run in fitEnd survives an exhaustive byte sweep" { const reference = struct { fn fitEnd(text: []const u8, start: usize, width: usize) usize { var end = start; var used: usize = 0; while (end < text.len) { const next_end = modal.nextGrapheme(text, end); const next_used = used +| graphemeDisplayWidth(text[end..next_end]); if (next_used > width) return if (end == start) next_end else end; used = next_used; end = next_end; } return end; } }.fitEnd; const neighbours = [_][]const u8{ "", "z", "\u{301}", "\u{200d}\u{1f680}", "\u{903}", "\u{fe0f}", "\u{4e16}", "\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff", "\xe4\xb8", "\xe4\x28\xb8", }; var buf: [16]u8 = undefined; // The same pair again behind an ASCII prefix, so a break can land exactly at the run boundary // as well as before it and inside the multi-byte cluster that follows it. var prefixed: [18]u8 = undefined; prefixed[0] = 'a'; prefixed[1] = 'b'; var b: u8 = 0; while (b < 0x80) : (b += 1) { buf[0] = b; for (neighbours) |tail| { @memcpy(buf[1..][0..tail.len], tail); const pair = buf[0 .. 1 + tail.len]; @memcpy(prefixed[2..][0..pair.len], pair); for ([_][]const u8{ pair, prefixed[0 .. 2 + pair.len] }) |text| { var width: usize = 0; while (width <= text.len + 3) : (width += 1) { var start: usize = 0; while (start <= text.len) : (start += 1) { std.testing.expectEqual( reference(text, start, width), fitEnd(text, start, width), ) catch |e| { std.debug.print("fitEnd({any}, {d}, {d})\n", .{ text, start, width }); return e; }; } } } } } } fn fitEnd(text: []const u8, start: usize, width: usize) usize { var end = start; var used: usize = 0; while (used < width and end < text.len) { const b = text[end]; if (b < 0x20 or b >= 0x7f) break; if (end + 1 < text.len and text[end + 1] >= 0x80) break; used += 1; end += 1; } while (end < text.len) { const next_end = modal.nextGrapheme(text, end); const next_used = used +| graphemeDisplayWidth(text[end..next_end]); if (next_used > width) return if (end == start) next_end else end; used = next_used; end = next_end; } return end; } test "the ASCII run in fitEnd cuts where the grapheme walk would" { const reference = struct { fn fitEnd(text: []const u8, start: usize, width: usize) usize { var end = start; var used: usize = 0; while (end < text.len) { const next_end = modal.nextGrapheme(text, end); const next_used = used +| graphemeDisplayWidth(text[end..next_end]); if (next_used > width) return if (end == start) next_end else end; used = next_used; end = next_end; } return end; } }.fitEnd; const cases = [_][]const u8{ "", "hello world", // the fast path must hand over at the first non-ASCII byte, mid-run "abc\u{00e9}def", // a wide glyph is two columns, so a width boundary can land inside it "ab\u{4e16}\u{754c}cd", // a cluster the fast path must not split "a\u{0301}bc", // tabs and controls are excluded from the fast path by the range test "ab\tcd", "ab\rcd", // an ASCII byte followed by a continuation byte is NOT its own cluster "e\u{0301}x", "\u{1f1e6}\u{1f1e7}ok", }; for (cases) |text| { var width: usize = 0; while (width <= text.len + 3) : (width += 1) { var start: usize = 0; while (start <= text.len) : (start += 1) { try std.testing.expectEqual( reference(text, start, width), fitEnd(text, start, width), ); } } } } pub const lineCount = modal.cursorLineCount; // Content changes invalidate this lazily rebuilt byte-offset index. pub fn lineIndex(gpa: std.mem.Allocator, f: *State) ![]const usize { if (f.line_starts.len > 0) return f.line_starts; // Exact allocation: deinitPane frees `line_starts` itself, so the stored // slice must span the complete allocation rather than spare capacity. const starts = try gpa.alloc(usize, lineCount(f.content)); starts[0] = 0; var i: usize = 1; var off: usize = 0; while (std.mem.indexOfScalarPos(u8, f.content, off, '\n')) |nl| { off = nl + 1; starts[i] = off; i += 1; } f.line_starts = starts; f.line_cap = 0; return starts; } /// The whole buffer of a text's bytes, its room past them included. fn textRoom(f: *const State) []u8 { return f.content.ptr[0..@max(f.cap, f.content.len)]; } /// The whole buffer of a text's line index, as `textRoom` is its text's. fn indexRoom(f: *const State) []usize { return f.line_starts.ptr[0..@max(f.line_cap, f.line_starts.len)]; } /// `std.sort`'s order for offsets: a key against an item. fn ascending(key: usize, item: usize) std.math.Order { return std.math.order(key, item); } /// The bytes a splice left alone at either end, when its caller knows them. pub const Span = struct { head: usize, tail: usize }; /// `f`'s line index through the edit that puts `bytes` over the `len` bytes /// at `off` (`shiftStarts`), in its own buffer, grown by an eighth more when /// it must be. On no memory the index goes, to be made again when it is /// asked for. fn shiftIndex(gpa: std.mem.Allocator, f: *State, off: usize, len: usize, bytes: []const u8) void { const starts = f.line_starts; const room = indexRoom(f); const n = starts.len - (std.sort.upperBound(usize, starts, off + len, ascending) - std.sort.upperBound(usize, starts, off, ascending)) + std.mem.count(u8, bytes, "\n"); var out = room; if (n > room.len) { out = gpa.alloc(usize, n + @max(n / 8, 512)) catch { pardes.memory.freeRaw(gpa, room); f.line_starts = &.{}; f.line_cap = 0; return; }; @memcpy(out[0..starts.len], starts); } else pardes.rollback.Epoch.wrote(gpa, usize, room, starts.len, n, off, f.content[off..][0..len], bytes.len); shiftStarts(out, starts.len, n, off, len, bytes); if (out.ptr != room.ptr) pardes.memory.freeRaw(gpa, room); f.line_starts = out[0..n]; f.line_cap = out.len; } /// The first `len` of `room`, a line index, through the edit that puts /// `bytes` over the `cut` bytes at `off`, in place, `n` long after it /// (`room` holds that): the starts before the edit stand, those it took /// out go, its newlines' come in, and those after it move by what it added; /// only the moving is a pass over the index, and only from the edit on. The /// edit turned round (`cut` its length in, `bytes` what it took out) gives /// the index before it back: a rollback's (src/rollback.zig `unwrite`). pub fn shiftStarts(room: []usize, len: usize, n: usize, off: usize, cut: usize, bytes: []const u8) void { const starts = room[0..len]; const kept = std.sort.upperBound(usize, starts, off, ascending); const moved = len - std.sort.upperBound(usize, starts, off + cut, ascending); const from = len - moved; const to = n - moved; // The starts that move go back to front when the edit grew the index, // front to back when it shrank it. if (to > from) { var k = moved; while (k > 0) { k -= 1; room[to + k] = room[from + k] - cut + bytes.len; } } else for (0..moved) |k| { room[to + k] = room[from + k] - cut + bytes.len; } var i = kept; var at: usize = 0; while (std.mem.indexOfScalarPos(u8, bytes, at, '\n')) |nl| { at = nl + 1; room[i] = off + at; i += 1; } } test "a line index shifted in place equals a rebuilt one, and the edit turned round gives the one before back" { const gpa = std.testing.allocator; var prng = std.Random.DefaultPrng.init(11); const r = prng.random(); var text: std.ArrayList(u8) = .empty; defer text.deinit(gpa); for (0..300) |_| try text.append(gpa, "ab\n"[r.uintLessThan(usize, 3)]); var f: State = .{ .path = &.{}, .content = text.items, .history = undefined }; _ = try lineIndex(gpa, &f); defer gpa.free(indexRoom(&f)); for (0..2000) |round| { const at = r.uintAtMost(usize, text.items.len); const del = r.uintAtMost(usize, @min(12, text.items.len - at)); var ins: [12]u8 = undefined; const ins_len = r.uintAtMost(usize, 12); for (ins[0..ins_len]) |*c| c.* = "ab\n"[r.uintLessThan(usize, 3)]; const before = try gpa.dupe(usize, f.line_starts); defer gpa.free(before); const cut = try gpa.dupe(u8, text.items[at..][0..del]); defer gpa.free(cut); f.content = text.items; shiftIndex(gpa, &f, at, del, ins[0..ins_len]); try text.replaceRange(gpa, at, del, ins[0..ins_len]); var fresh: State = .{ .path = &.{}, .content = text.items, .history = undefined }; const want = try lineIndex(gpa, &fresh); defer gpa.free(want); try std.testing.expectEqualSlices(usize, want, f.line_starts); // Every tenth turned round in a copy, as a rollback takes it back. if (round % 10 == 0) { const room = try gpa.alloc(usize, indexRoom(&f).len); defer gpa.free(room); @memcpy(room[0..f.line_starts.len], f.line_starts); shiftStarts(room, f.line_starts.len, before.len, at, ins_len, cut); try std.testing.expectEqualSlices(usize, before, room[0..before.len]); } } } /// line count, O(1) once the index is warm pub fn nlines(gpa: std.mem.Allocator, f: *State) usize { const idx = lineIndex(gpa, f) catch return lineCount(f.content); return idx.len; } /// byte offset of line `row`, or content.len past the end — modal /// .lineStartOffset's contract exactly, without its walk pub fn lineStart(gpa: std.mem.Allocator, f: *State, row: usize) usize { const idx = lineIndex(gpa, f) catch return modal.lineStartOffset(f.content, row); return if (row >= idx.len) f.content.len else idx[row]; } pub fn cursorLines(arena: std.mem.Allocator, pane: *Pane, f: *State) ![]const []const u8 { const index = try lineIndex(pane.gpa, f); const lines = try arena.alloc([]const u8, index.len); for (index, 0..) |start, i| { const end = if (i + 1 < index.len) index[i + 1] - 1 else f.content.len; lines[i] = f.content[start..end]; } return lines; } /// Use the file's line index only when `text` is its complete live content. /// Edit-buffer fragments and other temporary text retain modal's scan path. fn contentIndex(t: *Text, text: []const u8) ?[]const usize { if (t.what != .body) return null; const pane = t.pane() orelse return null; const f = if (pane.file) |*file| file else return null; if (text.ptr != f.content.ptr or text.len != f.content.len) return null; return lineIndex(pane.gpa, f) catch null; } pub fn textOffset(t: *Text, text: []const u8, cursor: modal.Cursor) usize { const index = contentIndex(t, text) orelse return modal.offsetAt(text, cursor); const row = @min(cursor.row, index.len - 1); const start = index[row]; const end = if (row + 1 < index.len) index[row + 1] - 1 else text.len; return start + modal.graphemeStart(text[start..end], @min(cursor.col, end - start)); } pub fn textLineStart(t: *Text, text: []const u8, row: usize) usize { const index = contentIndex(t, text) orelse return modal.lineStartOffset(text, row); return if (row >= index.len) text.len else index[row]; } pub fn textLine(t: *Text, text: []const u8, row: usize) []const u8 { const index = contentIndex(t, text) orelse return modal.lineSlice(text, row); if (row >= index.len) return ""; const start = index[row]; const end = if (row + 1 < index.len) index[row + 1] - 1 else text.len; return text[start..end]; } test "indexed text rows preserve fragment and allocation failure semantics" { var allocator = std.testing.FailingAllocator.init(std.testing.allocator, .{}); const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true }); defer p.deinit(); const pane = try p.setTestFile("first\n\tλ界\n\nlast\n"); const text = pane.file.?.content; allocator.fail_index = allocator.alloc_index; for (0..lineCount(text) + 2) |row| try std.testing.expectEqualStrings(modal.lineSlice(text, row), textLine(&pane.body, text, row)); try std.testing.expectEqual(@as(usize, 0), pane.file.?.line_starts.len); allocator.fail_index = std.math.maxInt(usize); _ = try lineIndex(p.gpa, &pane.file.?); allocator.fail_index = allocator.alloc_index; const allocations = allocator.allocations; for ([_][]const u8{ text, text[2..], text[0..8], "other\nrows\n\n", "" }) |fragment| { for (0..lineCount(fragment) + 2) |row| try std.testing.expectEqualStrings( modal.lineSlice(fragment, row), textLine(&pane.body, fragment, row), ); } try std.testing.expectEqual(allocations, allocator.allocations); } pub fn textLineCount(t: *Text, text: []const u8) usize { const index = contentIndex(t, text) orelse return modal.cursorLineCount(text); return index.len; } pub fn textPosition(t: *Text, text: []const u8, offset: usize) modal.Cursor { const index = contentIndex(t, text) orelse return modal.positionAt(text, offset); const bounded = @min(offset, text.len); const row = std.sort.upperBound(usize, index, bounded, ascending) - 1; const start = index[row]; const end = if (row + 1 < index.len) index[row + 1] - 1 else text.len; return .{ .row = row, .col = modal.graphemeStart(text[start..end], @min(bounded - start, end - start)) }; } pub fn open(p: *Pardes, id: usize, path: []const u8, line: usize) !*Pane { std.debug.assert(p.panes[id] == null and !p.reserved_slots[id]); const path_copy = try p.gpa.dupe(u8, path); errdefer p.gpa.free(path_copy); const pane = try terminal.createDoc(p.gpa, p.view().screen_w, p.view().screen_h); errdefer p.gpa.destroy(pane); const history = try History.create(p.gpa); errdefer p.gpa.destroy(history); p.reserved_slots[id] = true; defer p.reserved_slots[id] = false; const content = try filesystem.read(p, path); errdefer p.gpa.free(content); const total = lineCount(content); const scroll: usize = if (line > 0 and line <= total) line - 1 else 0; pane.file = .{ .path = path_copy, .content = content, .scroll = scroll, .history = history }; pane.body.cur_pinned = true; pane.body.cur_row = @intCast(scroll); p.installPane(id, pane); if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); return pane; } pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (file.output == null) for (p.panes, 0..) |slot, id| { if (slot == pane) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); }; // A twin's text, undo and shared index stay with the twins left. var twins: [pardes.MAX_PANES]usize = undefined; const ntwins = twinsOf(p, file, &twins); const index_shared = for (twins[0..ntwins]) |id| { if (p.panes[id].?.file.?.line_starts.ptr == file.line_starts.ptr) break true; } else false; p.gpa.free(file.path); // Raw, as every text is freed (a replica's rollback copy keeps the // buffers it was taken over, src/rollback.zig, and a safe build's free // writes over what it frees). if (ntwins == 0) pardes.memory.freeRaw(p.gpa, textRoom(file)); if (file.listing) |listing| p.gpa.free(listing.names); if (file.mini) |*mini| mini.deinit(p.gpa); if (file.line_starts.len > 0 and !index_shared) pardes.memory.freeRaw(p.gpa, indexRoom(file)); if (file.highlights.len > 0) p.tree_sitter_gpa.free(file.highlights); p.gpa.free(file.pager_spans); locations.freeRows(p.gpa, file.location_rows); if (file.context_declarations.len > 0) p.tree_sitter_gpa.free(file.context_declarations); if (file.node_tree) |tree| syntax.freeTree(tree); for (file.node_history.items) |step| p.gpa.free(step.ranges); file.node_history.deinit(p.gpa); file.tables.deinit(p.gpa); if (ntwins > 0) return; file.history.destroy(p.gpa); } /// The edit `replace` makes told to the event readers, before it is made. fn reportEdit(p: *Pardes, f: *State, off: usize, len: usize, bytes: []const u8) void { if (p.fs.listeners == 0) return; const id = for (p.panes, 0..) |slot, i| { const pane = slot orelse continue; if (pane.file) |*state| if (state == f) break i; } else return; pardes.ctlfs.events.noteEdit(p, id, false, f.content, off, len, bytes); } pub fn setContent(p: *Pardes, f: *State, new: []u8) void { setContentSpan(p, f, new, null); } /// `setContent` for a splice that knows what it left alone at either end, /// so it is not found again by comparing the whole text. The edit is what /// lies between them, made by `edit`, which takes `new`, a whole text its /// caller made anyway, as the text's buffer. pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { const span = span_in orelse spanOf(f.content, new); _ = edit(p, f, span.head, f.content.len - span.head - span.tail, new[span.head .. new.len - span.tail], new, true, null); } /// The one change a text takes: the `len` bytes at `off` replaced by /// `bytes`, in its own buffer, so an edit costs its bytes and what follows /// them, never a copy of the text. False with no memory for it, and /// nothing changed. pub fn replace(p: *Pardes, f: *State, off: usize, len: usize, bytes: []const u8) bool { return edit(p, f, off, len, bytes, null, true, null); } /// A replacement that takes out at least this much, and most of the text, /// keeps the old buffer for its undo (Text.Journal.swap) instead of copying /// what it takes out. const swap_min = 64 << 10; /// `replace`, or with `whole` that text's new buffer, `bytes` its middle, /// taken as it is. Its room comes first, so an edit with no memory for it /// changes nothing; then what holds a place in the text moves through the /// edit (twins, other views, readers of its events, its line index, the /// journal when `journaled`: an undo's own edits are not), the bytes go in /// and it is a new revision. An edit that takes out most of the text puts /// a new buffer in and gives the old to the journal whole (a swap); an /// undo's swap back hands the buffer it replaces to `kept`. fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, whole: ?[]u8, journaled: bool, kept: ?*[]u8) bool { const old_len = f.content.len; const new_len = old_len - len + bytes_in.len; const span: Span = .{ .head = off, .tail = old_len - off - len }; var twins: [pardes.MAX_PANES]usize = undefined; const ntwins = twinsOf(p, f, &twins); const room = textRoom(f); // Bytes of the text's own (a move within it) are copied out first: the // text moves under them. const own = whole == null and @intFromPtr(bytes_in.ptr) < @intFromPtr(room.ptr) + room.len and @intFromPtr(bytes_in.ptr) + bytes_in.len > @intFromPtr(room.ptr); const bytes = if (own) p.scratch.allocator().dupe(u8, bytes_in) catch return false else bytes_in; const swapping = journaled and len >= swap_min and len * 2 >= old_len; // A text a join is still sending stays as it is (memory.zig `pin`). const keep = whole == null and (swapping or pardes.memory.isPinned(f.content)); const fresh: ?[]u8 = whole orelse if (keep) copySpliced(p.gpa, f.content, off, len, bytes) catch return false else null; if (fresh == null) { grow(p.gpa, f, new_len) catch return false; // The twins' buffer is this one, which may have moved. for (twins[0..ntwins]) |id| { const tf = &p.panes[id].?.file.?; tf.content = f.content; tf.cap = f.cap; } } // The views not live keep their place in it as twins do (views.zig). const viewed = pardes.views.several(p); var parked: ParkedPlaces = .{}; if (viewed) parked.before(p, f, span, new_len); defer if (viewed) parked.after(p); var moved: [pardes.MAX_PANES]struct { cursor: usize, scroll: usize } = undefined; for (twins[0..ntwins], 0..) |id, i| { const t = p.panes[id].?; const tf = &t.file.?; const at = lineStart(p.gpa, tf, @intCast(@max(0, t.body.cur_row))) + @as(usize, @intCast(@max(0, t.body.cur_col))); moved[i] = .{ .cursor = spliced(@min(at, old_len), old_len, new_len, span), .scroll = spliced(lineStart(p.gpa, tf, tf.scroll), old_len, new_len, span) }; } const old_starts = f.line_starts.ptr; // An edit drops the rows and the colours, made for the text it changes. locations.freeRows(p.gpa, f.location_rows); f.location_rows = &.{}; p.gpa.free(f.pager_spans); f.pager_spans = &.{}; for (p.panes) |slot| if (slot) |pane| { if (pane.file) |*file| if (file == f) { for (0..pane.sel.len) |button| { pane.clearPointerSelection(button); pane.sel[button].state = .none; } }; }; reportEdit(p, f, off, len, bytes); if (f.mini) |*mini| mini.deinit(p.gpa); f.mini = null; if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes); if (journaled and !swapping) f.history.journal.record(p.gpa, f.content, off, len, bytes.len); // ponytail: a whole-text hash per edit that keeps the saved length; a // rolling hash when that too matters. if (f.revision == f.saved_revision or f.saved_hash == null) { f.saved_hash = std.hash.Wyhash.hash(0, f.content); f.saved_len = f.content.len; } var undo_gone = false; if (fresh) |new| { const old = textRoom(f); f.content = new[0..new_len]; f.cap = new.len; if (kept) |k| { k.* = old; } else if (swapping) { undo_gone = f.history.journal.swap(p.gpa, old, old_len, new_len); } else { // Raw: `free` overwrites a safe build's freed memory, a whole // file's worth. pardes.memory.freeRaw(p.gpa, old); } } else { const buffer = textRoom(f); pardes.rollback.Epoch.wrote(p.gpa, u8, buffer, old_len, new_len, off, buffer[off..][0..len], bytes.len); const tail = old_len - off - len; if (bytes.len != len) @memmove(buffer[off + bytes.len ..][0..tail], buffer[off + len ..][0..tail]); @memcpy(buffer[off..][0..bytes.len], bytes); f.content = buffer[0..new_len]; f.cap = buffer.len; } f.revision +%= 1; f.splice = .{ .revision = f.revision, .head = span.head, .tail = span.tail, .old_len = old_len }; if (f.saved_hash) |h| if ((f.saved_len orelse new_len) == new_len and std.hash.Wyhash.hash(0, f.content) == h) { f.saved_revision = f.revision; }; // The clock the session that recorded read (src/tape.zig). f.mtime = pardes.tape.value(.mtime, pardes.ctlfs.events.now, .{}); if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); f.highlights = &.{}; f.highlight_start = 0; f.syntax_dirty = true; if (f.pager) f.pager_diff = looksLikeDiff(f.content[0..@min(new_len, 64 * 1024)]); if (undo_gone) for (p.panes, 0..) |slot, id| { if (slot) |pane| if (pane.file) |*state| if (state == f) p.setMessage(id, "undo keeps the last two texts replaced whole: the steps before them are gone"); }; for (twins[0..ntwins], 0..) |id, i| { const t = p.panes[id].?; const tf = &t.file.?; // Its own index, built while the shared one was empty, goes; the // shared one is this one's, moved through the edit. if (tf.line_starts.len > 0 and tf.line_starts.ptr != old_starts) pardes.memory.freeRaw(p.gpa, indexRoom(tf)); tf.content = f.content; tf.cap = f.cap; tf.line_starts = f.line_starts; tf.line_cap = f.line_cap; tf.revision = f.revision; tf.saved_revision = f.saved_revision; tf.saved_hash = f.saved_hash; tf.saved_len = f.saved_len; tf.mtime = f.mtime; if (tf.highlights.len > 0) p.tree_sitter_gpa.free(tf.highlights); tf.highlights = &.{}; tf.highlight_start = 0; tf.syntax_dirty = true; locations.freeRows(p.gpa, tf.location_rows); tf.location_rows = &.{}; if (tf.mini) |*mini| mini.deinit(p.gpa); tf.mini = null; for (0..t.sel.len) |button| { t.clearPointerSelection(button); t.sel[button].state = .none; } t.body.vsel.active = false; t.body.nsel = 0; const cursor = positionIn(p.gpa, tf, moved[i].cursor); t.body.cur_row = @intCast(cursor.row); t.body.cur_col = @intCast(cursor.col); tf.scroll = positionIn(p.gpa, tf, moved[i].scroll).row; } return true; } /// Room in `f`'s buffer for `n` bytes, grown by an eighth more when it must /// be: in place where its allocator can, else into a new buffer. fn grow(gpa: std.mem.Allocator, f: *State, n: usize) !void { const room = textRoom(f); if (n <= room.len) return; const want = n + @max(n / 8, 4096); if (room.len > 0) if (gpa.remap(room, want)) |grown| { f.content = grown[0..f.content.len]; f.cap = grown.len; return; }; const fresh = try pardes.memory.unfilled(gpa, want); @memcpy(fresh[0..f.content.len], f.content); pardes.memory.freeRaw(gpa, room); f.content = fresh[0..f.content.len]; f.cap = fresh.len; // The old buffer's pages back to the system now: an arena keeps a freed // run of a megabyte or more resident for the next block its size, which // a text grown with room never asks for (a 100 MB file held its copy). if (room.len >= 1 << 20) pardes.memory.trim(); } /// `text` with the `len` bytes at `off` replaced by `bytes`, in a new buffer. fn copySpliced(gpa: std.mem.Allocator, text: []const u8, off: usize, len: usize, bytes: []const u8) ![]u8 { const out = try pardes.memory.unfilled(gpa, text.len - len + bytes.len); @memcpy(out[0..off], text[0..off]); @memcpy(out[off..][0..bytes.len], bytes); @memcpy(out[off + bytes.len ..], text[off + len ..]); return out; } /// Each other view's cursor and scroll on a text being changed, as offsets /// moved by the splice (`spliced`), put back as places in the new text once /// it is in: a view not live keeps its place, as a twin does. Its /// selections go, as a twin's do. const ParkedPlaces = struct { const max = pardes.MAX_PANES * pardes.views.max; at: [max]struct { pane: *Pane, view: u8, cursor: usize, scroll: usize } = undefined, n: usize = 0, fn before(pp: *ParkedPlaces, p: *Pardes, f: *State, span: Span, new_len: usize) void { for (p.panes) |slot| { const pane = slot orelse continue; const tf = if (pane.file) |*file| file else continue; if (tf != f and (f.twin == 0 or tf.twin != f.twin)) continue; for (0..pardes.views.max) |k| { if (k == p.live or !pardes.views.isOpen(p, @intCast(k)) or pp.n == max) continue; const v = &pane.pv[k]; const at = lineStart(p.gpa, tf, @intCast(@max(0, v.body.cur_row))) + @as(usize, @intCast(@max(0, v.body.cur_col))); pp.at[pp.n] = .{ .pane = pane, .view = @intCast(k), .cursor = spliced(@min(at, f.content.len), f.content.len, new_len, span), .scroll = spliced(@min(lineStart(p.gpa, tf, v.scroll), f.content.len), f.content.len, new_len, span), }; pp.n += 1; } } } fn after(pp: *ParkedPlaces, p: *Pardes) void { for (pp.at[0..pp.n]) |a| { const tf = &(a.pane.file orelse continue); const v = &a.pane.pv[a.view]; const cursor = positionIn(p.gpa, tf, @min(a.cursor, tf.content.len)); v.body.cur_row = @intCast(cursor.row); v.body.cur_col = @intCast(cursor.col); v.body.vsel.active = false; v.body.nsel = 0; v.scroll = positionIn(p.gpa, tf, @min(a.scroll, tf.content.len)).row; } } }; /// Where an offset into the old text is in the new, after the splice that /// kept `span` at either end: past it, moved by what it added; inside it, /// at its start, as acme moves a clone's q0 (textinsert, textdelete). fn spliced(at: usize, old_len: usize, new_len: usize, span: Span) usize { if (at <= span.head) return at; if (at >= old_len - span.tail) return at - (old_len - span.tail) + (new_len - span.tail); return span.head; } /// What `old` and `new` agree on at either end. /// What `new` kept of `old` at either end, compared 32 bytes at a time /// (a byte at a time was most of a `d` in a 7 MB file). pub fn spanOf(old: []const u8, new: []const u8) Span { const block = 32; const n = @min(old.len, new.len); var head: usize = 0; while (head + block <= n and std.mem.eql(u8, old[head..][0..block], new[head..][0..block])) head += block; while (head < n and old[head] == new[head]) head += 1; var tail: usize = 0; while (tail + block <= n - head and std.mem.eql(u8, old[old.len - tail - block ..][0..block], new[new.len - tail - block ..][0..block])) tail += block; while (tail < n - head and old[old.len - 1 - tail] == new[new.len - 1 - tail]) tail += 1; return .{ .head = head, .tail = tail }; } test "spanOf by blocks finds the byte loop's ends" { var prng: std.Random.DefaultPrng = .init(5); const random = prng.random(); var a: [160]u8 = undefined; var b: [160]u8 = undefined; for (0..4000) |_| { const la = random.uintLessThan(usize, a.len); const lb = random.uintLessThan(usize, b.len); for (a[0..la]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)]; const keep = @min(la, lb); @memcpy(b[0..keep], a[0..keep]); for (b[keep..lb]) |*c| c.* = "ab"[random.uintLessThan(usize, 2)]; if (lb > 0 and random.boolean()) b[random.uintLessThan(usize, lb)] ^= 1; const old = a[0..la]; const new = b[0..lb]; const n = @min(old.len, new.len); const head = std.mem.indexOfDiff(u8, old[0..n], new[0..n]) orelse n; var tail: usize = 0; while (tail < n - head and old[old.len - 1 - tail] == new[new.len - 1 - tail]) tail += 1; try std.testing.expectEqual(Span{ .head = head, .tail = tail }, spanOf(old, new)); } } fn positionIn(gpa: std.mem.Allocator, f: *State, at: usize) struct { row: usize, col: usize } { const index = lineIndex(gpa, f) catch { const c = modal.positionAt(f.content, at); return .{ .row = c.row, .col = c.col }; }; const row = std.sort.upperBound(usize, index, at, ascending) -| 1; return .{ .row = row, .col = at - index[row] }; } /// The other panes on `f`'s buffer (Zerox), by id. pub fn twinsOf(p: *Pardes, f: *const State, out: *[pardes.MAX_PANES]usize) usize { if (f.twin == 0) return 0; var n: usize = 0; for (p.panes, 0..) |slot, id| { const pane = slot orelse continue; const other = if (pane.file) |*file| file else continue; if (other == f or other.twin != f.twin) continue; out[n] = id; n += 1; } return n; } /// acme's Zerox: pane `to` made on `from`'s buffer, its text, undo and /// dirty state shared, its scroll and cursor its own. An edit in either is /// spliced into both (setContentSpan, O(the edit) more); what Save and the /// disk watch change on one is copied to the others after each step /// (syncTwins). An event reader on one hears no edit made in another. pub fn zerox(p: *Pardes, from: usize, to: usize) !*Pane { const src = p.panes[from].?; const f = &src.file.?; const path = try p.gpa.dupe(u8, f.path); errdefer p.gpa.free(path); if (f.twin == 0) { const group = try p.gpa.dupe(u8, f.path); errdefer p.gpa.free(group); try p.twin_groups.append(p.gpa, .{ .id = p.next_twin, .saved_revision = f.saved_revision, .saved_hash = f.saved_hash, .saved_len = f.saved_len, .disk_newer = f.disk_newer, .disk_gone = f.disk_gone, .path = group }); f.twin = p.next_twin; p.next_twin +%= 1; } const pane = try p.newDocPane(to); pane.file = .{ .path = path, .content = f.content, .cap = f.cap, .line_starts = f.line_starts, .line_cap = f.line_cap, .history = f.history, .revision = f.revision, .saved_revision = f.saved_revision, .saved_hash = f.saved_hash, .saved_len = f.saved_len, .disk_newer = f.disk_newer, .disk_gone = f.disk_gone, .mtime = f.mtime, .scroll = f.scroll, .tree_context = f.tree_context, .twin = f.twin, }; pane.body.cur_row = src.body.cur_row; pane.body.cur_col = src.body.cur_col; pane.body.cur_pinned = true; if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(to), .on = true } }); return pane; } /// A twin closed: it keeps a text of its own until it is torn down (a /// retired pane is drawn going), and the twins keep the shared one, its /// undo and its index. pub fn leaveTwins(p: *Pardes, f: *State) void { var twins: [pardes.MAX_PANES]usize = undefined; const n = twinsOf(p, f, &twins); f.twin = 0; if (n == 0) return; const shared_index = for (twins[0..n]) |id| { if (p.panes[id].?.file.?.line_starts.ptr == f.line_starts.ptr) break true; } else false; if (shared_index) { f.line_starts = &.{}; f.line_cap = 0; } f.content = p.gpa.dupe(u8, f.content) catch p.gpa.alloc(u8, 0) catch &.{}; f.cap = 0; f.history = History.create(p.gpa) catch f.history; // The one left is no twin now (syncTwins drops the group). if (n == 1) p.panes[twins[0]].?.file.?.twin = 0; } pub const TwinGroup = struct { id: u32, saved_revision: u32, saved_hash: ?u64, saved_len: ?usize, disk_newer: ?u64, disk_gone: bool, path: []u8, }; /// After each step: what a Save, a get, the disk watch or a rename changed /// on one twin is copied to the others; a group down to one pane is no /// group. O(panes) a step, and nothing at all with no twins. pub fn syncTwins(p: *Pardes) void { var g: usize = 0; while (g < p.twin_groups.items.len) { const group = &p.twin_groups.items[g]; var members: [pardes.MAX_PANES]*State = undefined; var member_panes: [pardes.MAX_PANES]*Pane = undefined; var n: usize = 0; for (p.panes) |slot| { const pane = slot orelse continue; const f = if (pane.file) |*file| file else continue; if (f.twin == group.id) { members[n] = f; member_panes[n] = pane; n += 1; } } if (n < 2) { if (n == 1) members[0].twin = 0; p.gpa.free(group.path); _ = p.twin_groups.swapRemove(g); continue; } for (members[0..n]) |f| { if (f.saved_revision == group.saved_revision and f.saved_hash == group.saved_hash and f.saved_len == group.saved_len and f.disk_newer == group.disk_newer and f.disk_gone == group.disk_gone and std.mem.eql(u8, f.path, group.path)) continue; // This one changed: the group, and every other twin, take it. if (!std.mem.eql(u8, f.path, group.path)) { const path = p.gpa.dupe(u8, f.path) catch break; p.gpa.free(group.path); group.path = path; } group.saved_revision = f.saved_revision; group.saved_hash = f.saved_hash; group.saved_len = f.saved_len; group.disk_newer = f.disk_newer; group.disk_gone = f.disk_gone; for (members[0..n], member_panes[0..n]) |other, other_pane| { if (other == f) continue; other.saved_revision = f.saved_revision; other.saved_hash = f.saved_hash; other.saved_len = f.saved_len; other.disk_newer = f.disk_newer; other.disk_gone = f.disk_gone; if (!std.mem.eql(u8, other.path, f.path)) { const path = p.gpa.dupe(u8, f.path) catch continue; p.gpa.free(other.path); other.path = path; // Renamed, each twin says so, as the one renamed did. pardes.ctlfs.events.noteLog(p, .rename, other_pane); } } break; } // Tests hold the twins to one text, one revision, every step. if (builtin.is_test) for (members[1..n]) |other| { std.debug.assert(other.content.ptr == members[0].content.ptr and other.content.len == members[0].content.len); std.debug.assert(other.revision == members[0].revision); }; g += 1; } } /// Whether `head`, a text's first bytes, holds a diff, as a command pane's /// rows are told one (diff.looksLikeDiff, its first 200 lines). pub fn looksLikeDiff(head: []const u8) bool { var lines: [200][]const u8 = undefined; var it = std.mem.splitScalar(u8, head, '\n'); var n: usize = 0; while (n < lines.len) : (n += 1) lines[n] = it.next() orelse break; return diff.looksLikeDiff(lines[0..n]); } test "a +Pager pane holding a diff is drawn and looked at as one; holding other text, as text" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); const pane = try p.setTestFile(""); const f = &pane.file.?; p.gpa.free(f.path); f.path = try p.gpa.dupe(u8, "/tmp/+Pager"); f.pager = true; setContent(p, f, try p.gpa.dupe(u8, "commit abc\n\ndiff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a\n+b\n")); try std.testing.expect(pane.colorAlgo() == .diff); try std.testing.expect(pardes.look.diffLines(p, pane) != null); setContent(p, f, try p.gpa.dupe(u8, "commit abc\n\n a plain log\n")); try std.testing.expect(pane.colorAlgo() != .diff); } test "a pager's text is a diff when it holds a diff header or a unified hunk" { try std.testing.expect(looksLikeDiff("commit abc\nAuthor: x\n\n msg\n\ndiff --git a/f b/f\n")); try std.testing.expect(looksLikeDiff("--- a/f\n+++ b/f\n@@ -1 +1 @@\n-a\n+b\n")); try std.testing.expect(!looksLikeDiff("commit abc\nAuthor: x\n\n plain log\n")); } pub fn restoreSnap(pane: *Pane, f: *State, snap: Text.Place) void { const n = nlines(pane.gpa, f); const row: usize = @min(@as(usize, @intCast(@max(0, snap.cur_row))), n - 1); const llen = modal.lineSlice(f.content, row).len; pane.body.cur_row = @intCast(row); pane.body.cur_col = @intCast(@min(@as(usize, @intCast(@max(0, snap.cur_col))), llen)); pane.body.vsel = snap.vsel; pane.body.cur_pinned = true; pane.body.sticky_col = -1; pane.ensureCursorVisible(); } /// An undo step's edge, made before an edit (Text.Journal.mark): the edits /// after it are one step back to the pane's place now. pub fn pushUndo(p: *Pardes, pane: *Pane) void { const f = if (pane.file) |*file| file else return; f.history.journal.mark(p.gpa, f.content, placeOf(pane, f)); } /// Undo (back) or redo one step: its edits turned round in the text, and the /// place and name it began at (or, redone, was undone from) back. pub fn undoStep(p: *Pardes, pane: *Pane, back: bool) void { const f = if (pane.file) |*file| file else return; const j = &f.history.journal; if (if (back) j.done == 0 else j.done == j.steps.items.len) return; const at = if (back) j.done - 1 else j.done; const now = placeOf(pane, f); if (!turn(p, f, at, back)) return; const s = &j.steps.items[at]; if (back) { s.after = now; j.done -= 1; } else { s.before = now; j.done += 1; } const to = if (back) s.before else s.after; restoreName(p, pane, f, to); restoreSnap(pane, f, to); } /// Step `at`'s entries turned round in `f`'s text: each one's bytes put /// back and the bytes it had put in kept in its place, an undo's last first /// and a redo's first first. One entry is turned in place, its bytes alone /// moved; several in a copy of the text whose gap goes from one to the next /// (`Gap`), put in as one edit; none is an edit that changes nothing, as /// the snapshot equal to the text was put in once. A step that began by /// replacing the text whole has kept its old buffer: that buffer goes back /// in and the step becomes one swap the other way, holding the text it /// ended on for its redo, and no byte moves. False with no memory, nothing /// changed. fn turn(p: *Pardes, f: *State, at: usize, back: bool) bool { const j = &f.history.journal; const step = j.steps.items[at]; const entries = j.entries.items[step.first..][0..step.n]; if (entries.len == 0) return edit(p, f, f.content.len, 0, "", null, false, null); if (entries[0].swap and (back or entries.len == 1)) { const e = &entries[0]; const now_len = f.content.len; const back_len = e.removed.len; const span = spanOf(f.content, e.removed); var out: []u8 = undefined; if (!edit(p, f, span.head, now_len - span.head - span.tail, e.removed[span.head .. back_len - span.tail], e.removed.ptr[0..@max(e.room, back_len)], false, &out)) return false; j.fold(p.gpa, at); e.* = .{ .off = 0, .removed = out[0..now_len], .ins = back_len, .swap = true, .room = out.len }; return true; } if (entries.len == 1) { const e = &entries[0]; const put = pardes.memory.dupeUnfilled(p.gpa, f.content[e.off..][0..e.ins]) catch return false; if (!edit(p, f, e.off, e.ins, e.removed, null, false, null)) { pardes.memory.freeRaw(p.gpa, put); return false; } j.bytes = j.bytes - e.removed.len + put.len; e.ins = e.removed.len; pardes.memory.freeRaw(p.gpa, e.removed); e.removed = put; return true; } // The gap's room: what the entries put back past what they take out. var room: usize = 0; for (entries) |e| room += e.removed.len -| e.ins; var gap = Gap.init(p.gpa, f.content, room) catch return false; const puts = p.scratch.allocator().alloc([]u8, entries.len) catch { pardes.memory.freeRaw(p.gpa, gap.buf); return false; }; @memset(puts, &.{}); for (0..entries.len) |k| { const i = if (back) entries.len - 1 - k else k; const e = entries[i]; puts[i] = gap.replace(p.gpa, e.off, e.ins, e.removed) catch { for (puts) |put| pardes.memory.freeRaw(p.gpa, put); pardes.memory.freeRaw(p.gpa, gap.buf); return false; }; } const new = gap.whole(); const span = spanOf(f.content, new); if (!edit(p, f, span.head, f.content.len - span.head - span.tail, new[span.head .. new.len - span.tail], gap.buf, false, null)) { for (puts) |put| pardes.memory.freeRaw(p.gpa, put); pardes.memory.freeRaw(p.gpa, gap.buf); return false; } for (entries, puts) |*e, put| { const was = e.*; e.* = .{ .off = was.off, .removed = put, .ins = was.removed.len }; j.bytes = j.bytes - Text.Journal.held(was) + Text.Journal.held(e.*); Text.Journal.free(p.gpa, was); } return true; } /// A copy of a text with a gap where it was last written (`turn`): moving /// the gap costs the bytes between two places, never the text, so a step of /// many entries (a key typed at many ranges) is turned in one pass. const Gap = struct { /// The text is `buf[0..lo]` then `buf[hi..]`. buf: []u8, lo: usize, hi: usize, fn init(gpa: std.mem.Allocator, bytes: []const u8, room: usize) !Gap { const buf = try pardes.memory.unfilled(gpa, bytes.len + room); @memcpy(buf[0..bytes.len], bytes); return .{ .buf = buf, .lo = bytes.len, .hi = buf.len }; } fn move(g: *Gap, at: usize) void { if (at < g.lo) { const n = g.lo - at; @memmove(g.buf[g.hi - n .. g.hi], g.buf[at..g.lo]); g.lo = at; g.hi -= n; } else if (at > g.lo) { const n = at - g.lo; @memmove(g.buf[g.lo..][0..n], g.buf[g.hi..][0..n]); g.lo += n; g.hi += n; } } /// The `len` bytes at `at` replaced by `bytes` (in the room `init` was /// given); what they were, copied out. fn replace(g: *Gap, gpa: std.mem.Allocator, at: usize, len: usize, bytes: []const u8) ![]u8 { g.move(at); const out = try pardes.memory.dupeUnfilled(gpa, g.buf[g.hi..][0..len]); g.hi += len; @memcpy(g.buf[g.lo..][0..bytes.len], bytes); g.lo += bytes.len; return out; } /// The text whole, at the buffer's start. fn whole(g: *Gap) []u8 { g.move(g.lo + (g.buf.len - g.hi)); return g.buf[0..g.lo]; } }; /// A pane's file deleted on disk: the text stays, and it says so once, /// ` deleted on disk (Save writes it again)`, logged `changed /// deleted`. pub fn deleted(p: *Pardes, id: u8) void { const taped = pardes.tape.enter(p, .{ .file_deleted = id }); defer pardes.tape.leave(p, taped); const pane = p.panes[id] orelse return; const f = if (pane.file) |*file| file else return; if (f.disk_gone) return; f.disk_gone = true; var said: [limits.host_path_cap + 64]u8 = undefined; p.setMessage(id, std.fmt.bufPrint(&said, "{s} deleted on disk (Save writes it again)", .{f.path}) catch "deleted on disk (Save writes it again)"); var rec: [32]u8 = undefined; pardes.ctlfs.events.notePath(p, "changed", std.fmt.bufPrint(&rec, "{d} deleted", .{pane.serial}) catch ""); } /// A file rewritten on disk. A clean buffer takes the new text, committed /// onto the same undo history as typed edits. A buffer with unsaved edits /// is never reloaded under them: it keeps its text and stays dirty, and /// says so once (` changed on disk (get reloads it, Save overwrites /// it)`, logged `changed `); get then asks before throwing /// the edits away and Save before overwriting the newer file. acme reloads /// nothing by itself: its get asks (winclean) and its Put refuses a file /// modified since it was read (exec.c:577). pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void { const pane = p.panes[id] orelse return; const f = if (pane.file) |*file| file else return; f.disk_gone = false; if (std.mem.eql(u8, f.content, bytes)) return; if (f.saved_hash) |saved| if (f.revision != f.saved_revision and std.hash.Wyhash.hash(0, f.content) != saved) { const disk = std.hash.Wyhash.hash(0, bytes); // The disk holds what the text was read or saved as: no news, only // a watch looking again (an undo back to this name, a restart). if (disk == saved) return; if (f.disk_newer == disk) return; f.disk_newer = disk; f.disk_newer_warned = false; var said: [limits.host_path_cap + 64]u8 = undefined; p.setMessage(id, std.fmt.bufPrint(&said, "{s} changed on disk (get reloads it, Save overwrites it)", .{f.path}) catch "changed on disk (get reloads it, Save overwrites it)"); var rec: [16]u8 = undefined; pardes.ctlfs.events.notePath(p, "changed", std.fmt.bufPrint(&rec, "{d}", .{pane.serial}) catch ""); return; }; f.disk_newer = null; // Logged as the dirty case is, with what became of it. var rec: [32]u8 = undefined; pardes.ctlfs.events.notePath(p, "changed", std.fmt.bufPrint(&rec, "{d} reloaded", .{pane.serial}) catch ""); const new = p.gpa.dupe(u8, bytes) catch return; pushUndo(p, pane); setContent(p, f, new); f.saved_revision = f.revision; restoreSnap(pane, f, .{ .cur_row = pane.body.cur_row, .cur_col = pane.body.cur_col, .vsel = pane.body.vsel }); } /// re-highlight the visible window of any file whose syntax went stale /// (edit, scroll, load) — visible-range-first so big files stay snappy /// The declarations a TreeContext pane's context rows are drawn from, for /// its text as it is now: found in the step (Pardes.sync), never only by a /// render, since the view's geometry is made of them (`geometry`). pub fn refreshContext(p: *Pardes, pane: *Pane, f: *State) void { if (!f.tree_context or !supportsContext(pane) or f.context_revision == f.revision) return; const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch return; if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); f.context_declarations = declarations; f.context_revision = f.revision; } pub fn refreshHighlights(p: *Pardes) void { const tz = tracy.zone(@src(), "refreshHighlights"); defer tz.end(); for (p.panes) |slot| { const pane = slot orelse continue; if (pane.file == null) continue; const f = &pane.file.?; if (f.mini != null) { f.syntax_dirty = false; continue; } if (!f.syntax_dirty) continue; // ponytail: a stretch over `long_paint` bytes (a minified line) is // painted only around the caret, so a key typed in a 500 KB line // parses 16 KB, not the line (190 ms a key in ReleaseSafe; 25 ms at // 64 KB, so 16, still eight views of 80x25); its first // tokens may be coloured as if the line began there. The upgrade: an // incremental parse of the whole text, edited in place. const caret = @min(f.content.len, lineStart(p.gpa, f, @intCast(@max(0, pane.body.cur_row))) + @as(usize, @intCast(@max(0, pane.body.cur_col)))); const around = struct { fn clamp(a: usize, b: usize, at: usize) [2]usize { if (b - a <= long_paint) return .{ a, b }; const lo = std.math.clamp(at -| long_paint / 2, a, b - long_paint); return .{ lo, lo + long_paint }; } }; const need = around.clamp(lineStart(p.gpa, f, f.scroll), @max(lineStart(p.gpa, f, f.scroll), lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS)), caret); const need_start = need[0]; const need_end = need[1]; if ((!f.tree_context or (f.highlight_start == 0 and f.highlights.len == f.content.len)) and f.highlights.len > 0 and need_start >= f.highlight_start and need_end <= f.highlight_start + f.highlights.len) { f.syntax_dirty = false; if (!f.tree_context and pane.colorAlgo() == .source) paintAhead(p, pane, f); continue; } // A view far from what is painted (a jump: ge, a Look) paints only // itself on the frame, a third of the work; the stretch around it // comes painted ahead (paintAhead) where there are workers. const far = f.highlights.len == 0 or need_end <= f.highlight_start or need_start >= f.highlight_start + f.highlights.len; const slack: usize = if (far and p.host.vtable.search != null) 0 else if (f.highlights.len == 0) 0 else pane.rows; const whole = pane.colorAlgo() == .locations or f.tree_context; // Painted ahead on a worker (Stream.paintAhead): taken, no parse. if (!whole and pane.colorAlgo() == .source) if (Stream.takeAhead(p, pane.serial, f.revision, need_start, need_end)) |got| { if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); f.highlights = got.styles; f.highlight_start = got.start; f.syntax_dirty = false; p.stream.painted_ahead += 1; paintAhead(p, pane, f); continue; }; if (pane.colorAlgo() == .source and !whole) p.stream.painted_on_loop += 1; const wide_start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack); const wide_end = if (whole) f.content.len else @max(wide_start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS + slack)); const window = if (whole) [2]usize{ wide_start, wide_end } else around.clamp(wide_start, wide_end, caret); const start = window[0]; const end = window[1]; const new_highlights = (switch (pane.colorAlgo()) { .none => @as([]u8, &.{}), .diff => syntax.highlightDiff(p.tree_sitter_gpa, f.content, start, end), .locations => if (f.location_rows.len > 0) syntax.highlightLocationRows(p.tree_sitter_gpa, f.content, f.location_rows) else syntax.highlightLocations(p.tree_sitter_gpa, f.content, start, end), else => syntax.highlightFileRange(p.tree_sitter_gpa, f.path, f.content, start, end), }) catch { f.syntax_dirty = false; continue; }; if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); f.highlights = new_highlights; f.highlight_start = if (f.highlights.len > 0) start else 0; f.syntax_dirty = false; if (!whole and pane.colorAlgo() == .source) paintAhead(p, pane, f); } } /// The stretch a scroll of `pane` is heading into, the view and three more /// the way it last went, asked of a worker (Stream.paintAhead) so it is /// painted by the time the view gets there. From the view itself: two /// views out, an upward stretch stopped short of the view's own bottom /// rows, and a half page up (or down) missed it by a line and painted on /// the frame, 11-17 ms in ReleaseSafe. fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void { const seen = &p.stream.ahead_seen; // A view standing still (typing in it) needs nothing ahead: each edit // is a new revision, and a paint asked at every keystroke was a copy // of the stretch and a worker's start on the loop per key. if (seen.serial == pane.serial and f.scroll == seen.scroll) return; if (seen.serial == pane.serial) seen.dir = if (f.scroll > seen.scroll) 1 else -1; if (seen.serial != pane.serial) seen.dir = 1; seen.serial = pane.serial; seen.scroll = f.scroll; const rows: usize = pane.rows; const start = lineStart(p.gpa, f, if (seen.dir > 0) f.scroll else f.scroll -| 3 * rows); const end = @max(start, lineStart(p.gpa, f, f.scroll + (if (seen.dir > 0) 4 * rows else rows) + SYNTAX_CONTEXT_AFTER_ROWS)); // Painted already, as far as that goes. if (f.highlights.len > 0 and start >= f.highlight_start and end <= f.highlight_start + f.highlights.len) return; Stream.paintAhead(p, pane.serial, f.revision, f.path, f.content, start, end); } test "a long line is painted only around the caret" { if (!syntax.enabled) return; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const line = try std.testing.allocator.alloc(u8, 600 << 10); defer std.testing.allocator.free(line); for (line, 0..) |*c, i| c.* = if (i % 2 == 0) '1' else ','; const pane = try p.setTestFile(line); p.gpa.free(pane.file.?.path); pane.file.?.path = try p.gpa.dupe(u8, "/tmp/long.json"); pane.body.cur_col = 300 << 10; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); _ = try p.render(arena.allocator()); const f = &pane.file.?; try std.testing.expect(f.highlights.len == long_paint); try std.testing.expect(f.highlight_start <= 300 << 10 and f.highlight_start + f.highlights.len >= 300 << 10); } test "a caret deep in a long line scrolls the view sideways to it, measured from the view's edge" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); p.settings.wrap = false; const line = try std.testing.allocator.alloc(u8, 500 << 10); defer std.testing.allocator.free(line); @memset(line, 'a'); const pane = try p.setTestFile(line); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); _ = try p.render(arena.allocator()); // Right, far: the caret ends a margin in from the right edge. pane.body.cur_col = 400_000; pane.ensureCursorVisible(); const w: i32 = @as(i32, pane.cols) - @as(i32, gutterWidth(pane)); const hmargin: i32 = @min(config.scroll_off, @divTrunc(w - 1, 2)); try std.testing.expectEqual(@as(i32, 400_000 - (w - 1 - hmargin)), pane.hscroll); // Back left of the view: a margin in from the left edge. pane.body.cur_col = 100_000; pane.ensureCursorVisible(); try std.testing.expectEqual(@as(i32, 100_000 - hmargin), pane.hscroll); // At the start: none. pane.body.cur_col = 1; pane.ensureCursorVisible(); try std.testing.expectEqual(@as(i32, 0), pane.hscroll); } pub fn supportsContext(pane: *const Pane) bool { const file = pane.file orelse return false; return file.output == null and file.mini == null and syntax.supportsPath(file.path); } fn prepareContextRows(pane: *Pane, file: *State) void { pane.context_rows = 0; if (!file.tree_context or !supportsContext(pane) or pane.rows > pane.wrap_line.len) return; const capacity = @min(pane.rows -| 1, pane.context_row_limit orelse std.math.maxInt(u16)); // retain an ordinary body row for (file.context_declarations) |declaration| { if (declaration.start_line >= file.scroll) break; if (declaration.end_line < file.scroll) continue; var row = declaration.start_line; while (row <= declaration.header_end_line and row < file.scroll) : (row += 1) { if (pane.context_rows >= capacity) return; if (pane.context_rows > 0 and row <= pane.wrap_line[pane.context_rows - 1]) continue; pane.wrap_line[pane.context_rows] = @intCast(row); pane.context_rows += 1; } } } pub fn wrapWidth(pane: *const Pane, wrap: bool) usize { if (!wrap or pane.rows > pane.wrap_line.len) return 0; return @max(1, @as(usize, pane.cols -| gutterWidth(pane)) -| 1); } pub const VisualRow = struct { start: usize, end: usize }; pub fn visualRow(line: []const u8, col: usize, width: usize) VisualRow { if (width == 0) return .{ .start = 0, .end = line.len }; var start: usize = 0; while (true) { const end = fitEnd(line, start, width); if (col < end or end >= line.len) return .{ .start = start, .end = end }; start = end; } } /// The body's text as it is shown: its geometry's rows, each a line's /// number and its stretch of the line. pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State) ![]const u8 { geometry(pane); const out = try arena.alloc(u8, pane.geometry_len); const filled = fillBody(out, pane, f, wrapWidth(pane, pane.view.wrap), false); std.debug.assert(filled == out.len); return out; } /// The pane's view geometry for its state as it is now: the wrap map (the /// line, column and pad each body row starts at), the context rows, and how /// many rows the body layer has. Made again when anything it is made of /// changes (`geometryKey`), by whoever asks first, the render or a step that /// reads it: so a core that never draws has the geometry of one that does /// (docs/next-daemon.md). What it reads of the session is the pane's `view`, /// which each step sets. pub fn geometry(pane: *Pane) void { const f = if (pane.file) |*file| file else return; const key = geometryKey(pane, f); if (pane.geometry_key == key) return; pane.geometry_key = key; pane.body_rows = 0; pane.body_visible_rows = 0; pane.context_row_limit = null; pane.geometry_len = mapRows(pane, f); // A body with context rows is a layer taller than the grid's rows, its // context rows at the tagline's height (body_layer.joinBody). const v = pane.view; if (!v.layered or pane.collapsed or v.rect_h <= pane.tag_rows) return; const logical_rows = @min(@as(usize, pane.rows) + pane.context_rows, pane.wrap_line.len); if (pane.context_rows == 0 or logical_rows <= pane.context_rows) return; pane.body_rows = @intCast(logical_rows); const bh: usize = @max(1, v.body_h); const th: usize = std.math.clamp(v.tagline_h, 1, bh); const body_pixels = @as(usize, v.rect_h - pane.tag_rows) * bh; const limit: u16 = @intCast(@min(pane.context_rows, (body_pixels -| bh) / th)); pane.context_row_limit = limit; pane.body_visible_rows = @intCast(@min(logical_rows, limit + @max(1, (body_pixels -| @as(usize, limit) * th) / bh))); pane.geometry_len = mapRows(pane, f); } /// One walk of the rows the body shows, recording the wrap map: the bytes /// its text takes. fn mapRows(pane: *Pane, f: *State) usize { const width = wrapWidth(pane, pane.view.wrap); pane.body_wrapped = width > 0; f.tables.ensure(pane.gpa, f); prepareContextRows(pane, f); return fillBody(null, pane, f, width, true); } /// Everything the geometry is made of, hashed: the text (its revision and /// length), where it is scrolled, the pane's size, its tables as far as /// they have been read, its context declarations, the session's view. fn geometryKey(pane: *const Pane, f: *const State) u64 { const t = &f.tables; const facts = .{ f.revision, f.content.len, f.scroll, pane.hscroll, pane.rows, pane.cols, pane.tag_rows, pane.collapsed, pane.query != null, f.pager, f.mini != null, f.output != null, if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context_revision orelse 0, f.context_declarations.len, f.line_starts.len, t.revision orelse 0, t.list.items.len, if (t.pending) |r| r.at else std.math.maxInt(usize), pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h, pane.view.rect_h, }; // One hash of them laid out as words: every reader of the map asks for // this key, a hundred times a frame. var words: [facts.len]u64 = undefined; inline for (facts, &words) |fact, *w| w.* = if (@TypeOf(fact) == bool) @intFromBool(fact) else if (@typeInfo(@TypeOf(fact)).int.signedness == .signed) @bitCast(@as(i64, fact)) else fact; // Never the "made for nothing" key a reset leaves (`geometry_key = 0`). return std.hash.Wyhash.hash(0x9e0, std.mem.sliceAsBytes(&words)) | 1; } /// Run the file-body row walk. With no destination it is the exact sizing /// pass; with one it fills that allocation and records the wrapping map. fn fillBody(dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize { if (record_wrap) pane.wrap_n = 0; const prefix_width = gutterWidth(pane); const total = nlines(pane.gpa, f); var body_line: usize = f.scroll; var body_at: usize = 0; var written: usize = 0; for (0..if (pane.body_rows > 0) pane.body_rows else pane.rows) |i| { if (i > 0) { if (dst) |out| out[written] = '\n'; written += 1; } const context = i < pane.context_rows; const row = if (context) @as(usize, @intCast(pane.wrap_line[i])) else body_line; const at = if (context) 0 else body_at; const text = if (row < total) sourceLine(pane, @intCast(row)) else ""; var guide_buffer: [1024]u8 = undefined; const guide = if (at > 0 and width > 1 and f.output != null and std.meta.eql(f.output.?.from, Output.Origin{ .query = .callstack })) locations.treeContinuation(text, &guide_buffer, width - 1) else ""; const pad = displayWidth(guide); // Headers use one screen row per source row, preserving signatures // without allowing a long ancestor to consume the whole viewport. const end = if (width == 0 or context) text.len else fitEnd(text, at, width - pad); const cut = if (pane.hscroll > 0 and width == 0) modal.graphemeStart(text, @min(@as(usize, @intCast(pane.hscroll)), text.len)) else at; if ((width > 0 or pane.context_rows > 0) and record_wrap) { pane.wrap_line[i] = @intCast(row); pane.wrap_col[i] = @intCast(cut); pane.wrap_pad[i] = @intCast(pad); pane.wrap_n = @intCast(i + 1); } if (row < total) { var lbuf: [@max(config.PREFIX_W, 32)]u8 = undefined; const prefix = lbuf[0..prefix_width]; @memset(prefix, ' '); if (at == 0 and prefix.len > 0) { var lineno = row + 1; var digit: usize = prefix.len - 1; while (true) { digit -= 1; prefix[digit] = '0' + @as(u8, @intCast(lineno % 10)); lineno /= 10; if (lineno == 0) break; } } if (dst) |out| @memcpy(out[written..][0..prefix.len], prefix); written += prefix.len; if (dst) |out| @memcpy(out[written..][0..guide.len], guide); written += guide.len; // A table row's pads are blanks before their bytes: the row // drawn as the column functions above measure it. var pad_buf: [Tables.max_pads]Tables.Pad = undefined; var from = cut; for (rowPads(pane, @intCast(row), &pad_buf)) |table_pad| { if (table_pad.at <= cut) continue; if (table_pad.at > end) break; if (dst) |out| @memcpy(out[written..][0 .. table_pad.at - from], text[from..table_pad.at]); written += table_pad.at - from; if (dst) |out| @memset(out[written..][0..table_pad.cells], ' '); written += table_pad.cells; from = table_pad.at; } const shown = text[from..end]; if (dst) |out| @memcpy(out[written..][0..shown.len], shown); written += shown.len; } if (!context) { if (width > 0 and end < text.len) { body_at = end; } else { body_line += 1; body_at = 0; } } } return written; } pub fn drawGutter(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, active: bool) void { const prefix_width = gutterWidth(pane); const goff = pane.scroll(); const gcur = terminal.gridCursor(pane); const gcrow = if (pane.body.cur_pinned) pane.body.cur_row else @as(i32, gcur.y) + goff; const typing_body = pane.focus == .body and (pane.prompt == .none or pane.prompt == .del_side or pane.prompt == .repl_choice); const cur_line: i32 = if (active and typing_body) gcrow else std.math.minInt(i32); // the body's first row, the way renderPane derives it const body_y = p.bodyTop(pane, r); const context_bg = p.theme().tag_bg; for (0..@min(pane.context_rows, body_h)) |context_row| { for (0..tw) |col| { const cell = s.at(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(context_row))); cell.default = false; cell.style.bg = .{ .rgb = context_bg }; cell.style.font_role = .tagline; } } var vr: u16 = 0; while (vr < body_h) : (vr += 1) { const row_line: i32 = if (pane.wrap_n == 0) goff + @as(i32, vr) else if (vr < pane.wrap_n) pane.wrap_line[vr] else std.math.maxInt(i32); const on_cursor = row_line == cur_line; var c: u16 = 0; while (c < prefix_width and c < tw) : (c += 1) { const cell = s.at(tx + c, body_y + vr); cell.default = false; // paints blank gutter rows too cell.style.fg = .{ .rgb = if (on_cursor) p.theme().lineno_active orelse p.theme().lineno else p.theme().lineno }; cell.style.bold = on_cursor; } } } const SynStyle = struct { fg: [3]u8, bold: bool }; /// How added and removed diff lines are told apart in every theme: a row /// ground a little way from the page toward green or red (the theme's own /// ANSI green and red where it has a palette), and the prefix column in /// that hue, pushed toward the ink until it reads on that ground. The /// syntax colours are drawn over the ground unchanged. pub const DiffTints = struct { added_bg: [3]u8, removed_bg: [3]u8, /// The words a changed line changed (hunks.zig), on a stronger ground. added_emph_bg: [3]u8, removed_emph_bg: [3]u8, /// A +Diff line added or removed whole: a faint ground. added_line_bg: [3]u8, removed_line_bg: [3]u8, added_fg: [3]u8, removed_fg: [3]u8, pub fn of(p: *Pardes) DiffTints { const th = p.theme(); const page = th.bg orelse th.tag_bg; const ink = th.fg orelse th.tag_fg; const green: [3]u8 = if (th.palette) |pal| pal[2] else .{ 0x2e, 0xa0, 0x43 }; const red: [3]u8 = if (th.palette) |pal| pal[1] else .{ 0xcf, 0x22, 0x2e }; const added_bg = Colors.mixOklab(page, green, 0.18); const removed_bg = Colors.mixOklab(page, red, 0.18); return .{ .added_bg = added_bg, .removed_bg = removed_bg, .added_emph_bg = Colors.mixOklab(page, green, 0.28), .removed_emph_bg = Colors.mixOklab(page, red, 0.28), .added_line_bg = Colors.mixOklab(page, green, 0.07), .removed_line_bg = Colors.mixOklab(page, red, 0.07), .added_fg = legible(green, ink, added_bg), .removed_fg = legible(red, ink, removed_bg), }; } fn legible(hue: [3]u8, ink: [3]u8, ground: [3]u8) [3]u8 { var fg = hue; var step: usize = 0; while (step < 4 and Colors.contrast(fg, ground) < 3) : (step += 1) fg = Colors.mixOklab(fg, ink, 0.4); return fg; } /// The row ground and prefix ink for a line's style byte, if it is an /// added or removed line. pub fn forStyle(t: *const DiffTints, style: u8) ?struct { bg: [3]u8, fg: [3]u8, emph_bg: [3]u8, line_bg: [3]u8 } { if (style & syntax.diff_added != 0) return .{ .bg = t.added_bg, .fg = t.added_fg, .emph_bg = t.added_emph_bg, .line_bg = t.added_line_bg }; if (style & syntax.diff_removed != 0) return .{ .bg = t.removed_bg, .fg = t.removed_fg, .emph_bg = t.removed_emph_bg, .line_bg = t.removed_line_bg }; return null; } }; fn synStyle(p: *Pardes, sy: syntax.Syn) ?SynStyle { return switch (sy) { .none => null, .keyword => .{ .fg = p.theme().kw, .bold = false }, .string => .{ .fg = p.theme().str, .bold = false }, .number => .{ .fg = p.theme().num, .bold = false }, .comment => .{ .fg = p.theme().comment, .bold = false }, }; } /// syntax colors: recolor each content cell from its tree-sitter style byte; /// content starts after the lineno gutter pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes.Rect, tx: u16, tw: u16, body_h: u16) void { const highlights = if (f.mini) |mini| mini.colors else f.highlights; const highlight_start = if (f.mini != null) 0 else f.highlight_start; const spans = f.pager_spans; if (highlights.len == 0 and f.output == null and spans.len == 0) return; const prefix_width = gutterWidth(pane); const tz_recolor = tracy.zone(@src(), "synRecolor"); defer tz_recolor.end(); // A diff's rows, and a +Diff's (hunks.zig): their markers coloured. // A diff's whole row is tinted; a +Diff tints only the words a // changed line changed. const algo = pane.colorAlgo(); const tints: ?DiffTints = if ((algo == .diff or algo == .locations) and highlights.len > 0) DiffTints.of(p) else null; const row_tint = algo == .diff; var filtered_storage: terminal.FilteredColors = undefined; const filtered: ?*terminal.FilteredColors = if (pane.tty_filter and spans.len > 0) blk: { filtered_storage = .init(p, null); break :blk &filtered_storage; } else null; // indexed start, same as bodyText — an empty tail simply paints nothing var flines = std.mem.splitScalar(u8, f.content[lineStart(p.gpa, f, f.scroll)..], '\n'); const total = nlines(p.gpa, f); const body_y = p.bodyTop(pane, r); var vr: u16 = 0; while (vr < body_h) : (vr += 1) { var base: usize = undefined; var line: []const u8 = undefined; var hs: usize = @intCast(@max(0, pane.hscroll)); var limit: usize = undefined; if (pane.wrap_n == 0) { line = flines.next() orelse break; base = @intFromPtr(line.ptr) - @intFromPtr(f.content.ptr); limit = line.len; } else { if (vr >= pane.wrap_n) break; const lrow: usize = @intCast(@max(0, pane.wrap_line[vr])); if (lrow >= total) break; base = lineStart(p.gpa, f, lrow); const lend = if (lrow + 1 < total) lineStart(p.gpa, f, lrow + 1) -| 1 else f.content.len; line = f.content[base..lend]; hs = @intCast(pane.wrap_col[vr]); limit = if (vr + 1 < pane.wrap_n and pane.wrap_line[vr + 1] == pane.wrap_line[vr]) @min(line.len, @as(usize, @intCast(pane.wrap_col[vr + 1]))) else line.len; } hs = modal.graphemeStart(line, @min(hs, line.len)); const row_index: usize = @intCast(@max(0, pane.wrapAt(vr).line)); const metadata: ?locations.Row = if (row_index < f.location_rows.len) f.location_rows[row_index] else null; const decoration = Output.decorateRow(f.output, line, metadata); const chrome: ?*const pardes.ChromeTheme = if (decoration.plain()) null else p.bodyChrome(); // An added or removed line's row, every wrapped row of it, is tinted. const tint = if (tints) |*t| if (base >= highlight_start and base - highlight_start < highlights.len) t.forStyle(highlights[base - highlight_start]) else null else null; if (tint) |t| if (prefix_width < tw) { // A +Diff line added or removed whole: faintly (hunks.zig). const whole = highlights[base - highlight_start] & syntax.diff_whole != 0; if (row_tint or whole) s.tint(tx + @as(u16, @intCast(prefix_width)), body_y + vr, tw -| @as(u16, @intCast(prefix_width)), if (row_tint) t.bg else t.line_bg, null); }; // A +Pager's own colours (pager.zig) on every row but one the diff // look tints: an added or removed line is drawn as pardes draws a // diff, and git's red and green on it would only fight the tint. var span_at = if (tint != null) spans.len else std.sort.partitionPoint(pardes.pager.Span, spans, base + hs, struct { fn before(at: usize, span: pardes.pager.Span) bool { return span.end <= at; } }.before); var c: usize = 0; const pad: usize = if (vr < pane.wrap_n) pane.wrap_pad[vr] else 0; for (0..@min(pad, tw -| prefix_width)) |col| { s.at(tx + @as(u16, @intCast(prefix_width + col)), body_y + vr).style.fg = .{ .rgb = p.bodyChrome().diagnostic_info }; } var screen_c: usize = pad; var table_buf: [Tables.max_pads]Tables.Pad = undefined; const table_pads = rowPads(pane, @intCast(row_index), &table_buf); var table_at: usize = 0; while (hs + c < limit and prefix_width + screen_c < tw) { while (table_at < table_pads.len and table_pads[table_at].at < hs + c) table_at += 1; if (table_at < table_pads.len and table_pads[table_at].at == hs + c and hs + c > hs) { screen_c += table_pads[table_at].cells; table_at += 1; if (prefix_width + screen_c >= tw) break; } const grapheme_end = @min(limit, modal.nextGrapheme(line, hs + c)); const cells = graphemeDisplayWidth(line[hs + c .. grapheme_end]); const idx = base + hs + c; // What paints the grapheme's cells, in this order: its syntax // colour (a diff line's prefix its tint's ink), the words a diff // changed on their stronger ground, a result's emphasis (only // the location, matched source range and diagnostic label gain // it: results keep their exact live text), a +Pager's colours. const highlight: ?u8 = if (idx >= highlight_start and idx - highlight_start < highlights.len) highlights[idx - highlight_start] else null; const syn = if (highlight) |h| (if (tint != null and hs + c == 0) SynStyle{ .fg = tint.?.fg, .bold = true } else synStyle(p, @fromBackingInt(@intCast(h & syntax.syn_bits)))) else null; const emph = if (highlight != null and tint != null and highlight.? & syntax.diff_emph != 0) tint.?.emph_bg else null; const deco = if (chrome) |ch| decoration.styleAt(ch, hs + c) else null; while (span_at < spans.len and spans[span_at].end <= idx) span_at += 1; const span = if (span_at < spans.len and spans[span_at].start <= idx) spans[span_at].style else null; for (0..@min(cells, @as(usize, tw) - prefix_width - screen_c)) |fill| { const cell = s.at(tx + @as(u16, @intCast(prefix_width + screen_c + fill)), body_y + vr); if (!cell.default) if (syn) |ss| { cell.style.fg = .{ .rgb = ss.fg }; cell.style.bold = ss.bold; }; if (emph) |bg| cell.style.bg = .{ .rgb = bg }; if (cell.default) continue; if (deco) |style| { cell.style.fg = style.fg; cell.style.bold = style.bold; if (style.bg != .default) cell.style.bg = style.bg; } const st = span orelse continue; // As a terminal pane's cell: through Filter, or a palette // colour through the theme's sixteen and truecolor as it is. if (filtered) |colors| { if (st.fg != .default) cell.style.fg = colors.spanColor(st.fg, false); if (st.bg != .default) cell.style.bg = colors.spanColor(st.bg, true); } else { if (st.fg != .default) cell.style.fg = if (st.fg == .index) terminal.palColor(p, st.fg.index) else st.fg; if (st.bg != .default) cell.style.bg = if (st.bg == .index) terminal.palColor(p, st.bg.index) else st.bg; } cell.style.bold = cell.style.bold or st.bold; cell.style.dim = st.dim; cell.style.italic = st.italic; cell.style.blink = st.blink; cell.style.reverse = st.reverse; cell.style.invisible = st.invisible; cell.style.strikethrough = st.strikethrough; if (st.ul != .off) cell.style.ul = st.ul; } screen_c += cells; c = grapheme_end - hs; } } } /// What finished command panes printed, kept while it cannot change: a /// copy of the terminal's rows, and whether they are a diff. Taken once /// when a finished pane is first drawn, so neither a frame nor a look dumps /// the whole scrollback again; a still running command is shown as it /// printed. Keyed by the pane and its width (a resize rewraps every row); /// a rerun in the pane drops it (exec.runCommand). pub const DiffOutput = struct { const Entry = struct { serial: u32, cols: u16, is_diff: bool, /// The rows joined by '\n'; `rows` are slices of it. text: []u8, rows: [][]const u8, used: u64, }; slots: [4]?Entry = @splat(null), clock: u64 = 0, pub fn reset(o: *DiffOutput, gpa: std.mem.Allocator) void { for (&o.slots) |*slot| { if (slot.*) |e| { gpa.free(e.text); gpa.free(e.rows); } slot.* = null; } } /// A pane's command runs again: what it printed before is gone. pub fn drop(o: *DiffOutput, gpa: std.mem.Allocator, serial: u32) void { for (&o.slots) |*slot| if (slot.*) |e| if (e.serial == serial) { gpa.free(e.text); gpa.free(e.rows); slot.* = null; }; } /// A finished command pane's rows when they are a diff, else null. pub fn lines(o: *DiffOutput, p: *Pardes, pane: *Pane) ?[]const []const u8 { if (pane.command == null or !pane.command_done or pane.command_pty or pane.terminal == null) return null; o.clock += 1; for (&o.slots) |*slot| if (slot.*) |*e| if (e.serial == pane.serial and e.cols == pane.cols) { e.used = o.clock; return if (e.is_diff) e.rows else null; }; const shown = terminal.shellRows(p, pane) catch return null; const is_diff = diff.looksLikeDiff(shown); var total: usize = 0; if (is_diff) for (shown) |row| { total += row.len + 1; }; const text = p.gpa.alloc(u8, total) catch return null; const rows = p.gpa.alloc([]const u8, if (is_diff) shown.len else 0) catch { p.gpa.free(text); return null; }; var at: usize = 0; for (rows, 0..) |*row, i| { @memcpy(text[at..][0..shown[i].len], shown[i]); row.* = text[at..][0..shown[i].len]; at += shown[i].len; text[at] = '\n'; at += 1; } // An empty slot, else the least recently used. var oldest: usize = 0; for (&o.slots, 0..) |*slot, i| { const e = slot.* orelse { oldest = i; break; }; if (e.used < o.slots[oldest].?.used) oldest = i; } if (o.slots[oldest]) |old| { p.gpa.free(old.text); p.gpa.free(old.rows); } o.slots[oldest] = .{ .serial = pane.serial, .cols = pane.cols, .is_diff = is_diff, .text = text, .rows = rows, .used = o.clock }; return if (is_diff) rows else null; } }; /// A command pane whose output is a diff (`git diff`, `diff -u`): its /// rows coloured as a .diff file's are, over what the program printed. /// The rows are the terminal's own, so a line it wrapped goes on in the /// row below (syntax.highlightDiffRows). pub fn recolorDiffOutput(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, body: []const u8) void { if (pane.command == null) return; const tz = tracy.zone(@src(), "diffRecolor"); defer tz.end(); const rows = p.diff_output.lines(p, pane) orelse return; // The copy's rows are one text, joined as a file's lines are. const flat_start = @intFromPtr(rows[0].ptr); const last = rows[rows.len - 1]; const flat = rows[0].ptr[0 .. @intFromPtr(last.ptr) + last.len - flat_start]; const raw = pane.body.mode == .tty; const View = struct { row: usize, at: usize }; const arena = p.scratch.allocator(); const views = arena.alloc(?View, body_h) catch return; var lo: usize = std.math.maxInt(usize); var hi: usize = 0; var shown = std.mem.splitScalar(u8, body, '\n'); for (views, 0..) |*view, vi| { view.* = null; const line = shown.next() orelse continue; const vr: i32 = @intCast(vi); const w = pane.wrapAt(vr); const row_i: i32 = if (raw) terminal.gridOffset(pane) + vr else w.line; if (row_i < 0 or row_i >= @as(i64, @intCast(rows.len))) continue; const row: usize = @intCast(row_i); const from: usize = if (raw) 0 else @intCast(@max(0, w.at)); if (from > rows[row].len or !std.mem.startsWith(u8, rows[row][from..], line)) continue; view.* = .{ .row = row, .at = from }; const start = @intFromPtr(rows[row].ptr) - flat_start; lo = @min(lo, start); hi = @max(hi, start + rows[row].len); } if (lo > hi) return; const styles = syntax.highlightDiffRows(arena, flat, lo, hi, true) catch return; const tints = DiffTints.of(p); const ink = p.theme().fg orelse p.theme().tag_fg; const body_y = p.bodyTop(pane, r); shown = std.mem.splitScalar(u8, body, '\n'); for (views, 0..) |maybe, vr| { const line = shown.next() orelse break; const view = maybe orelse continue; const base = @intFromPtr(rows[view.row].ptr) - flat_start - lo; const tint = if (base < styles.len) tints.forStyle(styles[base]) else null; if (tint) |t| s.tint(tx, body_y + @as(u16, @intCast(vr)), tw, t.bg, null); var c: usize = 0; var screen_c: usize = 0; while (c < line.len and screen_c < tw) { const grapheme_end = modal.nextGrapheme(line, c); if (grapheme_end <= c) break; const cells = graphemeDisplayWidth(line[c..grapheme_end]); const style = styles[base + view.at + c]; const prefix = view.at + c == 0 and (line[0] == '+' or line[0] == '-'); // On an added or removed line, what the program coloured red or // green is the tint's to say now: its code takes the page's ink. const paint: ?SynStyle = if (tint != null and prefix) .{ .fg = tint.?.fg, .bold = true } else if (synStyle(p, @fromBackingInt(@intCast(style & syntax.syn_bits)))) |ss| ss else if (tint != null) .{ .fg = ink, .bold = false } else null; if (paint) |ss| { var fill: usize = 0; while (fill < cells and screen_c + fill < tw) : (fill += 1) { const cell = s.at(tx + @as(u16, @intCast(screen_c + fill)), body_y + @as(u16, @intCast(vr))); if (cell.default) continue; cell.style.fg = .{ .rgb = ss.fg }; cell.style.bold = ss.bold; } } screen_c += cells; c = grapheme_end; } } } pub fn drawWrapMarkers( p: *Pardes, s: *Surface, pane: *const Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, pane_bg: pardes.Color, ) void { if (tw <= gutterWidth(pane) + 1) return; const body_y = p.bodyTop(pane, r); const marker_fg = p.chromeTheme().lineno; var row: u16 = 0; while (row + 1 < pane.wrap_n and row + 1 < body_h) : (row += 1) { if (pane.wrap_line[row + 1] != pane.wrap_line[row]) continue; s.set(tx + tw - 1, body_y + row, config.wrap_marker, .{ .fg = .{ .rgb = marker_fg }, .bg = pane_bg, }); } } pub fn paintWordSelection( p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, row: i32, word_lo: i32, word_hi: i32, bg: [3]u8, ) void { const tx = r.x + config.GUTTER; const tw = r.w - config.GUTTER; const prefix_width = gutterWidth(pane); const body_y = p.bodyTop(pane, r); var vr: i32 = 0; while (vr + @as(i32, pane.tag_rows) < @as(i32, r.h)) : (vr += 1) { const here = pane.wrapAt(vr); if (here.line != row) continue; var hi = word_hi; const next = pane.wrapAt(vr + 1); if (next.line == row) hi = @min(hi, next.at); const lo = @max(word_lo, here.at); if (hi <= lo) continue; const c0 = @as(i32, prefix_width) + here.pad + displayOffset(pane, row, here.at, lo); const c1 = @as(i32, prefix_width) + here.pad + displayEndOffset(pane, row, here.at, hi - 1); const col = @max(@as(i32, prefix_width), c0); const end = @min(c1 + 1, @as(i32, tw)); if (col < end) s.tint(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(vr)), @intCast(end - col), bg, null); } } test "undo and redo walk back and forth through every step of random edits, joined entries and all" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const pane = try p.setTestFile("seed text\nof two lines\n"); const f = &pane.file.?; const j = &f.history.journal; var prng = std.Random.DefaultPrng.init(17); const r = prng.random(); // The text at each step's edge, the last the text now. var texts: std.ArrayList([]u8) = .empty; defer { for (texts.items) |t| gpa.free(t); texts.deinit(gpa); } var at: usize = 0; for (0..80) |_| { const was = j.done; try texts.append(gpa, try gpa.dupe(u8, f.content)); pushUndo(p, pane); // A step that changed nothing goes on, its edge where it was. if (j.done == was) gpa.free(texts.pop().?); // Up to six edits: most where the last ended, as typing goes on. for (0..1 + r.uintLessThan(usize, 6)) |_| { const len = f.content.len; at = if (r.uintLessThan(u8, 4) == 0) r.uintAtMost(usize, len) else @min(at, len); const del = r.uintAtMost(usize, @min(3, len - at)); var ins: [3]u8 = undefined; const n = r.uintAtMost(usize, 3); for (ins[0..n]) |*c| c.* = "ab\n"[r.uintLessThan(usize, 3)]; try std.testing.expect(replace(p, f, at, del, ins[0..n])); at += n; } } try texts.append(gpa, try gpa.dupe(u8, f.content)); try std.testing.expectEqual(texts.items.len - 1, j.done); var k = texts.items.len - 1; while (k > 0) { k -= 1; undoStep(p, pane, true); try std.testing.expectEqualStrings(texts.items[k], f.content); } for (texts.items[1..]) |t| { undoStep(p, pane, false); try std.testing.expectEqualStrings(t, f.content); } } test "a text replaced whole keeps its old buffer for its undo, swapped back and forth, two at most" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const pane = try p.setTestFile(""); const f = &pane.file.?; const j = &f.history.journal; var texts: [5][]u8 = undefined; for (&texts, 0..) |*t, i| { t.* = try gpa.alloc(u8, swap_min + 1000 * i); @memset(t.*, 'a' + @as(u8, @intCast(i))); } defer for (texts) |t| gpa.free(t); setContent(p, f, try gpa.dupe(u8, texts[0])); const first = f.content.ptr; pushUndo(p, pane); setContent(p, f, try gpa.dupe(u8, texts[1])); // The old buffer itself, kept: no byte of it copied, none counted. const e = j.entries.items[j.entries.items.len - 1]; try std.testing.expect(e.swap and e.removed.ptr == first); try std.testing.expectEqual(@as(usize, @sizeOf(Text.Entry)), j.bytes); const second = f.content.ptr; undoStep(p, pane, true); try std.testing.expect(f.content.ptr == first); try std.testing.expectEqualStrings(texts[0], f.content); undoStep(p, pane, false); try std.testing.expect(f.content.ptr == second); try std.testing.expectEqualStrings(texts[1], f.content); // A third and a fourth: two swaps kept, the steps before them gone, said. for (texts[2..4]) |t| { pushUndo(p, pane); setContent(p, f, try gpa.dupe(u8, t)); } try std.testing.expectEqual(@as(usize, 2), j.done); try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "undo keeps the last two texts replaced whole") != null); undoStep(p, pane, true); undoStep(p, pane, true); try std.testing.expectEqualStrings(texts[1], f.content); undoStep(p, pane, true); try std.testing.expectEqualStrings(texts[1], f.content); // A body emptied and written again on one open (a shell's >): its undo // swaps the old text back and folds the step into one swap the other way. pushUndo(p, pane); try std.testing.expect(replace(p, f, 0, f.content.len, "")); try std.testing.expect(replace(p, f, 0, 0, "fresh\n")); undoStep(p, pane, true); try std.testing.expectEqualStrings(texts[1], f.content); try std.testing.expectEqual(@as(usize, 1), j.steps.items[j.done].n); undoStep(p, pane, false); try std.testing.expectEqualStrings("fresh\n", f.content); undoStep(p, pane, true); try std.testing.expectEqualStrings(texts[1], f.content); } test "undo keeps its steps within a byte bound, the newest always" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const step = limits.undo_bytes / 4 + 1; // Each edit takes out the middle, under half the text: a copy each, not // a swap of the whole. const base = try gpa.alloc(u8, 2 * step + 1); defer gpa.free(base); @memset(base, 'z'); const pane = try p.setTestFile(base); const f = &pane.file.?; const text = try gpa.alloc(u8, step); defer gpa.free(text); for (0..8) |i| { @memset(text, 'a' + @as(u8, @intCast(i))); pushUndo(p, pane); try std.testing.expect(replace(p, f, step / 2, step, text)); } pushUndo(p, pane); const j = &f.history.journal; try std.testing.expect(j.done > 0 and j.done < 8); try std.testing.expect(j.bytes <= limits.undo_bytes + step + @sizeOf(Text.Entry)); var sum: usize = 0; for (j.entries.items) |e| { try std.testing.expect(!e.swap); sum += Text.Journal.held(e); } try std.testing.expectEqual(sum, j.bytes); undoStep(p, pane, true); try std.testing.expectEqual(text[0], f.content[step / 2]); // Back as far as the bound kept: each step's middle whole again. while (j.done > 0) { undoStep(p, pane, true); try std.testing.expect(std.mem.allEqual(u8, f.content[step / 2 ..][0..step], f.content[step / 2])); } }