diff options
Diffstat (limited to 'src/Query.zig')
| -rw-r--r-- | src/Query.zig | 107 |
1 files changed, 65 insertions, 42 deletions
diff --git a/src/Query.zig b/src/Query.zig index 55d7b0b5..747022b0 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -48,6 +48,7 @@ const memory = @import("memory.zig"); const Names = @import("Names.zig"); const Places = @import("Places.zig"); const Stream = @import("Stream.zig"); +const Store = @import("Store.zig"); const Surface = @import("surface.zig").Surface; const Pardes = pardes.Pardes; const Pane = panes.Pane; @@ -173,6 +174,7 @@ capped: bool = false, saved: ?Saved = null, pub const Saved = struct { + pub const replica = .{ .blob = .{"rows"} }; content: []u8, rows: []locations.Row, asked: []u8, @@ -370,24 +372,8 @@ fn save(p: *Pardes, rp: *Pane, q: *Query) !Saved { const f = &rp.file.?; const content = try p.gpa.dupe(u8, f.content); errdefer p.gpa.free(content); - const rows = try p.gpa.alloc(locations.Row, f.location_rows.len); - var made: usize = 0; - errdefer { - for (rows[0..made]) |r| { - p.gpa.free(r.path); - p.gpa.free(r.colors); - } - p.gpa.free(rows); - } - for (f.location_rows, 0..) |r, i| { - rows[i] = r; - rows[i].path = try p.gpa.dupe(u8, r.path); - rows[i].colors = p.gpa.dupe(u8, r.colors) catch |err| { - p.gpa.free(rows[i].path); - return err; - }; - made += 1; - } + const rows = try locations.cloneRows(p.gpa, f.location_rows); + errdefer locations.freeRows(p.gpa, rows); const asked = try p.gpa.dupe(u8, q.asked); errdefer p.gpa.free(asked); return .{ .content = content, .rows = rows, .asked = asked, .source = q.source, .items = try p.gpa.dupe(Item, q.items.items), .current = q.current, .total = q.total, .k = q.k }; @@ -590,12 +576,9 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz if (p.fs.serving) Stream.step(p); }, .recent => { - // Every window's files: what the others wrote since, read. - p.store.refresh(p); var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); - Output.recentText(p, &out.writer, true) catch {}; - var lines = std.mem.splitScalar(u8, out.written(), '\n'); + var lines = std.mem.splitScalar(u8, pardes.tape.authority(recentRows, .{ p, &out }), '\n'); while (lines.next()) |ln| { if (ln.len == 0) continue; const rel = Names.relative(ln, dir); @@ -616,10 +599,9 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz } // Then the panes this session closed, the latest first, each // `(closed)` and dimmer (makeNameRows): Enter brings one back - // where it was. Its name alone is matched, not the word. - var i = p.store.closed.items.len; - while (i > 0) : (i -= 1) { - const c = p.store.closed.items[i - 1]; + // where it was. Its name alone is matched, not the word. The + // store's, which a replica has from the authority. + for (pardes.tape.authority(Store.closedNames, .{p})) |c| { var buf: [4096 + 48]u8 = undefined; const row = std.fmt.bufPrint(&buf, config.pane_addr ++ "{d} {s}" ++ closed_word, .{ c.serial, Names.relative(c.name, dir) }) catch continue; const key_at = std.mem.indexOfScalar(u8, row, ' ').? + 1; @@ -651,6 +633,14 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz /// What a Panes row of a closed pane ends with. const closed_word = " (closed)"; +/// Every window's files, as a Recent query lists them: what the others +/// wrote since, read first. The store's (`tape.authority`): in `out`. +fn recentRows(p: *Pardes, out: *std.Io.Writer.Allocating) []const u8 { + p.store.refresh(p); + Output.recentText(p, &out.writer, true) catch {}; + return out.written(); +} + /// The owner's declarations, from a parse that already exists: its sticky /// headers' (TreeContext) at this revision, or the locations cache's. None /// is made here, on the loop, unless `small` and the text is small: a @@ -859,18 +849,29 @@ pub fn recolor(p: *Pardes) void { const f = &rp.file.?; var sh: Shades = .{}; var lines = std.mem.splitScalar(u8, f.content, '\n'); - var changed = false; - for (f.location_rows) |row| { + // The rows are a blob (File.State): a digest keeps its word for + // them and a rollback copy pins them, so they are made again, never + // written. Written in place, a session's digest kept the word of + // their colours before; a client joining hashed them after, and + // loaded "as another session" (and so did a replica that hashed them + // at another time). + var fresh: ?[]locations.Row = null; + for (f.location_rows, 0..) |row, i| { const text = lines.next() orelse break; if (row.kind != .match or row.location_end == 0 or row.at.line == 0 or row.colors.len == 0) continue; if (std.mem.indexOfNone(u8, row.colors, &.{0}) != null) continue; const code = text[@min(row.code_start, text.len)..]; const got = sh.of(p, row.path, row.at.line, code[0..@min(code.len, row.colors.len)], row.source_column); if (got.len == 0) continue; - @memcpy(row.colors[0..got.len], got); - changed = true; + const rows = fresh orelse locations.cloneRows(p.gpa, f.location_rows) catch break; + fresh = rows; + @memcpy(rows[i].colors[0..got.len], got); + } + if (fresh) |rows| { + locations.freeRows(p.gpa, f.location_rows); + f.location_rows = rows; + dropHighlights(p, f); } - if (changed) dropHighlights(p, f); } } @@ -1743,11 +1744,7 @@ fn displayPreview(p: *Pardes, qid: usize, path: []const u8, at: look.Spot) void const f = &pane.file.?; if (!std.mem.eql(u8, f.path, path)) { if (exec.kindOf(path)) |kind| if (kind != .file) return; - const bytes = blk: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :blk filesystem.read(p, path) catch return; - }; + const bytes = pardes.tape.site(.preview, p.gpa, filesystem.read, .{ p, path }) catch return; if (std.mem.indexOfScalar(u8, bytes[0..@min(bytes.len, 8000)], 0) != null) { p.gpa.free(bytes); return; @@ -1869,11 +1866,15 @@ pub fn land(p: *Pardes, id: usize, row: ?usize) void { p.active = oid; return; } - const c = q.current orelse { + const f = &rp.file.?; + // A current row whose place went with its text (a frozen pane's rows + // edited: setContentSpan drops them, and an Esc's `saved` keeps that) + // is none: there is nowhere to land, as with no current one. + const current = if (q.current) |i| (if (i < q.items.items.len and i + 1 < f.location_rows.len) i else null) else null; + const c = current orelse { if (owner_id) |oid| p.active = oid; return; }; - const f = &rp.file.?; const it = q.items.items[c]; const meta = f.location_rows[c + 1]; p.jump_force = true; @@ -2288,7 +2289,7 @@ test "a / query: keystroke by keystroke, the owner shows the set, rows from its try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); } -test "a dump holding a query pane, live or frozen, restores, the pane as plain text" { +test "a dump holding a query pane, live or frozen, restores it as it was" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); defer p.deinit(); _ = try p.setTestFile("alpha one\nbeta\nalphabet\n"); @@ -2299,9 +2300,10 @@ test "a dump holding a query pane, live or frozen, restores, the pane as plain t const restored = try pardes.dump.initFromDump(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }, p.dump_out.?); defer restored.deinit(); const back = restored.panes[qid].?; - try std.testing.expect(back.query == null); - try std.testing.expectEqual(@as(usize, 0), back.file.?.location_rows.len); - try std.testing.expect(std.mem.startsWith(u8, back.file.?.content, "/alpha\n")); + const was = p.panes[qid].?; + try std.testing.expectEqual(was.query.?.frozen, back.query.?.frozen); + try std.testing.expectEqual(was.file.?.location_rows.len, back.file.?.location_rows.len); + try std.testing.expectEqualStrings(was.file.?.content, back.file.?.content); p.update(.{ .key = .{ .cp = Key.enter } }); } } @@ -2711,6 +2713,27 @@ test "B1 on a query pane's rows places the caret and selects, nothing else, the try std.testing.expectEqual(@as(i32, 3), pane.body.cur_row); } +test "Enter on a frozen query pane's rows after its text was edited: no row to land on, the keyboard to its owner" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); + defer p.deinit(); + _ = try p.setTestFile("alpha\n"); + p.newScratchBelow(0); + p.active = 0; + // SPC b b and Enter: the current row lands, the rows frozen. + leader(p, "bb"); + const qid = queryPane(p).?; + p.update(.{ .key = .{ .cp = Key.enter } }); + const qp = p.panes[qid].?; + try std.testing.expect(qp.query.?.frozen and qp.query.?.current != null); + // Its text edited: the rows' places go with the text they were of. + p.active = qid; + typeKeys(p, "ggjxd"); + try std.testing.expectEqual(@as(usize, 0), qp.file.?.location_rows.len); + try std.testing.expect(qp.body.cur_row > 0); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(@as(usize, 0), p.active); +} + test "a query row's place is its text: the match tinted after it, B3 on it opens the place, the rows saved and read again still Look" { if (!filesystem.platform_has_fs) return; var tmp = std.testing.tmpDir(.{}); |
