summaryrefslogtreecommitdiff
path: root/src/syntax.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax.zig')
-rw-r--r--src/syntax.zig45
1 files changed, 43 insertions, 2 deletions
diff --git a/src/syntax.zig b/src/syntax.zig
index 803d815a..81722ce5 100644
--- a/src/syntax.zig
+++ b/src/syntax.zig
@@ -572,7 +572,7 @@ pub fn analyzeSource(gpa: std.mem.Allocator, path: []const u8, content: []const
defer tree.destroy();
var result: SourceAnalysis = .{};
errdefer result.deinit(gpa);
- if (want_declarations) result.declarations = try declarationsFromTree(gpa, content, tree);
+ if (want_declarations) result.declarations = try declarationsFromTree(gpa, content, tree, null);
if (want_colors) {
result.colors = try gpa.alloc(u8, content.len);
@memset(result.colors, 0);
@@ -586,14 +586,55 @@ pub fn contextDeclarations(gpa: std.mem.Allocator, path: []const u8, content: []
return analysis.declarations;
}
-fn declarationsFromTree(gpa: std.mem.Allocator, content: []const u8, tree: *ts.Tree) ![]ContextDeclaration {
+/// 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.
+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 &.{};
+ const ext = std.fs.path.extension(path);
+ 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;
+ defer tree.destroy();
+ return declarationsFromTree(gpa, content, tree, halt);
+}
+
+fn declarationsFromTree(gpa: std.mem.Allocator, content: []const u8, tree: *ts.Tree, halt: ?*const std.atomic.Value(bool)) ![]ContextDeclaration {
var result: std.ArrayList(ContextDeclaration) = .empty;
errdefer result.deinit(gpa);
// The cursor retains its ancestor stack: sibling visits do not repeatedly
// reconstruct parents in broad syntax trees. Preorder preserves source order.
var cursor = tree.rootNode().walk();
defer cursor.destroy();
+ var visited: usize = 0;
while (true) {
+ visited += 1;
+ if (halt) |flag| if (visited % 4096 == 0 and flag.load(.monotonic)) return error.Canceled;
const node = cursor.node();
if (node.isNamed()) if (contextSpan(node, content)) |span| {
if (result.items.len == 0 or result.items[result.items.len - 1].start_line != span.start_line or