summaryrefslogtreecommitdiff
path: root/src/Query.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Query.zig')
-rw-r--r--src/Query.zig116
1 files changed, 89 insertions, 27 deletions
diff --git a/src/Query.zig b/src/Query.zig
index 2ab687ee..a5d33cf4 100644
--- a/src/Query.zig
+++ b/src/Query.zig
@@ -145,7 +145,8 @@ made_rev: u32 = 0,
/// The owner's revision seen last tick: rows follow an edit of the owner
/// once it has stood a frame (the rows refresh on idle frames).
seen_rev: u32 = 0,
-/// The body cursor's row as last seen: B1, j or k on a row makes it current.
+/// The body cursor's row as last seen: j or k onto a row makes it current
+/// (a caret B1 put there is only a caret).
seen_row: i32 = 0,
/// The places a `/`'s rows are a window of: [lo, hi) of its owner's hits.
window: struct { lo: usize = 0, hi: usize = 0 } = .{},
@@ -274,7 +275,7 @@ pub fn open(p: *Pardes, from: usize, source: Source, arg: []const u8, focus: boo
// Every view as the search found it, for Esc to put back.
owner.places.remember(p, owner);
// Before the first key: a names source lists every candidate at once.
- produce(p, id, false);
+ produce(p, id, false, false);
// What a 9P exec reads back: the pane it filled (ninep/ctl.zig).
p.fs.refilled = p.panes[id].?.serial;
if (focus) toLineOne(p, id) else p.active = from;
@@ -397,11 +398,11 @@ fn save(p: *Pardes, rp: *Pane, q: *Query) !Saved {
/// source has something new, has its rows made again. A walk takes a slice
/// on a tick. Nothing here is synchronous with an edit of the owner: its
/// places shift (Places.shift) and the rows follow once it stands a frame.
-pub fn produceAll(p: *Pardes, tick: bool) void {
+pub fn produceAll(p: *Pardes, tick: bool, keyed: bool) void {
for (p.panes, 0..) |slot, id| {
const pane = slot orelse continue;
if (pane.query == null) continue;
- produce(p, id, tick);
+ produce(p, id, tick, keyed);
}
}
@@ -416,7 +417,9 @@ pub fn busy(p: *const Pardes) bool {
return false;
}
-fn produce(p: *Pardes, id: usize, tick: bool) void {
+/// `keyed`: this step was a key, so a caret moved onto a row (j, k) makes
+/// it current; a caret B1 placed is only a caret.
+fn produce(p: *Pardes, id: usize, tick: bool, keyed: bool) void {
const rp = p.panes[id] orelse return;
const q = rp.query orelse return;
const f = if (rp.file) |*file| file else return;
@@ -445,10 +448,12 @@ fn produce(p: *Pardes, id: usize, tick: bool) void {
if (asked == null or asked.?.source != .text) clearPlaces(p, q, rp.serial, owner_id, asked);
}
const a = asked orelse return noteRows(p, id, " line 1 asks: /text, Find, Grep, Recent, Panes, Symbols, All, Refs, Def, Impl or Diagnostics, then what to find\n");
- // A row clicked or stepped onto (B1, j, k): current, and shown.
+ // A row stepped onto (j, k): current, and shown. One B1 put the caret
+ // on is not: B1 places the caret and selects, nothing else, as on any
+ // text; no other pane moves.
if (rp.body.cur_row != q.seen_row) {
q.seen_row = rp.body.cur_row;
- if (rp.body.cur_row > 0 and rp.body.cur_row <= q.items.items.len) {
+ if (keyed and rp.body.cur_row > 0 and rp.body.cur_row <= q.items.items.len) {
const i: usize = @intCast(rp.body.cur_row - 1);
if (q.items.items[i].kind == .hit) setCurrent(p, id, i, true);
}
@@ -1217,8 +1222,7 @@ pub fn fillList(p: *Pardes, owner_id: usize, source: Source, rows: []const u8) v
if (t.mode == .insert) edit.exitInsert(p, t);
t.vsel.active = false;
t.nsel = 0;
- t.cur_row = @intCast((q.current orelse 0) + 1);
- showCurrent(rp);
+ caretTo(rp, q.current orelse 0);
showTarget(p, qid);
}
@@ -1350,25 +1354,12 @@ fn clearPlaces(p: *Pardes, q: *Query, serial: u32, owner_id: ?usize, asked: anyt
/// is current. Scrolled into view, line 1 kept when it can be.
fn showCurrent(rp: *Pane) void {
const q = rp.query orelse return;
- const on_rows = rp.body.cur_row != 0 and rp.focus == .body;
- if (on_rows and rp.body.mode != .normal) return;
+ if (rp.body.cur_row != 0 and rp.focus == .body) return;
const c = q.current orelse {
rp.body.msel.active = false;
return;
};
const r: i32 = @intCast(c + 1);
- if (on_rows) {
- // The keyboard on the rows is the current row: n and N take it there.
- rp.body.msel.active = false;
- rp.body.cur_row = r;
- rp.body.cur_col = 0;
- rp.body.cur_pinned = true;
- q.seen_row = r;
- const off = rp.scroll();
- const shown = off + @as(i32, rp.rows) - 1;
- if (r > shown) rp.scrollBy(r - shown) else if (r < off) rp.scrollBy(r - off);
- return;
- }
rp.body.msel = .{ .active = true, .r0 = r, .r1 = r };
// Typed into, line 1 stays in view whatever the current row: what you
// type is never scrolled away (the live rows start just before the
@@ -1382,6 +1373,19 @@ fn showCurrent(rp: *Pane) void {
if (r > shown) rp.scrollBy(r - shown) else if (r < off) rp.scrollBy(r - off);
}
+/// The caret on item `i`'s row, the row in view: the current row's own
+/// mark from here (the line selection is for the caret on line 1).
+fn caretTo(rp: *Pane, i: usize) void {
+ const r: i32 = @intCast(i + 1);
+ rp.body.msel.active = false;
+ rp.body.cur_row = r;
+ rp.body.cur_col = 0;
+ rp.body.cur_pinned = true;
+ rp.query.?.seen_row = r;
+ const off = rp.scroll();
+ if (r >= off + @as(i32, rp.rows)) rp.scrollBy(r - off - @as(i32, rp.rows) + 1) else if (r < off) rp.scrollBy(r - off);
+}
+
/// The line (0-based) of byte `at` of `f`'s text.
fn lineOfOffset(p: *Pardes, f: *File.State, at: usize) usize {
const index = File.lineIndex(p.gpa, f) catch return 0;
@@ -1848,6 +1852,11 @@ pub fn key(p: *Pardes, id: usize, k: Key) bool {
if (t.normal.prefix != .none or k.ctrl or k.alt or t.normal.count != 0) return false;
if (k.cp == 'n' or k.cp == 'N') {
step(p, id, if (k.cp == 'n') 1 else -1);
+ // The caret on the rows goes to the current row, as j and k
+ // would have taken it there.
+ if (t.cur_row != 0) if (q.current) |c| {
+ caretTo(rp, c);
+ };
return true;
}
// A language server's list, live: Esc is back to the text it was
@@ -1856,10 +1865,10 @@ pub fn key(p: *Pardes, id: usize, k: Key) bool {
cancel(p, id);
return true;
}
- if (hit(k, config.look_key) and t.cur_row > 0) {
- const i: usize = @intCast(t.cur_row - 1);
- if (i >= q.items.items.len or q.items.items[i].kind != .hit) return false;
- land(p, id, i);
+ // Enter lands the current row: the caret's when j or k took it
+ // there, never one a B1 only put the caret on.
+ if (hit(k, config.look_key) and t.cur_row > 0 and q.current != null) {
+ land(p, id, null);
return true;
}
return false;
@@ -2428,3 +2437,56 @@ test "a language server's list: one answer lands with no pane, several open the
try std.testing.expectEqual(@as(usize, 0), p.active);
try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row);
}
+
+test "B1 on a query pane's rows places the caret and selects, nothing else; j, k and n make a row current, Enter lands the current" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 });
+ defer p.deinit();
+ const pane = try p.setTestFile("alpha one\nbeta\nalphabet\ngamma alpha\nalpha two\ndelta\n");
+ typeKeys(p, "/alpha");
+ const qid = queryPane(p).?;
+ const qp = p.panes[qid].?;
+ const q = qp.query.?;
+ p.acknowledgePanelPresentation(&.{});
+ const owner_sel = pane.body.primaryRange(pane.file.?.content, 0);
+ const owner_scroll = pane.file.?.scroll;
+ const current = q.current;
+ const place = pane.places.current;
+ const r = p.rects[qid];
+ const y = r.y + qp.tag_rows + 3;
+ const x = r.x + config.GUTTER + File.gutterWidth(qp) + 6;
+ // a click: the caret there, the current row and every other pane as they were
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x, .row = y } });
+ try std.testing.expectEqual(@as(i32, 3), qp.body.cur_row);
+ try std.testing.expectEqual(current, q.current);
+ try std.testing.expectEqual(place, pane.places.current);
+ try std.testing.expectEqual(owner_sel, pane.body.primaryRange(pane.file.?.content, 0));
+ try std.testing.expectEqual(owner_scroll, pane.file.?.scroll);
+ try std.testing.expect(p.panes[qid] != null and queryPane2(p, qid) == null);
+ // a sweep selects the text it went over, and nothing else
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = x + 4, .row = y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x + 4, .row = y } });
+ const swept = qp.sel[@backingInt(config.select_button)];
+ try std.testing.expect(swept.state == .done and swept.r0 == swept.r1 and swept.c1 - swept.c0 == 4);
+ try std.testing.expectEqual(current, q.current);
+ try std.testing.expectEqual(owner_sel, pane.body.primaryRange(pane.file.?.content, 0));
+ // the keyboard: j onto the next row makes it current, the text follows
+ p.active = qid;
+ if (qp.body.mode == .insert) p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expect(p.panes[qid] != null);
+ typeKeys(p, "j");
+ try std.testing.expectEqual(@as(i32, 4), qp.body.cur_row);
+ try std.testing.expectEqual(@as(?usize, 3), pane.places.current);
+ // n round the end: the caret goes with the current row
+ typeKeys(p, "n");
+ try std.testing.expectEqual(@as(?usize, 0), pane.places.current);
+ try std.testing.expectEqual(@as(i32, 1), qp.body.cur_row);
+ // B1 elsewhere, then Enter: the current row lands, not the caret's
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = x, .row = y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = x, .row = y } });
+ p.active = qid;
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ try std.testing.expectEqual(@as(usize, 0), p.active);
+ try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row);
+}