summaryrefslogtreecommitdiff
path: root/src/syntax.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-24 14:38:19 -0300
committerGabriel Schneider <[email protected]>2026-10-24 21:00:18 -0300
commit0d3179f04a91c942fc10b0a05cdd6b8aa958d13e (patch)
tree2847000be687597a3a1c6963eae4b77f1cae6cf8 /src/syntax.zig
parenta87f917559e3712d36e5809d1f457fbf78c8266e (diff)
downloadpardes-0d3179f04a91c942fc10b0a05cdd6b8aa958d13e.tar.gz
pardes-0d3179f04a91c942fc10b0a05cdd6b8aa958d13e.zip
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
Diffstat (limited to 'src/syntax.zig')
-rw-r--r--src/syntax.zig49
1 files changed, 49 insertions, 0 deletions
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.