diff options
Diffstat (limited to 'src/File.zig')
| -rw-r--r-- | src/File.zig | 387 |
1 files changed, 0 insertions, 387 deletions
diff --git a/src/File.zig b/src/File.zig index 84222191..48a996a4 100644 --- a/src/File.zig +++ b/src/File.zig @@ -13,7 +13,6 @@ const filesystem = @import("fs.zig"); const syntax = @import("syntax.zig"); const diff = @import("diff.zig"); const tracy = @import("tracy.zig"); -const dump = @import("dump.zig"); const limits = @import("memory.zig").limits; const locations = @import("locations.zig"); const Colors = @import("colors.zig"); @@ -221,231 +220,6 @@ pub const State = struct { pub const NodeStep = struct { ranges: []modal.Selection, primary: usize }; -pub fn dumpPane( - p: *Pardes, - arena: std.mem.Allocator, - pane: *const Pane, - file: *const State, - tag: []const u8, - body: []const u8, - scroll: usize, - origin: []const u8, - origin_arg: []const u8, -) !dump.Pane { - const dirty = file.revision != file.saved_revision; - const on_disk = onDisk(file.path); - // A clean file's text is its file's: read from disk when restored, so - // a change made there since is what comes back. Any other is kept, once. - const from_disk = file.output == null and !dirty and on_disk; - // What an unsaved pane's text was read against: the disk as it is, or, - // when a change there was seen already, something it is not. - const disk_hash: ?u64 = if (file.output != null or !dirty or !on_disk) null else if (file.disk_newer) |newer| newer +% 1 else blk: { - const bytes = filesystem.read(p, file.path) catch break :blk null; - defer p.gpa.free(bytes); - break :blk std.hash.Wyhash.hash(0, bytes); - }; - return .{ - .kind = .file, - .tag = tag, - .body = body, - .scroll = scroll, - .cols = pane.cols, - .rows = pane.rows, - .vweight = pane.vweight, - .collapsed = pane.collapsed, - .file = .{ - .tree_context = file.tree_context, - // A query pane's rows are its own (line 1 the query, gutter - // numbers), not location rows Restore reads: it comes back as - // plain text. - .location_rows = if (pane.query != null) &.{} else try dumpLocationRows(arena, file.location_rows), - .path = file.path, - .content_b64 = if (from_disk) "" else try dump.encodeBytes(arena, file.content), - .dirty = dirty, - .disk_hash = disk_hash, - .on_disk = on_disk, - .origin = origin, - .origin_arg = origin_arg, - .mini_source = if (file.mini) |mini| mini.source else "", - .mini_colors_b64 = if (file.mini) |mini| try dump.encodeBytes(arena, mini.colors) else "", - }, - }; -} - -fn dumpLocationRows(arena: std.mem.Allocator, rows: []const locations.Row) ![]const dump.LocationRow { - const saved = try arena.alloc(dump.LocationRow, rows.len); - for (rows, saved) |row, *out| out.* = .{ - .kind = @fromBackingInt(@intCast(@backingInt(row.kind))), - .declaration = row.declaration, - .mark_occurrence = row.mark_occurrence, - .source_column = row.source_column, - .path = row.path, - .line = row.at.line, - .col = row.at.col, - .end_line = row.at.end_line, - .end_col = row.at.end_col, - .location_end = row.location_end, - .code_start = row.code_start, - .colors_b64 = try dump.encodeBytes(arena, row.colors), - }; - return saved; -} - -fn sameSavedLocation(a: dump.LocationRow, b: dump.LocationRow) bool { - return std.mem.eql(u8, a.path, b.path) and a.line == b.line and a.col == b.col and - a.end_line == b.end_line and a.end_col == b.end_col; -} - -fn restoreLocationRows(gpa: std.mem.Allocator, content: []const u8, saved: []const dump.LocationRow) ![]locations.Row { - return restoreOutputLocationRows(gpa, content, saved, false); -} - -fn restoreOutputLocationRows(gpa: std.mem.Allocator, content: []const u8, saved: []const dump.LocationRow, callstack: bool) ![]locations.Row { - if (saved.len == 0) return &.{}; - if (saved.len > std.mem.count(u8, content, "\n") + 1) return error.InvalidLocationRows; - const rows = try gpa.alloc(locations.Row, saved.len); - var initialized: usize = 0; - errdefer { - for (rows[0..initialized]) |row| { - gpa.free(row.path); - gpa.free(row.colors); - } - gpa.free(rows); - } - var lines = std.mem.splitScalar(u8, content, '\n'); - var previous_line: []const u8 = ""; - for (saved, rows, 0..) |row, *out, index| { - const line = lines.next() orelse return error.InvalidLocationRows; - if ((!callstack and row.path.len == 0) or row.path.len >= 4096 or - row.location_end > row.code_start or row.code_start > line.len or - (row.kind == .match and row.location_end == 0) or - (row.declaration and row.kind != .context) or - row.line > std.math.maxInt(i32) or row.col > std.math.maxInt(i32) or - row.end_line > std.math.maxInt(i32) or row.end_col > std.math.maxInt(i32)) - return error.InvalidLocationRows; - if (callstack) { - if (row.location_end > 0) { - const prefix = locations.treePrefix(line); - const parsed = locations.parseRaw(line[prefix.end..]); - if (row.kind != .match or parsed.at.line == 0 or - !std.mem.eql(u8, parsed.path, row.path) or - parsed.at.line != row.line or parsed.at.col != row.col or - parsed.at.end_line != row.end_line or parsed.at.end_col != row.end_col or - prefix.end + parsed.end != row.location_end or - prefix.end + parsed.code_start != row.code_start) return error.InvalidLocationRows; - } else if (row.kind == .preview) { - if (index == 0 or saved[index - 1].kind != .match or !sameSavedLocation(saved[index - 1], row)) return error.InvalidLocationRows; - } else if (row.kind != .context or (row.path.len == 0 and (row.line != 0 or row.code_start != line.len))) return error.InvalidLocationRows; - } else { - const inline_row = row.code_start > row.location_end and row.code_start > 0 and line[row.code_start - 1] == '\t'; - const address_row = !inline_row and row.location_end > 0 and row.code_start == line.len; - const source_row = row.location_end == 0 and row.code_start == 0; - if (!inline_row and !address_row and !source_row) return error.InvalidLocationRows; - if (row.kind == .preview) { - if (!source_row or index == 0) return error.InvalidLocationRows; - const previous = saved[index - 1]; - if (previous.kind != .match or previous.location_end == 0 or previous.code_start != previous_line.len or previous_line[previous_line.len - 1] == '\t' or - !sameSavedLocation(previous, row)) return error.InvalidLocationRows; - } - if (address_row) { - if (index + 1 >= saved.len) return error.InvalidLocationRows; - const following = saved[index + 1]; - const expected: @TypeOf(row.kind) = if (row.kind == .match) .preview else .context; - if (following.kind != expected or following.code_start != 0 or following.location_end != 0 or - following.declaration != row.declaration or !sameSavedLocation(row, following)) return error.InvalidLocationRows; - } - } - const code_len = line.len - row.code_start; - if (row.colors_b64.len > std.base64.standard.Encoder.calcSize(code_len)) return error.InvalidLocationRows; - const colors = try dump.decodeBytes(gpa, row.colors_b64); - errdefer gpa.free(colors); - if (colors.len != 0 and colors.len != code_len) return error.InvalidLocationRows; - for (colors) |color| if (color > @backingInt(syntax.Syn.comment)) return error.InvalidLocationRows; - const path = try gpa.dupe(u8, row.path); - out.* = .{ - .kind = @fromBackingInt(@intCast(@backingInt(row.kind))), - .declaration = row.declaration, - .mark_occurrence = row.mark_occurrence, - .source_column = row.source_column, - .path = path, - .at = .{ .line = row.line, .col = row.col, .end_line = row.end_line, .end_col = row.end_col }, - .location_end = row.location_end, - .code_start = row.code_start, - .colors = colors, - }; - initialized += 1; - previous_line = line; - } - if (lines.next()) |remaining| if (remaining.len != 0 or lines.next() != null) return error.InvalidLocationRows; - return rows; -} - -test "a clean pane whose file was never on disk comes back clean from a Restore; one whose file has gone is said deleted" { - if (comptime !pardes.hosted) return; - for ([_]bool{ false, true }) |was_on_disk| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const id = p.freeSlot().?; - const pane = try restore(p, id, .{ - .kind = .file, - .tag = "", - .body = "", - .file = .{ .path = "/tmp/pardes-never-written/init", .content = "", .on_disk = was_on_disk }, - }); - try std.testing.expectEqual(was_on_disk, pane.file.?.disk_gone); - try std.testing.expectEqual(pane.file.?.saved_revision, pane.file.?.revision); - } -} - -test "location metadata restore rejects offsets and invalid syntax bytes" { - const gpa = std.testing.allocator; - var row: dump.LocationRow = .{ - .kind = .context, - .path = "x.zig", - .line = 1, - .location_end = 0, - .code_start = 3, - }; - const rows = try restoreLocationRows(gpa, "| \tx\n", &.{row}); - defer locations.freeRows(gpa, rows); - try std.testing.expectEqualStrings("x.zig", rows[0].path); - row.code_start = 8; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "| \tx\n", &.{row})); - row.code_start = 3; - row.colors_b64 = "/w=="; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "| \tx\n", &.{row})); - row.colors_b64 = ""; - row.kind = .match; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "| \tx\n", &.{row})); -} - -test "stacked location metadata requires matching adjacent preview ownership" { - const gpa = std.testing.allocator; - const header = "x.c:2:1-3"; - const content = header ++ "\nfoo\n"; - const match: dump.LocationRow = .{ .kind = .match, .path = "x.c", .line = 2, .col = 1, .end_line = 2, .end_col = 3, .location_end = header.len, .code_start = header.len }; - const preview: dump.LocationRow = .{ .kind = .preview, .path = "x.c", .line = 2, .col = 1, .end_line = 2, .end_col = 3, .location_end = 0, .code_start = 0 }; - const restored = try restoreLocationRows(gpa, content, &.{ match, preview }); - defer locations.freeRows(gpa, restored); - try std.testing.expectEqual(.preview, restored[1].kind); - try std.testing.expectEqual(restored[0].at, restored[1].at); - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "foo\n", &.{preview})); - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, header ++ "\n", &.{match})); - var inline_match = match; - inline_match.code_start += 1; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, header ++ "\t\nfoo\n", &.{ inline_match, preview })); - var wrong = preview; - wrong.path = "other.c"; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, content, &.{ match, wrong })); - wrong = preview; - wrong.line = 3; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, content, &.{ match, wrong })); - wrong = preview; - wrong.code_start = 1; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, content, &.{ match, wrong })); -} - 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; @@ -1026,128 +800,6 @@ pub fn open(p: *Pardes, id: usize, path: []const u8, line: usize) !*Pane { return pane; } -pub fn restore(p: *Pardes, id: usize, src: dump.Pane) !*Pane { - const saved = src.file.?; - if (saved.mini_source.len > 0) { - const encoded_limit = std.base64.standard.Encoder.calcSize(Mini.max_output_bytes); - if (saved.mini_source.len >= 4096 or saved.content.len > Mini.max_output_bytes or - saved.content_b64.len > encoded_limit or saved.mini_colors_b64.len > encoded_limit) - return error.InvalidMini; - } - // A clean file's text is read from its file: one changed on disk since - // the dump comes back changed, never as the old text marked clean (and - // a dump from before, which kept it, is read so too). Gone from disk, the - // dump's text if it kept one. - const from_disk: ?[]u8 = if (Output.fromWord(saved.origin) == null and !saved.dirty and saved.on_disk and - saved.mini_source.len == 0 and filesystem.localPath(saved.path) != null) - filesystem.read(p, saved.path) catch null - else - null; - const content: []u8 = if (from_disk) |bytes| bytes else if (saved.content_b64.len > 0) - try dump.decodeBytes(p.gpa, saved.content_b64) - else - try p.gpa.dupe(u8, saved.content); - errdefer p.gpa.free(content); - const path = try p.gpa.dupe(u8, saved.path); - errdefer p.gpa.free(path); - - const output: ?Output.State = if (Output.fromWord(saved.origin)) |origin| blk: { - var value: Output.State = .{ .from = origin }; - try Output.setArg(&value, saved.origin_arg); - break :blk value; - } else null; - - if (saved.location_rows.len != 0 and output == null) return error.InvalidLocationRows; - const is_callstack = if (output) |state| std.meta.eql(state.from, Output.Origin{ .query = .callstack }) else false; - const location_rows = try restoreOutputLocationRows(p.gpa, content, saved.location_rows, is_callstack); - errdefer locations.freeRows(p.gpa, location_rows); - - var mini: ?Mini.State = null; - errdefer if (mini) |*state| state.deinit(p.gpa); - if (saved.mini_source.len > 0) { - if (output == null or !std.meta.eql(output.?.from, Output.Origin{ .cmd = .Mini })) return error.InvalidMini; - const source = try p.gpa.dupe(u8, saved.mini_source); - errdefer p.gpa.free(source); - const colors = try dump.decodeBytes(p.gpa, saved.mini_colors_b64); - errdefer p.gpa.free(colors); - if (colors.len != content.len or colors.len > Mini.max_output_bytes) return error.InvalidMini; - for (colors) |color| if (color > @backingInt(syntax.Syn.comment)) return error.InvalidMini; - mini = .{ .source = source, .colors = colors }; - } else if (saved.mini_colors_b64.len > 0) return error.InvalidMini; - - const history = try History.create(p.gpa); - errdefer p.gpa.destroy(history); - const pane = try p.newDocPane(id); - pane.file = .{ - .path = path, - .content = content, - .output = output, - .mini = mini, - .location_rows = location_rows, - .scroll = src.scroll, - .revision = @intFromBool(saved.dirty), - // Restored with unsaved text, it has text to lose as an edited pane - // has: a hash its text does not match, so Exit, Restore, Del and - // get ask first (builtins.warnModifiedIn reads only edited panes). - .saved_hash = if (saved.dirty) std.hash.Wyhash.hash(0, content) +% 1 else null, - .history = history, - }; - pane.file.?.tree_context = saved.tree_context and supportsContext(pane); - pane.body.cur_pinned = true; - pane.body.cur_row = @intCast(src.scroll); - // Its dot as it was, where the view the dump kept shows it. - if (saved.dot) |dot| if (dot[0] <= dot[1] and dot[1] <= content.len) { - const a = modal.positionAt(content, dot[0]); - const h = modal.positionAt(content, if (dot[1] > dot[0]) modal.prevGrapheme(content, dot[1]) else dot[0]); - pane.body.vsel = .{ .active = dot[1] > dot[0], .row = @intCast(a.row), .col = @intCast(a.col), .explicit = true }; - pane.body.cur_row = @intCast(h.row); - pane.body.cur_col = @intCast(h.col); - }; - pane.cols = @max(1, src.cols); - pane.rows = @max(1, src.rows); - // A directory pane is its directory as it is now, laid out at the next - // sync; one that cannot be read keeps the text the dump kept. - if (output) |o| if (o.from == .dir) { - const f = &pane.file.?; - if (panes.Dir.read(p.gpa, panes.Dir.ofName(path))) |names| { - f.listing = .{ .names = names, .revision = f.revision }; - } else |_| f.listing = .{ .names = &.{}, .revision = f.revision -% 1 }; - }; - if (output == null and filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true, .mode = .baseline_disk } }); - // Its file gone from disk since the dump: its text is nowhere else now, - // so it comes back as one deleted on disk, dirty, and Del, Exit and - // Restore ask before throwing it away. - // One never on disk (Config's new file, a name not yet saved) is - // restored as it was: clean, and nothing deleted. - if (output == null and saved.on_disk and !onDisk(path)) deleted(p, @intCast(id)); - // Unsaved, and its file changed on disk since the text was read: said, - // and its first Save asks before overwriting the newer file. - if (saved.disk_hash) |read_against| if (output == null and onDisk(path)) { - if (filesystem.read(p, path)) |bytes| { - defer p.gpa.free(bytes); - if (std.hash.Wyhash.hash(0, bytes) != read_against) changed(p, @intCast(id), bytes); - } else |_| {} - }; - // An older dump says nothing of what the text was read against: its - // first Save asks, as for a file changed on disk, rather than guess. - if (saved.dirty and saved.disk_hash == null and output == null and onDisk(path)) { - if (filesystem.read(p, path)) |bytes| { - defer p.gpa.free(bytes); - const f = &pane.file.?; - f.disk_newer = std.hash.Wyhash.hash(0, bytes); - f.disk_newer_warned = false; - } else |_| {} - } - return pane; -} - -/// Whether `path` names a file on this host's disk now. -fn onDisk(path: []const u8) bool { - if (comptime !pardes.hosted) return false; - const local = filesystem.localPath(path) orelse return false; - return @import("exec.zig").kindOf(local) != null; -} - 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 } }); @@ -1485,45 +1137,6 @@ pub fn leaveTwins(p: *Pardes, f: *State) void { if (n == 1) p.panes[twins[0]].?.file.?.twin = 0; } -/// Restore's: pane `id`, restored on its own, back on pane `of`'s buffer -/// as the Zerox twin it was dumped as (dump.File.twin_of). Not a file pane -/// there (a dump edited by hand), it stays on its own. -pub fn relink(p: *Pardes, of: usize, id: usize, dot: ?[2]u32) void { - const src = p.panes[of] orelse return; - const pane = p.panes[id] orelse return; - const sf = if (src.file) |*file| file else return; - const f = if (pane.file) |*file| file else return; - if (sf.output != null or f.output != null or sf.mini != null) return; - if (sf.twin == 0) { - const group = p.gpa.dupe(u8, sf.path) catch return; - p.twin_groups.append(p.gpa, .{ .id = p.next_twin, .saved_revision = sf.saved_revision, .saved_hash = sf.saved_hash, .saved_len = sf.saved_len, .disk_newer = sf.disk_newer, .disk_gone = sf.disk_gone, .path = group }) catch return p.gpa.free(group); - sf.twin = p.next_twin; - p.next_twin +%= 1; - } - pardes.memory.freeUnfilled(p.gpa, f.content); - if (f.line_starts.len > 0) pardes.memory.freeRaw(p.gpa, f.line_starts); - f.history.destroy(p.gpa); - f.content = sf.content; - f.line_starts = sf.line_starts; - f.history = sf.history; - f.revision = sf.revision; - f.saved_revision = sf.saved_revision; - f.saved_hash = sf.saved_hash; - f.saved_len = sf.saved_len; - f.twin = sf.twin; - if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); - f.highlights = &.{}; - f.syntax_dirty = true; - // Its dot as dumped, read now that its text is back. - if (dot) |d| if (d[0] <= d[1] and d[1] <= f.content.len) { - const a = modal.positionAt(f.content, d[0]); - const h = modal.positionAt(f.content, if (d[1] > d[0]) modal.prevGrapheme(f.content, d[1]) else d[0]); - pane.body.vsel = .{ .active = d[1] > d[0], .row = @intCast(a.row), .col = @intCast(a.col), .explicit = true }; - pane.body.cur_row = @intCast(h.row); - pane.body.cur_col = @intCast(h.col); - }; -} - pub const TwinGroup = struct { id: u32, saved_revision: u32, |
