summaryrefslogtreecommitdiff
path: root/core/Trail.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Trail.zig')
-rw-r--r--core/Trail.zig106
1 files changed, 105 insertions, 1 deletions
diff --git a/core/Trail.zig b/core/Trail.zig
index 73ea1ffb..96d505ae 100644
--- a/core/Trail.zig
+++ b/core/Trail.zig
@@ -198,7 +198,10 @@ pub fn replaceStep(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize {
/// acme's Get keeps a window's id: its walk, its serial (`@pN`, a script's
/// `/pane/N`), the 9P state its scripts hold and its /log record (a rename),
/// the file it left closed as any (Recent keeps its place). A line past the
-/// new file's end is a miss that changes nothing.
+/// new file's end is a miss that changes nothing. A file with changes it
+/// has not saved is never closed by a step, whatever changed it (§6.5): its
+/// pane is held, as Keep holds it, and the step opens where a held pane's
+/// go, in its reader.
fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize {
const pane = p.panes[id].?;
if (pane.file) |f| if (std.mem.eql(u8, f.path, path)) {
@@ -207,6 +210,19 @@ fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize {
lift(p, id);
return id;
};
+ if (pardes.tagline.dirty(pane)) {
+ keep(pane);
+ const free = switch (look.open(p, id, path, at, .replace)) {
+ .opened => |free| free,
+ .shown, .none => return p.view().active,
+ };
+ if (at.line > panes.File.nlines(p.gpa, &p.panes[free].?.file.?)) {
+ try p.removePane(free, null);
+ p.view().active = id;
+ return error.LineMissing;
+ }
+ return free;
+ }
const free = p.freeSlot() orelse return error.NoPaneSlots;
const nt = try panes.File.open(p, free, path, at.line);
// Refused: closed at the step's end, never placed (Pardes.unplaced).
@@ -1193,3 +1209,91 @@ test "Enter on a word of the walk, the keyboard in the tag, goes back to its ste
try std.testing.expectEqualStrings("rank.zig:10", f.at(&at));
try std.testing.expect(pane.focus == .body);
}
+
+test "a walk step never closes a file with unsaved changes: a reading pane changed by any path, one that holds it or one that does not, is held as Keep holds it, and the step opens in its reader, the change kept (SPEC §6.5)" {
+ const th = @import("ninep/harness.zig");
+ const tree = @import("ninep/tree.zig");
+ const Key = pardes.Key;
+ const Path = enum { body, edit, pipe, lsp, paste, typed_unwalked, load };
+ var lost: usize = 0;
+ for (std.enums.values(Path)) |path| {
+ var f: Fixture = .{ .p = undefined, .tmp = undefined };
+ try f.init(60);
+ defer f.deinit();
+ const p = f.p;
+ // rank.zig in the held pane's reader, a reading pane.
+ f.look(f.held, "rank.zig", 12);
+ const r = p.view().active;
+ const pane = p.panes[r].?;
+ try std.testing.expect(reading(p, pane));
+ switch (path) {
+ // A 9P write to its body.
+ .body => try std.testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(pane.serial, .body), "changed\n").reply.status),
+ // An Edit.
+ .edit => try std.testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(pane.serial, .ctl), "Edit 3a/ changed/\n").reply.status),
+ // A pipe of its line, answered.
+ .pipe => {
+ while (p.nextEffect()) |_| {}
+ typeKeys(p, "x|");
+ p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } });
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ var id: ?u32 = null;
+ while (p.nextEffect()) |e| switch (e) {
+ .pipe => |asked| id = asked.id,
+ else => {},
+ };
+ p.update(.{ .pipe_resp = .{ .id = id.?, .success = true, .outputs = &.{"CHANGED\n"} } });
+ },
+ // A language server's edit (a code action's records).
+ .lsp => try std.testing.expect(@import("lsp/Ask.zig").applyAction(p, pane, pane.file.?.revision, "@put 0 0 changed%0A\n")),
+ // A paste.
+ .paste => p.update(.{ .paste = "changed\n" }),
+ // Typed under `Jumps new-pane`, where an edit holds nothing,
+ // the session then walking.
+ .typed_unwalked => {
+ p.settings.jumps = .@"new-pane";
+ typeKeys(p, "ichanged");
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ p.settings.jumps = .walk;
+ },
+ // A new text put in as a load (File.load holds no pane: the
+ // Edit command's way before it said Trail.edited).
+ .load => panes.File.load(p, &pane.file.?, try p.gpa.dupe(u8, "changed\n")),
+ }
+ const text = try std.testing.allocator.dupe(u8, pane.file.?.content);
+ defer std.testing.allocator.free(text);
+ try std.testing.expect(std.mem.indexOf(u8, text, "changed") != null or std.mem.indexOf(u8, text, "CHANGED") != null);
+ try std.testing.expect(pardes.tagline.dirty(pane));
+ // A Look from it at another file.
+ f.look(r, "place.zig", 8);
+ var buf: [64]u8 = undefined;
+ try std.testing.expectEqualStrings("place.zig:8", f.at(&buf));
+ const reader_pane = f.active();
+ // rank.zig still open with its change, held; place.zig in its reader.
+ var kept: ?*Pane = null;
+ for (p.panes) |slot| if (slot) |other| if (other.file) |of| if (std.mem.endsWith(u8, of.path, "/rank.zig")) {
+ kept = other;
+ };
+ if (kept == null) {
+ std.debug.print("{t}: the changed rank.zig was closed by the step\n", .{path});
+ lost += 1;
+ continue;
+ }
+ try std.testing.expectEqualStrings(text, kept.?.file.?.content);
+ try std.testing.expect(kept.?.trail.held and kept.? != reader_pane);
+ try std.testing.expectEqual(kept.?.serial, reader_pane.trail.steps[0].owner);
+ }
+ try std.testing.expectEqual(@as(usize, 0), lost);
+ // Its owner closed: a reader with unsaved changes is held, never closed
+ // with it (the tether, SPEC §11, closes a reader that only read).
+ var f: Fixture = .{ .p = undefined, .tmp = undefined };
+ try f.init(60);
+ defer f.deinit();
+ f.look(f.held, "rank.zig", 12);
+ const r = f.p.view().active;
+ panes.File.load(f.p, &f.p.panes[r].?.file.?, try f.p.gpa.dupe(u8, "changed\n"));
+ try f.p.removePane(f.held, null);
+ const kept = f.p.panes[r] orelse return error.TheChangedReaderWasClosed;
+ try std.testing.expectEqualStrings("changed\n", kept.file.?.content);
+ try std.testing.expect(kept.trail.held);
+}