summaryrefslogtreecommitdiff
path: root/core/Names.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Names.zig')
-rw-r--r--core/Names.zig47
1 files changed, 44 insertions, 3 deletions
diff --git a/core/Names.zig b/core/Names.zig
index d063bd17..9105e3c4 100644
--- a/core/Names.zig
+++ b/core/Names.zig
@@ -50,6 +50,9 @@ pub const Cand = struct {
/// What the row is of: a line of the owner (Symbols, 1-based), a
/// pane's serial (Panes); 0 for a path.
ref: u32 = 0,
+ /// Its group: the hits stay in their groups' order, scored within
+ /// each (a Symbols row's kind, UX DESIGN N3); 0 for every other source.
+ group: u8 = 0,
};
pub const Hit = struct { cand: u32, score: i32 };
@@ -128,7 +131,9 @@ pub fn typed(n: *Names, gpa: std.mem.Allocator, text: []const u8) void {
// only those are scored again (and then the ones not yet scored).
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);
+ // Its own query typed again, to score names that came after it
+ // (Pick.declsArrived), is the buffer itself.
+ std.mem.copyForwards(u8, n.query_buf[0..q.len], q);
n.reach = max_rows;
if (grew) {
const pending = n.pending.items[n.pending_at..];
@@ -187,9 +192,17 @@ pub fn reachTo(n: *Names, rows: usize) void {
n.gen +%= 1;
}
-/// Hit `a` goes before `b`: the higher score, then the shorter key, then
-/// the candidate added first.
+/// Hit `a` goes before `b`: the lower group, then `better`.
fn before(cands: []const Cand, a: Hit, b: Hit) bool {
+ const ga = cands[a.cand].group;
+ const gb = cands[b.cand].group;
+ if (ga != gb) return ga < gb;
+ return better(cands, a, b);
+}
+
+/// Hit `a` matched better than `b`: the higher score, then the shorter
+/// key, then the candidate added first.
+fn better(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;
@@ -230,6 +243,22 @@ fn sink(heap: []Hit, cands: []const Cand, from: usize) void {
}
}
+/// The row of the hit the query matched best, wherever its group puts it:
+/// where a list's cursor starts (UX DESIGN N3); 0 for an empty query,
+/// whose rows are the source's order. Its rows up to it are sorted once
+/// `reachTo` has gone past it.
+pub fn bestRow(n: *const Names) usize {
+ const hits = n.hits.items;
+ if (hits.len == 0 or std.mem.trim(u8, n.queryText(), " ").len == 0) return 0;
+ var top = hits[0];
+ for (hits[1..]) |h| if (better(n.cands.items, h, top)) {
+ top = h;
+ };
+ var at: usize = 0;
+ for (hits) |h| at += @intFromBool(before(n.cands.items, h, top));
+ return at;
+}
+
/// 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];
@@ -435,3 +464,15 @@ test "Names keeps its best `reach` hits first in a full sort's order, the rest a
n.reachTo(max_rows + 500);
try std.testing.expectEqualSlices(Hit, all[0 .. max_rows + 500], n.hits.items[0 .. max_rows + 500]);
}
+
+test "Names takes its own query typed again: names added after it (a Symbols picker's declarations from a worker, Pick.declsArrived) are scored against it" {
+ const gpa = std.testing.allocator;
+ var n: Names = .{};
+ defer n.deinit(gpa);
+ n.typed(gpa, "zzlast");
+ for ([_][]const u8{ "fn f0001() void {", "fn zzlastfn() void {" }) |cand| try n.add(gpa, cand, 0, cand.len, 0, 0);
+ n.typed(gpa, n.queryText());
+ while (n.busy()) n.rescore(gpa);
+ try std.testing.expectEqualStrings("zzlast", n.queryText());
+ try std.testing.expectEqual(@as(usize, 1), n.hits.items.len);
+}