diff options
Diffstat (limited to 'src/Query.zig')
| -rw-r--r-- | src/Query.zig | 306 |
1 files changed, 153 insertions, 153 deletions
diff --git a/src/Query.zig b/src/Query.zig index 77119ca9..c05992f6 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -221,6 +221,15 @@ fn lineOne(content: []const u8) []const u8 { // ---- opening ---- +/// Whether `pane` is a query pane's +Preview: that query pane's slot. +pub fn isPreview(p: *Pardes, pane: *const Pane) ?usize { + const f = pane.file orelse return null; + const o = f.output orelse return null; + if (o.from != .preview) return null; + for (p.panes, 0..) |slot, i| if (slot) |qp| if (qp.query) |q| if (q.preview == pane.serial) return i; + return null; +} + /// A search from pane `from`: its owner's query pane, made under the owner /// (with a +Preview when `source` can need one) or reused, line 1 the /// source's word and `arg`. With `focus` the keyboard goes to line 1's end @@ -390,6 +399,150 @@ pub fn busy(p: *const Pardes) bool { return false; } +/// The places query `serial` fed in panes other than the owner, cleared +/// (a new source or pattern makes its own); the owner's too when the new +/// line is no `/`. +fn clearPlaces(p: *Pardes, serial: u32, owner_id: ?usize) void { + for (p.panes, 0..) |slot, i| { + const tp = slot orelse continue; + if (tp.places.from != serial) continue; + if (owner_id != null and owner_id.? == i) tp.places.restore(tp); + tp.places.clear(p.gpa); + } +} + +/// Every open pane's rows for All, grouped under `@pN name (count)` in +/// the order of the panes' serials (everywhere's other bug: it went by +/// column). +fn makeAllRows(p: *Pardes, id: usize) !void { + const rp = p.panes[id].?; + const q = rp.query.?; + q.items.clearRetainingCapacity(); + q.more = false; + var out: std.ArrayList(u8) = .empty; + defer out.deinit(p.gpa); + var rows: std.ArrayList(locations.Row) = .empty; + var order: [pardes.MAX_PANES]usize = undefined; + var n: usize = 0; + for (p.panes, 0..) |slot, i| if (slot) |tp| if (tp.places.from == rp.serial and tp.places.started) { + order[n] = i; + n += 1; + }; + std.mem.sort(usize, order[0..n], p, struct { + fn lt(pp: *Pardes, a: usize, b: usize) bool { + return pp.panes[a].?.serial < pp.panes[b].?.serial; + } + }.lt); + var budget: usize = max_rows; + q.total = 0; + for (order[0..n]) |tid| { + const tp = p.panes[tid].?; + const set = &tp.places; + if (!set.shows(tp) or set.hits.items.len == 0) { + q.more = q.more or !set.complete(); + continue; + } + q.total += set.hits.items.len; + q.more = q.more or !set.complete(); + var hb: pardes.Kind.Buf(600) = .{}; + const name = if (tp.file) |f| relative(f.path, std.Io.Dir.path.dirname(rp.file.?.path) orelse "") else ""; + const head = hb.print(config.pane_addr ++ "{d} {s} ({d})", .{ tp.serial, name[0..@min(name.len, 500)], set.hits.items.len }); + try q.items.append(p.gpa, .{ .kind = .header, .serial = tp.serial }); + try writeRow(p, &out, &rows, head, .{ .kind = .context, .declaration = true, .path = @constCast(addressOf(p.scratch.allocator(), tp)), .at = .{}, .location_end = 0, .code_start = 0 }); + try writePlacesRows(p, q, &out, &rows, tp, std.Io.Dir.path.dirname(rp.file.?.path) orelse "/", 0, &budget); + } + if (budget == 0) try noteRow(p, q, &out, &rows, " ... and more: the count says how many"); + setRows(p, rp, &out, &rows); + // The current one: the first hit row, until a step moves it. + if (q.current == null or q.current.? >= q.items.items.len) q.current = firstHit(q); + countCurrent(q); + showCurrent(rp); +} + +/// The names the query matches, best first, `Names.max_rows` of them: each +/// a row as its source has it, the matched bytes tinted. +fn makeNameRows(p: *Pardes, id: usize) !void { + const rp = p.panes[id].?; + const q = rp.query.?; + const dir = std.Io.Dir.path.dirname(rp.file.?.path) orelse "/"; + q.items.clearRetainingCapacity(); + var out: std.ArrayList(u8) = .empty; + defer out.deinit(p.gpa); + var rows: std.ArrayList(locations.Row) = .empty; + var spans: std.ArrayList(pardes.pager.Span) = .empty; + errdefer spans.deinit(p.gpa); + const chrome = p.bodyChrome(); + const tint: pardes.CellStyle = .{ .fg = .{ .rgb = chrome.search_fg }, .bg = .{ .rgb = chrome.search_bg }, .bold = true }; + const shown = @min(q.names.hits.items.len, Names.max_rows); + var marks: [Names.max_query * 4]u16 = undefined; + const base = lineOne(rp.file.?.content).len + 1; + for (0..shown) |i| { + var nmarks: usize = 0; + const row = q.names.row(i, &marks, &nmarks); + const cand = q.names.cands.items[q.names.hits.items[i].cand]; + const at = base + out.items.len; + // Where it points: a path (Find, Recent), a pane (Panes), a line + // of the owner (Symbols). + var meta: locations.Row = .{ .kind = .match, .path = &.{}, .at = .{}, .location_end = 0, .code_start = 0, .mark_occurrence = false }; + var pathbuf: [4096 + 32]u8 = undefined; + switch (q.source) { + .find => meta.path = @constCast(std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), row }) catch row), + .recent => { + const t = look.parsePathLine(row); + meta.path = @constCast(if (t.path.len > 0 and t.path[0] == '/') t.path else std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), t.path }) catch t.path); + meta.at = t.at; + }, + .panes => meta.path = @constCast(std.fmt.bufPrint(&pathbuf, config.pane_addr ++ "{d}", .{cand.ref}) catch unreachable), // @p and a u32 fit 4128 + .symbols => { + const owner = p.panes[p.paneBySerial(q.owner) orelse 0]; + meta.path = @constCast(if (owner) |o| addressOf(p.scratch.allocator(), o) else ""); + meta.at = .{ .line = cand.ref }; + }, + else => {}, + } + try q.items.append(p.gpa, .{ .kind = .hit, .ref = cand.ref }); + try writeRow(p, &out, &rows, row, meta); + // A closed pane's row is dimmer, the gutter's colour, between its + // marks (spans are walked in order, never overlapping). + const dim = q.source == .panes and p.paneBySerial(cand.ref) == null; + const dimmed: pardes.CellStyle = .{ .fg = .{ .rgb = chrome.lineno } }; + var gap: usize = 0; + var k: usize = 0; + while (k < nmarks) { + var e = k + 1; + while (e < nmarks and marks[e] == marks[e - 1] + 1) e += 1; + if (dim and marks[k] > gap) try spans.append(p.gpa, .{ .start = @intCast(at + gap), .end = @intCast(at + marks[k]), .style = dimmed }); + try spans.append(p.gpa, .{ .start = @intCast(at + marks[k]), .end = @intCast(at + marks[e - 1] + 1), .style = tint }); + gap = marks[e - 1] + 1; + k = e; + } + if (dim and row.len > gap) try spans.append(p.gpa, .{ .start = @intCast(at + gap), .end = @intCast(at + row.len), .style = dimmed }); + } + 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 &.{}; + q.total = q.names.hits.items.len; + q.more = q.names.busy() or p.stream.busyFor(rp.serial); + // The best is current until a step or a click moves it. + q.current = if (shown > 0) 0 else null; + countCurrent(q); + showCurrent(rp); + if (q.current == null) blankPreview(p, id) else showTarget(p, id); +} + +/// A Grep's hits in each open file are that pane's places: its tints, +/// ticks and n/N. +fn feedGrepPlaces(p: *Pardes, serial: u32, pat: []const u8) void { + for (p.panes) |slot| { + const tp = slot orelse continue; + const f = tp.file orelse continue; + if (f.output != null or tp.query != null) continue; + tp.places.search(p, tp, .list, serial, pat, true, null, false); + } +} + /// `keyed`: this step was a key, so a caret moved onto a row (j, k) makes /// it current; a caret B1 placed is only a caret. fn produce(p: *Pardes, id: usize, tick: bool, keyed: bool) void { @@ -1027,54 +1180,6 @@ fn showPlacesCurrent(p: *Pardes, id: usize) void { showCurrent(rp); } -/// Every open pane's rows for All, grouped under `@pN name (count)` in -/// the order of the panes' serials (everywhere's other bug: it went by -/// column). -fn makeAllRows(p: *Pardes, id: usize) !void { - const rp = p.panes[id].?; - const q = rp.query.?; - q.items.clearRetainingCapacity(); - q.more = false; - var out: std.ArrayList(u8) = .empty; - defer out.deinit(p.gpa); - var rows: std.ArrayList(locations.Row) = .empty; - var order: [pardes.MAX_PANES]usize = undefined; - var n: usize = 0; - for (p.panes, 0..) |slot, i| if (slot) |tp| if (tp.places.from == rp.serial and tp.places.started) { - order[n] = i; - n += 1; - }; - std.mem.sort(usize, order[0..n], p, struct { - fn lt(pp: *Pardes, a: usize, b: usize) bool { - return pp.panes[a].?.serial < pp.panes[b].?.serial; - } - }.lt); - var budget: usize = max_rows; - q.total = 0; - for (order[0..n]) |tid| { - const tp = p.panes[tid].?; - const set = &tp.places; - if (!set.shows(tp) or set.hits.items.len == 0) { - q.more = q.more or !set.complete(); - continue; - } - q.total += set.hits.items.len; - q.more = q.more or !set.complete(); - var hb: pardes.Kind.Buf(600) = .{}; - const name = if (tp.file) |f| relative(f.path, std.Io.Dir.path.dirname(rp.file.?.path) orelse "") else ""; - const head = hb.print(config.pane_addr ++ "{d} {s} ({d})", .{ tp.serial, name[0..@min(name.len, 500)], set.hits.items.len }); - try q.items.append(p.gpa, .{ .kind = .header, .serial = tp.serial }); - try writeRow(p, &out, &rows, head, .{ .kind = .context, .declaration = true, .path = @constCast(addressOf(p.scratch.allocator(), tp)), .at = .{}, .location_end = 0, .code_start = 0 }); - try writePlacesRows(p, q, &out, &rows, tp, std.Io.Dir.path.dirname(rp.file.?.path) orelse "/", 0, &budget); - } - if (budget == 0) try noteRow(p, q, &out, &rows, " ... and more: the count says how many"); - setRows(p, rp, &out, &rows); - // The current one: the first hit row, until a step moves it. - if (q.current == null or q.current.? >= q.items.items.len) q.current = firstHit(q); - countCurrent(q); - showCurrent(rp); -} - fn firstHit(q: *const Query) ?usize { for (q.items.items, 0..) |it, i| if (it.kind == .hit) return i; return null; @@ -1089,79 +1194,6 @@ fn countCurrent(q: *Query) void { q.k += 1; } -/// The names the query matches, best first, `Names.max_rows` of them: each -/// a row as its source has it, the matched bytes tinted. -fn makeNameRows(p: *Pardes, id: usize) !void { - const rp = p.panes[id].?; - const q = rp.query.?; - const dir = std.Io.Dir.path.dirname(rp.file.?.path) orelse "/"; - q.items.clearRetainingCapacity(); - var out: std.ArrayList(u8) = .empty; - defer out.deinit(p.gpa); - var rows: std.ArrayList(locations.Row) = .empty; - var spans: std.ArrayList(pardes.pager.Span) = .empty; - errdefer spans.deinit(p.gpa); - const chrome = p.bodyChrome(); - const tint: pardes.CellStyle = .{ .fg = .{ .rgb = chrome.search_fg }, .bg = .{ .rgb = chrome.search_bg }, .bold = true }; - const shown = @min(q.names.hits.items.len, Names.max_rows); - var marks: [Names.max_query * 4]u16 = undefined; - const base = lineOne(rp.file.?.content).len + 1; - for (0..shown) |i| { - var nmarks: usize = 0; - const row = q.names.row(i, &marks, &nmarks); - const cand = q.names.cands.items[q.names.hits.items[i].cand]; - const at = base + out.items.len; - // Where it points: a path (Find, Recent), a pane (Panes), a line - // of the owner (Symbols). - var meta: locations.Row = .{ .kind = .match, .path = &.{}, .at = .{}, .location_end = 0, .code_start = 0, .mark_occurrence = false }; - var pathbuf: [4096 + 32]u8 = undefined; - switch (q.source) { - .find => meta.path = @constCast(std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), row }) catch row), - .recent => { - const t = look.parsePathLine(row); - meta.path = @constCast(if (t.path.len > 0 and t.path[0] == '/') t.path else std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), t.path }) catch t.path); - meta.at = t.at; - }, - .panes => meta.path = @constCast(std.fmt.bufPrint(&pathbuf, config.pane_addr ++ "{d}", .{cand.ref}) catch unreachable), // @p and a u32 fit 4128 - .symbols => { - const owner = p.panes[p.paneBySerial(q.owner) orelse 0]; - meta.path = @constCast(if (owner) |o| addressOf(p.scratch.allocator(), o) else ""); - meta.at = .{ .line = cand.ref }; - }, - else => {}, - } - try q.items.append(p.gpa, .{ .kind = .hit, .ref = cand.ref }); - try writeRow(p, &out, &rows, row, meta); - // A closed pane's row is dimmer, the gutter's colour, between its - // marks (spans are walked in order, never overlapping). - const dim = q.source == .panes and p.paneBySerial(cand.ref) == null; - const dimmed: pardes.CellStyle = .{ .fg = .{ .rgb = chrome.lineno } }; - var gap: usize = 0; - var k: usize = 0; - while (k < nmarks) { - var e = k + 1; - while (e < nmarks and marks[e] == marks[e - 1] + 1) e += 1; - if (dim and marks[k] > gap) try spans.append(p.gpa, .{ .start = @intCast(at + gap), .end = @intCast(at + marks[k]), .style = dimmed }); - try spans.append(p.gpa, .{ .start = @intCast(at + marks[k]), .end = @intCast(at + marks[e - 1] + 1), .style = tint }); - gap = marks[e - 1] + 1; - k = e; - } - if (dim and row.len > gap) try spans.append(p.gpa, .{ .start = @intCast(at + gap), .end = @intCast(at + row.len), .style = dimmed }); - } - 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 &.{}; - q.total = q.names.hits.items.len; - q.more = q.names.busy() or p.stream.busyFor(rp.serial); - // The best is current until a step or a click moves it. - q.current = if (shown > 0) 0 else null; - countCurrent(q); - showCurrent(rp); - if (q.current == null) blankPreview(p, id) else showTarget(p, id); -} - /// A Grep's rows: each file's name over its lines, while typed a window /// of `live_rows` around the current one, `full` (frozen) every one sent. fn makeGrepRows(p: *Pardes, id: usize, full: bool) !void { @@ -1271,17 +1303,6 @@ pub fn take(p: *Pardes, q: *Query, kind: Stream.Kind, rows: []const u8) void { q.names.gen +%= 1; } -/// A Grep's hits in each open file are that pane's places: its tints, -/// ticks and n/N. -fn feedGrepPlaces(p: *Pardes, serial: u32, pat: []const u8) void { - for (p.panes) |slot| { - const tp = slot orelse continue; - const f = tp.file orelse continue; - if (f.output != null or tp.query != null) continue; - tp.places.search(p, tp, .list, serial, pat, true, null, false); - } -} - /// The language server's answer for an owner's Refs, Def, Impl or /// Diagnostics, into its query pane (made or reused) with line 1 the word: /// what follows the word filters the rows; the places in the owner's text @@ -1442,18 +1463,6 @@ fn makeListRows(p: *Pardes, id: usize, pat: []const u8) !void { showCurrent(rp); } -/// The places query `serial` fed in panes other than the owner, cleared -/// (a new source or pattern makes its own); the owner's too when the new -/// line is no `/`. -fn clearPlaces(p: *Pardes, serial: u32, owner_id: ?usize) void { - for (p.panes, 0..) |slot, i| { - const tp = slot orelse continue; - if (tp.places.from != serial) continue; - if (owner_id != null and owner_id.? == i) tp.places.restore(tp); - tp.places.clear(p.gpa); - } -} - // ---- the current row ---- /// The current row scrolled into view while the keyboard is on line 1 or @@ -1647,15 +1656,6 @@ fn previewOf(p: *Pardes, qid: usize) ?usize { }; } -/// Whether `pane` is a query pane's +Preview: that query pane's slot. -pub fn isPreview(p: *Pardes, pane: *const Pane) ?usize { - const f = pane.file orelse return null; - const o = f.output orelse return null; - if (o.from != .preview) return null; - for (p.panes, 0..) |slot, i| if (slot) |qp| if (qp.query) |q| if (q.preview == pane.serial) return i; - return null; -} - /// 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 |
