summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 23:04:59 -0300
committerGabriel Schneider <[email protected]>2026-11-05 01:16:28 -0300
commita6554511da894afb91169fce9af84511b5e60504 (patch)
tree1fdf451c80cee27c47943231c5811f16d8c8470a /src
parent4423bc2f88fe2e88202056d6a11f759b4b143b74 (diff)
downloadpardes-a6554511da894afb91169fce9af84511b5e60504.tar.gz
pardes-a6554511da894afb91169fce9af84511b5e60504.zip
syntax: the two worker parses are one parseApart (its parser, Reader and halt callback written once), and highlightApart no longer sets a parser runQuery never reads
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
-rw-r--r--src/syntax.zig70
1 files changed, 27 insertions, 43 deletions
diff --git a/src/syntax.zig b/src/syntax.zig
index acbe543a..312678c4 100644
--- a/src/syntax.zig
+++ b/src/syntax.zig
@@ -318,16 +318,32 @@ pub fn readyApart(path: []const u8) bool {
}
/// `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
+/// parse 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 tree = try parseApart(shared.lang, source, halt);
+ defer tree.destroy();
+ if (halt.load(.monotonic)) return error.Canceled;
const cursor = ts.QueryCursor.create();
defer cursor.destroy();
+ const styles = try gpa.alloc(u8, source.len);
+ @memset(styles, 0);
+ var own = shared;
+ own.cursor = cursor;
+ runQuery(styles, own, tree, 0);
+ return styles;
+}
+
+/// `text` parsed with a parser of its own, so a worker thread may ask: the
+/// loop's parsers (`ensure`'s, one a language) are not touched, and a
+/// language is safe to share. `halt`, set from another thread, ends the
+/// parse early (error.Canceled).
+fn parseApart(lang: *const ts.Language, text: []const u8, halt: *const std.atomic.Value(bool)) !*ts.Tree {
+ const parser = ts.Parser.create();
+ defer parser.destroy();
+ try parser.setLanguage(lang);
const Reader = struct {
text: []const u8,
fn read(payload: ?*anyopaque, byte: u32, _: ts.Point, n: *u32) callconv(.c) [*c]const u8 {
@@ -341,21 +357,12 @@ pub fn highlightApart(gpa: std.mem.Allocator, path: []const u8, source: []const
return flag.load(.monotonic);
}
};
- var reader: Reader = .{ .text = source };
- const tree = parser.parseWithOptions(
+ var reader: Reader = .{ .text = text };
+ return 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();
- if (halt.load(.monotonic)) return error.Canceled;
- 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;
+ ) orelse error.Canceled;
}
/// Source spans are independent of editor rows. Locations output uses the same
@@ -664,11 +671,9 @@ pub fn contextDeclarations(gpa: std.mem.Allocator, path: []const u8, content: []
return analysis.declarations;
}
-/// The declarations of `content` (its path's language), parsed with a
-/// parser of its own, so a worker thread may ask: the loop's parsers
-/// (`ensure`'s, one a language) are not touched, and a language is safe to
-/// share. `stop`, set from another thread, ends the parse and the walk
-/// early (error.Canceled): a 1.2 MB file is seconds in a Debug build.
+/// The declarations of `content` (its path's language), on a worker
+/// (`parseApart`). `halt` ends the parse and the walk early
+/// (error.Canceled): a 1.2 MB file is seconds in a Debug build.
pub fn declarationsApart(gpa: std.mem.Allocator, path: []const u8, content: []const u8, halt: *const std.atomic.Value(bool)) ![]ContextDeclaration {
if (comptime !enabled) return &.{};
if (content.len == 0) return &.{};
@@ -676,28 +681,7 @@ pub fn declarationsApart(gpa: std.mem.Allocator, path: []const u8, content: []co
const spec = specs: for (&specs) |*spec| {
for (spec.exts) |choice| if (std.ascii.eqlIgnoreCase(ext, choice)) break :specs spec;
} else return &.{};
- const parser = ts.Parser.create();
- defer parser.destroy();
- try parser.setLanguage(spec.language());
- 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 = content };
- const tree = parser.parseWithOptions(
- .{ .payload = &reader, .read = Reader.read, .encoding = .utf8 },
- null,
- .{ .payload = @constCast(halt), .progress_callback = @ptrCast(&Reader.progress) },
- ) orelse return error.Canceled;
+ const tree = try parseApart(spec.language(), content, halt);
defer tree.destroy();
return declarationsFromTree(gpa, content, tree, halt);
}