summaryrefslogtreecommitdiff
path: root/core/syntax.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 06:33:52 -0300
committerGabriel Schneider <[email protected]>2026-11-20 06:33:53 -0300
commitffe05cceb897b3ae6a6d36491f9318a8aa3426e7 (patch)
tree352414579559060b3e8486af34e29d49fd5df781 /core/syntax.zig
parent5b7124d10f9b2db8d1c5e99eacb9b6d2fffff0cc (diff)
parenta403340ec5c90238b136ca85fc64d65809b29a65 (diff)
downloadpardes-ffe05cceb897b3ae6a6d36491f9318a8aa3426e7.tar.gz
pardes-ffe05cceb897b3ae6a6d36491f9318a8aa3426e7.zip
integration: the build's second pass on the handle and the devices once: pardes-build-pass2 (pardes-isolate and the tree-sitter=disabled tier deleted; one option, grammars = none | minimal | full; build.zig with the consumer's lines as its head comment; test/lib -> test/consumer; the manual's library section) and pardes-merge31, merged without a textual conflict; one line follows a deletion: dev/file_watch.zig's end-to-end test of a session's watches (written since on the base) guarded on fs.platform_has_fs, which the first of these changes deleted: the guard reads as the file's other tests' (comptime !supported); tui core-test 857/1008 (151 skipped), shell-test 1048/1051, the window builds; the coordinator's gate in the ledger (reorg/merge32)
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/syntax.zig')
-rw-r--r--core/syntax.zig113
1 files changed, 35 insertions, 78 deletions
diff --git a/core/syntax.zig b/core/syntax.zig
index ba3851ab..19831ec2 100644
--- a/core/syntax.zig
+++ b/core/syntax.zig
@@ -7,17 +7,11 @@ const regexp = @import("regexp.zig");
const c_heap = @import("c_heap");
const diff = @import("diff.zig");
-pub const enabled = config.grammars != .disabled;
-const minimal_grammars = config.grammars == .minimal or config.grammars == .full;
+const minimal_grammars = config.grammars != .none;
const full_grammars = config.grammars == .full;
-pub const ts = if (enabled) @import("tree-sitter") else struct {
- pub const Language = opaque {};
- pub const Query = opaque {};
- pub const Parser = opaque {};
- pub const QueryCursor = opaque {};
-};
-const ts_queries = if (enabled) @import("ts_queries") else struct {};
+pub const ts = @import("tree-sitter");
+const ts_queries = @import("ts_queries");
const LanguageFn = *const fn () callconv(.c) *const ts.Language;
@@ -43,7 +37,6 @@ const Spec = struct {
fn grammarSelected(comptime g: grammar_manifest.Grammar) bool {
return switch (g.tier) {
- .zig => enabled,
.minimal => minimal_grammars,
.full => full_grammars,
};
@@ -92,28 +85,24 @@ extern fn ts_set_allocator(
) void;
pub fn start(gpa: std.mem.Allocator) void {
- if (comptime enabled) {
- std.debug.assert(!syntax_started);
- stop();
- syntax_allocator = gpa;
- syntax_started = true;
- ts_set_allocator(syntax_heap.malloc, syntax_heap.calloc, syntax_heap.realloc, syntax_heap.free);
- }
+ std.debug.assert(!syntax_started);
+ stop();
+ syntax_allocator = gpa;
+ syntax_started = true;
+ ts_set_allocator(syntax_heap.malloc, syntax_heap.calloc, syntax_heap.realloc, syntax_heap.free);
}
pub fn stop() void {
- if (comptime enabled) {
- for (&specs) |*spec| dropSpec(spec);
- forgetLanguages();
- for (&diff_pieces) |*slot| {
- if (slot.*) |piece| syntax_allocator.free(piece.styles);
- slot.* = null;
- }
- diff_clock = 0;
- ts_set_allocator(null, null, null, null);
- syntax_started = false;
- syntax_allocator = undefined;
+ for (&specs) |*spec| dropSpec(spec);
+ forgetLanguages();
+ for (&diff_pieces) |*slot| {
+ if (slot.*) |piece| syntax_allocator.free(piece.styles);
+ slot.* = null;
}
+ diff_clock = 0;
+ ts_set_allocator(null, null, null, null);
+ syntax_started = false;
+ syntax_allocator = undefined;
}
const Selected = struct {
@@ -254,7 +243,6 @@ fn runQuery(styles: []u8, sel: Selected, tree: *ts.Tree, base: usize) void {
pub fn highlightFileRange(gpa: std.mem.Allocator, path: []const u8, content: []const u8, start_raw: usize, end_raw: usize) ![]u8 {
const zone = tracy.zone(@src(), "highlightFileRange");
defer zone.end();
- if (!enabled) return &.{};
const selected = (try forExt(std.Io.Dir.path.extension(path))) orelse return &.{};
const start_byte = @min(start_raw, content.len);
const end_byte = @max(start_byte, @min(end_raw, content.len));
@@ -269,7 +257,6 @@ pub fn highlightFileRange(gpa: std.mem.Allocator, path: []const u8, content: []c
/// 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.Io.Dir.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");
}
@@ -278,7 +265,6 @@ pub fn readyApart(path: []const u8) bool {
/// 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.Io.Dir.path.extension(path))) orelse return &.{};
const tree = try parseApart(shared.lang, source, halt);
defer tree.destroy();
@@ -335,7 +321,6 @@ pub const ContextDeclaration = struct {
};
pub fn supportsPath(path: []const u8) bool {
- if (!enabled) return false;
return byExt(std.Io.Dir.path.extension(path)) != null;
}
@@ -345,7 +330,6 @@ pub fn supportsPath(path: []const u8) bool {
/// since its parse (`editTree`), the parse reuses what those left alone: a
/// character typed into a 9k-line file costs a few hundred µs, not 50 ms.
pub fn parseTree(path: []const u8, content: []const u8, old: ?*anyopaque) ?*anyopaque {
- if (!enabled) return null;
const selected = (forExt(std.Io.Dir.path.extension(path)) catch return null) orelse return null;
const tree = selected.parser.parseString(content, @ptrCast(@alignCast(old))) orelse return null;
if (held_trees) |h| h.born.append(h.meta, @ptrCast(tree)) catch {};
@@ -357,7 +341,6 @@ pub fn parseTree(path: []const u8, content: []const u8, old: ?*anyopaque) ?*anyo
/// places follow the edit; what it says of the edited bytes is stale until
/// `parseTree` with it.
pub fn editTree(tree: *anyopaque, before: []const u8, old: []const u8, ins: []const u8) void {
- if (!enabled) return;
const from = advance(.{ .row = 0, .column = 0 }, before);
const t: *ts.Tree = @ptrCast(@alignCast(tree));
t.edit(.{
@@ -387,7 +370,6 @@ pub const HeldTrees = struct {
};
pub fn freeTree(tree: *anyopaque) void {
- if (!enabled) return;
if (held_trees) |h| {
for (h.born.items, 0..) |t, i| if (t == tree) {
_ = h.born.swapRemove(i);
@@ -400,7 +382,6 @@ pub fn freeTree(tree: *anyopaque) void {
}
pub fn destroyTree(tree: *anyopaque) void {
- if (!enabled) return;
const t: *ts.Tree = @ptrCast(@alignCast(tree));
t.destroy();
}
@@ -412,7 +393,7 @@ pub const Span = struct { from: usize, to: usize };
/// The spans `walk` takes the byte range [from, to) to, into `out`; `at` is
/// the range's cursor. None when the tree has nothing to say there.
pub fn walkNodes(tree_ptr: *anyopaque, walk: NodeWalk, from: usize, to: usize, at: usize, out: []Span) usize {
- if (!enabled or out.len == 0) return 0;
+ if (out.len == 0) return 0;
const tree: *const ts.Tree = @ptrCast(@alignCast(tree_ptr));
const lo: u32 = @intCast(@min(from, std.math.maxInt(u32)));
const hi: u32 = @intCast(@min(to, std.math.maxInt(u32)));
@@ -497,7 +478,6 @@ fn objectQuery(path: []const u8) ?*ts.Query {
/// The grammar's indent query for `path` (core/indent.zig) and the
/// grammar's name; null when it has none.
pub fn indentQuery(path: []const u8) ?struct { *ts.Query, []const u8 } {
- if (!enabled) return null;
const spec = specFor(path) orelse return null;
return .{ lazyQuery(spec, &spec.indents) orelse return null, spec.name };
}
@@ -563,7 +543,6 @@ fn matchSpan(q: *const ts.Query, match: anytype, capture: u32, text: []const u8)
/// `mi`/`ma`: the smallest `object.inside` (or `.around`) holding byte `at`
/// (helix textobject_treesitter). None without a query or such an object.
pub fn objectAt(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object: Object, around: bool, at: usize) ?Span {
- if (!enabled) return null;
const q = objectQuery(path) orelse return null;
var buf: [32]u8 = undefined;
const want = std.fmt.bufPrint(&buf, "{s}.{s}", .{ objectName(object), if (around) "around" else "inside" }) catch return null;
@@ -587,7 +566,6 @@ pub fn objectAt(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object
/// previous one ending before it (helix goto_treesitter_object), from the
/// first of `.movement`, `.around`, `.inside` the query names.
pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object: Object, forward: bool, at: usize) ?Span {
- if (!enabled) return null;
const q = objectQuery(path) orelse return null;
var buf: [32]u8 = undefined;
const capture = for ([_][]const u8{ "movement", "around", "inside" }) |kind| {
@@ -628,7 +606,7 @@ pub const SourceAnalysis = struct {
};
pub fn analyzeSource(gpa: std.mem.Allocator, path: []const u8, content: []const u8, want_declarations: bool, want_colors: bool) !SourceAnalysis {
- if (!enabled or content.len == 0 or (!want_declarations and !want_colors)) return .{};
+ if (content.len == 0 or (!want_declarations and !want_colors)) return .{};
const selected = (try forExt(std.Io.Dir.path.extension(path))) orelse return .{};
const tree = selected.parser.parseString(content, null) orelse return .{};
defer tree.destroy();
@@ -652,7 +630,6 @@ pub fn contextDeclarations(gpa: std.mem.Allocator, path: []const u8, content: []
/// (`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 &.{};
const ext = std.Io.Dir.path.extension(path);
const spec = specs: for (&specs) |*spec| {
@@ -796,7 +773,6 @@ pub fn highlightLocations(gpa: std.mem.Allocator, content: []const u8, start_byt
const start_byte = @min(start_byte_raw, content.len);
const end_byte = @max(start_byte, @min(end_byte_raw, content.len));
const source = content[start_byte..end_byte];
- if (!enabled) return &.{};
const styles = try gpa.alloc(u8, source.len);
errdefer gpa.free(styles);
@memset(styles, 0);
@@ -829,7 +805,7 @@ pub fn highlightLocations(gpa: std.mem.Allocator, content: []const u8, start_byt
/// padded location column. This also colors context whose location is hidden.
/// Consecutive source rows share one parse, retaining multiline syntax.
pub fn highlightLocationRows(gpa: std.mem.Allocator, content: []const u8, rows: []const locations.Row) ![]u8 {
- if (!enabled or content.len == 0 or rows.len == 0) return &.{};
+ if (content.len == 0 or rows.len == 0) return &.{};
const styles = try gpa.alloc(u8, content.len);
errdefer gpa.free(styles);
@memset(styles, 0);
@@ -1111,7 +1087,7 @@ const DiffWindow = struct {
};
fn paintDiffPiece(gpa: std.mem.Allocator, window: DiffWindow, bytes: []const u8, at: usize, kinds: []const diff.Kind, path: []const u8) !void {
- const selected: ?Selected = if (enabled and syntax_started) (forExt(std.Io.Dir.path.extension(path)) catch null) else null;
+ const selected: ?Selected = if (syntax_started) (forExt(std.Io.Dir.path.extension(path)) catch null) else null;
if (selected) |sel| {
const hash = std.hash.Wyhash.hash(std.hash.Wyhash.hash(0, sel.name), bytes);
diff_clock += 1;
@@ -1269,7 +1245,6 @@ test {
}
test "every vendored textobject and indent query compiles against its grammar" {
- if (!enabled) return;
var failed: usize = 0;
for (&specs) |*spec| for ([_]Lazy{ spec.objects, spec.indents }, [_][]const u8{ "textobjects", "indents" }) |lazy, what| {
if (lazy.src.len == 0) continue;
@@ -1285,7 +1260,6 @@ test "every vendored textobject and indent query compiles against its grammar" {
}
test "syntax context nested Zig containers and multiline function signatures" {
- if (!enabled) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1317,7 +1291,7 @@ test "syntax context nested Zig containers and multiline function signatures" {
}
test "syntax context Python class and function body excludes first statement" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1330,7 +1304,6 @@ test "syntax context Python class and function body excludes first statement" {
}
test "syntax context multiline bindings and C++ namespaces preserve declaration starts" {
- if (!enabled) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1340,7 +1313,7 @@ test "syntax context multiline bindings and C++ namespaces preserve declaration
try std.testing.expectEqual(@as(usize, 1), zig_declarations.len);
try std.testing.expectEqual(@as(usize, 0), zig_declarations[0].start_line);
try std.testing.expectEqual(@as(usize, 1), zig_declarations[0].header_end_line);
- if (!minimal_grammars and !full_grammars) return;
+ if (!minimal_grammars) return;
const cpp_source = "namespace example {\nstruct Outer {\n int run() {\n return 1;\n }\n};\n}\n";
const cpp_declarations = try contextDeclarations(gpa, "nested.cpp", cpp_source);
defer gpa.free(cpp_declarations);
@@ -1352,7 +1325,7 @@ test "syntax context multiline bindings and C++ namespaces preserve declaration
}
test "syntax context Rust modules impls and methods remain nested" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1368,7 +1341,7 @@ test "syntax context Rust modules impls and methods remain nested" {
}
test "syntax formatted locations color hidden context and preserve multiline source" {
- if (!enabled or (!minimal_grammars and !full_grammars)) return;
+ if (!minimal_grammars) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1393,7 +1366,7 @@ test "syntax formatted locations color hidden context and preserve multiline sou
}
test "syntax formatted locations separate alignment from Markdown indentation" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1409,7 +1382,6 @@ test "syntax formatted locations separate alignment from Markdown indentation" {
}
test "syntax a results row is coloured by the file its location names" {
- if (!enabled) return;
start(std.testing.allocator);
defer stop();
const gpa = std.testing.allocator;
@@ -1432,7 +1404,6 @@ test "syntax a results row is coloured by the file its location names" {
}
test "syntax a buffer with no locations in it keeps no highlights at all" {
- if (!enabled) return;
start(std.testing.allocator);
defer stop();
const content = "nothing has been said yet\n0: save: AccessDenied (x2)\n";
@@ -1460,7 +1431,7 @@ test "syntax codeAfterLocation takes whole-token locations and nothing else" {
}
test "syntax default full grammar set highlights Typst source" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
start(std.testing.allocator);
defer stop();
@@ -1483,7 +1454,7 @@ test "syntax default full grammar set highlights Typst source" {
}
test "syntax Typst markup constructs paint and raw blocks inject their language" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
start(std.testing.allocator);
defer stop();
@@ -1527,7 +1498,7 @@ test "syntax Typst markup constructs paint and raw blocks inject their language"
}
test "syntax markdown highlights markup, injects inline spans and fenced code blocks" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
start(std.testing.allocator);
defer stop();
@@ -1559,7 +1530,7 @@ test "syntax markdown highlights markup, injects inline spans and fenced code bl
}
test "syntax inline fast path agrees with full Markdown query" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
start(std.testing.allocator);
defer stop();
const selected = (try forLang("markdown_inline")).?;
@@ -1606,7 +1577,7 @@ test "syntax inline fast path agrees with full Markdown query" {
}
test "syntax plain Markdown keeps block styles without starting the inline parser" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
start(std.testing.allocator);
defer stop();
const source = "# Heading\n\nPlain prose.\n\n indented code\n";
@@ -1648,7 +1619,6 @@ test "syntax highlightDiff colors unified diff lines by prefix" {
}
test "syntax a Zig hunk is coloured as Zig, each side parsed whole" {
- if (!enabled) return;
start(std.testing.allocator);
defer stop();
const diff_text =
@@ -1689,7 +1659,7 @@ test "syntax a Zig hunk is coloured as Zig, each side parsed whole" {
}
test "syntax a Python hunk is coloured as Python" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
start(std.testing.allocator);
defer stop();
const diff_text =
@@ -1710,7 +1680,6 @@ test "syntax a Python hunk is coloured as Python" {
}
test "syntax a deleted file's lines are coloured in its language" {
- if (!enabled) return;
start(std.testing.allocator);
defer stop();
const diff_text =
@@ -1730,7 +1699,6 @@ test "syntax a deleted file's lines are coloured in its language" {
}
test "syntax a hunk in no known language keeps the line colours" {
- if (!enabled) return;
start(std.testing.allocator);
defer stop();
const diff_text =
@@ -1747,7 +1715,6 @@ test "syntax a hunk in no known language keeps the line colours" {
}
test "syntax git diff output in a command pane is told and coloured by its rows" {
- if (!enabled) return;
start(std.testing.allocator);
defer stop();
// A command pane's rows: its command line, then git's output, with one
@@ -1780,7 +1747,6 @@ test "syntax git diff output in a command pane is told and coloured by its rows"
}
test "syntax a section's small hunks share one parse" {
- if (!enabled) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1802,7 +1768,6 @@ test "syntax a section's small hunks share one parse" {
}
test "syntax a hunk painted once is kept, and a long one is parsed in pieces" {
- if (!enabled) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1832,7 +1797,6 @@ test "syntax a hunk painted once is kept, and a long one is parsed in pieces" {
}
test "syntax result fragments preserve source indentation and inline markup" {
- if (!enabled) return;
start(std.testing.allocator);
defer stop();
const fixtures = [_]struct { path: []const u8, source: []const u8 }{
@@ -1862,7 +1826,6 @@ test "syntax result fragments preserve source indentation and inline markup" {
}
test "syntax query filtering preserves upstream colors" {
- if (!enabled) return;
var allocator: std.heap.DebugAllocator(.{ .stack_trace_frames = 0, .safety = true }) = .init;
defer if (allocator.deinit() != .ok) @panic("leaked syntax query allocations");
start(allocator.allocator());
@@ -1893,7 +1856,6 @@ test "syntax query filtering preserves upstream colors" {
}
test "syntax Zig keyword captures cover both bytes beyond line ten thousand" {
- if (!enabled) return;
start(std.testing.allocator);
defer stop();
const code = "pub fn main() void {\n if (true) return;\n}\n";
@@ -1911,7 +1873,6 @@ test "syntax Zig keyword captures cover both bytes beyond line ten thousand" {
}
test "syntax allocator switching clears default-runtime caches" {
- if (!enabled) return;
const source = "fn main() void {}";
const initial = try highlightFileRange(std.testing.allocator, "a.zig", source, 0, source.len);
std.testing.allocator.free(initial);
@@ -1927,7 +1888,7 @@ test "syntax allocator switching clears default-runtime caches" {
}
test "syntax location context snapshots retain omitted multiline comment scope" {
- if (!enabled or (!minimal_grammars and !full_grammars)) return;
+ if (!minimal_grammars) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1953,7 +1914,6 @@ test "syntax location context snapshots retain omitted multiline comment scope"
}
test "syntax declaration locations clear snapshots without muting ordinary context" {
- if (!enabled) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1973,7 +1933,7 @@ test "syntax declaration locations clear snapshots without muting ordinary conte
}
test "syntax stacked addresses do not split multiline source groups" {
- if (!enabled or (!minimal_grammars and !full_grammars)) return;
+ if (!minimal_grammars) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -1998,7 +1958,6 @@ test "syntax stacked addresses do not split multiline source groups" {
}
test "syntax source analysis preserves declarations and injected colors" {
- if (!enabled) return;
const gpa = std.testing.allocator;
start(gpa);
defer stop();
@@ -2059,7 +2018,7 @@ test "syntax source analysis preserves declarations and injected colors" {
}
test "tables are the pipe_tables tree-sitter finds" {
- if (!enabled or !full_grammars) return;
+ if (!full_grammars) return;
start(std.testing.allocator);
defer stop();
const Tables = @import("Tables.zig");
@@ -2128,7 +2087,6 @@ test "tables are the pipe_tables tree-sitter finds" {
}
test "a kept tree told its edits parses to the fresh parse's tree, places and all" {
- if (!enabled) return error.SkipZigTest;
const gpa = std.testing.allocator;
const a = "const std = @import(\"std\");\n\nfn f(x: u32) u32 {\n return x;\n}\n\ntest \"t\" {\n _ = f(1);\n}\n";
const old_tree = parseTree("a.zig", a, null) orelse return error.NoTree;
@@ -2168,7 +2126,6 @@ test "a kept tree told its edits parses to the fresh parse's tree, places and al
}
test "syntax: a grammar handed in at run time colours and walks as the one compiled in" {
- if (!enabled) return error.SkipZigTest;
const gpa = std.testing.allocator;
start(gpa);
defer stop();