diff options
Diffstat (limited to 'core/File.zig')
| -rw-r--r-- | core/File.zig | 609 |
1 files changed, 507 insertions, 102 deletions
diff --git a/core/File.zig b/core/File.zig index c4fc8a38..0c742eee 100644 --- a/core/File.zig +++ b/core/File.zig @@ -199,8 +199,194 @@ pub const State = struct { /// In its twin group's Follow chain (builtins.Follow), whose panes /// show the text as consecutive pages (followChain). follow: bool = false, + /// Its `Changes` layer's lines while it is shown (`ReadOnly`, jj.zig): + /// read-only lines of the buffer, in order, gpa-owned (a twin holds its + /// own copy); none while it is hidden. + read_only: []ReadOnly = &.{}, + /// Whose layer they are: the serial of the pane whose `Changes` answer + /// they are the ghosts of, in its order (jj.answered); 0, none shown. + layer: u32 = 0, }; +/// A read-only line of a pane's buffer: one of its `Changes` layer's (a +/// line a change took away, a changed line's old text, a change's +/// description: jj.zig `Ghost`), its bytes `content[at..][0..len]`, its +/// text and its `\n` (`lead`: after the last line of a text that ends +/// with none, its `\n` first). Regular text to everything that reads the +/// buffer (a motion, a selection, a search, a Look); the file's text is the +/// buffer less them (`fileText`); an edit touching one is refused whole +/// (`edit`). +pub const ReadOnly = struct { + at: usize, + len: usize, + /// The file's line it stands before, from 0 (the file's line count: + /// after its last). + line: u32, + lead: bool = false, +}; + +/// THE FILE'S TEXT: the buffer less its read-only lines, the one way to it. +/// Whatever reads "the file" (a Save, `body` over 9P, the dirty check, the +/// language server, the syntax parse, jj, a reread's comparison) asks this, +/// never `content`, which is the buffer the drawing and the editing read: +/// the buffer itself while it has none (a file with no layer pays nothing), +/// else a copy in `arena`. +pub fn fileText(f: *const State, arena: std.mem.Allocator) error{OutOfMemory}![]const u8 { + if (f.read_only.len == 0) return f.content; + const out = try arena.alloc(u8, fileLen(f)); + var parts: FileParts = .{ .f = f }; + var n: usize = 0; + while (parts.next()) |part| { + @memcpy(out[n..][0..part.len], part); + n += part.len; + } + return out; +} + +/// The file's text's length (`fileText`). +pub fn fileLen(f: *const State) usize { + var n = f.content.len; + for (f.read_only) |r| n -= r.len; + return n; +} + +/// The file's text a stretch at a time, the buffer between its read-only +/// lines (`fileText` without the copy). +pub const FileParts = struct { + f: *const State, + i: usize = 0, + at: usize = 0, + done: bool = false, + + pub fn next(it: *FileParts) ?[]const u8 { + if (it.done) return null; + const ro = it.f.read_only; + if (it.i == ro.len) { + it.done = true; + return it.f.content[it.at..]; + } + const part = it.f.content[it.at..ro[it.i].at]; + it.at = ro[it.i].at + ro[it.i].len; + it.i += 1; + return part; + } +}; + +/// Whether the file's text is `bytes` (`fileText` without the copy). +pub fn fileEql(f: *const State, bytes: []const u8) bool { + if (fileLen(f) != bytes.len) return false; + var parts: FileParts = .{ .f = f }; + var at: usize = 0; + while (parts.next()) |part| { + if (!std.mem.eql(u8, part, bytes[at..][0..part.len])) return false; + at += part.len; + } + return true; +} + +/// The file's text hashed (Wyhash, as `std.hash.Wyhash.hash(0, text)`): +/// what the dirty check compares, never a copy. +pub fn fileHash(f: *const State) u64 { + if (f.read_only.len == 0) return std.hash.Wyhash.hash(0, f.content); + var h = std.hash.Wyhash.init(0); + var parts: FileParts = .{ .f = f }; + while (parts.next()) |part| h.update(part); + return h.final(); +} + +/// A buffer offset as the file's: less the read-only lines before it; one +/// inside a read-only line, after the file line above it (where the line +/// it stands before starts). +pub fn fileOffset(f: *const State, at: usize) usize { + var less: usize = 0; + for (f.read_only) |r| { + if (r.lead) { + if (at <= r.at) break; + if (at <= r.at + r.len) return r.at - less; + } else { + if (at < r.at) break; + if (at < r.at + r.len) return r.at - less; + } + less += r.len; + } + return at - less; +} + +/// A file offset as the buffer's: a line's start after the read-only lines +/// standing before it (`:N`, a Look, a language server's place land on the +/// file's line, never on one of the layer's); `end`, a range's end, before +/// them (a range of the file's lines holds none of the layer's after it). +pub fn bufferOffset(f: *const State, at: usize, end: bool) usize { + var more: usize = 0; + for (f.read_only) |r| { + const place = r.at - more; + if (if (r.lead or end) place >= at else place > at) break; + more += r.len; + } + return at + more; +} + +/// A buffer row as the file's: a read-only line's, the file line it stands +/// before. +pub fn fileRow(f: *const State, row: usize) usize { + var less: usize = 0; + for (f.read_only, 0..) |r, k| { + const at = r.line + k; + if (at > row) break; + if (at == row) return r.line; + less += 1; + } + return row - less; +} + +/// A file row as the buffer's: after the read-only lines standing before it. +pub fn bufferRow(f: *const State, row: usize) usize { + var more: usize = 0; + for (f.read_only) |r| { + if (r.line > row) break; + more += 1; + } + return row + more; +} + +/// Which read-only line buffer row `row` is (its place in `read_only`), +/// null for a line of the file. +pub fn readOnlyRow(f: *const State, row: usize) ?usize { + for (f.read_only, 0..) |r, k| { + const at = r.line + k; + if (at == row) return k; + if (at > row) return null; + } + return null; +} + +/// Whether the edit putting `bytes` over the `len` bytes at `off` touches +/// a read-only line: changes a byte of one, puts any in at its start or +/// inside it, or leaves it on the end of a line above it (an edit ending +/// where one starts must leave a line's end before it: a line deleted +/// whole above one is the file's edit; a `J` into one is not). +fn touchesReadOnly(f: *const State, off: usize, len: usize, bytes: []const u8) bool { + for (f.read_only) |r| { + const lo = r.at; + const hi = r.at + r.len; + if (lo > off + len) break; + if (off < hi and off + len > lo) return true; + if (r.lead) { + if (len == 0 and off > lo and off <= hi) return true; + } else { + if (len == 0 and off >= lo and off < hi) return true; + if (len > 0 and off + len == lo) { + const ends_line = if (bytes.len > 0) bytes[bytes.len - 1] == '\n' else off == 0 or f.content[off - 1] == '\n'; + if (!ends_line) return true; + } + } + } + return false; +} + +/// What an edit refused for a read-only line says (`edit`). +pub const read_only_said = "a change's line is read-only: nothing edited (Changes hides them)"; + pub const NodeStep = @import("Editor.zig").NodeStep; pub fn graphemeDisplayWidth(tab: u16, grapheme: []const u8) usize { @@ -370,7 +556,8 @@ pub fn gutterWidth(pane: *const Pane) u16 { 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); + // The file's lines: the layer's have no number. + var lines = (if (file.line_starts.len > 0) file.line_starts.len else lineCount(file.content)) - file.read_only.len; var digits: u16 = 1; while (lines >= 10) : (lines /= 10) digits += 1; return @max(config.PREFIX_W, digits + 1) + laneCells(pane) + pane.layers.heat; @@ -626,6 +813,7 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { 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); + p.gpa.free(file.read_only); locations.freeRows(p.gpa, file.location_rows); file.context.deinit(); if (file.node_tree) |tree| syntax.freeTree(tree); @@ -636,14 +824,16 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { file.history.destroy(p.gpa); } -/// The edit `replace` makes told to the event readers, before it is made. +/// The edit `replace` makes told to the event readers, before it is made: +/// in the file's text, by its offsets. 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); + const text = fileText(f, p.scratch.allocator()) catch return; + pardes.ctlfs.events.noteEdit(p, id, false, text, fileOffset(f, off), len, bytes); } pub fn setContent(p: *Pardes, f: *State, new: []u8) void { @@ -678,16 +868,178 @@ pub fn setRows(p: *Pardes, f: *State, rows: []locations.Row) error{RowsMismatch} /// 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); + var span = span_in orelse spanOf(f.content, new); + if (f.read_only.len > 0) span = clearOfReadOnly(f, new, span); + if (edit(p, f, span.head, f.content.len - span.head - span.tail, new[span.head .. new.len - span.tail], new, true, null, .edit)) return; + // Refused (read-only text, said) or no memory: `new` kept until the + // next such, as the editor keeps a text it made (edit.zig + // retireEditText): its caller may still read it. + if (p.retired_edit) |old| p.gpa.free(old); + p.retired_edit = new; +} + +/// Of the splices that make `new` of the buffer, one that touches no +/// read-only line when the shortest (`span`, what it keeps at either end) +/// does: the editor's whole new text says what it changed only by the +/// bytes, and the shortest splice can run into a line of the layer's where +/// the edit did not (`a();` deleted above ` b();`, both starting with +/// four blanks, is the splice `a();\n `; it is also ` a();\n`). +/// Searched back from the shortest's head, a page of bytes at most. +fn clearOfReadOnly(f: *const State, new: []const u8, span: Span) Span { + const old = f.content; + if (!touchesReadOnly(f, span.head, old.len - span.head - span.tail, new[span.head .. new.len - span.tail])) return span; + const n = @min(old.len, new.len); + // The whole tail they share (`spanOf`'s stops at its head). + var shared: usize = 0; + while (shared < n and old[old.len - 1 - shared] == new[new.len - 1 - shared]) shared += 1; + // Never back across a read-only line: an edit made on one (a paste + // below it) is no edit of the line above it. + var floor: usize = 0; + for (f.read_only) |r| { + if (r.at + r.len > span.head) break; + floor = r.at + r.len; + } + var head = span.head; + while (head > floor and span.head - head < 4096) { + head -= 1; + const tail = @min(shared, n - head); + if (!touchesReadOnly(f, head, old.len - head - tail, new[head .. new.len - tail])) return .{ .head = head, .tail = tail }; + } + return span; } /// 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. +/// them, never a copy of the text. False with no memory for it, or for an +/// edit of read-only text (said), 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); + return edit(p, f, off, len, bytes, null, true, null, .edit); +} + +/// What an edit is (`edit`): the editor's, of the buffer (refused where +/// it touches read-only text); a program's new text for the file (`load`: +/// never refused); the layer's lines going in or out (`jj.showLayer`: +/// nothing that reads the file hears of it). +pub const How = enum { edit, load, layer }; + +/// A new text for the FILE, `new` (owned), from what is not an edit of +/// the buffer: a Get, a reread from the disk, a language server's edits: +/// never refused for the layer's lines, which leave the buffer around it +/// and come back where the file's lines they stood before went (jj.edited +/// carries them). +pub fn load(p: *Pardes, f: *State, new: []u8) void { + const layer = f.layer; + if (layer != 0) jj.hideLayer(p, f); + defer if (layer != 0) jj.showLayer(p, f, layer); + 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], new, true, null, .load)) pardes.memory.freeRaw(p.gpa, new); +} + +/// `replace` of the file's `len` bytes at `off` (its own offsets, `body` +/// over 9P's), as `load` is a new text: the layer's lines out and back. +pub fn replaceFile(p: *Pardes, f: *State, off: usize, len: usize, bytes: []const u8) bool { + const layer = f.layer; + if (layer != 0) jj.hideLayer(p, f); + defer if (layer != 0) jj.showLayer(p, f, layer); + return replace(p, f, off, len, bytes); +} + +/// `f`'s buffer made `new` (owned) with the read-only lines `spans` (owned) +/// of pane `layer`'s layer, or none (`layer` 0): the layer's lines in or +/// out (jj.showLayer, jj.hideLayer). No edit of the file: nothing that +/// reads it hears of it (no undo step, no event, the text as clean as it +/// was, its declarations its own), and every view of every pane on the +/// buffer keeps its place in the file (`fileOffset`), so one in a line that +/// leaves goes to the start of the file line it stood before, and one at a +/// line's start stays on that line. False with no memory, nothing changed. +pub fn relayer(p: *Pardes, f: *State, new: []u8, spans: []ReadOnly, layer: u32) bool { + const arena = p.scratch.allocator(); + // Each twin its own copy of the read-only lines, made first: a twin + // without them would read the layer's lines as the file's. + var twins: [pardes.MAX_PANES]usize = undefined; + const ntwins = twinsOf(p, f, &twins); + var copies: [pardes.MAX_PANES][]ReadOnly = undefined; + for (0..ntwins) |i| copies[i] = p.gpa.dupe(ReadOnly, spans) catch { + for (copies[0..i]) |c| p.gpa.free(c); + return false; + }; + // Every place on the buffer, as the file's offsets: a cursor, a + // selection's anchor and ranges in the live view, a cursor and a top + // line in the others. + const Place = struct { row: *i32, col: *i32, at: usize }; + const Top = struct { line: *usize, at: usize }; + var places: std.ArrayList(Place) = .empty; + var tops: std.ArrayList(Top) = .empty; + const At = struct { + fn of(q: *Pardes, g: *State, row: i32, col: i32) usize { + const r: usize = @intCast(@max(0, row)); + const start = lineStart(q.gpa, g, r); + const end = if (r + 1 < nlines(q.gpa, g)) lineStart(q.gpa, g, r + 1) - 1 else g.content.len; + return fileOffset(g, @min(@max(start, end), start + @as(usize, @intCast(@max(0, col))))); + } + }; + 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; + const b = &pane.body; + places.append(arena, .{ .row = &b.cur_row, .col = &b.cur_col, .at = At.of(p, f, b.cur_row, b.cur_col) }) catch return false; + if (b.vsel.active) places.append(arena, .{ .row = &b.vsel.row, .col = &b.vsel.col, .at = At.of(p, f, b.vsel.row, b.vsel.col) }) catch return false; + for (b.sels[0..b.nsel]) |*sr| { + places.append(arena, .{ .row = &sr.row, .col = &sr.col, .at = At.of(p, f, sr.row, sr.col) }) catch return false; + places.append(arena, .{ .row = &sr.arow, .col = &sr.acol, .at = At.of(p, f, sr.arow, sr.acol) }) catch return false; + } + tops.append(arena, .{ .line = &tf.scroll, .at = fileOffset(f, lineStart(p.gpa, f, tf.scroll)) }) catch return false; + for (0..pardes.views.max) |k| { + if (k == p.live or !pardes.views.isOpen(p, @intCast(k))) continue; + const v = &pane.pv[k]; + places.append(arena, .{ .row = &v.body.cur_row, .col = &v.body.cur_col, .at = At.of(p, f, v.body.cur_row, v.body.cur_col) }) catch return false; + tops.append(arena, .{ .line = &v.scroll, .at = fileOffset(f, lineStart(p.gpa, f, v.scroll)) }) catch return false; + } + } + 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], new, false, null, .layer)) { + for (copies[0..ntwins]) |c| p.gpa.free(c); + return false; + } + p.gpa.free(f.read_only); + f.read_only = spans; + f.layer = layer; + for (twins[0..ntwins], copies[0..ntwins]) |id, c| { + const tf = &p.panes[id].?.file.?; + p.gpa.free(tf.read_only); + tf.read_only = c; + tf.layer = layer; + } + for (places.items) |place| { + const pos = positionIn(p.gpa, f, bufferOffset(f, place.at, false)); + place.row.* = @intCast(pos.row); + place.col.* = @intCast(pos.col); + } + for (tops.items) |top| top.line.* = positionIn(p.gpa, f, bufferOffset(f, top.at, false)).row; + return true; +} + +/// Whether an edit of the `len` bytes at `off` is of read-only text: a +/// file at a revision (jj.zig) is that whole, to the editor and to a load; +/// the `Changes` layer's lines are each one, to the editor +/// (`touchesReadOnly`: a load takes them out first). The one check, +/// `edit`'s. +fn readOnly(f: *const State, off: usize, len: usize, bytes: []const u8, how: How) bool { + if (how == .layer) return false; + if (f.output) |o| if (o.from == .revision) return true; + return how == .edit and f.read_only.len > 0 and touchesReadOnly(f, off, len, bytes); +} + +/// Said on each pane of `f` (`readOnly`). +fn refuse(p: *Pardes, f: *const State) void { + for (p.panes, 0..) |slot, id| { + const pane = slot orelse continue; + if (pane.file) |*state| if (state == f) { + pane.body.refused = true; + p.setMessage(.at(id), if (f.read_only.len > 0) read_only_said else "a file at a revision is read-only"); + }; + } } /// A replacement that takes out at least this much, and most of the text, @@ -703,10 +1055,21 @@ const swap_min = 64 << 10; /// 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 { - // A file at a revision is read-only (jj.zig): a key's splice changes - // nothing (a whole text is the editor's to refuse, edit.zig). - if (whole == null) if (f.output) |o| if (o.from == .revision) return false; +fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, whole: ?[]u8, journaled: bool, kept: ?*[]u8, how: How) bool { + // Read-only text (a file at a revision, the layer's lines) takes no + // edit of the editor's: refused whole, in words. + if (readOnly(f, off, len, bytes_in, how)) { + refuse(p, f); + return false; + } + // What reads the file hears of the edit in the file's own offsets (the + // buffer less its read-only lines: `fileOffset`); the layer's own lines + // going in or out it never hears of. The undo's (`turn`) and a load's + // edits come with the layer out (undoStep, `load`). + const told = how != .layer; + const file_off = fileOffset(f, off); + const lines_taken = if (f.read_only.len > 0) std.mem.count(u8, f.content[off..][0..len], "\n") else 0; + const was_clean = f.revision == f.saved_revision; 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 }; @@ -717,7 +1080,9 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who // 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; + // The journal keeps the file's text: never a buffer holding the + // layer's lines. + const swapping = journaled and len >= swap_min and len * 2 >= old_len and f.read_only.len == 0; // 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; @@ -756,18 +1121,19 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who } }; }; - reportEdit(p, f, off, len, bytes); - // A pinned change's per-line values ride with the lines (jj.zig). - if (p.changes.answers.items.len | p.changes.heats.items.len > 0) jj.edited(p, f, off, len, bytes); + if (told) reportEdit(p, f, off, len, bytes); + // A pinned change's per-line values ride with the lines (jj.zig), in + // the file's lines. + if (told and p.changes.answers.items.len | p.changes.heats.items.len > 0) jj.edited(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); + if (told and journaled and !swapping) f.history.journal.record(p.gpa, f.content[off..][0..len], file_off, 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; + if (told and (f.revision == f.saved_revision or f.saved_hash == null)) { + f.saved_hash = fileHash(f); + f.saved_len = fileLen(f); } // The kept tree follows the edit (normal.fileTree parses with it), but // not while a rollback copy holds it: an edit in place would change the @@ -799,13 +1165,28 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who f.content = buffer[0..new_len]; f.cap = buffer.len; } + // The read-only lines after the edit move with what it put in and took + // out, and the layer's ghosts with them (none is in it: refused). + if (told and f.read_only.len > 0) { + const lines_put = std.mem.count(u8, bytes, "\n"); + for (f.read_only) |*r| if (r.at >= off + len) { + r.at = r.at - len + bytes.len; + r.line = @intCast(r.line + lines_put - lines_taken); + }; + jj.placed(p, f); + } 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) { + if (!told) { + // The file's text is as it was: as clean as it was, its + // declarations still its own. + if (was_clean) f.saved_revision = f.revision; + if (f.context.revision == f.revision -% 1) f.context.revision = f.revision; + } else if (f.saved_hash) |h| if ((f.saved_len orelse fileLen(f)) == fileLen(f) and fileHash(f) == h) { f.saved_revision = f.revision; }; // The clock the session that recorded read (core/tape.zig). - f.mtime = p.tape.value(.mtime, pardes.ctlfs.events.now, .{}); + if (told) f.mtime = p.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; @@ -824,6 +1205,10 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who tf.cap = f.cap; tf.line_starts = f.line_starts; tf.line_cap = f.line_cap; + if (!told and tf.context.revision == f.revision -% 1) tf.context.revision = f.revision; + // Its copy of the read-only lines, moved as this one's were (the + // layer's own edit sets them after, jj.showLayer). + if (told and tf.read_only.len == f.read_only.len) @memcpy(tf.read_only, f.read_only); tf.revision = f.revision; tf.saved_revision = f.saved_revision; tf.saved_hash = f.saved_hash; @@ -994,8 +1379,15 @@ pub fn zerox(p: *Pardes, from: usize, to: usize) !*Pane { f.twin = p.next_twin; p.next_twin +%= 1; } + // The layer's lines are the buffer's: a twin shows them too, a copy of + // their places its own. + const read_only = try p.gpa.dupe(ReadOnly, f.read_only); + errdefer p.gpa.free(read_only); const pane = try p.newDocPane(to); + pane.layers.lanes = src.layers.lanes; pane.file = .{ + .read_only = read_only, + .layer = f.layer, .path = path, .content = f.content, .cap = f.cap, @@ -1235,7 +1627,6 @@ fn pageEnd(tab: u16, pane: *Pane) usize { if (pane.wrap_n == 0) return f.scroll + pane.rows; const last = (if (pane.body_visible_rows > 0) @min(pane.wrap_n, pane.body_visible_rows) else pane.wrap_n) - 1; const line: usize = @intCast(pane.wrap_line[last]); - if (pane.wrap_col[last] < 0) return @max(line, f.scroll + 1); if (!pane.body_wrapped or line >= nlines(pane.gpa, f)) return line + 1; const text = sourceLine(pane, @intCast(line)); const width = wrapWidth(pane, pane.view.wrap) -| pane.wrap_pad[last]; @@ -1283,10 +1674,14 @@ pub fn looksLikeDiff(head: []const u8) bool { /// 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 { + // In the file's rows: an undo step is the file's (undoStep takes it + // with the layer's lines out). + var vsel = pane.body.vsel; + vsel.row = @intCast(fileRow(f, @intCast(@max(0, vsel.row)))); return .{ - .cur_row = pane.body.cur_row, - .cur_col = pane.body.cur_col, - .vsel = pane.body.vsel, + .cur_row = @intCast(fileRow(f, @intCast(@max(0, pane.body.cur_row)))), + .cur_col = if (readOnlyRow(f, @intCast(@max(0, pane.body.cur_row))) != null) 0 else pane.body.cur_col, + .vsel = vsel, .gen = f.history.gen, .clean = f.revision == f.saved_revision, .saved_hash = f.saved_hash, @@ -1342,17 +1737,22 @@ pub fn restoreSnap(tab: u16, pane: *Pane, f: *State, snap: Text.Place) void { /// after it are one step back to the pane's place now. pub fn pushUndo(p: *Pardes, pane: *Pane) error{OutOfMemory}!void { const f = if (pane.file) |*file| file else return; - try f.history.journal.mark(p.gpa, f.content, placeOf(pane, f)); + try f.history.journal.mark(p.gpa, try fileText(f, p.scratch.allocator()), 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. +/// place and name it began at (or, redone, was undone from) back. The +/// journal is the file's: with the layer's lines shown, the step is taken +/// with them out, and they come back after it where its lines went. pub fn undoStep(p: *Pardes, pane: *Pane, back: bool) error{OutOfMemory}!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); + const layer = f.layer; + if (layer != 0) jj.hideLayer(p, f); + defer if (layer != 0) jj.showLayer(p, f, layer); if (!turn(p, f, at, back)) return error.OutOfMemory; const s = &j.steps.items[at]; if (back) { @@ -1381,14 +1781,14 @@ 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.len == 0) return edit(p, f, f.content.len, 0, "", null, false, null, .load); 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; + 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, .load)) 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; @@ -1396,7 +1796,7 @@ fn turn(p: *Pardes, f: *State, at: usize, back: bool) bool { 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)) { + if (!edit(p, f, e.off, e.ins, e.removed, null, false, null, .load)) { pardes.memory.freeRaw(p.gpa, put); return false; } @@ -1426,7 +1826,7 @@ fn turn(p: *Pardes, f: *State, at: usize, back: bool) bool { } 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)) { + if (!edit(p, f, span.head, f.content.len - span.head - span.tail, new[span.head .. new.len - span.tail], gap.buf, false, null, .load)) { for (puts) |put| pardes.memory.freeRaw(p.gpa, put); pardes.memory.freeRaw(p.gpa, gap.buf); return false; @@ -1514,8 +1914,8 @@ 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) { + if (fileEql(f, bytes)) return; + if (f.saved_hash) |saved| if (f.revision != f.saved_revision and fileHash(f) != 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). @@ -1535,7 +1935,7 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void { pardes.ctlfs.events.notePath(p, "changed", std.fmt.bufPrint(&rec, "{d} reloaded", .{pane.serial}) catch unreachable); // a u32 fits 32 pushUndo(p, pane) catch |err| return p.reportError(.at(id), "reload", err); const new = p.gpa.dupe(u8, bytes) catch |err| return p.reportError(.at(id), "reload", err); - setContent(p, f, new); + load(p, f, new); f.saved_revision = f.revision; restoreSnap(p.settings.tab, pane, f, .{ .cur_row = pane.body.cur_row, .cur_col = pane.body.cur_col, .vsel = pane.body.vsel }); } @@ -1547,7 +1947,8 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void { /// 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; + // The file's declarations, in its lines (the layer's are none of it). + const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, fileText(f, p.scratch.allocator()) catch return) catch return; f.context.replace(p.tree_sitter_gpa, declarations, f.revision); } @@ -1588,6 +1989,10 @@ pub fn refreshHighlights(p: *Pardes) void { if (!f.tree_context and pane.colorAlgo() == .source) paintAhead(p, pane, f); continue; } + if (f.read_only.len > 0) { + highlightLayered(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. @@ -1640,6 +2045,8 @@ pub fn refreshHighlights(p: *Pardes) void { /// 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 { + // A worker paints the buffer: with the layer's lines in it, never. + if (f.read_only.len > 0) return; 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 @@ -1657,6 +2064,41 @@ fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void { Stream.paintAhead(p, pane.serial, f.revision, langPath(f), f.content, start, end); } +/// The layer's lines shown (`read_only`): the file's text parsed for its +/// colours, never they (a line a change took away is no line of the +/// file's syntax: an unmatched brace in one would colour what follows), +/// the colours laid out in the buffer around them and none on them +/// (jj.paint inks them): the view's stretch and a view either side of it, +/// on the loop (a worker paints the buffer, paintAhead). +fn highlightLayered(p: *Pardes, pane: *Pane, f: *State) void { + f.syntax_dirty = false; + const rows: usize = pane.rows; + const start = if (f.tree_context) 0 else lineStart(p.gpa, f, f.scroll -| rows); + const end = if (f.tree_context) f.content.len else @max(start, lineStart(p.gpa, f, f.scroll + 2 * rows + SYNTAX_CONTEXT_AFTER_ROWS)); + const text = fileText(f, p.scratch.allocator()) catch return; + const file_start = fileOffset(f, start); + const got = syntax.highlightFileRange(p.tree_sitter_gpa, langPath(f), text, file_start, fileOffset(f, end)) catch return; + defer p.tree_sitter_gpa.free(got); + const out = p.tree_sitter_gpa.alloc(u8, end - start) catch return; + @memset(out, 0); + // The file's bytes between the read-only lines, a stretch at a time. + var from: usize = 0; + var at = start; + for (f.read_only) |r| { + if (r.at + r.len <= start) continue; + if (r.at >= end) break; + const n = @min(r.at -| at, got.len -| from); + @memcpy(out[at - start ..][0..n], got[from..][0..n]); + from += n; + at = @max(at, @min(end, r.at + r.len)); + } + const n = @min(end - at, got.len -| from); + @memcpy(out[at - start ..][0..n], got[from..][0..n]); + if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); + f.highlights = out; + f.highlight_start = start; +} + /// The path a text's language is known by: a file at a revision's /// (jj.zig) without its `@rev`. pub fn langPath(f: *const State) []const u8 { @@ -1672,12 +2114,15 @@ 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 + // The declarations are the file's lines; the rows, the buffer's. + const top = fileRow(file, file.scroll); 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 (declaration.start_line >= top) break; + if (declaration.end_line < top) continue; + var line = declaration.start_line; + while (line <= declaration.header_end_line and line < top) : (line += 1) { if (pane.context_rows >= capacity) return; + const row = bufferRow(file, line); 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; @@ -1687,12 +2132,6 @@ fn prepareContextRows(pane: *Pane, file: *State) void { pub fn wrapWidth(pane: *const Pane, wrap: bool) usize { if (!wrap or pane.rows > pane.wrap_line.len) return 0; - return bodyWidth(pane); -} - -/// The cells a body row has for text after the gutter (and a cell for a -/// wrapped line's marker). -pub fn bodyWidth(pane: *const Pane) usize { return @max(1, @as(usize, pane.cols -| gutterWidth(pane)) -| 1); } @@ -1714,7 +2153,7 @@ pub fn bodyText(tab: u16, arena: std.mem.Allocator, pane: *Pane, f: *State) ![]c geometry(tab, pane); const out = try arena.alloc(u8, pane.geometry_len); const width = wrapWidth(pane, pane.view.wrap); - const filled = if (hasGhosts(pane)) fillBody(true, tab, out, pane, f, width, false) else fillBody(false, tab, out, pane, f, width, false); + const filled = fillBody(tab, out, pane, f, width, false); std.debug.assert(filled == out.len); return out; } @@ -1758,13 +2197,7 @@ fn mapRows(tab: u16, pane: *Pane, f: *State) usize { pane.body_wrapped = width > 0; f.tables.ensure(tab, pane.gpa, f); prepareContextRows(pane, f); - return if (hasGhosts(pane)) fillBody(true, tab, null, pane, f, width, true) else fillBody(false, tab, null, pane, f, width, true); -} - -/// Whether this view of the pane shows ghost rows (jj.zig): its pinned -/// changes shown, and some. -fn hasGhosts(pane: *const Pane) bool { - return pane.layers.lanes > 0 and pane.ghosts.len > 0; + return fillBody(tab, null, pane, f, width, true); } /// Everything the geometry is made of, hashed: the text (its revision and @@ -1791,10 +2224,9 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 { /// 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. -/// `ghosted`: the view shows ghost rows (`Pane.ghosts`), each a row of its -/// own before the line it stands at, no number, its text cut to the row; a -/// body with none walks without them. -fn fillBody(comptime ghosted: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize { +/// A read-only line (the `Changes` layer's) is a line as any, wrapped as +/// any; the numbers are the file's, and it has none. +fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize { if (record_wrap) pane.wrap_n = 0; const prefix_width = gutterWidth(pane); // Where a number ends: a blank, then the heat lane and the lanes after it. @@ -1806,42 +2238,12 @@ fn fillBody(comptime ghosted: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State var body_line: usize = f.scroll; var body_at: usize = 0; var written: usize = 0; - // The first ghost row at or below the top line. - var ghost: usize = 0; - if (ghosted) while (ghost < pane.ghosts.len and pane.ghosts[ghost].at < f.scroll) { - ghost += 1; - }; 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; - if (ghosted and !context and body_at == 0 and ghost < pane.ghosts.len and pane.ghosts[ghost].at == body_line) { - const g = pane.ghosts[ghost]; - const said = pane.ghost_text[g.text..][0..g.len]; - var fit: [jj.row_max]u8 = undefined; - // A description is one compact row whatever the scroll. - const shown = if (g.kind == .description) - jj.fitted(tab, said, bodyWidth(pane), &fit) - else if (width > 0) - said[0..fitEnd(tab, said, 0, width)] - else - said[modal.graphemeStart(said, @min(@as(usize, @intCast(@max(0, pane.hscroll))), said.len))..]; - if (record_wrap) { - pane.wrap_line[i] = @intCast(body_line); - pane.wrap_col[i] = -1 - @as(i32, @intCast(ghost)); - pane.wrap_pad[i] = 0; - pane.wrap_n = @intCast(i + 1); - } - if (dst) |out| { - @memset(out[written..][0..prefix_width], ' '); - @memcpy(out[written + prefix_width ..][0..shown.len], shown); - } - written += prefix_width + shown.len; - ghost += 1; - continue; - } 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 ""; @@ -1859,7 +2261,7 @@ fn fillBody(comptime ghosted: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State modal.graphemeStart(text, @min(@as(usize, @intCast(pane.hscroll)), text.len)) else at; - if ((width > 0 or pane.context_rows > 0 or ghosted) and record_wrap) { + 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); @@ -1871,8 +2273,10 @@ fn fillBody(comptime ghosted: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State if (dst) |out| { const prefix = out[written..][0..prefix_width]; @memset(prefix, ' '); - if (at == 0 and prefix.len > 0) { - var lineno = row + 1; + // A read-only line has no number; the others, the file's. + const numbered = f.read_only.len == 0 or readOnlyRow(f, row) == null; + if (at == 0 and prefix.len > 0 and numbered) { + var lineno = (if (f.read_only.len == 0) row else fileRow(f, row)) + 1; var digit: usize = number_end; if (row == end_slot) { prefix[digit - 1] = '~'; @@ -1933,21 +2337,22 @@ fn fillBody(comptime ghosted: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State /// 3. changes (`Changes [revset]`, jj.zig): while `lanes` is set, a /// lane each of the pane's pinned changes after the line number (the /// gutter `lanes` cells wider), a line one added whole green across -/// it (jj.paint). Ruled, not built (PARDES.md "Changes on the source -/// view"): a changed line's words tinted, ghost rows for what the -/// shown revision has not. -/// 4. churn, ruled, not built: a heat lane after the number and a strip -/// by the scroll thumb. +/// it, a changed line's words, and the ghost lines (the buffer's +/// read-only lines, `State.read_only`) inked (jj.paint). +/// 4. churn (`Churn`): a heat lane after the number and a strip by the +/// scroll thumb. /// /// Two bytes, hashed in one update: a digest takes every view's of every /// pane at every step. pub const Layers = struct { ink: bool = true, - /// Whether this view shows the pane's pinned changes (`Pardes.changes`: - /// the revset and jj's answers are the pane's), as the cells their - /// lanes take (laneCells), 0 while it does not: set from the view's - /// frontend, in a window two, which its strips of pixels fill one and - /// a half of (Surface.Lane), in a terminal three, a glyph each. + /// Whether the pane shows its pinned changes (`Pardes.changes`: the + /// revset and jj's answers are the pane's, and shown, their lines are + /// its buffer's, so every view of it shows them or none does: + /// jj.setLanes), as the cells their lanes take in this view + /// (laneCells), 0 while it does not: by the view's frontend, in a window + /// two, which its strips of pixels fill one and a half of + /// (Surface.Lane), in a terminal three, a glyph each. lanes: u8 = 0, /// Whether this view shows the pane's churn (`Churn`, jj.Heat), as the /// cells its lane takes: one after the number, before the lanes. @@ -2156,7 +2561,7 @@ pub fn paintWordSelection( var vr: i32 = 0; while (vr + @as(i32, pane.tag_rows) < @as(i32, r.h)) : (vr += 1) { const here = pane.wrapAt(p.settings.tab, vr); - if (here.line != row or here.ghost) continue; + if (here.line != row) continue; var hi = word_hi; const next = pane.wrapAt(p.settings.tab, vr + 1); if (next.line == row) hi = @min(hi, next.at); |
