summaryrefslogtreecommitdiff
path: root/src/Query.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Query.zig')
-rw-r--r--src/Query.zig176
1 files changed, 132 insertions, 44 deletions
diff --git a/src/Query.zig b/src/Query.zig
index 48378a18..3a75a7f5 100644
--- a/src/Query.zig
+++ b/src/Query.zig
@@ -153,8 +153,10 @@ window: struct { lo: usize = 0, hi: usize = 0 } = .{},
/// made every third, the frame's time going to the finding.
waited: u8 = 0,
names: Names = .{},
-/// A Grep's lines, as the disk engine sends them (Stream.zig, `take`).
+/// A Grep's lines, as the disk engine sends them (Stream.zig, `take`), and
+/// the current one's index among them.
found: std.ArrayList(Found) = .empty,
+grep_at: usize = 0,
found_bytes: std.ArrayList(u8) = .empty,
grep_gen: u32 = 0,
/// The language server's rows for Refs, Def, Impl, Diagnostics, as it gave
@@ -256,6 +258,10 @@ pub fn open(p: *Pardes, from: usize, source: Source, arg: []const u8, focus: boo
File.setContent(p, f, new);
q.reset(p);
if (rp.collapsed) layout.toggleCollapse(p, id);
+ // A source that previews gets its +Preview now, at the key that
+ // asked, not under the typing later.
+ if (previews(source) and previewOf(p, id) == null)
+ _ = makePreview(p, id, @max(pardes.BOX_H, rp.tag_rows) + p.settings.search_rows, layout.groupRoot(p, id));
} else {
if (existing) |old| {
// SearchPane new: the last one stays where it is, a result of
@@ -297,8 +303,20 @@ fn create(p: *Pardes, owner_id: usize, line: []const u8, source: Source) ?usize
const body: u16 = 1 + p.settings.search_rows;
const tag = @max(pardes.BOX_H, np.tag_rows);
const with_preview = previews(source);
- // The preview, made right after, takes its own rows from the owner.
- const want = tag + body;
+ // The preview, made right after, takes its own rows from the owner:
+ // when the owner has not the rows for both, they share what it has, so
+ // the group is whole from the first key and nothing moves later.
+ var want: u16 = tag + body;
+ var preview_rows: u16 = tag + p.settings.search_rows;
+ if (with_preview) {
+ layout.compute(p);
+ const spare = p.rects[owner_id].h -| layout.minRows(p, owner_id);
+ if (spare < want + preview_rows) {
+ const both = want + preview_rows;
+ want = @max(tag + 2, @as(u16, @intCast(@as(u32, spare) * want / both)));
+ preview_rows = spare -| want;
+ }
+ }
if (p.settings.search_place == .column) {
// The finder column (phase 6): until it exists, under the owner.
if (!layout.attach(p, owner_id, free, want)) exec.placeNew(p, owner_id, owner_id, free, .doc);
@@ -307,7 +325,7 @@ fn create(p: *Pardes, owner_id: usize, line: []const u8, source: Source) ?usize
// new pane is, the one place a search moves another pane.
exec.placeNew(p, owner_id, owner_id, free, .doc);
}
- if (with_preview) _ = makePreview(p, free);
+ if (with_preview) _ = makePreview(p, free, preview_rows, owner_id);
return free;
}
@@ -495,9 +513,14 @@ fn produce(p: *Pardes, id: usize, tick: bool) void {
q.found.clearRetainingCapacity();
q.found_bytes.clearRetainingCapacity();
q.grep_gen +%= 1;
+ q.grep_at = 0;
q.current = null;
// On the disk engine's worker, once the typing pauses.
- if (a.pat.len > 0) Stream.query(p, rp.serial, .grep, a.pat, dir, .typed) else if (p.stream.target == rp.serial) {
+ // A script's (a 9P write): run to its end before it answers.
+ if (a.pat.len > 0) {
+ Stream.query(p, rp.serial, .grep, a.pat, dir, if (p.fs.serving) .now else .typed);
+ if (p.fs.serving) Stream.step(p);
+ } else if (p.stream.target == rp.serial) {
_ = p.stream.stop();
}
feedGrepPlaces(p, q, rp.serial, a.pat);
@@ -548,7 +571,10 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz
switch (source) {
// The files under the directory, listed once on the disk engine's
// worker (rg --files, else the walk); each keystroke scores them.
- .find => Stream.query(p, rp.serial, .files, "", dir, .now),
+ .find => {
+ Stream.query(p, rp.serial, .files, "", dir, .now);
+ if (p.fs.serving) Stream.step(p);
+ },
.recent => {
var out: std.Io.Writer.Allocating = .init(gpa);
defer out.deinit();
@@ -828,7 +854,9 @@ fn makeTextRows(p: *Pardes, id: usize, oid: usize, full: bool) !void {
// Grouped once a parse of the text exists: asked of the worker now.
if (ownerDecls(p, &owner.file.?, false) == null) Stream.parseFor(p, oid);
const at = set.current orelse (if (set.seek) |s| Places.firstFrom(set.hits.items, s) else 0);
- const from = if (full) 0 else at -| live_rows / 2;
+ // While typed, a window of rows from just before the current place, so
+ // it sits under line 1.
+ const from = if (full) 0 else at -| 2;
q.window = .{ .lo = from, .hi = from };
if (set.started and set.from == rp.serial) {
if (from > 0) {
@@ -856,7 +884,9 @@ fn showPlacesCurrent(p: *Pardes, id: usize) void {
const owner = p.panes[p.paneBySerial(q.owner) orelse return].?;
const set = &owner.places;
q.total = if (set.from == rp.serial) set.hits.items.len else 0;
- q.more = q.more or (set.from == rp.serial and !set.complete());
+ // still counting: the places not all found yet (the rows' own cut is
+ // said in a row of theirs)
+ q.more = set.from == rp.serial and !set.complete();
q.k = if (set.current) |c| c + 1 else 0;
if (set.from != rp.serial) q.k = 0;
q.current = null;
@@ -995,6 +1025,7 @@ fn makeNameRows(p: *Pardes, id: usize, pat: []const u8) !void {
}
}
if (q.names.hits.items.len > shown) try noteRow(p, q, &out, &rows, " ... and more: type more of the name");
+ if (q.source == .find) try skippedRow(p, q, rp, &out, &rows);
setRows(p, rp, &out, &rows);
const f = &rp.file.?;
f.pager_spans = spans.toOwnedSlice(p.gpa) catch &.{};
@@ -1004,7 +1035,7 @@ fn makeNameRows(p: *Pardes, id: usize, pat: []const u8) !void {
q.current = if (shown > 0) 0 else null;
countCurrent(q);
showCurrent(rp);
- showTarget(p, id);
+ if (q.current == null) blankPreview(p, id) else showTarget(p, id);
}
/// A Grep keeps this many lines: the rest wait (the disk engine's `More`).
@@ -1016,14 +1047,14 @@ fn makeGrepRows(p: *Pardes, id: usize, full: bool) !void {
const rp = p.panes[id].?;
const q = rp.query.?;
const dir = std.fs.path.dirname(rp.file.?.path) orelse "/";
- const at: usize = if (q.current) |c| (if (c < q.items.items.len) q.items.items[c].hit else 0) else 0;
+ const at: usize = @min(q.grep_at, q.found.items.len -| 1);
q.items.clearRetainingCapacity();
var out: std.ArrayList(u8) = .empty;
defer out.deinit(p.gpa);
var rows: std.ArrayList(locations.Row) = .empty;
var last_path: []const u8 = "";
var pathbuf: [4096 + 8]u8 = undefined;
- const lo = if (full) 0 else at -| live_rows / 2;
+ const lo = if (full) 0 else at -| 2;
const hi = if (full) q.found.items.len else @min(q.found.items.len, lo + live_rows);
if (lo > 0) {
var hb: [64]u8 = undefined;
@@ -1048,12 +1079,23 @@ fn makeGrepRows(p: *Pardes, id: usize, full: bool) !void {
try noteRow(p, q, &out, &rows, std.fmt.bufPrint(&hb, " ... {d} lines after these", .{q.found.items.len - hi}) catch " ...");
}
if (p.stream.target == rp.serial and p.stream.state == .capped) try noteRow(p, q, &out, &rows, " ... stopped at 2000 lines: More in the tag goes on");
+ try skippedRow(p, q, rp, &out, &rows);
setRows(p, rp, &out, &rows);
q.total = q.found.items.len;
q.more = p.stream.busyFor(rp.serial);
q.current = current orelse firstHit(q);
countGrepCurrent(q);
showCurrent(rp);
+ if (q.current == null) blankPreview(p, id);
+}
+
+/// What the disk walk passed over, said under the rows: a search that
+/// found nothing where it could not look says so, never just nothing.
+fn skippedRow(p: *Pardes, q: *Query, rp: *Pane, out: *std.ArrayList(u8), rows: *std.ArrayList(locations.Row)) !void {
+ const n = p.stream.skipped;
+ if (p.stream.target != rp.serial or n == 0) return;
+ var hb: [96]u8 = undefined;
+ try noteRow(p, q, out, rows, std.fmt.bufPrint(&hb, " {d} director{s} skipped: permission denied", .{ n, if (n == 1) "y" else "ies" }) catch " directories skipped");
}
/// `k` for a Grep: the current row's line among all the lines sent.
@@ -1128,7 +1170,9 @@ pub fn fillList(p: *Pardes, owner_id: usize, source: Source, rows: []const u8) v
const line = lineOne(rp.file.?.content);
const a = parse(line) orelse return;
makeListRows(p, qid, a.pat) catch |err| p.reportError(qid, "search", err);
- toLineOne(p, qid);
+ // A list to pick from, typed into; diagnostics are a jump (]d): the
+ // keyboard stays on the place in the text.
+ if (source != .diagnostics) toLineOne(p, qid) else p.active = owner_id;
}
fn makeListRows(p: *Pardes, id: usize, pat: []const u8) !void {
@@ -1141,8 +1185,10 @@ fn makeListRows(p: *Pardes, id: usize, pat: []const u8) !void {
var rows: std.ArrayList(locations.Row) = .empty;
const owner_id = p.paneBySerial(q.owner);
const owner_path: []const u8 = if (owner_id) |oid| (if (p.panes[oid].?.file) |f| f.path else "") else "";
- var feed: std.ArrayList(Places.Hit) = .empty;
- defer feed.deinit(p.gpa);
+ // Each open file's places in the list, its tints and n/N: the owner's
+ // and any other pane's the rows land in.
+ var feeds: [pardes.MAX_PANES]std.ArrayList(Places.Hit) = @splat(.empty);
+ defer for (&feeds) |*f| f.deinit(p.gpa);
// In order: by path, then place, as a list of places reads.
var sorted: std.ArrayList([]const u8) = .empty;
defer sorted.deinit(p.gpa);
@@ -1174,27 +1220,28 @@ fn makeListRows(p: *Pardes, id: usize, pat: []const u8) !void {
}
const rest = std.mem.trimStart(u8, ln[@min(t.end, ln.len)..], " \t:");
const col0 = t.at.col -| 1;
- try q.items.append(p.gpa, .{ .kind = .hit, .n = 1 });
- // The row's text: the line in the owner when it is the owner's,
- // else as the server said it.
+ // The row's text: the line as an open pane has it, when one shows
+ // the file, else as the server said it.
var code: []const u8 = rest;
- var col = col0;
- var end = if (t.at.end_col > 0 and t.at.end_line == t.at.line) t.at.end_col else col0 + 1;
- if (owner_id) |oid| if (std.mem.eql(u8, path, owner_path)) {
- const owner_f = &p.panes[oid].?.file.?;
- if (t.at.line <= File.nlines(p.gpa, owner_f)) {
- const ls = File.lineStart(p.gpa, owner_f, t.at.line - 1);
- const nx = File.lineStart(p.gpa, owner_f, t.at.line);
- const le = if (nx > ls and owner_f.content[nx - 1] == '\n') nx - 1 else nx;
- code = owner_f.content[ls..le];
- col = @min(col0, code.len);
- end = @min(@max(end, col + 1), code.len);
- feed.append(p.gpa, .{ .start = ls + col, .end = ls + @max(end, col + 1) }) catch {};
- }
- } else {
- col = 0;
- end = 0;
- };
+ var col = @min(col0, rest.len);
+ var end = @min(if (t.at.end_col > 0 and t.at.end_line == t.at.line) t.at.end_col else col0 + 1, rest.len);
+ var shown_in: u32 = 0;
+ for (p.panes, 0..) |slot, i| {
+ const tp = slot orelse continue;
+ if (!searchable(tp)) continue;
+ const tf = &tp.file.?;
+ if (tf.output != null or !std.mem.eql(u8, tf.path, path) or t.at.line > File.nlines(p.gpa, tf)) continue;
+ const ls = File.lineStart(p.gpa, tf, t.at.line - 1);
+ const nx = File.lineStart(p.gpa, tf, t.at.line);
+ const le = if (nx > ls and tf.content[nx - 1] == '\n') nx - 1 else nx;
+ code = tf.content[ls..le];
+ col = @min(col0, code.len);
+ end = @min(@max(end, col + 1), code.len);
+ feeds[i].append(p.gpa, .{ .start = ls + col, .end = ls + @max(end, col + 1) }) catch {};
+ shown_in = tp.serial;
+ break;
+ }
+ try q.items.append(p.gpa, .{ .kind = .hit, .n = 1, .serial = shown_in });
try writeHitRow(p, &out, &rows, 9, path, t.at.line, code, col, end, &.{});
n += 1;
}
@@ -1203,12 +1250,14 @@ fn makeListRows(p: *Pardes, id: usize, pat: []const u8) !void {
q.total = n;
q.more = false;
q.current = firstHit(q);
+ for (p.panes, 0..) |slot, i| if (slot) |tp| {
+ if (feeds[i].items.len > 0) tp.places.feed(p, tp, rp.serial, feeds[i].items, null) else if (tp.places.from == rp.serial) tp.places.clear(p.gpa);
+ };
if (owner_id) |oid| {
const owner = p.panes[oid].?;
// Current: the first place after where it was asked (the answer
// may come after the cursor moved), shown in the owner.
const asked_at: usize = if (p.lsp_last) |last| (if (last.serial == owner.serial) last.offset else owner.places.origin(owner)) else owner.places.origin(owner);
- owner.places.feed(p, owner, rp.serial, feed.items, null);
const hits = owner.places.hits.items;
var next: ?usize = null;
for (hits, 0..) |h, i| if (h.start > asked_at) {
@@ -1261,6 +1310,13 @@ fn showCurrent(rp: *Pane) void {
};
const r: i32 = @intCast(c + 1);
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
+ // current one, so it sits under line 1). Frozen, the row is scrolled to.
+ if (!q.frozen) {
+ if (rp.scroll() != 0) rp.scrollBy(-rp.scroll());
+ return;
+ }
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);
@@ -1296,6 +1352,7 @@ fn setCurrent(p: *Pardes, id: usize, i: usize, show: bool) void {
const q = rp.query.?;
q.current = i;
const it = q.items.items[i];
+ if (q.source == .grep and it.kind == .hit) q.grep_at = it.hit;
const rows = rp.file.?.location_rows;
if (it.serial != 0 and i + 1 < rows.len) if (p.paneBySerial(it.serial)) |tid| {
const tp = p.panes[tid].?;
@@ -1325,6 +1382,17 @@ pub fn step(p: *Pardes, id: usize, delta: i32) void {
return;
}
};
+ // A Grep: by its lines, the window made again when the next is past it.
+ if (q.source == .grep and q.found.items.len > 0) {
+ const n = q.found.items.len;
+ q.grep_at = if (delta > 0) (q.grep_at + 1) % n else (q.grep_at + n - 1) % n;
+ const in_window = for (q.items.items) |it| {
+ if (it.kind == .hit and it.hit == q.grep_at) break true;
+ } else false;
+ if (!in_window) makeGrepRows(p, id, q.frozen) catch {};
+ for (q.items.items, 0..) |it, i| if (it.kind == .hit and it.hit == q.grep_at) return setCurrent(p, id, i, true);
+ return;
+ }
const items = q.items.items;
if (items.len == 0) return;
var i: usize = q.current orelse (if (delta > 0) items.len - 1 else 0);
@@ -1335,6 +1403,26 @@ pub fn step(p: *Pardes, id: usize, delta: i32) void {
setCurrent(p, id, i, true);
}
+/// Nothing current (a query that matches nothing): the +Preview shows
+/// nothing, rather than a file the query no longer names.
+fn blankPreview(p: *Pardes, qid: usize) void {
+ const pid = previewOf(p, qid) orelse return;
+ const pane = p.panes[pid].?;
+ const f = &pane.file.?;
+ if (f.content.len == 0) return;
+ const dir = std.fs.path.dirname(p.panes[qid].?.file.?.path) orelse "/";
+ const path = std.fmt.allocPrint(p.gpa, "{s}/{s}", .{ dir, config.preview_buffer }) catch return;
+ const empty = p.gpa.alloc(u8, 0) catch {
+ p.gpa.free(path);
+ return;
+ };
+ p.gpa.free(f.path);
+ f.path = path;
+ File.setContent(p, f, empty);
+ f.saved_revision = f.revision;
+ f.scroll = 0;
+}
+
/// Where the current row points, shown with the keyboard left where it is:
/// a place in an open pane's text selected there (its places' current),
/// in view; a file an open pane shows, its line there; any other file in
@@ -1399,8 +1487,11 @@ pub fn isPreview(p: *Pardes, pane: *const Pane) ?usize {
return null;
}
-/// A +Preview made under query pane `qid`, out of its rows alone.
-fn makePreview(p: *Pardes, qid: usize) ?usize {
+/// A +Preview made under query pane `qid`, `rows` tall, out of `giver`'s
+/// rows: the text that asked, at the key that asked; the query pane itself
+/// when its source word was edited to one that previews while typing (its
+/// line 1 stays where it is).
+fn makePreview(p: *Pardes, qid: usize, rows: u16, giver: usize) ?usize {
const rp = p.panes[qid].?;
const free = p.freeSlot() orelse return null;
const dir = std.fs.path.dirname(rp.file.?.path) orelse "/";
@@ -1410,10 +1501,7 @@ fn makePreview(p: *Pardes, qid: usize) ?usize {
return null;
};
layout.compute(p);
- const rows = @max(pardes.BOX_H, np.tag_rows) + p.settings.search_rows;
- // Out of the group's root, the text that asked: the query pane keeps
- // the rows it was typed into with (made later, for a reused pane).
- if (!layout.attachFrom(p, qid, free, rows, layout.groupRoot(p, qid))) {
+ if (!layout.attachFrom(p, qid, free, rows, giver)) {
p.removePane(free, qid) catch {};
return null;
}
@@ -1425,7 +1513,7 @@ fn makePreview(p: *Pardes, qid: usize) ?usize {
/// file open in a pane would be shown there instead), the place centred
/// and selected. Made when the pane has none and its owner has the rows.
fn displayPreview(p: *Pardes, qid: usize, path: []const u8, at: look.Spot) void {
- const id = previewOf(p, qid) orelse makePreview(p, qid) orelse return;
+ const id = previewOf(p, qid) orelse makePreview(p, qid, p.rects[qid].h / 2, qid) orelse return;
const pane = p.panes[id].?;
const f = &pane.file.?;
if (!std.mem.eql(u8, f.path, path)) {
@@ -1706,8 +1794,8 @@ pub fn key(p: *Pardes, id: usize, k: Key) bool {
land(p, id, null);
return true;
}
- const down = (k.cp == Key.down and !k.ctrl and !k.alt) or (k.ctrl and !k.alt and k.cp == 'n');
- const up = (k.cp == Key.up and !k.ctrl and !k.alt) or (k.ctrl and !k.alt and k.cp == 'p');
+ const down = hit(k, config.query_next);
+ const up = hit(k, config.query_prev);
if (down or up) {
step(p, id, if (down) 1 else -1);
return true;