summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig104
1 files changed, 89 insertions, 15 deletions
diff --git a/src/File.zig b/src/File.zig
index e4bca000..d795eaa1 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -104,6 +104,7 @@ fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Snapshot) void {
}
pub const History = struct {
+ pub const replica = .{ .len = .{ .{ "undo", "undo_len" }, .{ "redo", "redo_len" } } };
undo: [limits.undo_max]Snapshot = undefined,
undo_len: usize = 0,
redo: [limits.undo_max]Snapshot = undefined,
@@ -137,6 +138,10 @@ pub const History = struct {
/// 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 = .{ "line_starts", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" } };
+
path: []u8,
content: []u8,
revision: u32 = 0,
@@ -1252,7 +1257,8 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void {
if (f.saved_hash) |h| if ((f.saved_len orelse new.len) == new.len and std.hash.Wyhash.hash(0, new) == h) {
f.saved_revision = f.revision;
};
- f.mtime = pardes.ctlfs.events.now();
+ // The clock the session that recorded read (src/tape.zig).
+ f.mtime = pardes.tape.value(.mtime, pardes.ctlfs.events.now, .{});
if (f.line_starts.len > 0) p.gpa.free(f.line_starts);
f.line_starts = starts;
if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights);
@@ -1647,6 +1653,8 @@ pub fn redo(p: *Pardes, pane: *Pane) void {
/// `<file> deleted on disk (Save writes it again)`, logged `changed
/// <serial> 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;
@@ -1703,6 +1711,17 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void {
/// 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();
@@ -1710,12 +1729,6 @@ pub fn refreshHighlights(p: *Pardes) void {
const pane = slot orelse continue;
if (pane.file == null) continue;
const f = &pane.file.?;
- if (f.tree_context and supportsContext(pane) and f.context_revision != f.revision) {
- const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch continue;
- if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations);
- f.context_declarations = declarations;
- f.context_revision = f.revision;
- }
if (f.mini != null) {
f.syntax_dirty = false;
continue;
@@ -1907,18 +1920,79 @@ pub fn visualRow(line: []const u8, col: usize, width: usize) VisualRow {
}
}
-pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State, wrap: bool) ![]const u8 {
- const width = wrapWidth(pane, wrap);
+/// 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);
- pane.wrap_n = 0;
+ return fillBody(null, pane, f, width, true);
+}
- const len = fillBody(null, pane, f, width, false);
- const out = try arena.alloc(u8, len);
- const filled = fillBody(out, pane, f, width, true);
- std.debug.assert(filled == out.len);
- return out;
+/// 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