From 0d3179f04a91c942fc10b0a05cdd6b8aa958d13e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 14:38:19 -0300 Subject: anim 5: a scroll's colours are painted ahead on a worker (Stream.paintAhead), two views on the way it goes, so a page after a page parses nothing on the frame --- src/syntax.zig | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/syntax.zig') 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. -- cgit v1.3