summaryrefslogtreecommitdiff
path: root/core/Names.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Names.zig')
-rw-r--r--core/Names.zig31
1 files changed, 29 insertions, 2 deletions
diff --git a/core/Names.zig b/core/Names.zig
index d063bd17..415064ad 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 };
@@ -187,9 +190,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 +241,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];