diff options
Diffstat (limited to 'src/syntax.zig')
| -rw-r--r-- | src/syntax.zig | 70 |
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); } |
