diff options
| -rw-r--r-- | src/File.zig | 32 | ||||
| -rw-r--r-- | src/Stream.zig | 201 | ||||
| -rw-r--r-- | src/draw.zig | 23 | ||||
| -rw-r--r-- | src/pardes.zig | 1 | ||||
| -rw-r--r-- | src/syntax.zig | 49 |
5 files changed, 302 insertions, 4 deletions
diff --git a/src/File.zig b/src/File.zig index cf44c69a..798e3e84 100644 --- a/src/File.zig +++ b/src/File.zig @@ -18,6 +18,7 @@ const limits = @import("memory.zig").limits; const locations = @import("locations.zig"); const Colors = @import("colors.zig"); const Tables = @import("Tables.zig"); +const Stream = @import("Stream.zig"); const Pane = panes.Pane; const Output = panes.Output; const Mini = panes.mini; @@ -1730,10 +1731,22 @@ pub fn refreshHighlights(p: *Pardes) void { need_end <= f.highlight_start + f.highlights.len) { f.syntax_dirty = false; + if (!f.tree_context and pane.colorAlgo() == .source) paintAhead(p, pane, f); continue; } const slack: usize = if (f.highlights.len == 0) 0 else pane.rows; const whole = pane.colorAlgo() == .locations or f.tree_context; + // Painted ahead on a worker (Stream.paintAhead): taken, no parse. + if (!whole and pane.colorAlgo() == .source) if (Stream.takeAhead(p, pane.serial, f.revision, need_start, need_end)) |got| { + if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); + f.highlights = got.styles; + f.highlight_start = got.start; + f.syntax_dirty = false; + p.stream.painted_ahead += 1; + paintAhead(p, pane, f); + continue; + }; + if (pane.colorAlgo() == .source and !whole) p.stream.painted_on_loop += 1; const start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack); const end = if (whole) f.content.len @@ -1755,9 +1768,28 @@ pub fn refreshHighlights(p: *Pardes) void { f.highlights = new_highlights; f.highlight_start = if (f.highlights.len > 0) start else 0; f.syntax_dirty = false; + if (!whole and pane.colorAlgo() == .source) paintAhead(p, pane, f); } } +/// The stretch a scroll of `pane` is heading into, two views on the way it +/// last went, asked of a worker (Stream.paintAhead) so it is painted by the +/// time the view gets there. +fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void { + const seen = &p.stream.ahead_seen; + if (seen.serial == pane.serial and f.scroll != seen.scroll) seen.dir = if (f.scroll > seen.scroll) 1 else -1; + if (seen.serial != pane.serial) seen.dir = 1; + seen.serial = pane.serial; + seen.scroll = f.scroll; + const rows: usize = pane.rows; + const view = if (seen.dir > 0) f.scroll + 2 * rows else f.scroll -| 2 * rows; + const start = lineStart(p.gpa, f, view -| rows); + const end = @max(start, lineStart(p.gpa, f, view + 2 * rows + SYNTAX_CONTEXT_AFTER_ROWS)); + // Painted already, as far as that goes. + if (f.highlights.len > 0 and start >= f.highlight_start and end <= f.highlight_start + f.highlights.len) return; + Stream.paintAhead(p, pane.serial, f.revision, f.path, f.content, start, end); +} + pub fn supportsContext(pane: *const Pane) bool { const file = pane.file orelse return false; return file.output == null and file.mini == null and syntax.supportsPath(file.path); diff --git a/src/Stream.zig b/src/Stream.zig index eb420b42..ace73ca4 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -68,12 +68,17 @@ pub const Batch = struct { /// text at `revision`. parsed: ?struct { serial: u32, revision: u32 } = null, decls: []syntax.ContextDeclaration = &.{}, + /// A paint's answer (`paintAhead`): the colours of pane `serial`'s text + /// at `revision` from byte `start`, one syntax.Syn a byte. + painted: ?struct { serial: u32, revision: u32, start: usize } = null, + styles: []u8 = &.{}, pub const End = enum { done, failed }; pub fn deinit(b: *const Batch, gpa: std.mem.Allocator) void { gpa.free(b.rows); gpa.free(b.decls); + gpa.free(b.styles); } }; @@ -101,6 +106,41 @@ pub const ParseJob = struct { const ParseTask = struct { job: *ParseJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +/// The colours of the text a scroll is heading into, worked out on a +/// worker before it gets there, as a game streams in the level ahead of +/// the player: refreshHighlights paints only the rows a view shows (and a +/// view's worth either side), so a scroll past them used to parse the next +/// stretch on the frame (6-58 ms in Debug on a 1.6 MB file, a hitch every +/// few pages). Now it finds them painted already, and only a jump past +/// what was foreseen (ge, a Look) paints on the frame. +/// ponytail: one paint at a time, for the pane last scrolled, in the way it +/// went; a pane per worker if two scroll at once. +pub const PaintJob = struct { + gpa: std.mem.Allocator, + out: std.mem.Allocator, + gen: u32, + serial: u32, + revision: u32, + /// Where `source` starts in the text. + start: usize, + path: []u8, + source: []u8, + stop: std.atomic.Value(bool) = .init(false), + + pub fn deinit(job: *PaintJob) void { + job.gpa.free(job.path); + job.gpa.free(job.source); + job.gpa.destroy(job); + } +}; + +const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; + +/// A paint's generation has the top bit, so it can never be a search's or +/// a parse's (those count from 0 in `gen`, and it would take two billion +/// searches to reach it). +const paint_bit: u32 = 1 << 31; + /// Where a worker hands a batch: false when it was turned away (the inbox /// closed, the wait for room canceled), and the worker stops. pub const Sink = *const fn (ctx: ?*anyopaque, batch: Batch) bool; @@ -161,10 +201,23 @@ parse_gen: u32 = 0, parse_next: ?struct { serial: u32, revision: u32 } = null, /// What the last parse cost the worker. parse_ns: u64 = 0, +/// The paint running (one at a time), its generation's count, and the +/// painted stretch it gave, kept until the view gets there (tree_sitter_gpa). +paint: ?PaintTask = null, +paint_count: u32 = 0, +ahead: []u8 = &.{}, +ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{}, +/// Stretches a view took painted ahead, and painted on the loop itself +/// (the tests count them). +painted_ahead: u32 = 0, +painted_on_loop: u32 = 0, +/// The view the last paint was asked from, and which way it was going. +ahead_seen: struct { serial: u32 = 0, scroll: usize = 0, dir: i2 = 1 } = .{}, pub fn deinit(s: *Stream, gpa: std.mem.Allocator) void { s.cancelTask(); s.cancelParse(); + s.cancelPaint(); s.pattern.deinit(gpa); s.dir.deinit(gpa); } @@ -184,6 +237,96 @@ pub fn cancelParse(s: *Stream) void { t.job.deinit(); } +/// The paint's worker told to stop and joined. +pub fn cancelPaint(s: *Stream) void { + const t = s.paint orelse return; + s.paint = null; + t.job.stop.store(true, .release); + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.cancel(t.io.?); + pardes.turn.back(); + } + t.job.deinit(); +} + +/// The painted stretch kept for pane `serial`'s text at `revision`, if it +/// covers bytes `need_start..need_end`: handed over (the caller owns it, +/// tree_sitter_gpa), with where it starts. +pub fn takeAhead(p: *Pardes, serial: u32, revision: u32, need_start: usize, need_end: usize) ?struct { styles: []u8, start: usize } { + const s = &p.stream; + const a = s.ahead_for; + if (s.ahead.len == 0 or a.serial != serial or a.revision != revision) return null; + if (need_start < a.start or need_end > a.start + s.ahead.len) return null; + defer s.ahead = &.{}; + return .{ .styles = s.ahead, .start = a.start }; +} + +/// Asks a worker to paint bytes `start..end` of pane `serial`'s text ahead +/// of its view, unless that is painted or being painted already. Only with +/// workers, and only for a grammar a worker can paint (syntax.readyApart). +pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, content: []const u8, from: usize, to: usize) void { + const s = &p.stream; + if (p.host.vtable.search == null or p.fs.serving or to <= from or to > content.len) return; + const a = s.ahead_for; + if (s.ahead.len != 0 and a.serial == serial and a.revision == revision and from >= a.start and to <= a.start + s.ahead.len) return; + if (s.paint) |t| { + // One under way for the same text that reaches into the stretch + // asked for is let finish: held keys move the stretch every frame, + // and a paint restarted every frame would never land. + if (t.job.serial == serial and t.job.revision == revision and t.job.start < to and t.job.start + t.job.source.len > from) return; + s.cancelPaint(); + } + if (!syntax.readyApart(path)) return; + const job = p.gpa.create(PaintJob) catch return; + const path_copy = p.gpa.dupe(u8, path) catch { + p.gpa.destroy(job); + return; + }; + const source = p.gpa.dupe(u8, content[from..to]) catch { + p.gpa.free(path_copy); + p.gpa.destroy(job); + return; + }; + s.paint_count +%= 1; + const gen = paint_bit | (s.paint_count & ~paint_bit); + job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = serial, .revision = revision, .start = from, .path = path_copy, .source = source }; + s.paint = .{ .job = job }; + p.emit(.{ .search = .{ .gen = gen } }); +} + +fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { + const styles = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch return; + if (!sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done })) return; +} + +/// A paint's answer: its task joined, its stretch kept for when the view +/// gets there, if the text is still what was painted. +fn receivePaint(p: *Pardes, b: *const Batch) void { + const s = &p.stream; + const painted = b.painted.?; + if (s.paint) |t| if (t.job.gen == b.gen) { + s.paint = null; + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.await(t.io.?); + pardes.turn.back(); + } + t.job.deinit(); + }; + const id = p.paneBySerial(painted.serial) orelse return; + const f = p.panes[id].?.file orelse return; + if (f.revision != painted.revision or b.styles.len == 0) return; + const copy = p.tree_sitter_gpa.dupe(u8, b.styles) catch return; + if (s.ahead.len > 0) p.tree_sitter_gpa.free(s.ahead); + s.ahead = copy; + s.ahead_for = .{ .serial = painted.serial, .revision = painted.revision, .start = painted.start }; + // The next frame looks again: the view may be there already. + p.panes[id].?.file.?.syntax_dirty = true; +} + /// Pane `id`'s declarations asked for, as its text is now, unless a parse of /// that text is already on its way. With workers, on one; without (a test), /// here only for a small text, else not at all (the rows stay ungrouped). @@ -338,6 +481,20 @@ pub fn wake(s: *const Stream, next_frame: u64) ?u64 { /// inbox). Called by the shell for the `search` effect, on the loop. pub fn start(io: std.Io, gpa: std.mem.Allocator, p: *Pardes, gen: u32, ctx: ?*anyopaque, sink: Sink) void { const s = &p.stream; + // A paint's generation: its own task, its own worker. + if (gen & paint_bit != 0) { + if (s.paint) |*t| if (t.job.gen == gen and t.future == null) { + t.job.out = gpa; + t.future = io.concurrent(paintWork, .{ t.job, ctx, sink }) catch { + const job = t.job; + s.paint = null; + job.deinit(); + return; + }; + t.io = io; + }; + return; + } // A parse's generation: its own task. if (s.parse) |*t| if (gen == s.parse_gen and t.job.gen == gen and t.future == null) { t.job.out = gpa; @@ -424,6 +581,7 @@ const Collect = struct { pub fn receive(p: *Pardes, b: *const Batch) void { const s = &p.stream; if (b.parsed) |parsed| return receiveParse(p, b, parsed.serial, parsed.revision); + if (b.painted != null) return receivePaint(p, b); if (b.gen != s.gen or (s.state != .searching and s.state != .capped)) { s.dropped += 1; return; @@ -1079,6 +1237,49 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop, try testing.expect(p.stream.parse == null); } +test "Stream: a scroll's colours are painted ahead on a worker, so PageDown after PageDown parses nothing on the frame" { + if (comptime !filesystem.platform_has_fs or !syntax.enabled) return; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(testing.allocator); + for (0..6000) |i| try text.print(testing.allocator, "fn f{d}() void {{\n _ = \"needle\";\n}}\n", .{i}); + const pane = try p.setTestFile(text.items); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); + p.advance(1); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelParse(); + p.stream.cancelPaint(); + shell.q.deinit(); + } + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + _ = try p.render(arena.allocator()); + const first = p.stream.painted_on_loop; + for (0..20) |_| { + // The worker has had its time (a frame's work at a held key's pace + // is all it needs; here it is waited for, so the test is not a race). + const until = std.Io.Clock.awake.now(testing.io).toMilliseconds() + 60_000; + while (p.stream.paint != null) { + if (std.Io.Clock.awake.now(testing.io).toMilliseconds() > until) return error.Timeout; + shell.pump(p); + testing.io.sleep(.fromMilliseconds(1), .awake) catch {}; + } + p.update(.{ .key = .{ .cp = pardes.Key.page_down } }); + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + // Coloured: the rows shown have their colours. + const f = &pane.file.?; + try testing.expect(f.highlights.len > 0 and f.highlight_start <= f.line_starts[f.scroll]); + } + try testing.expect(p.stream.painted_on_loop == first); + try testing.expect(p.stream.painted_ahead >= 5); +} + test "zz stream timings on the stress file with a worker (QUERY_TIMES=<path>)" { const path_z = std.c.getenv("QUERY_TIMES") orelse return error.SkipZigTest; const path = std.mem.span(path_z); diff --git a/src/draw.zig b/src/draw.zig index 92fd8205..265e07c1 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -1971,7 +1971,7 @@ test "each Motion flavour reaches the lift as its own trajectory" { try std.testing.expect(peaks[2] <= 1.001 and peaks[3] > 1.25 and peaks[4] > peaks[3]); } -test "the focused cursor glides a jump, lands a step at once, and is exact at rest" { +test "the focused cursor glides a jump, eases a step, the thin caret too, and is exact at rest" { if (!builtins.capabilities.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 30 }); defer p.deinit(); @@ -1983,12 +1983,17 @@ test "the focused cursor glides a jump, lands a step at once, and is exact at re var now: u64 = std.time.ns_per_s; p.advance(now); _ = try p.render(arena.allocator()); - // A step down: at once. + // A step down: eases there in a few frames. p.update(.{ .key = .{ .cp = 'j' } }); now += 16 * ms; p.advance(now); _ = arena.reset(.retain_capacity); var s = try p.render(arena.allocator()); + try std.testing.expectEqual(@as(u8, 3), s.chrome.cursor_glide); + now += 100 * ms; + p.advance(now); + _ = arena.reset(.retain_capacity); + s = try p.render(arena.allocator()); try std.testing.expectEqual(@as(u8, 2), s.chrome.cursor_glide); const before = s.cursor.?; // To the last line: it glides, a quad partway, and asks for frames. @@ -2023,7 +2028,7 @@ test "the focused cursor glides a jump, lands a step at once, and is exact at re // At rest the quad is the cursor's own cell. try std.testing.expectEqual(@as(f32, @floatFromInt(target.x)), s.chrome.cursor_quad[0]); try std.testing.expectEqual(@as(f32, @floatFromInt(target.y)), s.chrome.cursor_quad[1]); - // A mode change mid-glide lands at once: insert mode never trails. + // A mode change mid-glide: the thin caret glides on to the mark. p.update(.{ .key = .{ .cp = 'g' } }); p.update(.{ .key = .{ .cp = 'g' } }); now += 7 * ms; @@ -2036,7 +2041,17 @@ test "the focused cursor glides a jump, lands a step at once, and is exact at re p.advance(now); _ = arena.reset(.retain_capacity); s = try p.render(arena.allocator()); - try std.testing.expectEqual(@as(u8, 2), s.chrome.cursor_glide); + try std.testing.expectEqual(@as(u8, 3), s.chrome.cursor_glide); + try std.testing.expect(s.cursor.?.bar); + while (p.continuous()) { + now += 7 * ms; + p.advance(now); + _ = arena.reset(.retain_capacity); + s = try p.render(arena.allocator()); + } + // At rest, a bar's width at the caret's cell. + try std.testing.expectEqual(@as(f32, @floatFromInt(s.cursor.?.x)), s.chrome.cursor_quad[0]); + try std.testing.expectApproxEqAbs(@as(f32, 1.0 / 8.0), s.chrome.cursor_quad[2] - s.chrome.cursor_quad[0], 1e-4); } test "a tag caret past its row's end glides to the tag's right edge, where the layer's is" { diff --git a/src/pardes.zig b/src/pardes.zig index df6d347f..9177721f 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -4983,6 +4983,7 @@ pub const Pardes = struct { p.pipe.deinit(gpa); p.stream.deinit(gpa); p.handover.deinit(gpa); + if (p.stream.ahead.len > 0) p.tree_sitter_gpa.free(p.stream.ahead); p.locations_cache.deinit(p.tree_sitter_gpa); p.fs.deinit(gpa); p.shell_rows.reset(gpa); diff --git a/src/syntax.zig b/src/syntax.zig index aca2c3ff..821754fa 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -304,6 +304,55 @@ pub fn highlightFileRange(gpa: std.mem.Allocator, path: []const u8, content: []c return styles; } +/// Whether `highlightApart` can colour `path` on a worker: a grammar with +/// no injections (markdown's and typst's reach the shared parsers), made +/// ready here, on the loop, so the worker only reads its query. +pub fn readyApart(path: []const u8) bool { + if (!enabled) return false; + const selected = (forExt(std.fs.path.extension(path)) catch return false) orelse return false; + return !std.mem.eql(u8, selected.name, "markdown") and !std.mem.eql(u8, selected.name, "typst"); +} + +/// `highlightFileRange` of `source` (a copy of the range), on a worker: a +/// parser and a query cursor of its own, the grammar's query shared (it +/// is only read); `halt` ends the parse early. After `readyApart`. +pub fn highlightApart(gpa: std.mem.Allocator, path: []const u8, source: []const u8, halt: *const std.atomic.Value(bool)) ![]u8 { + if (comptime !enabled) return &.{}; + const shared = (try forExt(std.fs.path.extension(path))) orelse return &.{}; + const parser = ts.Parser.create(); + defer parser.destroy(); + try parser.setLanguage(shared.lang); + const cursor = ts.QueryCursor.create(); + defer cursor.destroy(); + const Reader = struct { + text: []const u8, + fn read(payload: ?*anyopaque, byte: u32, _: ts.Point, n: *u32) callconv(.c) [*c]const u8 { + const r: *@This() = @ptrCast(@alignCast(payload.?)); + const at = @min(byte, r.text.len); + n.* = @intCast(r.text.len - at); + return r.text.ptr + at; + } + fn progress(state: *ts.Parser.State) callconv(.c) bool { + const flag: *const std.atomic.Value(bool) = @ptrCast(@alignCast(state.payload.?)); + return flag.load(.monotonic); + } + }; + var reader: Reader = .{ .text = source }; + const tree = parser.parseWithOptions( + .{ .payload = &reader, .read = Reader.read, .encoding = .utf8 }, + null, + .{ .payload = @constCast(halt), .progress_callback = @ptrCast(&Reader.progress) }, + ) orelse return error.Canceled; + defer tree.destroy(); + const styles = try gpa.alloc(u8, source.len); + @memset(styles, 0); + var own = shared; + own.parser = parser; + own.cursor = cursor; + runQuery(styles, own, tree, 0); + return styles; +} + /// Source spans are independent of editor rows. Locations output uses the same /// declarations as sticky editor headers. Byte ends are exclusive; rows are /// zero-based and inclusive, including the last signature/opening-brace row. |
