summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Names.zig92
-rw-r--r--core/Pick.zig12
-rw-r--r--core/body_layer.zig35
-rw-r--r--core/normal.zig4
-rw-r--r--core/panes.zig9
-rw-r--r--core/root.zig1
-rw-r--r--core/syntax.zig39
7 files changed, 150 insertions, 42 deletions
diff --git a/core/Names.zig b/core/Names.zig
index 47165f52..d063bd17 100644
--- a/core/Names.zig
+++ b/core/Names.zig
@@ -29,6 +29,10 @@ query_buf: [max_query]u8 = undefined,
query_len: u8 = 0,
/// Bumped whenever `hits` change: the rows are made again only then.
gen: u32 = 0,
+/// How many of `hits` are in order, best first: as many as a list shows
+/// (`max_rows`), or as far as its cursor went (`reachTo`); the matches after
+/// them are counted, in no order.
+reach: u32 = max_rows,
/// The query is `query_buf[0..query_len]`: what lies past it is undefined
/// (0xAA in Debug, not in a release build), so no digest reads it.
@@ -81,6 +85,7 @@ pub fn reset(n: *Names) void {
n.pending.clearRetainingCapacity();
n.pending_at = 0;
n.scored = 0;
+ n.reach = max_rows;
n.gen +%= 1;
}
@@ -124,6 +129,7 @@ pub fn typed(n: *Names, gpa: std.mem.Allocator, text: []const u8) void {
const grew = n.query_len > 0 and std.mem.startsWith(u8, q, n.queryText()) and std.mem.indexOfScalar(u8, q[n.query_len..], ' ') == null;
n.query_len = @intCast(q.len);
@memcpy(n.query_buf[0..q.len], q);
+ n.reach = max_rows;
if (grew) {
const pending = n.pending.items[n.pending_at..];
std.mem.copyForwards(u32, n.pending.items[0..pending.len], pending);
@@ -165,19 +171,65 @@ pub fn rescore(n: *Names, gpa: std.mem.Allocator) void {
if (!empty and left == 0) break;
}
// An empty query keeps the source's order (the walk's, recency): no
- // row moves as the walk goes on, it only grows at the foot.
- if (!empty) std.sort.pdq(Hit, n.hits.items, n.cands.items, struct {
- fn lt(cands: []const Cand, a: Hit, b: Hit) bool {
- if (a.score != b.score) return a.score > b.score;
- const la = cands[a.cand].key_len;
- const lb = cands[b.cand].key_len;
- if (la != lb) return la < lb;
- return a.cand < b.cand;
- }
- }.lt);
+ // row moves as the walk goes on, it only grows at the foot. Else the
+ // best `reach` first: a sort of every hit was most of a first key's
+ // 8 ms over 50,000 paths.
+ if (!empty) bestFirst(n.hits.items, n.cands.items, n.reach);
if (n.hits.items.len != had or n.scored != was_scored or count > 0) n.gen +%= 1;
}
+/// Hits sorted as far as row `rows`, for a cursor gone past the ones that
+/// were (an empty query's are in the source's order already).
+pub fn reachTo(n: *Names, rows: usize) void {
+ if (rows <= n.reach or std.mem.trim(u8, n.queryText(), " ").len == 0) return;
+ n.reach = @intCast(@min(rows, max_cands));
+ bestFirst(n.hits.items, n.cands.items, n.reach);
+ n.gen +%= 1;
+}
+
+/// Hit `a` goes before `b`: the higher score, then the shorter key, then
+/// the candidate added first.
+fn before(cands: []const Cand, a: Hit, b: Hit) bool {
+ if (a.score != b.score) return a.score > b.score;
+ const la = cands[a.cand].key_len;
+ const lb = cands[b.cand].key_len;
+ if (la != lb) return la < lb;
+ return a.cand < b.cand;
+}
+
+/// `hits`' best `k` to the front, in order, the rest after them in no
+/// order: the best so far kept as a heap whose root is the worst of them,
+/// then sorted. O(n log k).
+fn bestFirst(hits: []Hit, cands: []const Cand, k_in: usize) void {
+ const k = @min(k_in, hits.len);
+ if (k == 0) return;
+ const heap = hits[0..k];
+ var i = k / 2;
+ while (i > 0) {
+ i -= 1;
+ sink(heap, cands, i);
+ }
+ for (hits[k..]) |*h| if (before(cands, h.*, heap[0])) {
+ std.mem.swap(Hit, h, &heap[0]);
+ sink(heap, cands, 0);
+ };
+ std.sort.pdq(Hit, heap, cands, before);
+}
+
+/// Heap entry `at` taken down past the worse of its children while it is
+/// better than that child.
+fn sink(heap: []Hit, cands: []const Cand, from: usize) void {
+ var at = from;
+ while (true) {
+ const l = 2 * at + 1;
+ if (l >= heap.len) return;
+ const c = if (l + 1 < heap.len and before(cands, heap[l], heap[l + 1])) l + 1 else l;
+ if (!before(cands, heap[at], heap[c])) return;
+ std.mem.swap(Hit, &heap[at], &heap[c]);
+ at = c;
+ }
+}
+
/// The row of hit `i` and, in `marks`, the bytes of it the query matched.
pub fn row(n: *const Names, i: usize, marks: []u16, nmarks: *usize) []const u8 {
const c = n.cands.items[n.hits.items[i].cand];
@@ -363,3 +415,23 @@ test "Names ranks real paths as the pick prototype did" {
try std.testing.expectEqualStrings("src/gui/Post.zig", topFor("gui/po"));
try std.testing.expectEqualStrings("src/Output.zig", topFor("Out"));
}
+
+test "Names keeps its best `reach` hits first in a full sort's order, the rest after them, and sorts further as a cursor goes past them" {
+ const gpa = std.testing.allocator;
+ var n: Names = .{};
+ defer n.deinit(gpa);
+ var buf: [64]u8 = undefined;
+ for (0..3000) |i| {
+ const path = try std.fmt.bufPrint(&buf, "dir{d}/sub{d}/file_{d}.zig", .{ i % 7, i % 13, i });
+ try n.add(gpa, path, 0, path.len, baseOf(path), 0);
+ }
+ n.typed(gpa, "f1");
+ while (n.busy()) n.rescore(gpa);
+ const all = try gpa.dupe(Hit, n.hits.items);
+ defer gpa.free(all);
+ std.sort.pdq(Hit, all, n.cands.items, before);
+ try std.testing.expect(all.len > max_rows);
+ try std.testing.expectEqualSlices(Hit, all[0..max_rows], n.hits.items[0..max_rows]);
+ n.reachTo(max_rows + 500);
+ try std.testing.expectEqualSlices(Hit, all[0 .. max_rows + 500], n.hits.items[0 .. max_rows + 500]);
+}
diff --git a/core/Pick.zig b/core/Pick.zig
index 6d2c9ba1..ab63cc5d 100644
--- a/core/Pick.zig
+++ b/core/Pick.zig
@@ -1012,7 +1012,10 @@ pub fn key(p: *Pardes, k: Key) void {
}
if (plain and k.cp == Key.enter) return apply(p, pk, refAt(p, pk) orelse return);
if (pk.source == .actions and plain and s.field.len == 0 and k.cp >= '1' and k.cp <= '9') return apply(p, pk, k.cp - '1');
- if (s.rows.key(k, n)) return;
+ if (s.rows.key(k, n)) {
+ pk.names.reachTo(@as(usize, s.rows.at) + Band.Cursor.shown);
+ return;
+ }
if (s.field.key(k)) filter(p, pk);
return;
}
@@ -1054,7 +1057,11 @@ pub fn key(p: *Pardes, k: Key) void {
return;
}
if (plain and k.cp == Key.backspace and s.field.len == 0) return back(p, pk);
- if (s.rows.key(k, n)) return;
+ // A cursor gone past the rows sorted sorts as far (Names.reach).
+ if (s.rows.key(k, n)) {
+ if (fuzzy(pk)) pk.names.reachTo(@as(usize, s.rows.at) + Band.Cursor.shown);
+ return;
+ }
if (s.field.key(k)) filter(p, pk);
}
@@ -1210,6 +1217,7 @@ fn keep(p: *Pardes, pk: *Pick) void {
out.writer.print("{s} ", .{pk.word.slice()}) catch return;
for (pk.refined[0..pk.nrefined]) |r| out.writer.print("{s} | ", .{r.query.slice()}) catch return;
out.writer.print("{s}\n", .{s.field.text()}) catch return;
+ if (fuzzy(pk)) pk.names.reachTo(Query.max_rows);
var kept: usize = 0;
for (0..count(p, pk)) |i| {
if (kept == Query.max_rows) break;
diff --git a/core/body_layer.zig b/core/body_layer.zig
index 82bfcebc..8bc38e23 100644
--- a/core/body_layer.zig
+++ b/core/body_layer.zig
@@ -875,21 +875,34 @@ fn targetLine(p: *Pardes, pane: *Pane, f: *panes.File.State, role: modal.Normal.
if (forward) return if (a < hits.len) lineOf(f, hits[a].start) else null;
return if (a > 0) lineOf(f, hits[a - 1].start) else null;
},
- inline .goto_function, .goto_comment, .goto_test => |r| {
- // The tree as the motion reads it, made once a revision; with
- // the Changes layer's lines in the buffer its offsets are not the
- // buffer's: not painted then.
- if (f.layer.lines.len > 0) return null;
- const tree = normal.fileTree(p, f) orelse return null;
- const object = @field(modal.Normal.Object, @tagName(r)["goto_".len..]);
- const at = lineStart(f, caret) + @as(usize, @intCast(@max(0, pane.body.cur_col)));
- const span = @import("syntax.zig").objectNext(f.path, tree, f.content, object, forward, @min(at, f.content.len), .{ lineStart(f, lo), lineStart(f, hi + 1) }) orelse return null;
- return lineOf(f, span.from);
- },
+ // With the Changes layer's lines in the buffer the tree's offsets
+ // are not the buffer's: not painted then.
+ .goto_function => return if (f.layer.lines.len > 0) null else treeTargets(p, pane, f, forward, lo, hi)[0],
+ .goto_comment => return if (f.layer.lines.len > 0) null else treeTargets(p, pane, f, forward, lo, hi)[1],
+ .goto_test => return if (f.layer.lines.len > 0) null else treeTargets(p, pane, f, forward, lo, hi)[2],
else => return null,
}
}
+/// The lines `]f`, `]c` and `]T` (or `[`'s) go to from the caret, in lines
+/// `lo` to `hi` (the rows shown): one pass of the grammar's query over the
+/// tree the motions read (made once a revision), kept on the pane while
+/// its text, its caret and those lines stay (`Pane.targets`), so a frame
+/// that paints them again asks nothing.
+fn treeTargets(p: *Pardes, pane: *Pane, f: *panes.File.State, forward: bool, lo: usize, hi: usize) [3]?usize {
+ const at = lineStart(f, @intCast(@max(0, pane.body.cur_row))) + @as(usize, @intCast(@max(0, pane.body.cur_col)));
+ const key: panes.TargetsKey = .{ .revision = f.revision, .at = at, .lo = lo, .hi = hi, .forward = forward };
+ if (pane.targets.key) |was| if (std.meta.eql(was, key)) return pane.targets.lines;
+ var lines: [3]?usize = @splat(null);
+ if (normal.fileTree(p, f)) |tree| {
+ var spans: [3]?@import("syntax.zig").Span = undefined;
+ @import("syntax.zig").objectNext(f.path, tree, f.content, &.{ .function, .comment, .@"test" }, forward, @min(at, f.content.len), .{ lineStart(f, lo), lineStart(f, hi + 1) }, &spans);
+ for (spans, &lines) |span, *line| line.* = if (span) |s| lineOf(f, s.from) else null;
+ }
+ pane.targets = .{ .key = key, .lines = lines };
+ return lines;
+}
+
fn blank(pane: *const Pane, line: usize) bool {
return std.mem.trim(u8, File.sourceLine(pane, @intCast(line)), " \t\r").len == 0;
}
diff --git a/core/normal.zig b/core/normal.zig
index 2579fab6..3aad765d 100644
--- a/core/normal.zig
+++ b/core/normal.zig
@@ -260,7 +260,9 @@ fn objectJump(e: Editor, t: *Text, text: []const u8, range: modal.Selection, obj
const tree = nodeTree(e, t, text) orelse return;
var last = range;
for (0..count) |_| {
- const s = syntax.objectNext(tree.path, tree.root, tree.text, object, forward, tree.in(modal.selectionCursor(text, last)), null) orelse break;
+ var found: [1]?syntax.Span = undefined;
+ syntax.objectNext(tree.path, tree.root, tree.text, &.{object}, forward, tree.in(modal.selectionCursor(text, last)), null, &found);
+ const s = found[0] orelse break;
const next: modal.Selection = .{ .anchor = tree.out(s.from, false), .head = tree.out(s.to, s.to > s.from) };
if (std.meta.eql(next, last)) break;
last = next;
diff --git a/core/panes.zig b/core/panes.zig
index 20a03b25..c984a73e 100644
--- a/core/panes.zig
+++ b/core/panes.zig
@@ -26,13 +26,17 @@ const effect_path_cap = pardes.effect_path_cap;
/// Files use row zero; terminal overlays may begin anywhere in scrollback.
pub const EditText = struct { text: []u8, row0: i32 };
+/// What a pane's `]` and `[` tree targets were found for (Pane.targets):
+/// its text's revision, the caret's byte, the lines shown, the way.
+pub const TargetsKey = struct { revision: u32, at: usize, lo: usize, hi: usize, forward: bool };
+
pub const Pane = struct {
/// What the replay digest leaves out (core/walk.zig): the view's geometry,
/// made again from the rest (File.geometry); the other views' share of
/// it (views.zig). (The 9P server's part of `fs` says so in its type.)
pub const replica = .{
.local = .{ "pv", "pv_known" },
- .derived = .{ "band_rows", "ghost_at", "ghost_rows", "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "top_skip", "notices", "pan" },
+ .derived = .{ "band_rows", "ghost_at", "ghost_rows", "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "top_skip", "notices", "pan", "targets" },
.len = .{ .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } },
// A picture's memory is its own allocator's (a dump loaded makes
// it there).
@@ -367,6 +371,9 @@ pub const Pane = struct {
/// `ghost_at` (its line's last), the rows below drawn that many lower.
/// The mouse reads it, so that a click lands on what is drawn under it.
ghost_at: u16 = 0,
+ /// `]`'s and `[`'s tree targets as the body drawn last found them
+ /// (body_layer.treeTargets), and what for.
+ targets: struct { key: ?TargetsKey = null, lines: [3]?usize = @splat(null) } = .{},
ghost_rows: u16 = 0,
/// Each other view's share of this pane, while it is not live
/// (views.zig): its cursor, scroll and place in that view's layout.
diff --git a/core/root.zig b/core/root.zig
index d1a25d21..66e9e0e8 100644
--- a/core/root.zig
+++ b/core/root.zig
@@ -779,6 +779,7 @@ const kept_out = [_]struct { []const u8, []const u8, []const u8 }{
.{ "panes.zig views.zig", "notices", "made again from the step's state at the top of update and presented, and at sync (Messages.collectNotices); a frame's are its own" },
.{ "panes.zig", "ghost_at", "where a message's detail is drawn under the caret's line (Messages.detailUnder), made again from the step's state with the notices (Messages.collectNotices: at the top of update, presented, sync, place); a frame's are its own" },
.{ "panes.zig", "ghost_rows", "the detail's rows there, made again with ghost_at" },
+ .{ "panes.zig", "targets", "`]`'s and `[`'s tree targets as the body drawn last found them (body_layer.treeTargets), keyed by the text's revision, the caret, the lines shown and the way, so a frame asks again only when one moved; the draw's alone, no step reads it" },
.{ "panes.zig views.zig", "band_rows", "the band's rows under a tag (Band.takes), made again from the view's tag state at every sync before anything reads them (layout.floorRows reads the last sync's, as it reads tag_rows)" },
.{ "panes.zig views.zig", "pan", "written by steps only (sync, a view unparked) from the pane and its terminal" },
.{ "File.zig", "cap line_starts line_cap widest", "the text's line index, of its bytes, and the widest line it found (an upper bound: which walk answers, never what)" },
diff --git a/core/syntax.zig b/core/syntax.zig
index a388573b..5c05300e 100644
--- a/core/syntax.zig
+++ b/core/syntax.zig
@@ -566,34 +566,39 @@ pub fn objectAt(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object
/// `]f`/`[f` and kin: the next object starting after byte `at`, or the
/// previous one ending before it (helix goto_treesitter_object), from the
/// first of `.movement`, `.around`, `.inside` the query names.
-pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object: Object, forward: bool, at: usize, within: ?[2]usize) ?Span {
- const q = objectQuery(path) orelse return null;
- var buf: [32]u8 = undefined;
- const capture = for ([_][]const u8{ "movement", "around", "inside" }) |kind| {
- const want = std.fmt.bufPrint(&buf, "{s}.{s}", .{ objectName(object), kind }) catch return null;
- if (captureId(q, want)) |id| break id;
- } else return null;
+pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, objects: []const Object, forward: bool, at: usize, within: ?[2]usize, out: []?Span) void {
+ @memset(out, null);
+ const q = objectQuery(path) orelse return;
+ // Each object's capture; one pass of the query finds them all (`]`'s
+ // three painted targets, body_layer.treeTargets).
+ var captures: [8]?u32 = @splat(null);
+ for (objects, captures[0..objects.len]) |object, *capture| {
+ var buf: [32]u8 = undefined;
+ capture.* = for ([_][]const u8{ "movement", "around", "inside" }) |kind| {
+ const want = std.fmt.bufPrint(&buf, "{s}.{s}", .{ objectName(object), kind }) catch break null;
+ if (captureId(q, want)) |id| break id;
+ } else null;
+ }
const tree: *const ts.Tree = @ptrCast(@alignCast(tree_ptr));
const cursor = ts.QueryCursor.create();
defer cursor.destroy();
// Only the objects these bytes hold (a body's rows shown): its painted
// targets (body_layer.paintTargets).
- if (within) |w| cursor.setByteRange(@intCast(w[0]), @intCast(w[1])) catch return null;
+ if (within) |w| cursor.setByteRange(@intCast(w[0]), @intCast(w[1])) catch return;
cursor.exec(q, tree.rootNode());
- var best: ?Span = null;
- while (cursor.nextMatch()) |match| {
- const s = matchSpan(q, match, capture, text) orelse continue;
+ while (cursor.nextMatch()) |match| for (captures[0..objects.len], out) |capture, *best| {
+ const s = matchSpan(q, match, capture orelse continue, text) orelse continue;
if (forward) {
if (s.from <= at) continue;
- if (best == null or s.from < best.?.from or (s.from == best.?.from and s.to > best.?.to)) best = s;
+ if (best.* == null or s.from < best.*.?.from or (s.from == best.*.?.from and s.to > best.*.?.to)) best.* = s;
} else {
if (s.to >= at) continue;
- if (best == null or s.to > best.?.to or (s.to == best.?.to and s.from < best.?.from)) best = s;
+ if (best.* == null or s.to > best.*.?.to or (s.to == best.*.?.to and s.from < best.*.?.from)) best.* = s;
}
- }
- const s = best orelse return null;
- if (s.from >= text.len or s.to >= text.len) return null;
- return s;
+ };
+ for (out) |*best| if (best.*) |s| if (s.from >= text.len or s.to >= text.len) {
+ best.* = null;
+ };
}
/// Owned source analysis. Both slices use the allocator passed to analyzeSource.