diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-26 02:28:09 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-26 02:49:36 -0300 |
| commit | 148e48dd9b8cd7e66aa1521daa49a64852b1650e (patch) | |
| tree | edf3c1ca188018164a0713917ca0e336cecd14cd /core/Pick.zig | |
| parent | 1ce3bcb6178b720058518a12e45f25769be7d39c (diff) | |
| download | pardes-148e48dd9b8cd7e66aa1521daa49a64852b1650e.tar.gz pardes-148e48dd9b8cd7e66aa1521daa49a64852b1650e.zip | |
syntax, Query, Pick, Names, Band, Stream, File, colors: the Symbols picker grouped by kind, a kind mark on each row (UX DESIGN N, binding; the user: "The lsp symbols should be more organized: types first then functions then fields and the rest (and some indicator to tell the kind)", "For the symbols picker"): the rows are the file's own parse's, as before, now from a symbol walk of their own (syntax.zig `SymbolWalk`, in the same walk of the tree as the sticky headers' declarations, which keep theirs: contextFromTree), by one kind table a grammar (`symbol_rules`: Zig, C, C++, Rust, Go, Python, JavaScript, Java, C#, Kotlin, Scala, Ruby, PHP, bash, Haskell, OCaml, Erlang, Elixir, Clojure, Fortran, Ada, Pascal, PowerShell; JSON, CSS, HTML, Markdown and Typst have no declarations to list), each node's kind as given or found where its grammar cannot say it (Zig's `const S = struct`, `error{...}`, `@This()`, `@Type(...)` a type and `@import(...)` a module, JavaScript's `const f = () => ...` a function, C's declaration a function where a declarator is one's and a const where `const` says so, a Python class's or Kotlin's binding a field in a type); fields and consts are listed now (the sticky headers' declarations had neither), a function's consts and vars never (its locals), each named in the types, functions and modules it is in (`Counts.lines`, `torque.Parser.Parse`; an anonymous struct's members in its owner's, a Rust `impl S`'s in `S`, no row of its own); a row is `t Counts = struct`: its kind's mark (t type, f function, m field, c const, v var, n module, · other), its qualified name, two blanks and what its name's line says after the name (its body's `{`, a trailing `,` or `;` dropped), its line the dim meta at the right end (Band's); the mark in the keyword's ink (colors.Roles.kind, the theme's keyword pushed to 4.5:1 on every tag ground and the selection, as the key ink is), the qualifier and the words after the name dim; with the field empty the rows are types, functions, fields, then the rest (consts, vars, modules, other as one group), each in the text's order; typed, they stay grouped and are scored within each group (Names: `Cand.group`, `before` orders by it, then by `better`), the cursor starts on the best match wherever its group puts it (Names.bestRow, Pick.toBest: after a key, when the parse lands, and following the best while the rest are scored), and the count says where it is; the name alone is matched (`u8` in a signature matches nothing); no header rows: every row is a symbol, ordinary text, and C-q keeps them in a +Search with the mark a word; the query pane's `Symbols` rows the same (Query.gather), its current the best; the parse's answer carries the symbols beside the declarations (Stream.Batch.symbols, an input every replica takes), ContextCache keeps both for the revision (derived, made again at a load), and every producer makes both (the worker, a sticky header's refresh, a load), so a text at a revision has the same symbols everywhere; syntax.zig's tests ran in no test program (core-test's filter `syntax context nested` matched nothing on the base): core/root.zig imports it now, and its 34 pass; RUN (TEST FIRST): syntax.zig "syntax symbols: a Zig file's types, functions, fields and the rest ...", "... C, C++, Rust, Go, Python and JavaScript by their kind tables ...", "... every kind table names nodes of its grammar ..." (23 tables), Pick.zig "Symbols (UX DESIGN N): a Zig file's rows types, functions, fields, then the rest ... the mark in the keyword's ink, the qualifier and the rest dim", "Symbols (UX DESIGN N3): typed, the rows stay grouped ... the cursor starts on the best match ...; the name alone is matched, never the signature", "Symbols (UX DESIGN N): a C++ file's rows ...", "Symbols: every row is ordinary text ..."; on the base the rows were ` 2 pub const Counts = struct {` in the text's order, no mark, no field, the cursor on the first row; core-test -Dtest-filter=ymbols 18/18, core-test 1135/1308 (173 skipped); the stack's suites, the dump fuzz, the soak and the pictures are on the change after this one
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
Diffstat (limited to 'core/Pick.zig')
| -rw-r--r-- | core/Pick.zig | 255 |
1 files changed, 249 insertions, 6 deletions
diff --git a/core/Pick.zig b/core/Pick.zig index 1d332f25..2b5714cb 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -452,7 +452,10 @@ pub fn filter(p: *Pardes, pk: *Pick) void { const q = std.mem.trim(u8, p.view().tag_state.field.text(), " "); p.view().tag_state.rows.at = 0; pk.shown = no_row; - if (fuzzy(pk)) return pk.names.typed(p.gpa, q); + if (fuzzy(pk)) { + pk.names.typed(p.gpa, q); + return toBest(p, pk); + } if (pk.source == .grep) { pk.found.clearRetainingCapacity(); pk.found_bytes.clearRetainingCapacity(); @@ -514,15 +517,31 @@ pub fn declsArrived(p: *Pardes, serial: u32) void { Query.gather(p, &pk.names, .symbols, pk.dir.slice(), null, p.paneBySerial(serial)); pk.names.typed(p.gpa, pk.names.queryText()); pk.names.rescore(p.gpa); + if (live(p) == pk) toBest(p, pk); }; } +/// The live picker's cursor on its best match, wherever its group puts it +/// (UX DESIGN N3: a Symbols list stays grouped while typed), its rows +/// sorted as far as the band shows past it. +fn toBest(p: *Pardes, pk: *Pick) void { + const at = pk.names.bestRow(); + pk.names.reachTo(at + Band.Cursor.shown); + p.view().tag_state.rows.at = @intCast(@min(at, std.math.maxInt(u16))); +} + /// After every step: each live picker's names scored on (a frame's count), /// and on a frame of its own after the cursor stood, the preview of the /// current row. pub fn produceAll(p: *Pardes, tick: bool) void { for (p.picks) |slot| if (slot) |pk| if (pk.unrolled) { - if (pk.names.busy()) pk.names.rescore(p.gpa); + if (pk.names.busy()) { + // A cursor on the best match follows it while the rest are + // scored. + const on_best = live(p) == pk and p.view().tag_state.rows.at == pk.names.bestRow(); + pk.names.rescore(p.gpa); + if (on_best) toBest(p, pk); + } // A theme's preview in the step of the key that moved to it. if (pk.source == .themes) pardes.colors.previewTheme(p, refAt(p, pk)); if (tick) preview(p, pk); @@ -584,7 +603,7 @@ const Target = union(enum) { none, place: struct { path: []const u8, at: look.Sp /// Row `i` as the band shows it and what it points at, its text in /// `arena`. -const Row = struct { text: []const u8, key: u8 = 0, pre: u16 = 0, faint: u16 = 0, dim: bool = false, marks: []const u16 = &.{}, target: Target = .none, meta: []const u8 = "" }; +const Row = struct { text: []const u8, key: u8 = 0, kind: u8 = 0, pre: u16 = 0, faint: u16 = 0, quiet: [2]u16 = .{ 0, 0 }, dim: bool = false, marks: []const u16 = &.{}, target: Target = .none, meta: []const u8 = "" }; fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) std.mem.Allocator.Error!Row { const dir = pk.dir.slice(); @@ -646,8 +665,23 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) std.me row.dim = p.paneBySerial(cand.ref) == null; row.target = .{ .pane = cand.ref }; }, + // ` 12 m Counts.lines : usize = 0` (Query.gather): the line its + // meta, the mark in the keyword's ink, the qualifier and what + // follows the name dim (UX DESIGN N4). .symbols => { - row.pre = 6; + const line = 8; + if (text.len <= line or cand.key < line) return row; + row.text = text[line..]; + row.meta = std.mem.trimStart(u8, text[0..6], " "); + const marks_in = try arena.alloc(u16, row.marks.len); + for (marks_in, row.marks) |*m, at| m.* = at -| line; + row.marks = marks_in; + const name_at = cand.key - line; + const dot = std.mem.lastIndexOfScalar(u8, row.text[name_at .. name_at + cand.key_len], '.'); + row.kind = @intCast(name_at - 1); + row.pre = @intCast(name_at); + row.faint = @intCast(if (dot) |at| at + 1 else 0); + if (name_at + cand.key_len < row.text.len) row.quiet = .{ @intCast(name_at + cand.key_len), @intCast(row.text.len) }; const owner = p.panes[p.paneBySerial(pk.owner) orelse return row] orelse return row; const f = owner.file orelse return row; row.target = .{ .place = .{ .path = f.path, .at = .{ .line = cand.ref } } }; @@ -837,7 +871,7 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step { const rows = try arena.alloc(Band.Row, w.hi - w.lo); for (rows, w.lo..) |*row, i| { const r = try rowAt(p, pk, arena, i); - row.* = .{ .text = r.text, .key = r.key, .on = i == s.rows.at, .mark = marked(pk, idOf(pk, i)) != null, .dim = r.dim, .pre = r.pre, .faint = r.faint, .marks = r.marks, .index = @intCast(i), .meta = r.meta }; + row.* = .{ .text = r.text, .key = r.key, .kind = r.kind, .on = i == s.rows.at, .mark = marked(pk, idOf(pk, i)) != null, .dim = r.dim, .pre = r.pre, .faint = r.faint, .quiet = r.quiet, .marks = r.marks, .index = @intCast(i), .meta = r.meta }; } // A place's text (a Grep's line, a list's): its `path:line` in the dim // ink and the text after it in one column, the widest place shown @@ -1415,6 +1449,7 @@ fn refine(p: *Pardes, pk: *Pick, drop: bool) void { if (drop and (if (pk.marks.items.len > 0) marked(pk, h.cand) != null else h.cand == current)) continue; const c = pk.names.cands.items[h.cand]; next.add(p.gpa, pk.names.bytes.items[c.start..][0..c.len], c.key, c.key_len, c.base, c.ref) catch break; + next.cands.items[next.cands.items.len - 1].group = c.group; } } else { var arena = std.heap.ArenaAllocator.init(p.gpa); @@ -1514,7 +1549,7 @@ fn keep(p: *Pardes, pk: *Pick) void { const row = rowAt(p, pk, arena.allocator(), i) catch break; switch (row.target) { .place => |t| if (pk.source == .symbols) - out.writer.print("{s}:{d} {s}\n", .{ std.Io.Dir.path.basename(t.path), t.at.line, std.mem.trimStart(u8, row.text, " 0123456789") }) catch break + out.writer.print("{s}:{d} {s}\n", .{ std.Io.Dir.path.basename(t.path), t.at.line, row.text }) catch break else out.writer.print("{s}\n", .{row.text}) catch break, else => out.writer.print("{s}\n", .{row.text}) catch break, @@ -3283,3 +3318,211 @@ test "Ctrl-r in a picker unrolls its source's earlier queries (UX L2's last line p.update(.{ .key = .{ .cp = Key.escape } }); p.update(.{ .key = .{ .cp = Key.escape } }); } + +/// A Symbols picker opened from a file pane whose path is `name` and text +/// `text`, its symbols parsed (small: here and now), and the pane. +fn openSymbols(p: *Pardes, name: []const u8, text: []const u8) !*Pane { + const pane = try p.setTestFile(text); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, name); + open(p, p.view().active, .symbols, "", "", ""); + settle(p); + return pane; +} + +/// The live picker's rows, `meta text` a line each. +fn rowLines(p: *Pardes, a: std.mem.Allocator) ![]const u8 { + const pk = live(p).?; + var out: std.ArrayList(u8) = .empty; + for (0..count(p, pk)) |i| { + const r = try rowAt(p, pk, a, i); + try out.print(a, "{s: >2} {s}\n", .{ r.meta, r.text }); + } + return out.items; +} + +const counts_zig = + \\const std = @import("std"); + \\pub const Counts = struct { + \\ lines: usize = 0, + \\ words: usize = 0, + \\ pub const zero: Counts = .{}; + \\ pub fn add(c: *Counts, text: []const u8) void { + \\ const local = text.len; + \\ c.lines += local; + \\ } + \\}; + \\const Mode = enum { fast, slow }; + \\var hits: u32 = 0; + \\pub fn count(text: []const u8) Counts { + \\ return .{ .lines = text.len }; + \\} + \\test "count" {} + \\ +; + +test "Symbols (UX DESIGN N): a Zig file's rows types, functions, fields, then the rest, each in the text's order with the field empty; a row its kind's mark, its name qualified, what its first line says after it, its line the meta; the mark in the keyword's ink, the qualifier and the rest dim" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + _ = try openSymbols(p, "/w6/counts.zig", counts_zig); + try std.testing.expectEqualStrings( + \\ 2 t Counts = struct + \\11 t Mode = enum { fast, slow } + \\ 6 f Counts.add (c: *Counts, text: []const u8) void + \\13 f count (text: []const u8) Counts + \\ 3 m Counts.lines : usize = 0 + \\ 4 m Counts.words : usize = 0 + \\11 m Mode.fast + \\11 m Mode.slow + \\ 1 n std = @import("std") + \\ 5 c Counts.zero : Counts = .{} + \\12 v hits : u32 = 0 + \\16 · "count" + \\ + , try rowLines(p, a)); + // Drawn: `m Counts.lines : usize = 0` (not the cursor's row). + const pane = p.panes[p.paneBySerial(live(p).?.landing).?].?; + _ = try p.render(a); + const layer = &p.surface.tag_layers[pardes.BAND_LAYER]; + const roles = &p.chromeTheme().roles; + const inks = roles.on(if (Trail.reading(p, pane)) .reading_focused else .focused); + var found = false; + for (0..layer.rows) |y| { + const cells = layer.cells[y * layer.cols ..][0..layer.cols]; + var text: std.ArrayList(u8) = .empty; + var at: std.ArrayList(usize) = .empty; + for (cells, 0..) |c, x| { + try at.append(a, text.items.len); + try text.appendSlice(a, c.grapheme()); + _ = x; + } + const from = std.mem.indexOf(u8, text.items, "m Counts.lines") orelse continue; + var x: usize = 0; + while (at.items[x] < from) x += 1; + try std.testing.expectEqual(pardes.Color{ .rgb = roles.kind }, cells[x].style.fg); + try std.testing.expectEqual(pardes.Color{ .rgb = inks.dim }, cells[x + 2].style.fg); + try std.testing.expectEqual(pardes.Color{ .rgb = inks.text }, cells[x + 9].style.fg); + try std.testing.expectEqual(pardes.Color{ .rgb = inks.dim }, cells[x + 16].style.fg); + // Its line at the row's right end, dim. + const three = std.mem.lastIndexOfScalar(u8, text.items, '3').?; + var m: usize = 0; + while (at.items[m] < three) m += 1; + try std.testing.expect(m > x + 30); + try std.testing.expectEqual(pardes.Color{ .rgb = inks.dim }, cells[m].style.fg); + found = true; + } + try std.testing.expect(found); +} + +test "Symbols (UX DESIGN N3): typed, the rows stay grouped, scored within each group, and the cursor starts on the best match wherever its group puts it; the name alone is matched, never the signature" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const pane = try openSymbols(p, "/w6/counts.zig", counts_zig); + for ("count") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + settle(p); + const rows = try rowLines(p, a); + try std.testing.expect(std.mem.startsWith(u8, rows, + \\ 2 t Counts = struct + \\13 f count (text: []const u8) Counts + \\ 6 f Counts.add (c: *Counts, text: []const u8) void + \\ 3 m Counts.lines : usize = 0 + \\ 4 m Counts.words : usize = 0 + \\ + )); + // `count` matched best: the cursor on it, not on the first row, and + // the count says where it is. + const s = &p.view().tag_state; + try std.testing.expectEqual(@as(u16, 1), s.rows.at); + const h = (try head(p, a, p.panes[p.paneBySerial(live(p).?.landing).?].?)).?; + try std.testing.expect(std.mem.indexOf(u8, h.text, "2/7") != null); + // Enter lands on its line. + _ = pane; + // `u8` is in signatures only: nothing matches. + for (0..5) |_| p.update(.{ .key = .{ .cp = pardes.Key.backspace } }); + for ("u8") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + settle(p); + try std.testing.expectEqual(@as(usize, 0), count(p, live(p).?)); +} + +test "Symbols (UX DESIGN N): a C++ file's rows by its kind table, members named in their class and namespace, a function's locals none" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + _ = try openSymbols(p, "/w6/parser.cc", + \\#define LIMIT 4 + \\namespace torque { + \\class Parser : public Base { + \\ public: + \\ Parser(); + \\ int Parse(int depth) const { return depth; } + \\ int depth_; + \\}; + \\enum class Kind { kA, kB }; + \\int Parser::Run(int n) { int local = n; return local; } + \\static int counter = 0; + \\const int limit = 4; + \\using Alias = int; + \\} + \\ + ); + try std.testing.expectEqualStrings( + \\ 3 t torque.Parser : public Base + \\ 9 t torque.Kind + \\13 t torque.Alias = int + \\ 5 f torque.Parser.Parser () + \\ 6 f torque.Parser.Parse (int depth) const + \\10 f torque.Parser::Run (int n) + \\ 7 m torque.Parser.depth_ + \\ 9 m torque.Kind.kA + \\ 9 m torque.Kind.kB + \\ 1 · LIMIT 4 + \\ 2 n torque + \\11 v torque.counter = 0 + \\12 c torque.limit = 4 + \\ + , try rowLines(p, frame.allocator())); +} + +test "Symbols: every row is ordinary text: no header rows, the cursor steps onto each in turn, the band shows each row's text, and C-q keeps them in a +Search with the mark a word" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + _ = try openSymbols(p, "/w6/counts.zig", counts_zig); + const pk = live(p).?; + const n = count(p, pk); + try std.testing.expectEqual(@as(usize, 12), n); + const s = &p.view().tag_state; + for (0..n) |i| { + try std.testing.expectEqual(@as(u16, @intCast(i)), s.rows.at); + const r = try rowAt(p, pk, a, i); + const host = p.paneBySerial(pk.landing).?; + const band = (try Band.of(p, a, p.panes[host].?, host)).?; + var shown = false; + for (0..band.height) |y| { + const ln = try band.line(a, p.settings.tab, @intCast(y)); + shown = shown or std.mem.indexOf(u8, ln.text, r.text) != null; + } + try std.testing.expect(shown); + p.update(.{ .key = .{ .cp = pardes.Key.down } }); + } + p.update(.{ .key = .{ .cp = 'q', .ctrl = true } }); + var kept: ?[]const u8 = null; + for (p.panes) |slot| if (slot) |tp| if (tp.file) |f| if (std.mem.startsWith(u8, f.content, "Symbols ")) { + kept = f.content; + }; + try std.testing.expect(std.mem.indexOf(u8, kept.?, "\ncounts.zig:3 m Counts.lines : usize = 0\n") != null); + try std.testing.expect(std.mem.indexOf(u8, kept.?, "\ncounts.zig:2 t Counts = struct\n") != null); +} |
