summaryrefslogtreecommitdiff
path: root/core/Pick.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Pick.zig')
-rw-r--r--core/Pick.zig145
1 files changed, 130 insertions, 15 deletions
diff --git a/core/Pick.zig b/core/Pick.zig
index b76020ca..2bcaf76f 100644
--- a/core/Pick.zig
+++ b/core/Pick.zig
@@ -233,9 +233,11 @@ fn make(p: *Pardes, source: Source) !*Pick {
}
/// Picker `pk` asked from pane `from`: the pane it lands in is `from` when
-/// it reads; its reader when it is held and has one; else a pane made under
-/// it as its reader would be, empty until the preview fills it. The
-/// keyboard stays where it was: the landing pane is the tag's host.
+/// it reads; its reader when it is held and has one; else an untouched
+/// pane, `from` itself first (UX J1), which previews as a reading pane does
+/// and is given back as it was on Esc; else a pane made where its reader
+/// would go, empty until the preview fills it. The keyboard stays where it
+/// was: the landing pane is the tag's host.
fn hostIn(p: *Pardes, pk: *Pick, from: usize) !void {
const asker = p.panes[from].?;
pk.owner = asker.serial;
@@ -247,13 +249,17 @@ fn hostIn(p: *Pardes, pk: *Pick, from: usize) !void {
p.view().active = from;
return;
}
- const reader = if (Trail.reading(p, asker)) from else Trail.readerOf(p, asker);
+ var reader = if (Trail.reading(p, asker)) from else Trail.readerOf(p, asker);
+ if (reader == null) if (layout.freeSpace(p, from, .reader)) |free| if (free == .pane) {
+ reader = free.pane;
+ };
pk.landing = p.panes[reader orelse try landingFor(p, from)].?.serial;
pk.made = reader == null;
p.view().active = from;
}
-/// A pane made under pane `from` for a picker, where its reader goes.
+/// A pane made for a picker where pane `from`'s reader goes: under it, or
+/// an empty column (layout.freeSpace).
fn landingFor(p: *Pardes, from: usize) !usize {
const asker = p.panes[from].?;
const free = p.freeSlot() orelse return error.NoPaneSlots;
@@ -1340,8 +1346,10 @@ const How = enum { walk, below, column, queue };
/// Enter: the current row, or the marked ones, looked at from the asking
/// pane (look.open, the walk's way: a reading pane steps, a held one's
/// reader does), the first as `how` says, the others each in a pane below.
-/// The list rolls into the landing pane's trail as its word (item 4).
-fn land(p: *Pardes, how: How) void {
+/// The list rolls into the landing pane's trail as its word (item 4). From
+/// an untouched pane S-Enter and C-v are Enter: it is the free space a new
+/// pane would take (UX J1).
+fn land(p: *Pardes, wanted: How) void {
const pk = live(p) orelse return;
var arena = std.heap.ArenaAllocator.init(p.gpa);
defer arena.deinit();
@@ -1352,7 +1360,7 @@ fn land(p: *Pardes, how: How) void {
pk.queue.clearRetainingCapacity();
pk.queue_bytes.clearRetainingCapacity();
pk.queue_at = 0;
- if (how == .queue) for (picked[1..]) |t| switch (t) {
+ if (wanted == .queue) for (picked[1..]) |t| switch (t) {
.place => |at| {
const q: Queued = .{ .path = @intCast(pk.queue_bytes.items.len), .len = @intCast(@min(at.path.len, std.math.maxInt(u16))), .line = std.math.lossyCast(u32, at.at.line), .col = std.math.lossyCast(u32, at.at.col) };
pk.queue_bytes.appendSlice(p.gpa, at.path[0..q.len]) catch break;
@@ -1360,13 +1368,16 @@ fn land(p: *Pardes, how: How) void {
},
else => {},
};
- // A step from the pane it lands in when that reads (its owner's reader
- // or itself), else from its owner, whose reader the place opens as.
- const landing = if (pk.made) null else p.paneBySerial(pk.landing);
TagState.toRest(p);
const v = p.view();
const owner = p.paneBySerial(pk.owner) orelse v.active;
- const from = if (how == .walk or how == .queue) landing orelse owner else owner;
+ const how: How = if ((wanted == .below or wanted == .column) and Trail.untouched(p, p.panes[owner].?)) .walk else wanted;
+ // A step from the pane it lands in when that reads (its owner's reader
+ // or itself), else from its owner, whose reader the place opens as:
+ // where the pane made for the picker was, or in the untouched pane it
+ // previewed in (UX J1), given back now.
+ const landing = p.paneBySerial(pk.landing) orelse owner;
+ const from = if ((how == .walk or how == .queue) and Trail.reading(p, p.panes[landing].?)) landing else owner;
v.active = from;
v.jumps.force = true;
for (picked, 0..) |t, k| switch (t) {
@@ -1383,9 +1394,11 @@ fn land(p: *Pardes, how: How) void {
else => listed(p, pk),
}
} else if (k == 0 and how == .column) {
- layout.newColumn(p, from);
- const c = layout.activeColumn(p) orelse continue;
- p.exec_column = c;
+ // A column made only where there is no free space (UX J1).
+ if (layout.freeSpace(p, from, .doc) == null) {
+ layout.newColumn(p, from);
+ p.exec_column = layout.activeColumn(p) orelse continue;
+ }
defer p.exec_column = null;
pardes.tagline.exitHeader(p);
_ = look.open(p, from, at.path, at.at, .below);
@@ -1978,6 +1991,7 @@ test "Enter lands as a walk step in the reading pane; S-Enter opens below; from
const b_path = try testFile(p, &tmp, "beta.zig", "one\ntwo\nthree\n");
defer p.gpa.free(b_path);
look.lookAt(p, p.view().active, a_path);
+ p.sync();
const id = p.view().active;
const npanes = paneCount(p);
open(p, id, .list, "Refs", "", "beta.zig:3:1 three\n");
@@ -2193,6 +2207,7 @@ test "a picker from a reading pane unrolls in that pane, its own text set aside
const b_path = try testFile(p, &tmp, "beta.zig", "one\ntwo\nthree\n");
defer p.gpa.free(b_path);
look.lookAt(p, p.view().active, a_path);
+ p.sync();
const id = p.view().active;
try std.testing.expect(Trail.reading(p, p.panes[id].?));
const npanes = paneCount(p);
@@ -2620,3 +2635,103 @@ test "Backspace on a Messages picker's empty field goes back to the level it was
p.sync();
_ = try p.render(frame.allocator());
}
+
+test "a picker started in an untouched +New previews in it (UX J1): no pane made, Esc gives it back empty, Enter lands in it, the same pane by its number, reading, its trail starting at the list word; S-Enter there is Enter" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 });
+ defer p.deinit();
+ p.settings.jumps = .walk;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ const b_path = try testFile(p, &tmp, "beta.zig", "one\ntwo\nthree\n");
+ defer p.gpa.free(b_path);
+ const rows = try std.fmt.allocPrint(p.gpa, "{s}:3:1 three\n", .{b_path});
+ defer p.gpa.free(rows);
+ _ = try p.setTestFile("first\n");
+ for ([_]bool{ false, true }) |shift| {
+ exec.runBuiltin(p, .New, 0, "New", null);
+ p.sync();
+ const scratch = p.view().active;
+ const serial = p.panes[scratch].?.serial;
+ const top = p.view().rects[scratch].y;
+ const npanes = paneCount(p);
+ try std.testing.expect(Trail.untouched(p, p.panes[scratch].?));
+ open(p, scratch, .list, "Refs", "", rows);
+ settle(p);
+ p.update(.tick);
+ try std.testing.expect(!live(p).?.made);
+ try std.testing.expectEqual(serial, live(p).?.landing);
+ try std.testing.expectEqual(npanes, paneCount(p));
+ try std.testing.expectEqualStrings(b_path, p.panes[scratch].?.file.?.path);
+ // Esc: the +New as it was, still free space.
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expectEqual(npanes, paneCount(p));
+ try std.testing.expect(Trail.untouched(p, p.panes[scratch].?));
+ try std.testing.expectEqual(serial, p.panes[scratch].?.serial);
+ // Enter (or S-Enter): the file in it.
+ open(p, scratch, .list, "Refs", "", rows);
+ settle(p);
+ p.update(.{ .key = .{ .cp = Key.enter, .shift = shift } });
+ p.sync();
+ try std.testing.expect(live(p) == null);
+ const here = p.panes[p.view().active].?;
+ try std.testing.expectEqual(serial, here.serial);
+ try std.testing.expectEqualStrings(b_path, here.file.?.path);
+ try std.testing.expectEqual(@as(i32, 2), here.body.cur_row);
+ try std.testing.expectEqual(npanes, paneCount(p));
+ try std.testing.expectEqual(top, p.view().rects[p.view().active].y);
+ try std.testing.expect(Trail.reading(p, here));
+ try std.testing.expectEqual(@as(u8, 2), here.trail.n);
+ try std.testing.expect(here.trail.steps[0] == .list and here.trail.steps[1] == .place);
+ }
+}
+
+test "a picker from a held pane previews in an untouched +New (UX J1): Esc gives it back empty, Enter makes it the held pane's reader by its number; C-v lands in an empty column rather than making one" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 });
+ defer p.deinit();
+ p.settings.jumps = .walk;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ const b_path = try testFile(p, &tmp, "beta.zig", "one\ntwo\nthree\n");
+ defer p.gpa.free(b_path);
+ const rows = try std.fmt.allocPrint(p.gpa, "{s}:3:1 three\n", .{b_path});
+ defer p.gpa.free(rows);
+ _ = try p.setTestFile("held\n");
+ const held = p.view().active;
+ Trail.keep(p.panes[held].?);
+ exec.runBuiltin(p, .New, held, "New", null);
+ p.sync();
+ const scratch = p.view().active;
+ const serial = p.panes[scratch].?.serial;
+ p.view().active = held;
+ const npanes = paneCount(p);
+ open(p, held, .list, "Refs", "", rows);
+ settle(p);
+ p.update(.tick);
+ try std.testing.expect(!live(p).?.made);
+ try std.testing.expectEqual(serial, live(p).?.landing);
+ try std.testing.expectEqualStrings(b_path, p.panes[scratch].?.file.?.path);
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expectEqual(npanes, paneCount(p));
+ try std.testing.expect(Trail.untouched(p, p.panes[scratch].?));
+ open(p, held, .list, "Refs", "", rows);
+ settle(p);
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ p.sync();
+ const reader = Trail.readerOf(p, p.panes[held].?).?;
+ try std.testing.expectEqual(serial, p.panes[reader].?.serial);
+ try std.testing.expectEqualStrings(b_path, p.panes[reader].?.file.?.path);
+ try std.testing.expectEqual(p.panes[held].?.serial, p.panes[reader].?.trail.steps[0].owner);
+ try std.testing.expectEqual(npanes, paneCount(p));
+ // C-v with an empty column there: the place goes in it.
+ Trail.keep(p.panes[reader].?);
+ const c = layout.insertColumn(p, layout.findPane(p, held).?.col, false).?;
+ p.sync();
+ const ncol = p.view().ncol;
+ open(p, held, .list, "Refs", "", rows);
+ settle(p);
+ p.update(.{ .key = .{ .cp = 'v', .ctrl = true } });
+ p.sync();
+ try std.testing.expectEqual(ncol, p.view().ncol);
+ try std.testing.expectEqual(c, layout.findPane(p, p.view().active).?.col);
+ try std.testing.expectEqualStrings(b_path, p.panes[p.view().active].?.file.?.path);
+}