summaryrefslogtreecommitdiff
path: root/core/Pick.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Pick.zig')
-rw-r--r--core/Pick.zig127
1 files changed, 126 insertions, 1 deletions
diff --git a/core/Pick.zig b/core/Pick.zig
index fe7454b3..10c80dfc 100644
--- a/core/Pick.zig
+++ b/core/Pick.zig
@@ -564,11 +564,19 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row {
// changes, which the body is moved to while it is previewed.
.actions => {
row.key = @intFromBool(cand.ref < 9);
+ const act = action(pk, cand.ref) orelse return row;
+ // One Enter cannot apply (apply's refusals): dim, saying why
+ // (UX E5 12).
+ if (act.records.len == 0 or std.mem.eql(u8, act.records, "@elsewhere\n")) {
+ row.dim = true;
+ row.meta = if (act.records.len == 0) "command" else "other files";
+ return row;
+ }
const owner = p.panes[p.paneBySerial(pk.landing) orelse return row].?;
// With the `Changes` layer's lines shown the body stays where it is.
const f = if (owner.file) |*file| file else return row;
if (f.layer.lines.len > 0 or f.revision != pk.revision) return row;
- const edits = pardes.lsp.Ask.parseLspEdits(arena, (action(pk, cand.ref) orelse return row).records, null) orelse return row;
+ const edits = pardes.lsp.Ask.parseLspEdits(arena, act.records, null) orelse return row;
if (edits[0].start > f.content.len) return row;
const line = @import("Places.zig").lineOf(try File.lineIndex(p.gpa, f), edits[0].start);
row.target = .{ .place = .{ .path = f.path, .at = .{ .line = line + 1 } } };
@@ -717,6 +725,30 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step {
const r = try rowAt(p, pk, arena, i);
row.* = .{ .text = r.text, .key = r.key, .on = i == s.rows.at, .mark = marked(pk, idOf(pk, i)) != null, .dim = r.dim, .pre = r.pre, .faint = r.faint, .marks = r.marks, .index = @intCast(i), .meta = r.meta };
}
+ // A place's text (a Grep's line, a list's): its `path:line` in the dim
+ // ink and the text after it in one column, the widest place shown
+ // before it (UX E7 2b: the old +Search's column). Bounded by the rows
+ // shown.
+ if (pk.source == .grep or pk.source == .all or pk.source == .list) {
+ var widest: usize = 0;
+ for (rows) |row| if (row.pre > 0 and row.pre < row.text.len) {
+ widest = @max(widest, File.displayWidth(p.settings.tab, row.text[0 .. std.mem.indexOfScalarPos(u8, row.text, row.pre, ' ') orelse row.text.len]));
+ };
+ for (rows) |*row| if (row.pre > 0 and row.pre < row.text.len) {
+ // The place up to its blank: a column (`:3:1`) and the `:` a
+ // compiler's `path:line:col: words` puts after it are its.
+ const end = std.mem.indexOfScalarPos(u8, row.text, row.pre, ' ') orelse row.text.len;
+ row.quiet = .{ 0, @intCast(end) };
+ const pad = widest - File.displayWidth(p.settings.tab, row.text[0..end]);
+ if (pad == 0) continue;
+ const blanks = try arena.alloc(u8, pad);
+ @memset(blanks, ' ');
+ row.text = try std.mem.concat(arena, u8, &.{ row.text[0..end], blanks, row.text[end..] });
+ const marks = try arena.alloc(u16, row.marks.len);
+ for (marks, row.marks) |*m, at| m.* = if (at >= end) at + @as(u16, @intCast(pad)) else at;
+ row.marks = marks;
+ };
+ }
out[0] = .{ .rows = rows };
return out;
}
@@ -2429,3 +2461,96 @@ test "a picker's preview is legible as one (UX D7): its landing line on the hit
p.update(.{ .key = .{ .cp = pardes.Key.escape } });
try std.testing.expect(!pardes.Messages.noticeRow(p, 120, &buf).quiet_left);
}
+
+test "a list of places (a Grep's, a list's) shows each row's `path:line` in the dim ink and its text after it in one column, the widest place shown before it (UX E7 2b)" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 40 });
+ defer p.deinit();
+ p.settings.jumps = .walk;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ const a_path = try testFile(p, &tmp, "a.zig", "one\ntwo\nconst x = 1;\n");
+ defer p.gpa.free(a_path);
+ const b_path = try testFile(p, &tmp, "a_longer_name.zig", "fn y() void {}\n");
+ defer p.gpa.free(b_path);
+ _ = try p.setTestFile("held\n");
+ const asker = p.view().active;
+ Trail.keep(p.panes[asker].?);
+ const rows = try std.fmt.allocPrint(std.testing.allocator, "{s}:3:1 const x = 1;\n{s}:1:4 fn y() void {{}}\n", .{ a_path, b_path });
+ defer std.testing.allocator.free(rows);
+ open(p, asker, .list, "Refs", "", rows);
+ p.update(.tick);
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ const got = (try steps(p, frame.allocator()))[0].rows;
+ try std.testing.expectEqual(@as(usize, 2), got.len);
+ const code = [2][]const u8{ "const x = 1;", "fn y() void {}" };
+ var col: [2]usize = undefined;
+ for (got, code, &col) |row, text, *c| {
+ const at = std.mem.indexOf(u8, row.text, text).?;
+ c.* = File.displayWidth(8, row.text[0..at]);
+ // The place dim up to its blank (`path:3:1`); the text after it not.
+ try std.testing.expect(row.pre > 0);
+ try std.testing.expectEqual(@as(u16, 0), row.quiet[0]);
+ try std.testing.expectEqual(':', row.text[row.quiet[1] - 2]);
+ try std.testing.expectEqual(' ', row.text[row.quiet[1]]);
+ }
+ try std.testing.expectEqual(col[0], col[1]);
+ // One cell after the widest place.
+ try std.testing.expectEqual(File.displayWidth(8, b_path) + ":1:4 ".len, col[1]);
+ // As the band draws them, its last step the band's width (UX E3): the
+ // text still in one column.
+ const host = TagState.hostOf(p).?;
+ const band = (try Band.of(p, frame.allocator(), p.panes[host].?, host)).?;
+ const last = band.steps[band.nsteps - 1];
+ try std.testing.expectEqual(band.columns, last.x + last.width);
+ for (code, &col, 0..) |text, *c, y| {
+ const ln = try band.line(frame.allocator(), p.settings.tab, @intCast(y));
+ c.* = File.displayWidth(p.settings.tab, ln.text[0..std.mem.indexOf(u8, ln.text, text).?]);
+ }
+ try std.testing.expectEqual(col[0], col[1]);
+}
+
+test "code actions: a row that cannot be applied (a command's, an edit reaching other files) is dim, its meta `command` or `other files`; the edit under the cursor a third of the way down the body (UX E5 12, D7)" {
+ const Key_ = pardes.Key;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 });
+ defer p.deinit();
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ p.settings.jumps = .walk;
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(std.testing.allocator);
+ for (1..81) |i| try text.print(std.testing.allocator, "line {d}\n", .{i});
+ const pane = try p.setTestFile(text.items);
+ const id = p.view().active;
+ // Line 50 starts after lines 1-49.
+ var at: usize = 0;
+ for (1..50) |i| at += std.fmt.count("line {d}\n", .{i});
+ const rows = try std.fmt.allocPrint(frame.allocator(), "x:1:1 extract\nx:1:1 move to a new file\n@elsewhere\nx:1:1 rename\n@put {d} {d} LINE\n", .{ at, at + 4 });
+ open(p, id, .actions, "", "", rows);
+ live(p).?.revision = pane.file.?.revision;
+ const got = (try steps(p, frame.allocator()))[0].rows;
+ try std.testing.expectEqual(@as(usize, 3), got.len);
+ try std.testing.expect(got[0].dim);
+ try std.testing.expectEqualStrings("command", got[0].meta);
+ try std.testing.expect(got[1].dim);
+ try std.testing.expectEqualStrings("other files", got[1].meta);
+ try std.testing.expect(!got[2].dim);
+ try std.testing.expectEqualStrings("", got[2].meta);
+ // As the band draws it, its step the band's width (UX E3): the meta
+ // at the band's right end.
+ const band = (try Band.of(p, frame.allocator(), pane, TagState.hostOf(p).?)).?;
+ const ln = try band.line(frame.allocator(), p.settings.tab, 0);
+ try std.testing.expect(std.mem.endsWith(u8, ln.text, " command"));
+ try std.testing.expectEqual(@as(usize, band.columns), File.displayWidth(p.settings.tab, ln.text));
+ // The rename's: its line (50) a third of the way down the body.
+ p.update(.{ .key = .{ .cp = Key_.down } });
+ p.update(.{ .key = .{ .cp = Key_.down } });
+ settle(p);
+ p.sync();
+ var row_at: ?u16 = null;
+ for (0..pane.rows) |row| if (pane.wrapAt(p.settings.tab, @intCast(row)).line == 49) {
+ row_at = @intCast(row);
+ break;
+ };
+ try std.testing.expectEqual(@as(?u16, @intCast((pane.rows + 2) / 3)), row_at);
+}