diff options
Diffstat (limited to 'core/Pick.zig')
| -rw-r--r-- | core/Pick.zig | 86 |
1 files changed, 76 insertions, 10 deletions
diff --git a/core/Pick.zig b/core/Pick.zig index 10c80dfc..b76020ca 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -57,8 +57,9 @@ const Key = pardes.Key; /// (SPC l a, the body showing the one under the cursor as its edit). pub const Source = enum { find, grep, recent, panes, symbols, all, jumplist, list, messages, themes, actions }; -/// Each source's word in the tag and the leader keys of the builtin that -/// opens it, its key cells (`␣ f Find`). +/// Each source's word in the tag and its key cells (`␣ f Find`); a +/// source whose Backspace leaves it (`back`) goes back to the SPC level of +/// those keys: a group's. const words = std.EnumArray(Source, struct { word: []const u8, keys: []const u8 }).init(.{ .find = .{ .word = "Find", .keys = "f" }, .grep = .{ .word = "Grep", .keys = "f" }, @@ -68,7 +69,7 @@ const words = std.EnumArray(Source, struct { word: []const u8, keys: []const u8 .all = .{ .word = "All", .keys = "" }, .jumplist = .{ .word = "Jumplist", .keys = "j" }, .list = .{ .word = "List", .keys = "l" }, - .messages = .{ .word = "Messages", .keys = "hm" }, + .messages = .{ .word = "Messages", .keys = "h" }, .themes = .{ .word = "Themes", .keys = "t" }, .actions = .{ .word = "Actions", .keys = "la" }, }); @@ -120,8 +121,9 @@ aside: ?File.State = null, aside_row: i32 = 0, aside_col: i32 = 0, aside_top: usize = 0, -/// The row the preview shows (`no_row`: none yet); asked again on the -/// frame after the cursor moves, never on the key that moved it. +/// What the row the preview shows is (`idOf`; `no_row`: none yet): asked +/// again on the frame after the cursor moves to another, never on the key +/// that moved it, and on the frame a row first comes under it. shown: u32 = no_row, /// The preview on (M-p). previews: bool = true, @@ -437,7 +439,8 @@ pub fn produceAll(p: *Pardes, tick: bool) void { /// The frames a live picker still owes: scoring, a preview (nextWake's). pub fn busy(p: *const Pardes) bool { for (p.picks) |slot| if (slot) |pk| if (pk.unrolled) { - if (pk.names.busy() or (pk.previews and pk.shown != p.view().tag_state.rows.at)) return true; + const at = p.view().tag_state.rows.at; + if (pk.names.busy() or (pk.previews and at < count(p, pk) and pk.shown != idOf(pk, at))) return true; }; return false; } @@ -764,7 +767,7 @@ pub fn landingLine(p: *Pardes, arena: std.mem.Allocator, pane: *const Pane) ?usi // edit on the Changes grounds are their previews. if (!pk.previews or pk.landing != pane.serial or pk.source == .themes or pk.source == .actions) return null; const s = &p.view().tag_state; - if (pk.shown != s.rows.at or count(p, pk) == 0) return null; + if (s.rows.at >= count(p, pk) or pk.shown != idOf(pk, s.rows.at)) return null; const row = rowAt(p, pk, arena, s.rows.at) catch return null; const place = switch (row.target) { .place => |t| t, @@ -802,9 +805,11 @@ pub fn sentence(p: *const Pardes, w: *std.Io.Writer) ?[2]usize { /// open pane shows is taken from it; any other is read. fn preview(p: *Pardes, pk: *Pick) void { const s = &p.view().tag_state; - if (pk.shown == s.rows.at or !pk.previews) return; - pk.shown = s.rows.at; - if (count(p, pk) == 0) return; + // No row yet: none shown, so the first to come is (a Find's listing + // and a Grep's lines come frames after the field asked). + if (!pk.previews or s.rows.at >= count(p, pk)) return; + if (pk.shown == idOf(pk, s.rows.at)) return; + pk.shown = idOf(pk, s.rows.at); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); const row = rowAt(p, pk, frame.allocator(), s.rows.at) catch return; @@ -2554,3 +2559,64 @@ test "code actions: a row that cannot be applied (a command's, an edit reaching }; try std.testing.expectEqual(@as(?u16, @intCast((pane.rows + 2) / 3)), row_at); } + +test "a picker previews the row under its cursor from the frame its rows come, when a frame with none came first: a Find's listing, a Grep's lines after their pause (UX D7)" { + 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 a_path = try testFile(p, &tmp, "alpha.zig", "const alpha = 1;\npub fn a() void {}\n"); + defer p.gpa.free(a_path); + _ = try p.setTestFile("held\n"); + const asker = p.view().active; + Trail.keep(p.panes[asker].?); + p.gpa.free(p.panes[asker].?.file.?.path); + p.panes[asker].?.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ std.Io.Dir.path.dirname(a_path).?, "held.txt" }); + p.stream.engine = .walk; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + // Find, its field empty: a frame before the listing comes (a host's + // worker sends it by an input of its own, after the step that asked). + open(p, asker, .find, "", "", ""); + const reader = p.paneBySerial(live(p).?.landing).?; + produceAll(p, true); + try std.testing.expectEqual(@as(usize, 0), count(p, live(p).?)); + // No rows: no frame owed for a preview. + try std.testing.expect(!busy(p)); + settle(p); + try std.testing.expectEqual(@as(usize, 1), count(p, live(p).?)); + try std.testing.expectEqualStrings(a_path, p.panes[reader].?.file.?.path); + try std.testing.expectEqual(@as(?usize, 0), landingLine(p, frame.allocator(), p.panes[reader].?)); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Grep: its lines come once the typing pauses, frames with none before. + open(p, asker, .grep, "", "", ""); + const greader = p.paneBySerial(live(p).?.landing).?; + for ("pub") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + p.update(.tick); + try std.testing.expectEqual(@as(usize, 0), count(p, live(p).?)); + try std.testing.expect(!busy(p)); + p.clock.now_ns = p.stream.due_ns.?; + settle(p); + try std.testing.expectEqual(@as(usize, 1), count(p, live(p).?)); + try std.testing.expectEqualStrings(a_path, p.panes[greader].?.file.?.path); + try std.testing.expectEqual(@as(?usize, 1), landingLine(p, frame.allocator(), p.panes[greader].?)); +} + +test "Backspace on a Messages picker's empty field goes back to the level it was run from, SPC h, unrolled and drawn: its keys are its level's, as every other picker's are" { + const Key_ = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + _ = try p.setTestFile("one\ntwo\n"); + for (" hm") |c| p.update(.{ .key = .{ .cp = c } }); + try std.testing.expectEqual(Source.messages, live(p).?.source); + p.sync(); + _ = try p.render(frame.allocator()); + p.update(.{ .key = .{ .cp = Key_.backspace } }); + try std.testing.expect(p.view().prefix == .leader and p.view().tag_state.unrolled); + try std.testing.expectEqualStrings("h", p.view().leader_keys[0..p.view().leader_n]); + p.sync(); + _ = try p.render(frame.allocator()); +} |
