From 1e127c3968205f0e42faa4eb5b894cea296346d0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 03:23:49 -0300 Subject: build: pardes-isolate and `zig build run-isolated` are deleted (pardes-build-2 item 1; P31's Q3, decided on the evidence 2026-11-20): the terminal's pardes compiled with `isolated = true`, a build whose host supplied only the terminal (no file, pty, clipboard, LSP or watch calls: tui.zig's second Host table) and whose filesystem was absent (`fs.platform_has_fs` false: every libc path compiled away, the virtual tree and the embedded sources alone), arriving on `src/pardes.zig` read out of the binary when no file was named; it did not compile on the base (`zig build run-isolated -Dplatform=tui -- version`: core/builtins.zig:510 `error.FileNotFound` not in `fs.readFile`'s set, core/ninep/ctl.zig:1298 `error.OutOfMemory` not in `Dir.refresh`'s set) and no test, gate or document ran it; out with it: its module set and step in build.zig, `Core.isolated` and the `isolated` option in build/core.zig, `pardes.isolated` (core/root.zig), `fs.platform_has_fs` and the 60 guards on the two (whole lines in core/Stream.zig, Dir.zig, Plumb.zig, fs.zig, look.zig, Query.zig, edit_cmd.zig, dump.zig, test/fs_namespace.zig, test/terminal.zig; a term of a condition in dev/file_watch.zig's six tests, fs.zig's readFileLimit, whose virtual branch now takes `/virtual/` alone, and one Stream.zig test), Stream.zig's search failing at once without a filesystem, tui.zig's `isolated_vtable`, runtime/main.zig's `src/pardes.zig` default (a file that does not exist), and the mention in docs/next-terminal.md; the changelog says it is gone; build.zig and build/ 2,996 -> 2,981 lines; `pardes version`'s semantics line did not move (`isolated` was never in its text): 8b6382fddba7b382 on tui, gui, the bare build's two, both statics and FreeBSD (gpu/tree2's base binary says the same), 0520895ac88d25ad with -Dtree-sitter=minimal; every shape configures (12) and builds (tui, gui, bare, static musl and gui, FreeBSD, -Dtree-sitter=minimal, the macOS library for Linux and aarch64-macos), -Dtree-sitter=disabled failing as on the base (core/indent.zig:48); tui core-test 856/1006 (150 skipped), shell-test 1046/1049, pane-test 183/184, behaviour-test 275/275 and -- --replay 275/275, term-test 17/17, kitty-test 45/45, addon-test 5/5, lib-test, hxlib 538 cases 0 mismatches, process-test 1 of 144 failed (pagercolor, on the flaky list: passes alone, 931 ms), gui unit-test 1643/1647 (the perf check's BaselineHarnessMismatch, the base's); load 6-17 on 16 cores Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- build.zig | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'build.zig') diff --git a/build.zig b/build.zig index fc0c5044..5301b453 100644 --- a/build.zig +++ b/build.zig @@ -175,8 +175,8 @@ pub fn build(b: *std.Build) void { // core.module. const root_set = core.module(b, parts, .{ .root = if (platform == .macos) "runtime/macos.zig" else "runtime/main.zig", .platform = platform, .builtin_addons = compiled_in, .tracy = true, .util = true }); const root_mod = root_set.root; - // hxdiff's headless core and pardes-isolate are the primary shell's - // configuration compiled two more ways, not two more frontends. + // hxdiff's headless core is the primary shell's configuration compiled + // another way, not another frontend. const core_cfg: core.Core = .{ .root = "core/root.zig", .platform = platform, .builtin_addons = compiled_in, .tracy = true }; const core_set = core.module(b, parts, core_cfg); const core_module = core_set.root; @@ -186,7 +186,6 @@ pub fn build(b: *std.Build) void { // and shell-test runs them all with one. const lean_cfg: core.Core = .{ .root = "core/root.zig", .platform = platform, .builtin_addons = compiled_in, .terminal = false }; const core_lean = core.module(b, parts, lean_cfg); - const isolated_mod: ?*std.Build.Module = if (platform == .tui) core.module(b, parts, .{ .root = "runtime/main.zig", .platform = platform, .builtin_addons = compiled_in, .isolated = true }).root else null; // `zig build static`'s: the addons compiled in (Addon.zig). Linked // statically on musl alone, as glibc links only dynamically; the gui's // is dynamic (SDL and the GPU need the display's libraries) with the @@ -270,17 +269,6 @@ pub fn build(b: *std.Build) void { static_step.dependOn(&b.addFail("static is the terminal's on musl (-Dplatform=tui -Dtarget=x86_64-linux-musl) or the gui's (-Dplatform=gui)").step); const run = b.addRunArtifact(exe); b.step("run", "descend to the pardes").dependOn(&run.step); - if (isolated_mod) |iso| { - const iso_exe = b.addExecutable(.{ - .name = "pardes-isolate", - .root_module = iso, - }); - const run_iso = b.addRunArtifact(iso_exe); - // Something to look at on arrival, read out of the binary itself. - run_iso.addPassthruArgs(); - const iso_step = b.step("run-isolated", "descend to a pardes with no host but the terminal"); - iso_step.dependOn(&run_iso.step); - } const addon_libs: []const *std.Build.Step.Compile = if (a.pdf.lib) |l| b.allocator.dupe(*std.Build.Step.Compile, &.{ a.image_addon, l }) catch @panic("OOM") else &.{a.image_addon}; // The scripted language server the tests' sessions and the tty cases ask. -- cgit v1.3 From 852299dd8d056b90446508757d8c6dacf8975abe Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 03:57:20 -0300 Subject: build: the `-Dtree-sitter=disabled` tier is deleted (pardes-build-2 item 2; P31's Q5, decided on the evidence 2026-11-20): a pardes with no tree-sitter at all, whose syntax.zig stubbed `ts` with four opaque types and answered every call empty; it did not compile on the base (`zig build install -Dplatform=tui -Dtree-sitter=disabled`: core/indent.zig:48 `struct 'syntax.ts__struct_3541' has no member named 'Tree'`, since indent.zig reads the parse tree the stub never had) and nobody set it; out with it: the `disabled` value of `TreeSitterGrammars`, build/core.zig's `treeSitter` made always (its module and its queries no longer optional, so `TreeSitter.wire` and `core.module` add them plainly), the stub `ts` and `ts_queries`, `syntax.enabled` and every branch that was for it: 32 guard lines and 16 terms of a condition in core/syntax.zig (its `start` and `stop` unwrapped), whole test guards in core/draw.zig, Stream.zig, File.zig, Query.zig, Core.zig (2), mouse.zig, replica.zig (2), test/output.zig (7), test/panes.zig (11) and test/behaviour/{panes,settings}.zig, a term in Stream.zig's two worker tests, `TreeContext.enabled` (builtins.zig) and the config table's condition on it (config.zig), test/output.zig's two conditions on the grammars; abi/wire.zig hashes the grammar set as the same four words (`true true` for highlighting and the Zig grammar, which every set has now), so `pardes version`'s semantics line did not move: 8b6382fddba7b382 on tui, gui, the bare build's two, both statics and FreeBSD, 0520895ac88d25ad with -Dtree-sitter=minimal, as at item 1; the manual's option table and the changelog say it; build.zig and build/ 2,981 -> 2,974 lines; every shape configures (11) and builds (tui, gui, bare, static musl and gui, FreeBSD, -Dtree-sitter=minimal, the macOS library for Linux and aarch64-macos); tui core-test 856/1006 (150 skipped), shell-test 1046/1049, pane-test 183/184, behaviour-test 275/275 and -- --replay 275/275, term-test 17/17, kitty-test 45/45, addon-test 5/5, lib-test, hxlib 538 cases 0 mismatches, process-test 0 of 144 failed (diffstack failed once and passed on the runner's own retry), gui unit-test 1643/1647 (the perf check's BaselineHarnessMismatch, the base's); load 5-26 on 16 cores Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- abi/wire.zig | 13 +++--- build.zig | 4 +- build/core.zig | 111 +++++++++++++++++++++----------------------- build/library.zig | 2 +- core/CHANGELOG.md | 4 +- core/Core.zig | 2 - core/File.zig | 1 - core/Query.zig | 1 - core/Stream.zig | 5 +- core/builtins.zig | 2 - core/config.zig | 2 +- core/draw.zig | 1 - core/mouse.zig | 1 - core/replica.zig | 2 - core/syntax.zig | 110 ++++++++++++++----------------------------- docs/typ/building.typ | 2 +- test/behaviour/panes.zig | 1 - test/behaviour/settings.zig | 2 - test/output.zig | 11 +---- test/panes.zig | 12 ----- 20 files changed, 105 insertions(+), 184 deletions(-) (limited to 'build.zig') diff --git a/abi/wire.zig b/abi/wire.zig index 1f7d02d2..4f6e199b 100644 --- a/abi/wire.zig +++ b/abi/wire.zig @@ -63,18 +63,19 @@ const family = switch (config.platform) { pub const semantics: u64 = blk: { @setEvalBranchQuota(1_000_000); // The grammar set as the four words it was hashed as when it was four - // options (highlighting, zig, minimal, full), so the hash stays. + // options (highlighting, zig, minimal, full), so the hash stays: + // highlighting and the Zig grammar are in every set. const g = config.grammars; // The two long ones (the dependencies, 2.5 KB, and the tape's shape, // 11 KB) joined on, not formatted: formatting them at comptime was 5 % // of a compile of the core. break :blk std.hash.Wyhash.hash(0, std.fmt.comptimePrint( - "wire {d}\npardes {s} {s}\n{s} grammars {} {} {} {} workspace_tag {} sources {}\nusize {d} {t}\n", + "wire {d}\npardes {s} {s}\n{s} grammars true true {} {} workspace_tag {} sources {}\nusize {d} {t}\n", .{ - version, build_info.version, build_info.commit orelse "unstamped", - family, g != .disabled, g != .disabled, - g == .minimal or g == .full, g == .full, config.workspace_tag, - config.embed_sources, @sizeOf(usize), builtin.cpu.arch.endian(), + version, build_info.version, build_info.commit orelse "unstamped", + family, g != .zig, g == .full, + config.workspace_tag, config.embed_sources, @sizeOf(usize), + builtin.cpu.arch.endian(), }, ) ++ build_info.deps ++ "\n" ++ pardes.tape.shape); }; diff --git a/build.zig b/build.zig index 5301b453..2a20f5b9 100644 --- a/build.zig +++ b/build.zig @@ -32,7 +32,7 @@ pub const PerfBuild = struct { pub const macos_min_version: std.SemanticVersion = .{ .major = 13, .minor = 0, .patch = 0 }; -pub const TreeSitterGrammars = enum { disabled, zig, minimal, full }; +pub const TreeSitterGrammars = enum { zig, minimal, full }; pub fn build(b: *std.Build) void { // A package that depends on pardes takes its library; nothing of the @@ -108,7 +108,7 @@ pub fn build(b: *std.Build) void { }), }); const c_optimize: std.lang.Optimize = if (optimize == .debug) .fast else optimize; - const tree_sitter_grammars = b.option(TreeSitterGrammars, "tree-sitter", "tree-sitter grammar set: disabled, zig, minimal (c/c++/zig), full") orelse .full; + const tree_sitter_grammars = b.option(TreeSitterGrammars, "tree-sitter", "tree-sitter grammar set: zig, minimal (c/c++/zig), full") orelse .full; const tracy = b.option([]const u8, "tracy", "enable Tracy profiling; supply the path to a Tracy source checkout"); const macos_sdk = b.option([]const u8, "macos-sdk", "the macOS SDK a gui built for macOS from another system takes SDL's headers and frameworks from"); const macos_identity = b.option([]const u8, "macos-identity", "codesigning identity for pardes.app (default: ad-hoc)") orelse "-"; diff --git a/build/core.zig b/build/core.zig index 4e619243..903b5602 100644 --- a/build/core.zig +++ b/build/core.zig @@ -145,15 +145,15 @@ pub fn generatedThemes(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarg } pub const TreeSitter = struct { - mod: ?*std.Build.Module, - queries: ?*std.Build.Step.Options, + mod: *std.Build.Module, + queries: *std.Build.Step.Options, libs: std.ArrayList(*std.Build.Step.Compile), /// tree-sitter, the grammars' parsers and their queries, into `m`. pub fn wire(ts: TreeSitter, m: *std.Build.Module) void { - if (ts.mod) |x| m.addImport("tree-sitter", x); + m.addImport("tree-sitter", ts.mod); for (ts.libs.items) |lib| m.linkLibrary(lib); - if (ts.queries) |x| m.addOptions("ts_queries", x); + m.addOptions("ts_queries", ts.queries); } }; @@ -162,61 +162,54 @@ pub const TreeSitter = struct { /// A grammar not fetched yet is asked for and left out: the configure runs /// again once it is there. pub fn treeSitter(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarget, c_optimize: std.lang.Optimize, tree_sitter_grammars: TreeSitterGrammars) TreeSitter { - var ts_mod: ?*std.Build.Module = null; - var ts_queries_opts: ?*std.Build.Step.Options = null; + const tree_sitter_mod = b.dependency("tree_sitter", .{ .target = target, .optimize = c_optimize }).module("tree_sitter"); var ts_libs: std.ArrayList(*std.Build.Step.Compile) = .empty; - if (tree_sitter_grammars != .disabled) { - const tree_sitter_mod = b.dependency("tree_sitter", .{ .target = target, .optimize = c_optimize }).module("tree_sitter"); - ts_mod = tree_sitter_mod; - - const ts_queries = b.addOptions(); - inline for (grammar_manifest.all) |g| { - if (tree_sitter_grammars == .full or - (tree_sitter_grammars == .minimal and g.tier != .full) or - (tree_sitter_grammars == .zig and g.tier == .zig)) - grammar: { - const dep = b.dependencyLazy(g.dep, .{}) catch break :grammar; - const upstream = configureRead(b, io, dep.builder.root, g.query, 0x100000) catch @panic("read " ++ g.name ++ " highlights"); - // the patterns that colour, filtered here once and not in - // every compile, and the whole query, the filter's test's - const query = std.mem.concat(b.allocator, u8, &.{ upstream, g.highlights_extra }) catch @panic("OOM"); - ts_queries.addOption([]const u8, g.name ++ "_highlights_unfiltered", query); - ts_queries.addOption([]const u8, g.name ++ "_highlights", grammar_manifest.colorQuery(query, b.allocator.alloc(u8, query.len) catch @panic("OOM"))); - // helix's textobject queries, vendored (vendor/queries); a - // grammar without one gets none - const objects = configureRead(b, io, b.root, "vendor/queries/" ++ g.name ++ "/textobjects.scm", 0x100000) catch ""; - ts_queries.addOption([]const u8, g.name ++ "_textobjects", objects); - // and their indent queries, beside them - const indents = configureRead(b, io, b.root, "vendor/queries/" ++ g.name ++ "/indents.scm", 0x100000) catch ""; - ts_queries.addOption([]const u8, g.name ++ "_indents", indents); - - const lib = b.addLibrary(.{ - .name = "tree-sitter-" ++ g.name, - .root_module = b.createModule(.{ .target = target, .optimize = c_optimize, .link_libc = true }), - .linkage = .static, - }); - // Every allocation a grammar's external scanner makes goes to the - // runtime's allocator, which syntax.zig points at a Zig one: - // TREE_SITTER_REUSE_ALLOCATOR for the scanners that use - // ts_malloc, and core/tree_sitter_heap.h for the ones that call - // malloc themselves. - const ts_cflags = [_][]const u8{ - "-fno-sanitize=function", - "-DTREE_SITTER_REUSE_ALLOCATOR", - // From the compiler's working directory, the root - // package's: pardes's own, or the one that depends on it. - "-include", - b.root.joinString(b.allocator, "core/tree_sitter_heap.h") catch @panic("OOM"), - }; - lib.root_module.addCSourceFile(.{ .file = dep.path(g.src ++ "/parser.c"), .flags = &ts_cflags }); - if (g.scanner) lib.root_module.addCSourceFile(.{ .file = dep.path(g.src ++ "/scanner.c"), .flags = &ts_cflags }); - lib.root_module.addIncludePath(dep.path(g.src)); - ts_libs.append(b.allocator, lib) catch @panic("OOM"); - } + const ts_queries = b.addOptions(); + inline for (grammar_manifest.all) |g| { + if (tree_sitter_grammars == .full or + (tree_sitter_grammars == .minimal and g.tier != .full) or + (tree_sitter_grammars == .zig and g.tier == .zig)) + grammar: { + const dep = b.dependencyLazy(g.dep, .{}) catch break :grammar; + const upstream = configureRead(b, io, dep.builder.root, g.query, 0x100000) catch @panic("read " ++ g.name ++ " highlights"); + // the patterns that colour, filtered here once and not in + // every compile, and the whole query, the filter's test's + const query = std.mem.concat(b.allocator, u8, &.{ upstream, g.highlights_extra }) catch @panic("OOM"); + ts_queries.addOption([]const u8, g.name ++ "_highlights_unfiltered", query); + ts_queries.addOption([]const u8, g.name ++ "_highlights", grammar_manifest.colorQuery(query, b.allocator.alloc(u8, query.len) catch @panic("OOM"))); + // helix's textobject queries, vendored (vendor/queries); a + // grammar without one gets none + const objects = configureRead(b, io, b.root, "vendor/queries/" ++ g.name ++ "/textobjects.scm", 0x100000) catch ""; + ts_queries.addOption([]const u8, g.name ++ "_textobjects", objects); + // and their indent queries, beside them + const indents = configureRead(b, io, b.root, "vendor/queries/" ++ g.name ++ "/indents.scm", 0x100000) catch ""; + ts_queries.addOption([]const u8, g.name ++ "_indents", indents); + + const lib = b.addLibrary(.{ + .name = "tree-sitter-" ++ g.name, + .root_module = b.createModule(.{ .target = target, .optimize = c_optimize, .link_libc = true }), + .linkage = .static, + }); + // Every allocation a grammar's external scanner makes goes to the + // runtime's allocator, which syntax.zig points at a Zig one: + // TREE_SITTER_REUSE_ALLOCATOR for the scanners that use + // ts_malloc, and core/tree_sitter_heap.h for the ones that call + // malloc themselves. + const ts_cflags = [_][]const u8{ + "-fno-sanitize=function", + "-DTREE_SITTER_REUSE_ALLOCATOR", + // From the compiler's working directory, the root + // package's: pardes's own, or the one that depends on it. + "-include", + b.root.joinString(b.allocator, "core/tree_sitter_heap.h") catch @panic("OOM"), + }; + lib.root_module.addCSourceFile(.{ .file = dep.path(g.src ++ "/parser.c"), .flags = &ts_cflags }); + if (g.scanner) lib.root_module.addCSourceFile(.{ .file = dep.path(g.src ++ "/scanner.c"), .flags = &ts_cflags }); + lib.root_module.addIncludePath(dep.path(g.src)); + ts_libs.append(b.allocator, lib) catch @panic("OOM"); } - ts_queries_opts = ts_queries; } - return .{ .mod = ts_mod, .queries = ts_queries_opts, .libs = ts_libs }; + return .{ .mod = tree_sitter_mod, .queries = ts_queries, .libs = ts_libs }; } /// uucode with pardes's configuration (uucode_config.zig), which ghostty @@ -365,7 +358,7 @@ pub fn module(b: *std.Build, p: Parts, c: Core) Modules { const root = if (!in_runtime) m else if (std.mem.eql(u8, c.root, "runtime/root.zig")) runtime else b.createModule(.{ .target = p.target, .optimize = p.optimize, .root_source_file = b.path(c.root), .link_libc = true }); // Each options module once, so that the modules share it: a file is in // one module of a compilation. - const ts_queries = if (p.ts.queries) |q| q.createModule() else null; + const ts_queries = p.ts.queries.createModule(); const doc_typ = p.doc_typ.createModule(); const pardes_options = options(b, p.cfg, c, p.pdf != null).createModule(); const build_info = p.info.createModule(); @@ -377,8 +370,8 @@ pub fn module(b: *std.Build, p: Parts, c: Core) Modules { x.addImport("plain", p.plain); x.addImport("pardes_addon", p.kind); x.addImport("generated_themes", p.themes); - if (p.ts.mod) |ts| x.addImport("tree-sitter", ts); - if (ts_queries) |q| x.addImport("ts_queries", q); + x.addImport("tree-sitter", p.ts.mod); + x.addImport("ts_queries", ts_queries); x.addImport("mvzr", p.mvzr); x.addImport("c_heap", p.c_heap); x.addImport("vaxis", p.vaxis); diff --git a/build/library.zig b/build/library.zig index 8a23debc..cabb175b 100644 --- a/build/library.zig +++ b/build/library.zig @@ -12,7 +12,7 @@ const patches = @import("patches.zig"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const tree_sitter_grammars = b.option(TreeSitterGrammars, "tree-sitter", "tree-sitter grammar set: disabled, zig, minimal (c/c++/zig), full") orelse .full; + const tree_sitter_grammars = b.option(TreeSitterGrammars, "tree-sitter", "tree-sitter grammar set: zig, minimal (c/c++/zig), full") orelse .full; var threaded: std.Io.Threaded = .init(b.allocator, .{}); defer threaded.deinit(); const io = threaded.io(); diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 477e3461..b1875877 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -572,7 +572,9 @@ - `zig build run-isolated` and its `pardes-isolate` are gone: the terminal's pardes with no host but the terminal and no filesystem had - not compiled for some releases, and nothing ran it. + not compiled for some releases, and nothing ran it. So is + `-Dtree-sitter=disabled`, a pardes with no tree-sitter, which did not + compile either: every build has the runtime and the Zig grammar. ## 0.24 diff --git a/core/Core.zig b/core/Core.zig index 9a542940..17ad37bc 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -2502,7 +2502,6 @@ test "LocationsConfig command reports partial updates and survives restore" { } test "TreeContext rows in the tag style keep the message rows" { - if (comptime !syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 16 }); defer p.deinit(); const pane = try p.setTestFile("pub const Box = struct {\n pub fn run() void {\n" ++ (std.mem.asBytes(&@as([30][16]u8, @splat(" // body\n".*)))) ++ " }\n};\n"); @@ -2545,7 +2544,6 @@ test "TreeContext rows in the tag style keep the message rows" { } test "TreeContext command and custom tag survive a workspace round trip" { - if (comptime !syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); const pane = try p.setTestFile("pub fn main() void {}\n"); diff --git a/core/File.zig b/core/File.zig index 75d8049b..f768e595 100644 --- a/core/File.zig +++ b/core/File.zig @@ -2312,7 +2312,6 @@ test "a pager's text is a diff when it holds a diff header or a unified hunk" { } test "a long line is painted only around the caret" { - if (!syntax.enabled) return; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const line = try std.testing.allocator.alloc(u8, 600 << 10); diff --git a/core/Query.zig b/core/Query.zig index e06199c5..ebb44155 100644 --- a/core/Query.zig +++ b/core/Query.zig @@ -2658,7 +2658,6 @@ test "the +Preview: the query's hits in its file are its places, the row's one c sel = pv.body.primaryRange(pv.file.?.content, 0); try std.testing.expectEqual(expected.items[2].start, @min(sel.anchor, sel.head)); // its colours the file's, as its own pane paints them - if (!syntax.enabled) return; var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); _ = try p.render(frame.allocator()); diff --git a/core/Stream.zig b/core/Stream.zig index e5825e9d..2e3016f4 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -1304,7 +1304,6 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop, } test "Stream: a scroll's colours are painted ahead on a worker, so PageDown after PageDown parses nothing on the frame" { - if (comptime !syntax.enabled) return; const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); defer p.deinit(); var text: std.ArrayList(u8) = .empty; @@ -1395,7 +1394,7 @@ test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's } test "Stream: a Grep's rows recoloured are made again, never written, and a dump of them dropped by an edit loads to its digest" { - if (!syntax.enabled or !syntax.readyApart("c.zig")) return; + if (!syntax.readyApart("c.zig")) return; var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); // Rows enough that a digest keeps a word for them (walk's blob_min). @@ -1466,7 +1465,7 @@ test "Stream: a Grep's rows recoloured are made again, never written, and a dump } test "Stream: a Grep's rows are coloured as their file's language colours them, the file painted on a worker, the match tint on top" { - if (!syntax.enabled or !syntax.readyApart("c.zig")) return; + if (!syntax.readyApart("c.zig")) return; var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); const text = "const std = @import(\"std\");\npub fn value() u32 {\n return 42; // value\n}\n"; diff --git a/core/builtins.zig b/core/builtins.zig index 1a0b4ce7..43395e17 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -700,11 +700,9 @@ pub const Fonts = struct { /// Show the declarations enclosing the top row at the pane's head, on or off. pub const TreeContext = struct { - pub const enabled = pardes.syntax.enabled; pub const takes_arg = true; pub fn run(c: Ctx) void { - if (comptime !enabled) unreachable; if (!panes.File.supportsContext(c.pane)) return c.p.reportFailure(c.id, "TreeContext: only a source file the parser knows has context"); const file = &c.pane.file.?; const arg = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); diff --git a/core/config.zig b/core/config.zig index 3dbfe566..f43604ad 100644 --- a/core/config.zig +++ b/core/config.zig @@ -159,7 +159,7 @@ pub const leader_path = paths: { if (builtins.capabilities.pixels) table.set(.GripWidth, null); table.set(.InactiveDim, null); if (builtins.capabilities.pixels) table.set(.Pet, null); - if (builtins.TreeContext.enabled) table.set(.TreeContext, null); + table.set(.TreeContext, null); table.set(.ThemeFile, null); table.set(.DumpThemes, null); table.set(.Mount, null); diff --git a/core/draw.zig b/core/draw.zig index 5ff31af6..b87eb56f 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -1295,7 +1295,6 @@ test "a wide glyph cut by a tag's or a notice's edge is joined into the grid as } test "a body with context rows is painted once, and the grid's rows are what a plain paint of it gives" { - if (comptime !@import("syntax.zig").enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 16 }); defer p.deinit(); const pane = try p.setTestFile("pub const Box = struct {\n pub fn run() void {\n" ++ (std.mem.asBytes(&@as([30][21]u8, @splat(" // body line\n".*)))) ++ " }\n};\n"); diff --git a/core/mouse.zig b/core/mouse.zig index 1882b691..465d8a01 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -1819,7 +1819,6 @@ test "a press on a notice's chip is the same state whichever frame this process // The same through a body hit (edit.pointerTextSelection, a sweep's drag), // on a compact body layer (TreeContext), which a pixel shell inverts. test "a body press, drag and release are the same state whichever frame this process drew last" { - if (comptime !pardes.syntax.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); defer a.deinit(); diff --git a/core/replica.zig b/core/replica.zig index a52061f4..9b87548d 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -4955,7 +4955,6 @@ test "a client joined from a dump moves through another view's pane as the sessi } test "a client joined from a dump makes a `/` query's rows as the session does, though only the session has drawn its owner" { - if (comptime !pardes.syntax.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); defer a.deinit(); @@ -6493,7 +6492,6 @@ test "a client joined from a dump makes a `/` query's rows as the session does, } } }; - if (comptime !pardes.syntax.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, .{ .vtable = &WorkerSearch.vtable }, false); defer a.deinit(); diff --git a/core/syntax.zig b/core/syntax.zig index ba3851ab..1147b6b9 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 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,7 @@ const Spec = struct { fn grammarSelected(comptime g: grammar_manifest.Grammar) bool { return switch (g.tier) { - .zig => enabled, + .zig => true, .minimal => minimal_grammars, .full => full_grammars, }; @@ -92,28 +86,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 +244,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 +258,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 +266,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 +322,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 +331,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 +342,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 +371,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 +383,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 +394,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 +479,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 +544,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 +567,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 +607,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 +631,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 +774,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 +806,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 +1088,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 +1246,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 +1261,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 +1292,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 +1305,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(); @@ -1352,7 +1326,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 +1342,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 and !full_grammars) return; const gpa = std.testing.allocator; start(gpa); defer stop(); @@ -1393,7 +1367,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 +1383,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 +1405,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 +1432,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 +1455,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 +1499,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 +1531,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 +1578,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 +1620,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 +1660,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 +1681,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 +1700,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 +1716,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 +1748,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 +1769,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 +1798,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 +1827,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 +1857,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 +1874,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 +1889,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 and !full_grammars) return; const gpa = std.testing.allocator; start(gpa); defer stop(); @@ -1953,7 +1915,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 +1934,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 and !full_grammars) return; const gpa = std.testing.allocator; start(gpa); defer stop(); @@ -1998,7 +1959,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 +2019,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 +2088,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 +2127,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(); diff --git a/docs/typ/building.typ b/docs/typ/building.typ index 9350b484..901f2ca2 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -60,7 +60,7 @@ must share with its session; `pardes --help` lists every flag. [`-Dplatform`], [`tui`, `gui`, `macos`; absent: the tui and SDL shells together, installed into `~/.local`], [`-Dpdf`], [bool: build the pdf addon, fetching MuPDF the first time; default on once MuPDF is fetched, and off Linux], [`-Djpx`], [bool, `true`: JPEG 2000, and with it scanned PDFs], - [`-Dtree-sitter`], [`disabled`, `zig`, `minimal`, `full`; `full`], + [`-Dtree-sitter`], [`zig`, `minimal`, `full`; `full`], [`-Dembed-sources`], [bool, `false`: serve the sources under #file("/src")], [`-Dhome-install`], [bool: a bare `zig build` also copies the binaries into `~/.local/bin` and the addon steps theirs into `$XDG_DATA_HOME/pardes/addons`, default true; with `-Dplatform` only `true` copies, and the addons alone], [`-Dstamp-commit`], [bool: the git commit in `--version` and crash records; on for release builds and the `~/.local` install], diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index dcde9c4d..4d4c812c 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -888,7 +888,6 @@ test "panes: truncating body empties it, and a stat says the length after" { } test "panes: a rename colours a source its new name's language names, its text unchanged" { - if (@import("pardes_options").grammars == .disabled) return error.SkipZigTest; var s = try Session.initFile(std.testing.allocator, "fn check() void {}\n"); defer s.deinit(); const n = try s.focused(); diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index cacee701..a6e09f35 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -158,7 +158,6 @@ test "settings: Back and Forward written to exec keep to the focused file" { }); } -const syntax = @import("pardes_options").grammars != .disabled; /// The style /screen draws the cell `into` cells into the first place /// `text` is drawn. @@ -190,7 +189,6 @@ fn keywordsIn(s: *Session, want: []const u8) !usize { } test "settings: keywords stay coloured in every row a pane grows into" { - if (!syntax) return error.SkipZigTest; var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][33]u8, @splat("pub fn f() void { if (true) {} }\n".*)))); defer s.deinit(); try s.write(s.at("/pane/{d}/name", .{try s.focused()}), "/resize.zig\n"); diff --git a/test/output.zig b/test/output.zig index c5ddddd5..77de7649 100644 --- a/test/output.zig +++ b/test/output.zig @@ -138,7 +138,7 @@ test "location producers retain rendered location match and diagnostic styles" { } else if (origin == .search or origin == .cmd) { try std.testing.expectEqual(pardes.Color{ .rgb = chrome.search_bg }, first.style.bg); } - if (!diagnostic and pardes.syntax.enabled) { + if (!diagnostic) { const number = start + std.mem.indexOf(u8, code, "42").?; try std.testing.expectEqual(pardes.Color{ .rgb = p.theme().num }, outputCell(p, surface, id, number).style.fg); } @@ -428,7 +428,6 @@ test "Mini dump preserves its exact virtual snapshot and body writes detach colo } test "Mini syntax colors survive themes and rendering without source access" { - if (!pardes.syntax.enabled) return error.SkipZigTest; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "demo.zig", .data = "pub fn main() void {\n if (true) return;\n}\n" }); @@ -619,7 +618,6 @@ fn selectedOutputText(pane: *const pardes.Pane) ?[]const u8 { } test "LookNext/LookPrev skips formatted context and selects comment match locations" { - if (!pardes.syntax.enabled) return error.SkipZigTest; for ([_]panes.Output.Origin{ .search, .{ .query = .references } }) |origin| { for ([_]bool{ false, true }) |tslocations| { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); @@ -786,7 +784,7 @@ test "identical result refresh keeps warm content caches without allocation" { const index = try panes.File.lineIndex(p.gpa, file); const styles = try std.testing.allocator.dupe(u8, file.highlights); defer std.testing.allocator.free(styles); - if (@import("pardes_options").grammars != .disabled) try std.testing.expect(styles.len > 0); + try std.testing.expect(styles.len > 0); const revision = file.revision; const saved_revision = file.saved_revision; const body_ptr = file.content.ptr; @@ -1048,7 +1046,6 @@ test "result alignment chunks assign boundary context to nearest match with prev } test "declaration context ellipses mark only final header lines before omitted source" { - if (!pardes.syntax.enabled) return error.SkipZigTest; const text = "const Outer = struct {\n" ++ "\n" ++ " pub fn run(\n" ++ @@ -1161,7 +1158,6 @@ test "LSP occurrence tint follows the supplied source span through context" { } test "result context upper bound stops at earliest enclosing declaration" { - if (!pardes.syntax.enabled) return error.SkipZigTest; const text = "// unrelated prelude\n" ++ "const unrelated = 1;\n" ++ "\n" ++ @@ -1227,7 +1223,6 @@ test "result context upper bound stops at earliest enclosing declaration" { } test "stacked search and LSP results retain source tint navigation and dump ownership" { - if (!pardes.syntax.enabled) return error.SkipZigTest; const first = "\tfoo + 界 + foo;"; const second = " return foo;"; const source_text = "const Box = struct {\n fn run() void {\n" ++ first ++ "\n const neighbor = 42;\n" ++ second ++ "\n }\n};\n"; @@ -1348,7 +1343,6 @@ test "stacked search and LSP results retain source tint navigation and dump owne } test "location analysis cache refreshes unsaved ancestry and query context" { - if (!pardes.syntax.enabled) return error.SkipZigTest; const before = "const Outer = struct {\n" ++ " fn first() void {\n" ++ " marker();\n" ++ @@ -1429,7 +1423,6 @@ test "location analysis cache refreshes unsaved ancestry and query context" { } test "location cache eviction retains early colors and honors disk and unsaved replacements" { - if (!pardes.syntax.enabled) return error.SkipZigTest; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var dir_buf: [4096]u8 = undefined; diff --git a/test/panes.zig b/test/panes.zig index dbdbec2f..6bda3519 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -152,7 +152,6 @@ const BodyHitTests = struct { } test "compact TreeContext mouse follows source rows in reclaimed space" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 64, .rows = 18 }); defer p.deinit(); p.view().row_metrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 8, .tagline_h = 10 }; @@ -201,7 +200,6 @@ const BodyHitTests = struct { const LineNumberLookTests = struct { test "right-click on a line number looks at that line, sticky context headers included" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 64, .rows = 18 }); defer p.deinit(); p.view().row_metrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 8, .tagline_h = 10 }; @@ -259,7 +257,6 @@ const TreeContextTests = struct { } test "TreeContext separators mark source gaps without splitting multiline signatures" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 64, .rows = 18 }); defer p.deinit(); const pane = try setZigFile(p, "const Box = struct {\n\n // omitted member comment\n\n pub fn work(\n value: usize,\n ) void {\n" ++ std.mem.asBytes(&@as([30][16]u8, @splat(" // body\n".*))) ++ " }\n};\n"); @@ -291,7 +288,6 @@ const TreeContextTests = struct { test "TreeContext Look to an open file discards the old viewport extent" { for ([_]bool{ false, true }) |context| { - if (context and !syntax.enabled) continue; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 64, .rows = 18 }); defer p.deinit(); const pane = try setZigFile(p, "pub fn work() void {\n" ++ std.mem.asBytes(&@as([100][12]u8, @splat(" // body\n".*))) ++ "}\n"); @@ -316,7 +312,6 @@ const TreeContextTests = struct { } test "TreeContext sticky declarations preserve the body while clicked and edited" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 64, .rows = 18 }); defer p.deinit(); const pane = try setZigFile(p, source); @@ -368,7 +363,6 @@ const TreeContextTests = struct { } test "TreeContext unwrapped headers map Unicode and survive source replacement" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 48, .rows = 14 }); defer p.deinit(); const pane = try setZigFile(p, "pub fn @\"界name\"(\n\tvalue: usize,\n) void {\n" ++ std.mem.asBytes(&@as([12][15]u8, @splat(" _ = value;\n".*))) ++ "}\n"); @@ -407,7 +401,6 @@ const TreeContextTests = struct { } test "TreeContext clipped headers leave wrapped body cursor coordinates intact" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 24, .rows = 12 }); defer p.deinit(); const body = " // 012345678901234567890123456789界tail"; @@ -1717,7 +1710,6 @@ const OutputTests = struct { } test "Callstack aligns source below locations and keeps exact emphasis and navigation" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 30 }); defer p.deinit(); const source = " pub fn caller(\n value: u32,\n ) void {\n work(value);\n }\n"; @@ -1759,7 +1751,6 @@ const OutputTests = struct { } test "Callstack signatures share unsaved tree context and keep guides out of Look selections" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 110, .rows = 30 }); defer p.deinit(); const source = "const Box = struct {\n" ++ @@ -1853,7 +1844,6 @@ const OutputTests = struct { } test "Locations context uses unsaved sources and navigation visits only matches" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); p.locations_config.layout = .@"inline"; @@ -1968,7 +1958,6 @@ const OutputTests = struct { } test "Locations visible declarations are muted and stars count scopes rather than signature lines" { - if (!syntax.enabled) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); defer p.deinit(); p.locations_config.layout = .@"inline"; @@ -3541,7 +3530,6 @@ test "cancelled normal gestures leave no count or subprefix through raw tty mode } test "Zig keywords keep every rendered byte colored across line-number widths" { - if (!syntax.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; const samples = [_][]const u8{ "pub fn main() void { if (true) {} }\n", -- cgit v1.3 From fc9e2eaf5ad86719a3bc57d18ce4ad302461dfbd Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 04:36:44 -0300 Subject: build: the grammar set is one option, `grammars`, with `none | minimal | full` (pardes-build-2 item 3; the start design's step 3, P31's lean): for a package that depends on pardes `.grammars = .minimal` where it wrote `.@"tree-sitter" = .minimal`, and for pardes's own products `-Dgrammars=minimal` where `-Dtree-sitter=minimal` was (one option, not two spellings of one: no live gate script passes -Dtree-sitter, only two configure-matrix scripts of an earlier agent's and the first build pass's shapes script, which also name test/lib and the deleted `disabled` and change anyway; the three consumers' lines change for `.grammars` anyway); `none` is tree-sitter's runtime with no grammar, for a program that hands in its own at run time (sketchbook's examples/live, which set `zig` only because it was the smallest set that kept syntax on: `zig`, its one setter, goes, and the Zig grammar is a minimal one: grammar_manifest's tiers are minimal and full); syntax.zig's sets read `none` (`minimal_grammars` is `!= .none`, four tests' doubled condition one), the Build pane prints the set's own name (`none` was what it printed for the zig set), and abi/wire.zig hashes the set as the four words it was hashed as, so a minimal or a full set's semantics did not move (8b6382fddba7b382 full, 0520895ac88d25ad minimal, as at items 1 and 2) and `none` hashes as highlighting with no grammar (794d23e0255281f8; the old zig set's hash is gone with it); test/lib asks with `.grammars`; the manual's table, its library section's two lines and the changelog say it; build.zig and build/ 2,974 -> 2,971 lines; -Dgrammars=full, minimal and none each install and pass lib-test, and the old spelling is refused (`invalid option: "tree-sitter"`); the suites run on the stack's top (item 5) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- abi/wire.zig | 14 +++++++------- build.zig | 4 ++-- build/core.zig | 5 +---- build/library.zig | 4 ++-- core/CHANGELOG.md | 8 ++++++-- core/Output.zig | 2 +- core/grammar_manifest.zig | 4 ++-- core/lib_syntax.zig | 2 +- core/syntax.zig | 11 +++++------ docs/typ/building.typ | 10 +++++----- test/lib/build.zig | 2 +- 11 files changed, 33 insertions(+), 33 deletions(-) (limited to 'build.zig') diff --git a/abi/wire.zig b/abi/wire.zig index 4f6e199b..a36e746b 100644 --- a/abi/wire.zig +++ b/abi/wire.zig @@ -63,19 +63,19 @@ const family = switch (config.platform) { pub const semantics: u64 = blk: { @setEvalBranchQuota(1_000_000); // The grammar set as the four words it was hashed as when it was four - // options (highlighting, zig, minimal, full), so the hash stays: - // highlighting and the Zig grammar are in every set. + // options (highlighting, zig, minimal, full), so the hash of a minimal + // or a full set stays: every set has highlighting, `none` no grammar. const g = config.grammars; // The two long ones (the dependencies, 2.5 KB, and the tape's shape, // 11 KB) joined on, not formatted: formatting them at comptime was 5 % // of a compile of the core. break :blk std.hash.Wyhash.hash(0, std.fmt.comptimePrint( - "wire {d}\npardes {s} {s}\n{s} grammars true true {} {} workspace_tag {} sources {}\nusize {d} {t}\n", + "wire {d}\npardes {s} {s}\n{s} grammars true {} {} {} workspace_tag {} sources {}\nusize {d} {t}\n", .{ - version, build_info.version, build_info.commit orelse "unstamped", - family, g != .zig, g == .full, - config.workspace_tag, config.embed_sources, @sizeOf(usize), - builtin.cpu.arch.endian(), + version, build_info.version, build_info.commit orelse "unstamped", + family, g != .none, g != .none, + g == .full, config.workspace_tag, config.embed_sources, + @sizeOf(usize), builtin.cpu.arch.endian(), }, ) ++ build_info.deps ++ "\n" ++ pardes.tape.shape); }; diff --git a/build.zig b/build.zig index 2a20f5b9..e31464f7 100644 --- a/build.zig +++ b/build.zig @@ -32,7 +32,7 @@ pub const PerfBuild = struct { pub const macos_min_version: std.SemanticVersion = .{ .major = 13, .minor = 0, .patch = 0 }; -pub const TreeSitterGrammars = enum { zig, minimal, full }; +pub const TreeSitterGrammars = enum { none, minimal, full }; pub fn build(b: *std.Build) void { // A package that depends on pardes takes its library; nothing of the @@ -108,7 +108,7 @@ pub fn build(b: *std.Build) void { }), }); const c_optimize: std.lang.Optimize = if (optimize == .debug) .fast else optimize; - const tree_sitter_grammars = b.option(TreeSitterGrammars, "tree-sitter", "tree-sitter grammar set: zig, minimal (c/c++/zig), full") orelse .full; + const tree_sitter_grammars = b.option(TreeSitterGrammars, "grammars", "the tree-sitter grammars compiled in: none (the runtime alone), minimal (C, C++ and Zig) or full (all of them; the default)") orelse .full; const tracy = b.option([]const u8, "tracy", "enable Tracy profiling; supply the path to a Tracy source checkout"); const macos_sdk = b.option([]const u8, "macos-sdk", "the macOS SDK a gui built for macOS from another system takes SDL's headers and frameworks from"); const macos_identity = b.option([]const u8, "macos-identity", "codesigning identity for pardes.app (default: ad-hoc)") orelse "-"; diff --git a/build/core.zig b/build/core.zig index 903b5602..b668211d 100644 --- a/build/core.zig +++ b/build/core.zig @@ -166,10 +166,7 @@ pub fn treeSitter(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarget, c var ts_libs: std.ArrayList(*std.Build.Step.Compile) = .empty; const ts_queries = b.addOptions(); inline for (grammar_manifest.all) |g| { - if (tree_sitter_grammars == .full or - (tree_sitter_grammars == .minimal and g.tier != .full) or - (tree_sitter_grammars == .zig and g.tier == .zig)) - grammar: { + if (tree_sitter_grammars == .full or (tree_sitter_grammars == .minimal and g.tier == .minimal)) grammar: { const dep = b.dependencyLazy(g.dep, .{}) catch break :grammar; const upstream = configureRead(b, io, dep.builder.root, g.query, 0x100000) catch @panic("read " ++ g.name ++ " highlights"); // the patterns that colour, filtered here once and not in diff --git a/build/library.zig b/build/library.zig index cabb175b..9a1bfb67 100644 --- a/build/library.zig +++ b/build/library.zig @@ -6,13 +6,13 @@ const patches = @import("patches.zig"); /// pardes as a library, all a package that depends on it is given: the /// `pardes-syntax` module (core/lib_syntax.zig), the grammars, highlighting /// and themes for a program that draws source text itself. It takes -/// tree-sitter and the grammars of its `tree-sitter` option and no other +/// tree-sitter and the grammars of its `grammars` option and no other /// dependency, so nothing else is fetched (build.zig.zon). test/lib is such /// a package, which `lib-test` builds. pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const tree_sitter_grammars = b.option(TreeSitterGrammars, "tree-sitter", "tree-sitter grammar set: zig, minimal (c/c++/zig), full") orelse .full; + const tree_sitter_grammars = b.option(TreeSitterGrammars, "grammars", "the tree-sitter grammars compiled in: none (the runtime alone), minimal (C, C++ and Zig) or full (all of them; the default)") orelse .full; var threaded: std.Io.Threaded = .init(b.allocator, .{}); defer threaded.deinit(); const io = threaded.io(); diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index b1875877..813f24e6 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -212,7 +212,7 @@ grammars, highlighting, declarations, syntax tree and themes as the editor has them, for a program that draws source text itself. Every dependency but tree-sitter's runtime is lazy in the manifest, so such a - package fetches the runtime and the grammars of its `tree-sitter` set + package fetches the runtime and the grammars of its `grammars` set and nothing else; `zig build lib-test`, which `unit-test` runs, builds one (`test/lib`). The markdown grammar is pinned by its commit: its branch has moved, and a fresh checkout could not fetch it. @@ -574,7 +574,11 @@ terminal's pardes with no host but the terminal and no filesystem had not compiled for some releases, and nothing ran it. So is `-Dtree-sitter=disabled`, a pardes with no tree-sitter, which did not - compile either: every build has the runtime and the Zig grammar. + compile either; every build has tree-sitter's runtime. The grammar set + is `-Dgrammars` (`.grammars` for a package that depends on pardes, no + longer `.@"tree-sitter"`): `none`, the runtime alone, where a program + hands in a grammar of its own at run time, `minimal` or `full`; the + `zig` set, which `none` replaces, is gone. ## 0.24 diff --git a/core/Output.zig b/core/Output.zig index 759bada9..c7bfde14 100644 --- a/core/Output.zig +++ b/core/Output.zig @@ -907,7 +907,7 @@ pub fn openBuild(p: *Pardes, id: usize) !void { @tagName(builtin.abi), @tagName(build_options.platform), }); try w.print("grammars {s}\naddons {s}\nsemantics {x:0>16}\n\ndependencies\n{s}", .{ - if (build_options.grammars == .full) "full" else if (build_options.grammars == .minimal) "minimal" else "none", + @tagName(build_options.grammars), if (build_options.builtin_addons) "compiled in" else "loaded from /lib/pardes/addons", @import("wire").semantics, build_info.deps, diff --git a/core/grammar_manifest.zig b/core/grammar_manifest.zig index 711c31a4..b4ff30b9 100644 --- a/core/grammar_manifest.zig +++ b/core/grammar_manifest.zig @@ -5,7 +5,7 @@ const std = @import("std"); -pub const Tier = enum { zig, minimal, full }; +pub const Tier = enum { minimal, full }; pub const Grammar = struct { name: []const u8, @@ -58,7 +58,7 @@ pub const all = [_]Grammar{ \\"#" @keyword \\ }, - .{ .name = "zig", .dep = "ts_zig", .exts = &.{ ".zig", ".zon" }, .tier = .zig }, + .{ .name = "zig", .dep = "ts_zig", .exts = &.{ ".zig", ".zon" }, .tier = .minimal }, }; /// The names a language goes by besides its grammar's: a fence's `py`, diff --git a/core/lib_syntax.zig b/core/lib_syntax.zig index a25af884..f10f88a5 100644 --- a/core/lib_syntax.zig +++ b/core/lib_syntax.zig @@ -15,7 +15,7 @@ const colors = @import("colors.zig"); const modal = @import("modal.zig"); /// Every grammar pardes knows (`all`), by name or alias (`byName`) and by -/// path (`byPath`). The build's `tree-sitter` option says which of them are +/// path (`byPath`). The build's `grammars` option says which of them are /// compiled in, and `supportsPath` answers for this build. pub const grammars = @import("grammar_manifest.zig"); pub const supportsPath = syntax.supportsPath; diff --git a/core/syntax.zig b/core/syntax.zig index 1147b6b9..19831ec2 100644 --- a/core/syntax.zig +++ b/core/syntax.zig @@ -7,7 +7,7 @@ const regexp = @import("regexp.zig"); const c_heap = @import("c_heap"); const diff = @import("diff.zig"); -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 = @import("tree-sitter"); @@ -37,7 +37,6 @@ const Spec = struct { fn grammarSelected(comptime g: grammar_manifest.Grammar) bool { return switch (g.tier) { - .zig => true, .minimal => minimal_grammars, .full => full_grammars, }; @@ -1314,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); @@ -1342,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 (!minimal_grammars and !full_grammars) return; + if (!minimal_grammars) return; const gpa = std.testing.allocator; start(gpa); defer stop(); @@ -1889,7 +1888,7 @@ test "syntax allocator switching clears default-runtime caches" { } test "syntax location context snapshots retain omitted multiline comment scope" { - if (!minimal_grammars and !full_grammars) return; + if (!minimal_grammars) return; const gpa = std.testing.allocator; start(gpa); defer stop(); @@ -1934,7 +1933,7 @@ test "syntax declaration locations clear snapshots without muting ordinary conte } test "syntax stacked addresses do not split multiline source groups" { - if (!minimal_grammars and !full_grammars) return; + if (!minimal_grammars) return; const gpa = std.testing.allocator; start(gpa); defer stop(); diff --git a/docs/typ/building.typ b/docs/typ/building.typ index 901f2ca2..8403df9a 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -60,7 +60,7 @@ must share with its session; `pardes --help` lists every flag. [`-Dplatform`], [`tui`, `gui`, `macos`; absent: the tui and SDL shells together, installed into `~/.local`], [`-Dpdf`], [bool: build the pdf addon, fetching MuPDF the first time; default on once MuPDF is fetched, and off Linux], [`-Djpx`], [bool, `true`: JPEG 2000, and with it scanned PDFs], - [`-Dtree-sitter`], [`zig`, `minimal`, `full`; `full`], + [`-Dgrammars`], [the tree-sitter grammars compiled in: `none` (the runtime alone), `minimal` (C, C++ and Zig) or `full` (all of them; the default)], [`-Dembed-sources`], [bool, `false`: serve the sources under #file("/src")], [`-Dhome-install`], [bool: a bare `zig build` also copies the binaries into `~/.local/bin` and the addon steps theirs into `$XDG_DATA_HOME/pardes/addons`, default true; with `-Dplatform` only `true` copies, and the addons alone], [`-Dstamp-commit`], [bool: the git commit in `--version` and crash records; on for release builds and the `~/.local` install], @@ -104,7 +104,7 @@ it. const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, - .@"tree-sitter" = .minimal, + .grammars = .minimal, }); exe.root_module.addImport("pardes-syntax", pardes.module("pardes-syntax")); ``` @@ -122,8 +122,8 @@ for a language's name. [`start`, `stop`], [tree-sitter's allocator, and the end of the parsers kept per language], ) -The `tree-sitter` option is the build's own: `zig`, `minimal` (C, C++ -and Zig) or `full`, the default. Every dependency but tree-sitter's +The `grammars` option is the build's own: `none` (the runtime alone), +`minimal` (C, C++ and Zig) or `full` (all of them; the default). Every dependency but tree-sitter's runtime is lazy in the manifest, so the package fetches the runtime and its set's grammars and compiles those alone; pardes's own build asks for what it is missing in one round. Zig still unpacks a lazy package whose @@ -146,7 +146,7 @@ a package imports one of the two. const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, - .@"tree-sitter" = .minimal, + .grammars = .minimal, .editor = true, }); exe.root_module.addImport("pardes-editor", pardes.module("pardes-editor")); diff --git a/test/lib/build.zig b/test/lib/build.zig index 436517d4..171812e9 100644 --- a/test/lib/build.zig +++ b/test/lib/build.zig @@ -8,7 +8,7 @@ const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .@"tree-sitter" = .minimal, .editor = true }); + const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .minimal, .editor = true }); const step = b.step("test", "colour, outline and walk a C and a Zig source through pardes's library, and edit a text with its editor"); // A file belongs to one module: the syntax alone, and the editor (which // has it as `.syntax`), each a test of its own. -- cgit v1.3 From a3634ab3dbf2fabd997f137c47eafd7bc6c442b0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 04:37:18 -0300 Subject: build: build.zig reads like sketchbook's (pardes-build-2 item 4; the start design's 4.3 and 4.4): its head comment is a consumer's lines, both shapes (`pardes-syntax` with `.grammars = .minimal`, as computeranim's anim takes it; `pardes-editor` with `.grammars = .none, .editor = true` and a grammar of its own, as sketchbook's examples/live does) and what each option selects, and `build()` begins with what such a package may ask for, the mode and `grammars` (pardes's own build takes both too), then `if (!b.isRoot()) return library(...)` with the target and `editor`, which only a dependency declares: the root's target is not read there, since its default comes from -Dplatform (x86_64-linux-gnu.2.38, or the Mac's) and a package that passes none keeps the native one; build/library.zig is `library` at the foot of build.zig, the editor option read with the others and handed in (its body otherwise as it was); `-Deditor` stays a dependency's alone and the root's -Dgrammars help text is the head comment's words; lines: build.zig 361 -> 430 (26 of them the head comment), build/library.zig 49 -> 0, build.zig and build/ 2,971 -> 2,991; tui install, lib-test (the package still asks for `.grammars = .minimal, .editor = true`) and hxlib (538 cases, 0 mismatches) as before Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- build.zig | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++----- build/library.zig | 49 --------------------------------- 2 files changed, 75 insertions(+), 55 deletions(-) delete mode 100644 build/library.zig (limited to 'build.zig') diff --git a/build.zig b/build.zig index e31464f7..3498b331 100644 --- a/build.zig +++ b/build.zig @@ -1,3 +1,29 @@ +//! pardes: the editor, and the library it is for a program that draws +//! source text itself. `zig build -Dplatform=tui` (or `gui`, `macos`) +//! builds the editor; docs/typ/building.typ has the rest. +//! +//! A package that depends on pardes is given its syntax, `pardes-syntax` +//! (core/lib_syntax.zig): +//! +//! const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .minimal }); +//! exe.root_module.addImport("pardes-syntax", pardes.module("pardes-syntax")); +//! +//! and, when it asks, the text pane, `pardes-editor` (core/lib_editor.zig), +//! here with a grammar of its own handed in at run time: +//! +//! const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .none, .editor = true }); +//! exe.root_module.addImport("pardes-editor", pardes.module("pardes-editor")); +//! +//! `.grammars`: the tree-sitter grammars compiled in: `none` (the runtime +//! alone), `minimal` (C, C++ and Zig) or `full` (all of them; the default). +//! `.editor`: exports `pardes-editor` besides `pardes-syntax`, and with it +//! fetches uucode, mvzr and vaxis. +//! +//! Every dependency but tree-sitter's runtime is lazy in the manifest, so +//! such a package fetches what it asked for and nothing else. +//! `pardes-editor` has `pardes-syntax` in it as `.syntax`: a file belongs +//! to one module, so a package imports one of the two. test/lib is such a +//! package; `lib-test`, which `unit-test` runs, builds and runs it. const std = @import("std"); const builtin = @import("builtin"); const core = @import("build/core.zig"); @@ -8,6 +34,7 @@ const macos = @import("build/macos.zig"); const tools = @import("build/tools.zig"); const tests = @import("build/tests.zig"); const process_build = @import("build/process.zig"); +const patches = @import("build/patches.zig"); /// The package manifest, imported untyped (build/core.zig has the reason). const zon = @import("build.zig.zon"); @@ -35,9 +62,14 @@ pub const macos_min_version: std.SemanticVersion = .{ .major = 13, .minor = 0, . pub const TreeSitterGrammars = enum { none, minimal, full }; pub fn build(b: *std.Build) void { - // A package that depends on pardes takes its library; nothing of the - // editor is configured for it. - if (!b.isRoot()) return @import("build/library.zig").build(b); + // What a package that depends on pardes may ask for (the head comment). + // pardes's own build takes the mode and the grammars too, and its target + // below, whose default -Dplatform says. + const optimize = b.standardOptimizeOption(.{}); + const grammars = b.option(TreeSitterGrammars, "grammars", "the tree-sitter grammars compiled in: none (the runtime alone), minimal (C, C++ and Zig) or full (all of them; the default)") orelse .full; + if (!b.isRoot()) return library(b, b.standardTargetOptions(.{}), optimize, grammars, b.option(bool, "editor", "exports pardes-editor besides pardes-syntax, and with it fetches uucode, mvzr and vaxis") orelse false); + + // Everything below is pardes's own: the editor, its tests and tools. // This configure reads the environment (HOME, XDG_DATA_HOME, // HX_HARNESS), the clock (-Dtest-rebuild) and vendor/themes and // vendor/queries without telling the configure cache, so it is never @@ -79,7 +111,6 @@ pub fn build(b: *std.Build) void { }, }, }); - const optimize = b.standardOptimizeOption(.{}); // one substring, because that is what a dev loop needs: see the unit-test // step for why the whole-binary run is worth narrowing const test_filters: []const []const u8 = if (b.option([]const u8, "test-filter", "run only tests whose name contains this")) |f| @@ -108,7 +139,6 @@ pub fn build(b: *std.Build) void { }), }); const c_optimize: std.lang.Optimize = if (optimize == .debug) .fast else optimize; - const tree_sitter_grammars = b.option(TreeSitterGrammars, "grammars", "the tree-sitter grammars compiled in: none (the runtime alone), minimal (C, C++ and Zig) or full (all of them; the default)") orelse .full; const tracy = b.option([]const u8, "tracy", "enable Tracy profiling; supply the path to a Tracy source checkout"); const macos_sdk = b.option([]const u8, "macos-sdk", "the macOS SDK a gui built for macOS from another system takes SDL's headers and frameworks from"); const macos_identity = b.option([]const u8, "macos-identity", "codesigning identity for pardes.app (default: ad-hoc)") orelse "-"; @@ -141,7 +171,7 @@ pub fn build(b: *std.Build) void { const pdf_scroll_bench_step = b.step("pdf-scroll-bench", "benchmark fast continuous-strip PDF scrolling in ReleaseFast (-Doptimize=ReleaseFast -- [--json] [--reps N] [--warmup N] [--path FILE])"); const shell_cfg: core.ShellConfig = .{ - .tree_sitter_grammars = tree_sitter_grammars, + .tree_sitter_grammars = grammars, .tracy = tracy, .workspace_tag = workspace_tag, .embed_sources = embed_sources, @@ -359,3 +389,42 @@ fn installHome(b: *std.Build, artifact: *std.Build.Step.Compile, home_bin: ?[]co copy.has_side_effects = true; b.getInstallStep().dependOn(©.step); } + +/// What a package that depends on pardes is given (the head comment): +/// `pardes-syntax`, the grammars, highlighting and themes, over tree-sitter +/// and the grammars of its set alone; and with `editor`, `pardes-editor`, +/// whose uucode, mvzr and vaxis (with what its build names) are fetched +/// then and only then. Of vaxis it takes the grapheme width, which uucode +/// alone feeds. +fn library(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, grammars: TreeSitterGrammars, editor: bool) void { + var threaded: std.Io.Threaded = .init(b.allocator, .{}); + defer threaded.deinit(); + const io = threaded.io(); + const mod = b.addModule("pardes-syntax", .{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("core/lib_syntax.zig"), + .link_libc = true, + }); + // A library's options: its grammars, nothing stamped, no window. + const cfg: core.ShellConfig = .{ .tree_sitter_grammars = grammars }; + mod.addOptions("pardes_options", core.options(b, cfg, .{ .root = "core/lib_syntax.zig", .platform = .tui, .terminal = false }, false)); + const ts = core.treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, grammars); + const themes = core.generatedThemes(b, io, target, optimize); + mod.addImport("generated_themes", themes); + ts.wire(mod); + mod.addImport("c_heap", b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("core/c_heap.zig") })); + if (!editor) return; + const uucode = core.uucodeModule(b, target, optimize) orelse return; + const mvzr = patches.mvzr(b, patches.tool(b), b.dependencyLazy("mvzr", .{ .target = target, .optimize = optimize }) catch return, target, optimize); + const vaxis_dep = b.dependencyLazy("vaxis", .{ .target = target, .optimize = optimize, .external_uucode = true }) catch return; + const width = b.addWriteFiles(); + _ = width.addCopyFile(vaxis_dep.path("src/gwidth.zig"), "gwidth.zig"); + const vaxis = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = width.add("vaxis.zig", "pub const gwidth = @import(\"gwidth.zig\");\n"), + .imports = &.{.{ .name = "uucode", .module = uucode }}, + }); + _ = core.editorModule(b, "pardes-editor", target, optimize, cfg, .{ .themes = themes, .ts = ts, .uucode = uucode, .mvzr = mvzr, .vaxis = vaxis }); +} diff --git a/build/library.zig b/build/library.zig deleted file mode 100644 index 9a1bfb67..00000000 --- a/build/library.zig +++ /dev/null @@ -1,49 +0,0 @@ -const std = @import("std"); -const TreeSitterGrammars = @import("../build.zig").TreeSitterGrammars; -const core = @import("core.zig"); -const patches = @import("patches.zig"); - -/// pardes as a library, all a package that depends on it is given: the -/// `pardes-syntax` module (core/lib_syntax.zig), the grammars, highlighting -/// and themes for a program that draws source text itself. It takes -/// tree-sitter and the grammars of its `grammars` option and no other -/// dependency, so nothing else is fetched (build.zig.zon). test/lib is such -/// a package, which `lib-test` builds. -pub fn build(b: *std.Build) void { - const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{}); - const tree_sitter_grammars = b.option(TreeSitterGrammars, "grammars", "the tree-sitter grammars compiled in: none (the runtime alone), minimal (C, C++ and Zig) or full (all of them; the default)") orelse .full; - var threaded: std.Io.Threaded = .init(b.allocator, .{}); - defer threaded.deinit(); - const io = threaded.io(); - const mod = b.addModule("pardes-syntax", .{ - .target = target, - .optimize = optimize, - .root_source_file = b.path("core/lib_syntax.zig"), - .link_libc = true, - }); - // A library's options: its grammars, nothing stamped, no window. - const cfg: core.ShellConfig = .{ .tree_sitter_grammars = tree_sitter_grammars }; - mod.addOptions("pardes_options", core.options(b, cfg, .{ .root = "core/lib_syntax.zig", .platform = .tui, .terminal = false }, false)); - const ts = core.treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, tree_sitter_grammars); - const themes = core.generatedThemes(b, io, target, optimize); - mod.addImport("generated_themes", themes); - ts.wire(mod); - mod.addImport("c_heap", b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("core/c_heap.zig") })); - // The editor, `pardes-editor`, to a package that asks for it: uucode, - // mvzr and vaxis (with what its build names) are fetched then and only - // then. Of vaxis it takes the grapheme width, which uucode alone feeds. - if (!(b.option(bool, "editor", "also export `pardes-editor`, the text pane as a library (fetches uucode, mvzr and vaxis)") orelse false)) return; - const uucode = core.uucodeModule(b, target, optimize) orelse return; - const mvzr = patches.mvzr(b, patches.tool(b), b.dependencyLazy("mvzr", .{ .target = target, .optimize = optimize }) catch return, target, optimize); - const vaxis_dep = b.dependencyLazy("vaxis", .{ .target = target, .optimize = optimize, .external_uucode = true }) catch return; - const width = b.addWriteFiles(); - _ = width.addCopyFile(vaxis_dep.path("src/gwidth.zig"), "gwidth.zig"); - const vaxis = b.createModule(.{ - .target = target, - .optimize = optimize, - .root_source_file = width.add("vaxis.zig", "pub const gwidth = @import(\"gwidth.zig\");\n"), - .imports = &.{.{ .name = "uucode", .module = uucode }}, - }); - _ = core.editorModule(b, "pardes-editor", target, optimize, cfg, .{ .themes = themes, .ts = ts, .uucode = uucode, .mvzr = mvzr, .vaxis = vaxis }); -} -- cgit v1.3 From 403b46922af6e4e2012cd15f5f3f5a5702eef7e7 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 04:38:59 -0300 Subject: test: test/lib becomes test/consumer, the package a consumer would write, in both of build.zig's head-comment shapes (pardes-build-2 item 5; the start design's step 3): `pardes-syntax` with `.grammars = .minimal` (lib.zig, as before), and `pardes-editor` with `.grammars = .none, .editor = true` and a grammar of its own, C's, built by this package from the very package pardes's manifest pins (`ts_c`, its own build's `tree-sitter-c` artifact, static, ReleaseFast) and linked into the editor's test and the bench, so the case sketchbook's examples/live is (pardes compiles no grammar for it; the program hands one in at run time) is gated here and not only in sketchbook; no network: `lib-test` runs it with `--pkg-dir ../../zig-pkg`, which has that package; the package is named `pardes_consumer` (a new fingerprint, as zig asks for a renamed package); lib-test's run step is named for what it is, so `--summary all` shows `zig build test in test/consumer, a package that depends on pardes success`, under `unit-test` as before; the package's own run: 29/29 steps, 11/11 tests (3 of the syntax, 8 of the editor), its bench builds and runs; build/tools.zig, build.zig's head comment, core/lib_syntax.zig, docs/next-editor.md, the manual and the changelog name test/consumer; the stale `test/lib/zig-pkg` (1.1 GB, untracked, from a run without --pkg-dir) deleted with the old directory. Scripts and notes outside the tree that name test/lib (its cache among them: a wipe of `test/lib/.zig-cache` now misses `test/consumer/.zig-cache`) are listed in the agent's report for the coordinator Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- build.zig | 5 +- build/tools.zig | 7 +- core/CHANGELOG.md | 2 +- core/lib_syntax.zig | 2 +- docs/next-editor.md | 4 +- docs/typ/building.typ | 4 +- test/consumer/bench.zig | 80 +++++++++++++++ test/consumer/build.zig | 42 ++++++++ test/consumer/build.zig.zon | 16 +++ test/consumer/editor.zig | 241 ++++++++++++++++++++++++++++++++++++++++++++ test/consumer/lib.zig | 161 +++++++++++++++++++++++++++++ test/lib/bench.zig | 80 --------------- test/lib/build.zig | 34 ------- test/lib/build.zig.zon | 10 -- test/lib/editor.zig | 241 -------------------------------------------- test/lib/lib.zig | 161 ----------------------------- 16 files changed, 553 insertions(+), 537 deletions(-) create mode 100644 test/consumer/bench.zig create mode 100644 test/consumer/build.zig create mode 100644 test/consumer/build.zig.zon create mode 100644 test/consumer/editor.zig create mode 100644 test/consumer/lib.zig delete mode 100644 test/lib/bench.zig delete mode 100644 test/lib/build.zig delete mode 100644 test/lib/build.zig.zon delete mode 100644 test/lib/editor.zig delete mode 100644 test/lib/lib.zig (limited to 'build.zig') diff --git a/build.zig b/build.zig index 3498b331..7ea167ff 100644 --- a/build.zig +++ b/build.zig @@ -22,8 +22,9 @@ //! Every dependency but tree-sitter's runtime is lazy in the manifest, so //! such a package fetches what it asked for and nothing else. //! `pardes-editor` has `pardes-syntax` in it as `.syntax`: a file belongs -//! to one module, so a package imports one of the two. test/lib is such a -//! package; `lib-test`, which `unit-test` runs, builds and runs it. +//! to one module, so a package imports one of the two. test/consumer is +//! such a package, with both shapes; `lib-test`, which `unit-test` runs, +//! builds and runs it. const std = @import("std"); const builtin = @import("builtin"); const core = @import("build/core.zig"); diff --git a/build/tools.zig b/build/tools.zig index 62e9817a..a4b41dd7 100644 --- a/build/tools.zig +++ b/build/tools.zig @@ -250,12 +250,13 @@ pub fn add(b: *std.Build, c: struct { if (testMatch(b, c.unit_profile, c.test_filters, syntax_test_step)) |check| check.addArtifactArg(syntax_test); // The library as a package that depends on pardes is given it - // (`library`): test/lib's own `zig build test`, over the packages + // (`library`): test/consumer's own `zig build test`, over the packages // this checkout has fetched. const lib_test = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "test", "--pkg-dir", "../../zig-pkg" }); - lib_test.setCwd(b.path("test/lib")); + lib_test.setName("zig build test in test/consumer, a package that depends on pardes"); + lib_test.setCwd(b.path("test/consumer")); lib_test.has_side_effects = true; - const lib_test_step = b.step("lib-test", "build and test the library from a package that depends on pardes (test/lib)"); + const lib_test_step = b.step("lib-test", "build and test the library from a package that depends on pardes (test/consumer)"); lib_test_step.dependOn(&lib_test.step); const history = b.addExecutable(.{ diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 813f24e6..5c9ac98c 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -214,7 +214,7 @@ dependency but tree-sitter's runtime is lazy in the manifest, so such a package fetches the runtime and the grammars of its `grammars` set and nothing else; `zig build lib-test`, which `unit-test` runs, builds - one (`test/lib`). The markdown grammar is pinned by its commit: its + one (`test/consumer`). The markdown grammar is pinned by its commit: its branch has moved, and a fresh checkout could not fetch it. - Enter, `o` and `O` give the new line helix's indent where the file's diff --git a/core/lib_syntax.zig b/core/lib_syntax.zig index f10f88a5..f4f85820 100644 --- a/core/lib_syntax.zig +++ b/core/lib_syntax.zig @@ -4,7 +4,7 @@ //! coloured as pardes has it. Nothing is copied here: these are the editor's //! own grammars and highlighting (syntax.zig), themes (colors.zig) and line //! arithmetic (modal.zig), under the names they have there. Whatever is -//! named here compiles with tree-sitter and the grammars alone; test/lib +//! named here compiles with tree-sitter and the grammars alone; test/consumer //! imports it as such a package and holds it to that. //! //! A language is told by a path's extension, as in the editor; `pathFor` diff --git a/docs/next-editor.md b/docs/next-editor.md index e7168256..4b62f6b0 100644 --- a/docs/next-editor.md +++ b/docs/next-editor.md @@ -72,7 +72,7 @@ registers before it writes anything. A ring never refuses: it drops its oldest snapshots, never part of one (an undo ring holds at least one whole text, checked at compile time). -**Measured** (test/lib `zig build bench`, a text in C's shape, a key typed +**Measured** (test/consumer `zig build bench`, a text in C's shape, a key typed into its middle, 200 keys, this machine): a key at 1, 16, 64 KiB is 13, 173 and 692 µs in Debug (270k, 3.1M, 12.2M instructions) and 0.7, 7.7 and 30 µs in ReleaseFast: the 64 KiB key is 4% of a 16 ms frame in Debug. A @@ -82,7 +82,7 @@ text's and the cursor's); the first after a jump moves the gap, 3, 5 and optimised in every mode): once a changed key, not a frame, so no incremental re-parse. -A package taking the editor (test/lib) compiles cold in 6.4 s (126e9 +A package taking the editor (test/consumer) compiles cold in 6.4 s (126e9 instructions), an edit of its own file in 0.95 s plainly and 0.09-0.11 s under `--watch -fincremental`. pardes itself: core-test's full recompile 238.8e9 instructions before and after; the typing cells of `zig build diff --git a/docs/typ/building.typ b/docs/typ/building.typ index 8403df9a..8d14374d 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -130,7 +130,7 @@ what it is missing in one round. Zig still unpacks a lazy package whose archive its global cache already holds; `--pkg-dir` (or `ZIG_LOCAL_PKG_DIR`) at pardes's `zig-pkg` shares the one copy. What a pane measures stays out: a cell's width is vaxis's and uucode's, and a tab is -`config.tab_width` cells wherever it stands. `test/lib` is such a +`config.tab_width` cells wherever it stands. `test/consumer` is such a package, and `zig build lib-test` builds and runs it. === The editor @@ -186,7 +186,7 @@ zig build kitty-harness kitty graphics through a pane end to end (a fake kitt kitten icat and chafa too when installed) zig build syntax-test tree-sitter tests without building the editor zig build syntax deterministic per-byte highlighting snapshots -zig build lib-test the library, from a package that depends on pardes (test/lib); +zig build lib-test the library, from a package that depends on pardes (test/consumer); unit-test runs it too zig build hxlib helix's cases on the library's editor (-- --parity: against the file pane) zig build behaviour-test behaviour through the 9P tree, in process (test/behaviour/), diff --git a/test/consumer/bench.zig b/test/consumer/bench.zig new file mode 100644 index 00000000..7e945064 --- /dev/null +++ b/test/consumer/bench.zig @@ -0,0 +1,80 @@ +//! What a key costs on the library (`zig build bench -- `, Debug +//! or -Doptimize): a text of that size in the C grammar's shape, a key +//! typed into its middle `keys` times, each with an arena of its own; the +//! 4 KiB pages of the value a typed character changes; a full parse of the +//! text. `keys` 0 makes the text alone, to count what the keys add; a +//! third argument, `pages`, counts the pages (a copy and a compare a key, +//! which an instruction count of the keys leaves out). +const std = @import("std"); +const pe = @import("pardes-editor"); + +const Ed = pe.Editor(.{ .text = 128 * 1024, .undo = 512 * 1024 }); + +extern fn tree_sitter_c() *const pe.syntax.ts.Language; + +fn now() u64 { + var ts: std.c.timespec = undefined; + _ = std.c.clock_gettime(.MONOTONIC, &ts); + return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec)); +} + +pub fn main(init: std.process.Init) !void { + const gpa = std.heap.page_allocator; + const args = try init.minimal.args.toSlice(init.arena.allocator()); + const kib = try std.fmt.parseInt(usize, args[1], 10); + const keys = try std.fmt.parseInt(usize, args[2], 10); + const count_pages = args.len > 3; + var text: std.ArrayList(u8) = .empty; + var i: usize = 0; + while (text.items.len < kib * 1024) : (i += 1) try text.print(gpa, "int f{d}(int x) {{ return x + {d}; }}\n", .{ i, i }); + text.items.len = kib * 1024; + const ed = try gpa.create(Ed); + ed.* = .{}; + try ed.load(text.items); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + // to the middle, into insert mode + for ([_]pe.Key{ .{ .cp = '%', .text = "%" }, .{ .cp = ';', .text = ";" } }) |k| _ = ed.key(arena.allocator(), null, k); + var to_middle: [8]u8 = undefined; + for (std.fmt.bufPrint(&to_middle, "{d}G", .{text.items.len / 2 / 32}) catch unreachable) |c| _ = ed.key(arena.allocator(), null, .{ .cp = c, .text = &.{c} }); + _ = ed.key(arena.allocator(), null, .{ .cp = 'i', .text = "i" }); + _ = arena.reset(.free_all); + if (keys == 0) return; + + const before = try gpa.create(Ed); + var pages_max: usize = 0; + var pages_sum: usize = 0; + var key_ns: u64 = 0; + for (0..keys) |n| { + if (count_pages) before.* = ed.*; + const t0 = now(); + const out = ed.key(arena.allocator(), null, .{ .cp = 'a' + @as(u21, @intCast(n % 26)), .text = &.{'a' + @as(u8, @intCast(n % 26))} }); + key_ns += now() - t0; + if (out != .done) return error.NotTyped; + _ = arena.reset(.retain_capacity); + if (!count_pages) continue; + const a = std.mem.asBytes(before); + const b = std.mem.asBytes(ed); + var pages: usize = 0; + var at: usize = 0; + while (at < a.len) : (at += 4096) { + const end = @min(at + 4096, a.len); + if (!std.mem.eql(u8, a[at..end], b[at..end])) pages += 1; + } + pages_max = @max(pages_max, pages); + pages_sum += pages; + } + const per_key = key_ns / keys; + + pe.syntax.start(std.heap.smp_allocator); // tree-sitter mallocs: not a page each + const lang: pe.Language = .{ .name = "c-as-dsl", .ext = ".cdsl", .ts = tree_sitter_c() }; + var syn: pe.Syntax = .{}; + // the parser and its queries made once, then the parse alone timed + _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; + syn.tree.?.destroy(); + syn.tree = null; + const p0 = now(); + _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; + const parse = now() - p0; + std.debug.print("{d} KiB: {d} keys, {d} ns a key, pages changed a key {d} max {d:.2} mean, value {d} bytes, full parse {d} ns\n", .{ kib, keys, per_key, pages_max, @as(f64, @floatFromInt(pages_sum)) / @as(f64, @floatFromInt(keys)), @sizeOf(Ed), parse }); +} diff --git a/test/consumer/build.zig b/test/consumer/build.zig new file mode 100644 index 00000000..ca63bb6e --- /dev/null +++ b/test/consumer/build.zig @@ -0,0 +1,42 @@ +//! A package that depends on pardes, as a program would (build.zig's head +//! comment has its lines): pardes's `lib-test`, which `unit-test` runs, +//! runs `zig build test` here. `pardes-syntax` with the minimal grammars, +//! as a program that draws source text itself takes it; and +//! `pardes-editor` with no grammar of pardes's and one of its own, C's, +//! compiled here from the package pardes pins and handed in at run time, +//! as sketchbook's `examples/live` takes it. +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const syntax = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .minimal }); + const editor = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .none, .editor = true }); + // The grammar of its own: C's, from the package pardes's manifest pins + // (so `--pkg-dir` at pardes's zig-pkg has it), as that package builds it. + const grammar = b.dependency("ts_c", .{ .target = target, .optimize = .ReleaseFast, .@"build-shared" = false }).artifact("tree-sitter-c"); + const step = b.step("test", "colour, outline and walk a C and a Zig source through pardes's syntax, and edit a text with its editor and a grammar of this package's"); + // A file belongs to one module: the syntax alone, and the editor (which + // has it as `.syntax`), each a test of its own. + for ([_]struct { []const u8, []const u8, *std.Build.Dependency }{ .{ "lib.zig", "pardes-syntax", syntax }, .{ "editor.zig", "pardes-editor", editor } }) |t| { + const mod = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = b.path(t[0]), + .imports = &.{.{ .name = t[1], .module = t[2].module(t[1]) }}, + }); + if (t[2] == editor) mod.linkLibrary(grammar); + step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mod })).step); + } + // What a key costs, a page count and a parse: `zig build bench -- `. + const bench = b.addExecutable(.{ .name = "bench", .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("bench.zig"), + .imports = &.{.{ .name = "pardes-editor", .module = editor.module("pardes-editor") }}, + }) }); + bench.root_module.linkLibrary(grammar); + const run_bench = b.addRunArtifact(bench); + run_bench.addPassthruArgs(); + b.step("bench", "a key typed into the library's editor at a size, its pages and a parse").dependOn(&run_bench.step); +} diff --git a/test/consumer/build.zig.zon b/test/consumer/build.zig.zon new file mode 100644 index 00000000..c367775b --- /dev/null +++ b/test/consumer/build.zig.zon @@ -0,0 +1,16 @@ +.{ + .name = .pardes_consumer, + .version = "0.0.0", + .dependencies = .{ + .pardes = .{ .path = "../.." }, + // The grammar this package hands pardes's editor: the one pardes's + // own manifest pins, so `--pkg-dir` at its zig-pkg has it. + .ts_c = .{ + .url = "git+https://github.com/pierrelgol/tree-sitter-c#67949e4c8dc630c18a522919f2bde2e025a0d025", + .hash = "tree_sitter_c-0.24.2-y5boS-htQACe3pz6jHq4tpS8dB6WIo4-zk_ct7lAjvQT", + }, + }, + .minimum_zig_version = "0.17.1", + .paths = .{""}, + .fingerprint = 0xb5ce4ba417a79812, +} diff --git a/test/consumer/editor.zig b/test/consumer/editor.zig new file mode 100644 index 00000000..22690915 --- /dev/null +++ b/test/consumer/editor.zig @@ -0,0 +1,241 @@ +//! The editor as a package that depends on pardes takes it +//! (`pardes-editor`): a value of plain data a program keeps, typed into a +//! key at a time with an arena each call, copied anywhere, refused whole at +//! its capacities (docs/next-editor.md). +const std = @import("std"); +const pe = @import("pardes-editor"); + +const Ed = pe.Editor(.{ .text = 4096, .undo = 8192, .registers = 1024, .selections = 16 }); + +/// Keys in helix's notation, the few these tests type: printables, and +/// ``, ``, ``. +fn typeKeys(ed: anytype, gpa: std.mem.Allocator, keys: []const u8) !void { + var i: usize = 0; + while (i < keys.len) { + var k: pe.Key = .{ .cp = keys[i], .text = keys[i .. i + 1] }; + inline for (.{ .{ "", pe.Key.escape }, .{ "", pe.Key.enter }, .{ "", pe.Key.backspace } }) |special| { + if (std.mem.startsWith(u8, keys[i..], special[0])) k = .{ .cp = special[1] }; + } + i += if (k.text.len == 0) std.mem.indexOfScalarPos(u8, keys, i, '>').? + 1 - i else 1; + // every call its own arena, gone after it: nothing of a key outlives it + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + switch (ed.key(arena.allocator(), null, k)) { + .refused => return error.Refused, + else => {}, + } + } +} + +fn text(ed: anytype, buf: []u8) []const u8 { + return ed.read(buf); +} + +const first = "ifn main() { let x = 1;}ggxyp" ++ "wwCcfoo"; +const second = "%dpuuUjjAbar" ++ "ggiqu"; + +test "a value copied elsewhere, its old memory gone, types on as one never moved" { + const gpa = std.testing.allocator; + const still = try gpa.create(Ed); + defer gpa.destroy(still); + still.* = .{}; + try typeKeys(still, gpa, first); + try typeKeys(still, gpa, second); + + const before = try gpa.create(Ed); + before.* = .{}; + try typeKeys(before, gpa, first); + const moved = try gpa.create(Ed); + defer gpa.destroy(moved); + @memcpy(std.mem.asBytes(moved), std.mem.asBytes(before)); + @memset(std.mem.asBytes(before), 0xAA); + gpa.destroy(before); + try typeKeys(moved, gpa, second); + + try std.testing.expectEqualSlices(u8, std.mem.asBytes(still), std.mem.asBytes(moved)); + var buf: [4096]u8 = undefined; + // `%d` then `p`, both undone, the delete redone: `A` types into nothing + try std.testing.expectEqualStrings("bar", text(moved, &buf)); +} + +test "undo goes back as far as the ring holds, and redo all the way again" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + const xs: [1000]u8 = @splat('x'); + try ed.load(&xs); + // thirty edits, each a snapshot of a thousand bytes: an 8 KiB ring + // keeps the last few + for (0..30) |_| try typeKeys(ed, gpa, "ia"); + var buf: [4096]u8 = undefined; + var last: [4096]u8 = undefined; + const final = text(ed, &last); + try std.testing.expectEqual(1030, final.len); + try std.testing.expect(ed.undo.count < 30); + const kept = ed.undo.count; + for (0..40) |_| try typeKeys(ed, gpa, "u"); + try std.testing.expectEqual(1030 - kept, text(ed, &buf).len); + for (0..40) |_| try typeKeys(ed, gpa, "U"); + try std.testing.expectEqualStrings(final, text(ed, &buf)); +} + +test "a key whose text would not fit is refused, and the value is as it was" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + const ys: [5000]u8 = @splat('y'); + try std.testing.expectError(error.TextFull, ed.load(&ys)); + try ed.load(ys[0..4094]); + try typeKeys(ed, gpa, "iab"); + const kept = try gpa.create(Ed); + defer gpa.destroy(kept); + kept.* = ed.*; + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.key(arena.allocator(), null, .{ .cp = 'c', .text = "c" })); + try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed)); +} + +test "what is not the text's comes back, and the caller can do it" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + try ed.load("one two one\n"); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + try std.testing.expectEqual(pe.Role.command_line, ed.key(a, null, .{ .cp = ':', .text = ":" }).action.role); + _ = ed.key(a, null, .{ .cp = '%', .text = "%" }); + try std.testing.expectEqual(pe.Role.select_regex, ed.key(a, null, .{ .cp = 's', .text = "s" }).action.role); + _ = ed.selectRegex(a, "one", .select); + var out: [pe.max_selections]pe.Selection = undefined; + const got = try ed.ranges(a, &out); + try std.testing.expectEqual(2, got.n); + try std.testing.expectEqual(pe.Selection{ .anchor = 8, .head = 11 }, out[1]); + try std.testing.expectEqual(pe.Mode.normal, ed.mode()); + _ = ed.key(a, null, .{ .cp = 'g', .text = "g" }); + try std.testing.expectEqual(pe.Role.goto_prefix, ed.pending().?); +} + +extern fn tree_sitter_c() *const pe.syntax.ts.Language; + +test "a language handed in at run time parses and colours the text" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + try ed.load("int f() { return 1; }\n"); + pe.syntax.start(gpa); + defer pe.syntax.stop(); + const lang: pe.Language = .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .highlights = "\"return\" @keyword (number_literal) @number" }; + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + var syn: pe.Syntax = .{}; + defer syn.forget(); + const tree = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; + try std.testing.expectEqualStrings("translation_unit", tree.rootNode().kind()); + const colors = try pe.Syntax.colors(arena.allocator(), ed, lang, 0, ed.len()); + try std.testing.expectEqual(pe.syntax.Syn.keyword, colors[10]); + try std.testing.expectEqual(pe.syntax.Syn.number, colors[17]); + try std.testing.expectEqual(pe.syntax.Syn.none, colors[0]); +} + +test "with a parse handed in, the tree-sitter walks run in the library; without, they are the caller's" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + try ed.load("int f() { return 1 + 2; }\n"); + pe.syntax.start(gpa); + defer pe.syntax.stop(); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const alt_o: pe.Key = .{ .cp = 'o', .alt = true }; + const alt_i: pe.Key = .{ .cp = 'i', .alt = true }; + try std.testing.expectEqual(pe.Role.expand_node, ed.key(a, null, alt_o).action.role); + var syn: pe.Syntax = .{ .lang = .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .textobjects = "(function_definition body: (_) @function.inside) @function.around" } }; + defer syn.forget(); + for ("21l") |c| _ = ed.key(a, &syn, .{ .cp = c, .text = &.{c} }); + var out: [pe.max_selections]pe.Selection = undefined; + // `2`, then `1 + 2`, then `return 1 + 2;`; Alt-i goes back the way it + // came (helix's object_selections: not the first child, `1`) + for ([_]struct { pe.Key, pe.Selection }{ + .{ alt_o, .{ .anchor = 17, .head = 22 } }, + .{ alt_o, .{ .anchor = 10, .head = 23 } }, + .{ alt_i, .{ .anchor = 17, .head = 22 } }, + .{ alt_i, .{ .anchor = 21, .head = 22 } }, + }) |step| { + try std.testing.expectEqual(pe.Outcome.done, ed.key(a, &syn, step[0])); + _ = try ed.ranges(a, &out); + try std.testing.expectEqual(step[1], out[0]); + } + // the grammar's textobjects handed in: `maf`, the function around + for ("maf") |c| _ = ed.key(a, &syn, .{ .cp = c, .text = &.{c} }); + _ = try ed.ranges(a, &out); + try std.testing.expectEqual(pe.Selection{ .anchor = 0, .head = 25 }, out[0]); +} + +test "a byte range replaced is one step to undo, the ranges following it; a drag joined is one step, refused whole when it does not fit" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + try ed.load("a = 12 + 3;\n"); + try typeKeys(ed, gpa, "9l"); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var buf: [4096]u8 = undefined; + var out: [pe.max_selections]pe.Selection = undefined; + // a drag on the number: its first frame a step, the rest joined to it + try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 6, "130", false)); + _ = try ed.ranges(a, &out); + try std.testing.expectEqual(pe.Selection{ .anchor = 10, .head = 11 }, out[0]); // still on `3` + const steps = ed.undo.count; + try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "131", true)); + try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "14", true)); + try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); + try std.testing.expectEqual(steps, ed.undo.count); + _ = try ed.ranges(a, &out); + try std.testing.expectEqual(pe.Selection{ .anchor = 9, .head = 10 }, out[0]); + try typeKeys(ed, gpa, "u"); + try std.testing.expectEqualStrings("a = 12 + 3;\n", text(ed, &buf)); + try typeKeys(ed, gpa, "U"); + try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); + // what would not fit: refused, the value as it was + const kept = try gpa.create(Ed); + defer gpa.destroy(kept); + kept.* = ed.*; + const ys: [4096]u8 = @splat('y'); + try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.replace(a, 0, 1, &ys, false)); + try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed)); +} + +test "a new line's indent comes from the parse handed in, where its language has an indent query; else the plain one" { + const gpa = std.testing.allocator; + pe.syntax.start(gpa); + defer pe.syntax.stop(); + var buf: [4096]u8 = undefined; + // the plain indent is a level more after `)`: what a language whose + // lines are calls does not want, and says by an indent query + for ([_]struct { ?pe.Language, []const u8 }{ + .{ null, "f(1)\n y" }, + .{ .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c() }, "f(1)\n y" }, + .{ .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .indents = "(compound_statement) @indent \"}\" @outdent" }, "f(1)\ny" }, + }) |case| { + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + var syn: pe.Syntax = .{ .lang = case[0] }; + defer syn.forget(); + try ed.load("f(1)"); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + for ([_]pe.Key{ .{ .cp = 'A', .text = "A" }, .{ .cp = pe.Key.enter }, .{ .cp = 'y', .text = "y" }, .{ .cp = pe.Key.escape } }) |k| _ = ed.key(arena.allocator(), &syn, k); + try std.testing.expectEqualStrings(case[1], text(ed, &buf)); + } +} diff --git a/test/consumer/lib.zig b/test/consumer/lib.zig new file mode 100644 index 00000000..7e297920 --- /dev/null +++ b/test/consumer/lib.zig @@ -0,0 +1,161 @@ +//! pardes's library from outside pardes (build.zig here): a C and a Zig +//! source coloured, outlined and walked, and a theme's colours for them. +const std = @import("std"); +const pardes = @import("pardes-syntax"); + +const c_source = + \\#include + \\ + \\// say it + \\int main(void) { + \\ printf("hi %d\n", 42); + \\ return 0; + \\} + \\ +; + +const zig_source = + \\const std = @import("std"); + \\ + \\pub fn main() void { + \\ // say it + \\ std.debug.print("hi {d}\n", .{42}); + \\} + \\ +; + +/// Every byte of `text`'s first occurrence in `source` is drawn as `want`. +fn expectSyn(styles: []const u8, source: []const u8, text: []const u8, want: pardes.Syn) !void { + const at = std.mem.indexOf(u8, source, text).?; + for (styles[at..][0..text.len]) |style| try std.testing.expectEqual(want, @as(pardes.Syn, @fromBackingInt(style))); +} + +test "a C and a Zig source are coloured, outlined and walked as pardes has them, in a named theme's colours" { + const gpa = std.testing.allocator; + // under the testing allocator, so what tree-sitter keeps is counted too + pardes.start(gpa); + defer pardes.stop(); + + try std.testing.expect(pardes.supportsPath("main.c") and pardes.supportsPath("main.zig")); + try std.testing.expect(!pardes.supportsPath("main.rs")); // minimal: c, c++, zig + try std.testing.expectEqualStrings("a.c", pardes.pathFor("C").?); + try std.testing.expectEqualStrings("a.cpp", pardes.pathFor("c++").?); + try std.testing.expectEqual(null, pardes.pathFor("klingon")); + + var c = try pardes.analyzeSource(gpa, "main.c", c_source, true, true); + defer c.deinit(gpa); + try std.testing.expectEqual(c_source.len, c.colors.len); + try expectSyn(c.colors, c_source, "#include", .keyword); + try expectSyn(c.colors, c_source, "", .string); + try expectSyn(c.colors, c_source, "// say it", .comment); + try expectSyn(c.colors, c_source, "main", .none); + try expectSyn(c.colors, c_source, "\"hi %d\\n\"", .string); + try expectSyn(c.colors, c_source, "42", .number); + try expectSyn(c.colors, c_source, "return", .keyword); + try std.testing.expectEqual(1, c.declarations.len); + const main = c.declarations[0]; + try std.testing.expectEqualStrings("int main(void) {", c_source[main.start_byte..main.header_end_byte]); + try std.testing.expectEqual('}', c_source[main.end_byte - 1]); + try std.testing.expectEqual(3, main.start_line); + try std.testing.expectEqual(6, main.end_line); + try std.testing.expectEqualStrings("int main(void) {", pardes.lineSlice(c_source, main.start_line)); + try std.testing.expectEqual(main.start_byte, pardes.lineStartOffset(c_source, main.start_line)); + try std.testing.expectEqual(8, pardes.lineCount(c_source)); + + const zig = try pardes.highlightFileRange(gpa, pardes.pathFor("zig").?, zig_source, 0, zig_source.len); + defer gpa.free(zig); + try std.testing.expectEqual(zig_source.len, zig.len); + try expectSyn(zig, zig_source, "fn", .keyword); + try expectSyn(zig, zig_source, "\"std\"", .string); + try expectSyn(zig, zig_source, "// say it", .comment); + try expectSyn(zig, zig_source, "42", .number); + try expectSyn(zig, zig_source, "main", .none); + const outline = try pardes.analyzeSource(gpa, "main.zig", zig_source, true, false); + defer gpa.free(outline.declarations); + try std.testing.expectEqual(1, outline.declarations.len); + try std.testing.expectEqualStrings("pub fn main() void {", zig_source[outline.declarations[0].start_byte..outline.declarations[0].header_end_byte]); + + // the tree: a statement from a byte in it, and the nodes themselves + const tree = pardes.parse("main.c", c_source).?; + defer tree.destroy(); + try std.testing.expectEqualStrings("translation_unit", tree.rootNode().kind()); + const at = std.mem.indexOf(u8, c_source, "return").?; + var span: [1]pardes.Span = undefined; + try std.testing.expectEqual(1, pardes.walkNodes(tree, .expand_node, at, at, at, &span)); + try std.testing.expectEqualStrings("return", c_source[span[0].from..span[0].to]); + try std.testing.expectEqual(1, pardes.walkNodes(tree, .expand_node, span[0].from, span[0].to, at, &span)); + try std.testing.expectEqualStrings("return 0;", c_source[span[0].from..span[0].to]); + try std.testing.expectEqual(null, pardes.parse("main.rs", "fn main() {}")); + + const orchard = pardes.theme("Orchard").?; + try std.testing.expectEqual(orchard, &pardes.themes[0]); + try std.testing.expectEqual([3]u8{ 0x0d, 0x14, 0x10 }, pardes.page(orchard)); + try std.testing.expectEqual([3]u8{ 0xe5, 0xee, 0xe5 }, pardes.ink(orchard)); + try std.testing.expectEqual([3]u8{ 0xc9, 0xa7, 0xed }, pardes.synInk(orchard, .keyword).?); + try std.testing.expectEqual([3]u8{ 0xb8, 0xd8, 0x8a }, pardes.synInk(orchard, .string).?); + try std.testing.expectEqual([3]u8{ 0xf8, 0xc0, 0x82 }, pardes.synInk(orchard, .number).?); + try std.testing.expectEqual([3]u8{ 0xb9, 0xf4, 0x80 }, pardes.synInk(orchard, .comment).?); + try std.testing.expectEqual(null, pardes.synInk(orchard, .none)); + // `dark` has no page of its own, and an imported palette is in the ring + try std.testing.expectEqual(pardes.theme("dark").?.tag_bg, pardes.page(pardes.theme("dark").?)); + try std.testing.expect(pardes.theme("gruvbox") != null); + try std.testing.expectEqual(null, pardes.theme("no such theme")); +} + +test "a diff is a language: its hunks coloured as their file's, its added and removed rows on a theme's grounds" { + const gpa = std.testing.allocator; + pardes.start(gpa); + defer pardes.stop(); + const patch = + \\diff --git a/main.c b/main.c + \\--- a/main.c + \\+++ b/main.c + \\@@ -1,3 +1,3 @@ + \\ int main(void) { + \\- return 1; + \\+ return 0; + \\ } + \\ + ; + const styles = try pardes.highlightDiff(gpa, patch, 0, patch.len); + defer gpa.free(styles); + try std.testing.expectEqual(patch.len, styles.len); + const syn = struct { + fn at(s: []const u8, text: []const u8, needle: []const u8) pardes.Syn { + return @fromBackingInt(s[std.mem.indexOf(u8, text, needle).?] & pardes.syn_bits); + } + }.at; + try std.testing.expectEqual(pardes.Syn.comment, syn(styles, patch, "--- a/main.c")); + try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "@@ -1,3")); + // each side's code as C, behind its marker + try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "return 1")); + try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "return 0")); + try std.testing.expectEqual(pardes.Syn.number, syn(styles, patch, "0;")); + const added = std.mem.indexOf(u8, patch, "+ return 0").?; + const removed = std.mem.indexOf(u8, patch, "- return 1").?; + try std.testing.expectEqual(pardes.ground_add | pardes.ground_emph, styles[added]); + try std.testing.expect(styles[added + 5] & pardes.ground_add != 0 and styles[added + 5] & pardes.ground_emph == 0); + try std.testing.expectEqual(pardes.ground_del | pardes.ground_emph, styles[removed]); + try std.testing.expectEqual(0, styles[std.mem.indexOf(u8, patch, " int main").?] & (pardes.ground_add | pardes.ground_del)); + + // a row's hue in a theme: a ground between the page and the theme's green, a stronger and a fainter one + const th = pardes.theme("Orchard").?; + const g = pardes.Grounds.of(th); + const ground = g.forStyle(styles[added]).?; + try std.testing.expectEqual(g.add, ground); + try std.testing.expectEqual(g.del, g.forStyle(styles[removed]).?); + try std.testing.expectEqual(null, g.forStyle(@backingInt(pardes.Syn.keyword))); + try std.testing.expect(!std.mem.eql(u8, &ground.row, &pardes.page(th)) and !std.mem.eql(u8, &ground.row, &ground.emph) and !std.mem.eql(u8, &ground.row, &ground.line)); + + // what a row points at: the new line a `+` row is + var lines: std.ArrayList([]const u8) = .empty; + defer lines.deinit(gpa); + var it = std.mem.splitScalar(u8, patch, '\n'); + while (it.next()) |line| try lines.append(gpa, line); + var buf: [64]u8 = undefined; + try std.testing.expectEqualStrings("main.c:2", pardes.diff.targetAt(lines.items, 6, false).?.address(&buf).?); +} + +test "every name the library has compiles outside pardes" { + std.testing.refAllDecls(pardes); +} diff --git a/test/lib/bench.zig b/test/lib/bench.zig deleted file mode 100644 index 7e945064..00000000 --- a/test/lib/bench.zig +++ /dev/null @@ -1,80 +0,0 @@ -//! What a key costs on the library (`zig build bench -- `, Debug -//! or -Doptimize): a text of that size in the C grammar's shape, a key -//! typed into its middle `keys` times, each with an arena of its own; the -//! 4 KiB pages of the value a typed character changes; a full parse of the -//! text. `keys` 0 makes the text alone, to count what the keys add; a -//! third argument, `pages`, counts the pages (a copy and a compare a key, -//! which an instruction count of the keys leaves out). -const std = @import("std"); -const pe = @import("pardes-editor"); - -const Ed = pe.Editor(.{ .text = 128 * 1024, .undo = 512 * 1024 }); - -extern fn tree_sitter_c() *const pe.syntax.ts.Language; - -fn now() u64 { - var ts: std.c.timespec = undefined; - _ = std.c.clock_gettime(.MONOTONIC, &ts); - return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec)); -} - -pub fn main(init: std.process.Init) !void { - const gpa = std.heap.page_allocator; - const args = try init.minimal.args.toSlice(init.arena.allocator()); - const kib = try std.fmt.parseInt(usize, args[1], 10); - const keys = try std.fmt.parseInt(usize, args[2], 10); - const count_pages = args.len > 3; - var text: std.ArrayList(u8) = .empty; - var i: usize = 0; - while (text.items.len < kib * 1024) : (i += 1) try text.print(gpa, "int f{d}(int x) {{ return x + {d}; }}\n", .{ i, i }); - text.items.len = kib * 1024; - const ed = try gpa.create(Ed); - ed.* = .{}; - try ed.load(text.items); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - // to the middle, into insert mode - for ([_]pe.Key{ .{ .cp = '%', .text = "%" }, .{ .cp = ';', .text = ";" } }) |k| _ = ed.key(arena.allocator(), null, k); - var to_middle: [8]u8 = undefined; - for (std.fmt.bufPrint(&to_middle, "{d}G", .{text.items.len / 2 / 32}) catch unreachable) |c| _ = ed.key(arena.allocator(), null, .{ .cp = c, .text = &.{c} }); - _ = ed.key(arena.allocator(), null, .{ .cp = 'i', .text = "i" }); - _ = arena.reset(.free_all); - if (keys == 0) return; - - const before = try gpa.create(Ed); - var pages_max: usize = 0; - var pages_sum: usize = 0; - var key_ns: u64 = 0; - for (0..keys) |n| { - if (count_pages) before.* = ed.*; - const t0 = now(); - const out = ed.key(arena.allocator(), null, .{ .cp = 'a' + @as(u21, @intCast(n % 26)), .text = &.{'a' + @as(u8, @intCast(n % 26))} }); - key_ns += now() - t0; - if (out != .done) return error.NotTyped; - _ = arena.reset(.retain_capacity); - if (!count_pages) continue; - const a = std.mem.asBytes(before); - const b = std.mem.asBytes(ed); - var pages: usize = 0; - var at: usize = 0; - while (at < a.len) : (at += 4096) { - const end = @min(at + 4096, a.len); - if (!std.mem.eql(u8, a[at..end], b[at..end])) pages += 1; - } - pages_max = @max(pages_max, pages); - pages_sum += pages; - } - const per_key = key_ns / keys; - - pe.syntax.start(std.heap.smp_allocator); // tree-sitter mallocs: not a page each - const lang: pe.Language = .{ .name = "c-as-dsl", .ext = ".cdsl", .ts = tree_sitter_c() }; - var syn: pe.Syntax = .{}; - // the parser and its queries made once, then the parse alone timed - _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; - syn.tree.?.destroy(); - syn.tree = null; - const p0 = now(); - _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; - const parse = now() - p0; - std.debug.print("{d} KiB: {d} keys, {d} ns a key, pages changed a key {d} max {d:.2} mean, value {d} bytes, full parse {d} ns\n", .{ kib, keys, per_key, pages_max, @as(f64, @floatFromInt(pages_sum)) / @as(f64, @floatFromInt(keys)), @sizeOf(Ed), parse }); -} diff --git a/test/lib/build.zig b/test/lib/build.zig deleted file mode 100644 index 171812e9..00000000 --- a/test/lib/build.zig +++ /dev/null @@ -1,34 +0,0 @@ -//! A package that depends on pardes for its library, as a program that draws -//! source text itself would: pardes's `lib-test` step runs `zig build test` -//! here, so the `pardes-syntax` module is built the way such a package -//! builds it (build.zig's `library`), with the minimal grammars, and -//! `pardes-editor`, the text pane as a library, which it asks for. -const std = @import("std"); - -pub fn build(b: *std.Build) void { - const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{}); - const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .minimal, .editor = true }); - const step = b.step("test", "colour, outline and walk a C and a Zig source through pardes's library, and edit a text with its editor"); - // A file belongs to one module: the syntax alone, and the editor (which - // has it as `.syntax`), each a test of its own. - for ([_][2][]const u8{ .{ "lib.zig", "pardes-syntax" }, .{ "editor.zig", "pardes-editor" } }) |t| { - const tests = b.addTest(.{ .root_module = b.createModule(.{ - .target = target, - .optimize = optimize, - .root_source_file = b.path(t[0]), - .imports = &.{.{ .name = t[1], .module = pardes.module(t[1]) }}, - }) }); - step.dependOn(&b.addRunArtifact(tests).step); - } - // What a key costs, a page count and a parse: `zig build bench -- `. - const bench = b.addExecutable(.{ .name = "bench", .root_module = b.createModule(.{ - .target = target, - .optimize = optimize, - .root_source_file = b.path("bench.zig"), - .imports = &.{.{ .name = "pardes-editor", .module = pardes.module("pardes-editor") }}, - }) }); - const run_bench = b.addRunArtifact(bench); - run_bench.addPassthruArgs(); - b.step("bench", "a key typed into the library's editor at a size, its pages and a parse").dependOn(&run_bench.step); -} diff --git a/test/lib/build.zig.zon b/test/lib/build.zig.zon deleted file mode 100644 index d8c31c3f..00000000 --- a/test/lib/build.zig.zon +++ /dev/null @@ -1,10 +0,0 @@ -.{ - .name = .pardes_lib_test, - .version = "0.0.0", - .dependencies = .{ - .pardes = .{ .path = "../.." }, - }, - .minimum_zig_version = "0.17.1", - .paths = .{""}, - .fingerprint = 0x3f9948734dd377c7, -} diff --git a/test/lib/editor.zig b/test/lib/editor.zig deleted file mode 100644 index 22690915..00000000 --- a/test/lib/editor.zig +++ /dev/null @@ -1,241 +0,0 @@ -//! The editor as a package that depends on pardes takes it -//! (`pardes-editor`): a value of plain data a program keeps, typed into a -//! key at a time with an arena each call, copied anywhere, refused whole at -//! its capacities (docs/next-editor.md). -const std = @import("std"); -const pe = @import("pardes-editor"); - -const Ed = pe.Editor(.{ .text = 4096, .undo = 8192, .registers = 1024, .selections = 16 }); - -/// Keys in helix's notation, the few these tests type: printables, and -/// ``, ``, ``. -fn typeKeys(ed: anytype, gpa: std.mem.Allocator, keys: []const u8) !void { - var i: usize = 0; - while (i < keys.len) { - var k: pe.Key = .{ .cp = keys[i], .text = keys[i .. i + 1] }; - inline for (.{ .{ "", pe.Key.escape }, .{ "", pe.Key.enter }, .{ "", pe.Key.backspace } }) |special| { - if (std.mem.startsWith(u8, keys[i..], special[0])) k = .{ .cp = special[1] }; - } - i += if (k.text.len == 0) std.mem.indexOfScalarPos(u8, keys, i, '>').? + 1 - i else 1; - // every call its own arena, gone after it: nothing of a key outlives it - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - switch (ed.key(arena.allocator(), null, k)) { - .refused => return error.Refused, - else => {}, - } - } -} - -fn text(ed: anytype, buf: []u8) []const u8 { - return ed.read(buf); -} - -const first = "ifn main() { let x = 1;}ggxyp" ++ "wwCcfoo"; -const second = "%dpuuUjjAbar" ++ "ggiqu"; - -test "a value copied elsewhere, its old memory gone, types on as one never moved" { - const gpa = std.testing.allocator; - const still = try gpa.create(Ed); - defer gpa.destroy(still); - still.* = .{}; - try typeKeys(still, gpa, first); - try typeKeys(still, gpa, second); - - const before = try gpa.create(Ed); - before.* = .{}; - try typeKeys(before, gpa, first); - const moved = try gpa.create(Ed); - defer gpa.destroy(moved); - @memcpy(std.mem.asBytes(moved), std.mem.asBytes(before)); - @memset(std.mem.asBytes(before), 0xAA); - gpa.destroy(before); - try typeKeys(moved, gpa, second); - - try std.testing.expectEqualSlices(u8, std.mem.asBytes(still), std.mem.asBytes(moved)); - var buf: [4096]u8 = undefined; - // `%d` then `p`, both undone, the delete redone: `A` types into nothing - try std.testing.expectEqualStrings("bar", text(moved, &buf)); -} - -test "undo goes back as far as the ring holds, and redo all the way again" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - const xs: [1000]u8 = @splat('x'); - try ed.load(&xs); - // thirty edits, each a snapshot of a thousand bytes: an 8 KiB ring - // keeps the last few - for (0..30) |_| try typeKeys(ed, gpa, "ia"); - var buf: [4096]u8 = undefined; - var last: [4096]u8 = undefined; - const final = text(ed, &last); - try std.testing.expectEqual(1030, final.len); - try std.testing.expect(ed.undo.count < 30); - const kept = ed.undo.count; - for (0..40) |_| try typeKeys(ed, gpa, "u"); - try std.testing.expectEqual(1030 - kept, text(ed, &buf).len); - for (0..40) |_| try typeKeys(ed, gpa, "U"); - try std.testing.expectEqualStrings(final, text(ed, &buf)); -} - -test "a key whose text would not fit is refused, and the value is as it was" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - const ys: [5000]u8 = @splat('y'); - try std.testing.expectError(error.TextFull, ed.load(&ys)); - try ed.load(ys[0..4094]); - try typeKeys(ed, gpa, "iab"); - const kept = try gpa.create(Ed); - defer gpa.destroy(kept); - kept.* = ed.*; - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.key(arena.allocator(), null, .{ .cp = 'c', .text = "c" })); - try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed)); -} - -test "what is not the text's comes back, and the caller can do it" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - try ed.load("one two one\n"); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - const a = arena.allocator(); - try std.testing.expectEqual(pe.Role.command_line, ed.key(a, null, .{ .cp = ':', .text = ":" }).action.role); - _ = ed.key(a, null, .{ .cp = '%', .text = "%" }); - try std.testing.expectEqual(pe.Role.select_regex, ed.key(a, null, .{ .cp = 's', .text = "s" }).action.role); - _ = ed.selectRegex(a, "one", .select); - var out: [pe.max_selections]pe.Selection = undefined; - const got = try ed.ranges(a, &out); - try std.testing.expectEqual(2, got.n); - try std.testing.expectEqual(pe.Selection{ .anchor = 8, .head = 11 }, out[1]); - try std.testing.expectEqual(pe.Mode.normal, ed.mode()); - _ = ed.key(a, null, .{ .cp = 'g', .text = "g" }); - try std.testing.expectEqual(pe.Role.goto_prefix, ed.pending().?); -} - -extern fn tree_sitter_c() *const pe.syntax.ts.Language; - -test "a language handed in at run time parses and colours the text" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - try ed.load("int f() { return 1; }\n"); - pe.syntax.start(gpa); - defer pe.syntax.stop(); - const lang: pe.Language = .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .highlights = "\"return\" @keyword (number_literal) @number" }; - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - var syn: pe.Syntax = .{}; - defer syn.forget(); - const tree = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; - try std.testing.expectEqualStrings("translation_unit", tree.rootNode().kind()); - const colors = try pe.Syntax.colors(arena.allocator(), ed, lang, 0, ed.len()); - try std.testing.expectEqual(pe.syntax.Syn.keyword, colors[10]); - try std.testing.expectEqual(pe.syntax.Syn.number, colors[17]); - try std.testing.expectEqual(pe.syntax.Syn.none, colors[0]); -} - -test "with a parse handed in, the tree-sitter walks run in the library; without, they are the caller's" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - try ed.load("int f() { return 1 + 2; }\n"); - pe.syntax.start(gpa); - defer pe.syntax.stop(); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - const a = arena.allocator(); - const alt_o: pe.Key = .{ .cp = 'o', .alt = true }; - const alt_i: pe.Key = .{ .cp = 'i', .alt = true }; - try std.testing.expectEqual(pe.Role.expand_node, ed.key(a, null, alt_o).action.role); - var syn: pe.Syntax = .{ .lang = .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .textobjects = "(function_definition body: (_) @function.inside) @function.around" } }; - defer syn.forget(); - for ("21l") |c| _ = ed.key(a, &syn, .{ .cp = c, .text = &.{c} }); - var out: [pe.max_selections]pe.Selection = undefined; - // `2`, then `1 + 2`, then `return 1 + 2;`; Alt-i goes back the way it - // came (helix's object_selections: not the first child, `1`) - for ([_]struct { pe.Key, pe.Selection }{ - .{ alt_o, .{ .anchor = 17, .head = 22 } }, - .{ alt_o, .{ .anchor = 10, .head = 23 } }, - .{ alt_i, .{ .anchor = 17, .head = 22 } }, - .{ alt_i, .{ .anchor = 21, .head = 22 } }, - }) |step| { - try std.testing.expectEqual(pe.Outcome.done, ed.key(a, &syn, step[0])); - _ = try ed.ranges(a, &out); - try std.testing.expectEqual(step[1], out[0]); - } - // the grammar's textobjects handed in: `maf`, the function around - for ("maf") |c| _ = ed.key(a, &syn, .{ .cp = c, .text = &.{c} }); - _ = try ed.ranges(a, &out); - try std.testing.expectEqual(pe.Selection{ .anchor = 0, .head = 25 }, out[0]); -} - -test "a byte range replaced is one step to undo, the ranges following it; a drag joined is one step, refused whole when it does not fit" { - const gpa = std.testing.allocator; - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - try ed.load("a = 12 + 3;\n"); - try typeKeys(ed, gpa, "9l"); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - const a = arena.allocator(); - var buf: [4096]u8 = undefined; - var out: [pe.max_selections]pe.Selection = undefined; - // a drag on the number: its first frame a step, the rest joined to it - try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 6, "130", false)); - _ = try ed.ranges(a, &out); - try std.testing.expectEqual(pe.Selection{ .anchor = 10, .head = 11 }, out[0]); // still on `3` - const steps = ed.undo.count; - try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "131", true)); - try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "14", true)); - try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); - try std.testing.expectEqual(steps, ed.undo.count); - _ = try ed.ranges(a, &out); - try std.testing.expectEqual(pe.Selection{ .anchor = 9, .head = 10 }, out[0]); - try typeKeys(ed, gpa, "u"); - try std.testing.expectEqualStrings("a = 12 + 3;\n", text(ed, &buf)); - try typeKeys(ed, gpa, "U"); - try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); - // what would not fit: refused, the value as it was - const kept = try gpa.create(Ed); - defer gpa.destroy(kept); - kept.* = ed.*; - const ys: [4096]u8 = @splat('y'); - try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.replace(a, 0, 1, &ys, false)); - try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed)); -} - -test "a new line's indent comes from the parse handed in, where its language has an indent query; else the plain one" { - const gpa = std.testing.allocator; - pe.syntax.start(gpa); - defer pe.syntax.stop(); - var buf: [4096]u8 = undefined; - // the plain indent is a level more after `)`: what a language whose - // lines are calls does not want, and says by an indent query - for ([_]struct { ?pe.Language, []const u8 }{ - .{ null, "f(1)\n y" }, - .{ .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c() }, "f(1)\n y" }, - .{ .{ .name = "mine", .ext = ".mine", .ts = tree_sitter_c(), .indents = "(compound_statement) @indent \"}\" @outdent" }, "f(1)\ny" }, - }) |case| { - const ed = try gpa.create(Ed); - defer gpa.destroy(ed); - ed.* = .{}; - var syn: pe.Syntax = .{ .lang = case[0] }; - defer syn.forget(); - try ed.load("f(1)"); - var arena = std.heap.ArenaAllocator.init(gpa); - defer arena.deinit(); - for ([_]pe.Key{ .{ .cp = 'A', .text = "A" }, .{ .cp = pe.Key.enter }, .{ .cp = 'y', .text = "y" }, .{ .cp = pe.Key.escape } }) |k| _ = ed.key(arena.allocator(), &syn, k); - try std.testing.expectEqualStrings(case[1], text(ed, &buf)); - } -} diff --git a/test/lib/lib.zig b/test/lib/lib.zig deleted file mode 100644 index 7e297920..00000000 --- a/test/lib/lib.zig +++ /dev/null @@ -1,161 +0,0 @@ -//! pardes's library from outside pardes (build.zig here): a C and a Zig -//! source coloured, outlined and walked, and a theme's colours for them. -const std = @import("std"); -const pardes = @import("pardes-syntax"); - -const c_source = - \\#include - \\ - \\// say it - \\int main(void) { - \\ printf("hi %d\n", 42); - \\ return 0; - \\} - \\ -; - -const zig_source = - \\const std = @import("std"); - \\ - \\pub fn main() void { - \\ // say it - \\ std.debug.print("hi {d}\n", .{42}); - \\} - \\ -; - -/// Every byte of `text`'s first occurrence in `source` is drawn as `want`. -fn expectSyn(styles: []const u8, source: []const u8, text: []const u8, want: pardes.Syn) !void { - const at = std.mem.indexOf(u8, source, text).?; - for (styles[at..][0..text.len]) |style| try std.testing.expectEqual(want, @as(pardes.Syn, @fromBackingInt(style))); -} - -test "a C and a Zig source are coloured, outlined and walked as pardes has them, in a named theme's colours" { - const gpa = std.testing.allocator; - // under the testing allocator, so what tree-sitter keeps is counted too - pardes.start(gpa); - defer pardes.stop(); - - try std.testing.expect(pardes.supportsPath("main.c") and pardes.supportsPath("main.zig")); - try std.testing.expect(!pardes.supportsPath("main.rs")); // minimal: c, c++, zig - try std.testing.expectEqualStrings("a.c", pardes.pathFor("C").?); - try std.testing.expectEqualStrings("a.cpp", pardes.pathFor("c++").?); - try std.testing.expectEqual(null, pardes.pathFor("klingon")); - - var c = try pardes.analyzeSource(gpa, "main.c", c_source, true, true); - defer c.deinit(gpa); - try std.testing.expectEqual(c_source.len, c.colors.len); - try expectSyn(c.colors, c_source, "#include", .keyword); - try expectSyn(c.colors, c_source, "", .string); - try expectSyn(c.colors, c_source, "// say it", .comment); - try expectSyn(c.colors, c_source, "main", .none); - try expectSyn(c.colors, c_source, "\"hi %d\\n\"", .string); - try expectSyn(c.colors, c_source, "42", .number); - try expectSyn(c.colors, c_source, "return", .keyword); - try std.testing.expectEqual(1, c.declarations.len); - const main = c.declarations[0]; - try std.testing.expectEqualStrings("int main(void) {", c_source[main.start_byte..main.header_end_byte]); - try std.testing.expectEqual('}', c_source[main.end_byte - 1]); - try std.testing.expectEqual(3, main.start_line); - try std.testing.expectEqual(6, main.end_line); - try std.testing.expectEqualStrings("int main(void) {", pardes.lineSlice(c_source, main.start_line)); - try std.testing.expectEqual(main.start_byte, pardes.lineStartOffset(c_source, main.start_line)); - try std.testing.expectEqual(8, pardes.lineCount(c_source)); - - const zig = try pardes.highlightFileRange(gpa, pardes.pathFor("zig").?, zig_source, 0, zig_source.len); - defer gpa.free(zig); - try std.testing.expectEqual(zig_source.len, zig.len); - try expectSyn(zig, zig_source, "fn", .keyword); - try expectSyn(zig, zig_source, "\"std\"", .string); - try expectSyn(zig, zig_source, "// say it", .comment); - try expectSyn(zig, zig_source, "42", .number); - try expectSyn(zig, zig_source, "main", .none); - const outline = try pardes.analyzeSource(gpa, "main.zig", zig_source, true, false); - defer gpa.free(outline.declarations); - try std.testing.expectEqual(1, outline.declarations.len); - try std.testing.expectEqualStrings("pub fn main() void {", zig_source[outline.declarations[0].start_byte..outline.declarations[0].header_end_byte]); - - // the tree: a statement from a byte in it, and the nodes themselves - const tree = pardes.parse("main.c", c_source).?; - defer tree.destroy(); - try std.testing.expectEqualStrings("translation_unit", tree.rootNode().kind()); - const at = std.mem.indexOf(u8, c_source, "return").?; - var span: [1]pardes.Span = undefined; - try std.testing.expectEqual(1, pardes.walkNodes(tree, .expand_node, at, at, at, &span)); - try std.testing.expectEqualStrings("return", c_source[span[0].from..span[0].to]); - try std.testing.expectEqual(1, pardes.walkNodes(tree, .expand_node, span[0].from, span[0].to, at, &span)); - try std.testing.expectEqualStrings("return 0;", c_source[span[0].from..span[0].to]); - try std.testing.expectEqual(null, pardes.parse("main.rs", "fn main() {}")); - - const orchard = pardes.theme("Orchard").?; - try std.testing.expectEqual(orchard, &pardes.themes[0]); - try std.testing.expectEqual([3]u8{ 0x0d, 0x14, 0x10 }, pardes.page(orchard)); - try std.testing.expectEqual([3]u8{ 0xe5, 0xee, 0xe5 }, pardes.ink(orchard)); - try std.testing.expectEqual([3]u8{ 0xc9, 0xa7, 0xed }, pardes.synInk(orchard, .keyword).?); - try std.testing.expectEqual([3]u8{ 0xb8, 0xd8, 0x8a }, pardes.synInk(orchard, .string).?); - try std.testing.expectEqual([3]u8{ 0xf8, 0xc0, 0x82 }, pardes.synInk(orchard, .number).?); - try std.testing.expectEqual([3]u8{ 0xb9, 0xf4, 0x80 }, pardes.synInk(orchard, .comment).?); - try std.testing.expectEqual(null, pardes.synInk(orchard, .none)); - // `dark` has no page of its own, and an imported palette is in the ring - try std.testing.expectEqual(pardes.theme("dark").?.tag_bg, pardes.page(pardes.theme("dark").?)); - try std.testing.expect(pardes.theme("gruvbox") != null); - try std.testing.expectEqual(null, pardes.theme("no such theme")); -} - -test "a diff is a language: its hunks coloured as their file's, its added and removed rows on a theme's grounds" { - const gpa = std.testing.allocator; - pardes.start(gpa); - defer pardes.stop(); - const patch = - \\diff --git a/main.c b/main.c - \\--- a/main.c - \\+++ b/main.c - \\@@ -1,3 +1,3 @@ - \\ int main(void) { - \\- return 1; - \\+ return 0; - \\ } - \\ - ; - const styles = try pardes.highlightDiff(gpa, patch, 0, patch.len); - defer gpa.free(styles); - try std.testing.expectEqual(patch.len, styles.len); - const syn = struct { - fn at(s: []const u8, text: []const u8, needle: []const u8) pardes.Syn { - return @fromBackingInt(s[std.mem.indexOf(u8, text, needle).?] & pardes.syn_bits); - } - }.at; - try std.testing.expectEqual(pardes.Syn.comment, syn(styles, patch, "--- a/main.c")); - try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "@@ -1,3")); - // each side's code as C, behind its marker - try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "return 1")); - try std.testing.expectEqual(pardes.Syn.keyword, syn(styles, patch, "return 0")); - try std.testing.expectEqual(pardes.Syn.number, syn(styles, patch, "0;")); - const added = std.mem.indexOf(u8, patch, "+ return 0").?; - const removed = std.mem.indexOf(u8, patch, "- return 1").?; - try std.testing.expectEqual(pardes.ground_add | pardes.ground_emph, styles[added]); - try std.testing.expect(styles[added + 5] & pardes.ground_add != 0 and styles[added + 5] & pardes.ground_emph == 0); - try std.testing.expectEqual(pardes.ground_del | pardes.ground_emph, styles[removed]); - try std.testing.expectEqual(0, styles[std.mem.indexOf(u8, patch, " int main").?] & (pardes.ground_add | pardes.ground_del)); - - // a row's hue in a theme: a ground between the page and the theme's green, a stronger and a fainter one - const th = pardes.theme("Orchard").?; - const g = pardes.Grounds.of(th); - const ground = g.forStyle(styles[added]).?; - try std.testing.expectEqual(g.add, ground); - try std.testing.expectEqual(g.del, g.forStyle(styles[removed]).?); - try std.testing.expectEqual(null, g.forStyle(@backingInt(pardes.Syn.keyword))); - try std.testing.expect(!std.mem.eql(u8, &ground.row, &pardes.page(th)) and !std.mem.eql(u8, &ground.row, &ground.emph) and !std.mem.eql(u8, &ground.row, &ground.line)); - - // what a row points at: the new line a `+` row is - var lines: std.ArrayList([]const u8) = .empty; - defer lines.deinit(gpa); - var it = std.mem.splitScalar(u8, patch, '\n'); - while (it.next()) |line| try lines.append(gpa, line); - var buf: [64]u8 = undefined; - try std.testing.expectEqualStrings("main.c:2", pardes.diff.targetAt(lines.items, 6, false).?.address(&buf).?); -} - -test "every name the library has compiles outside pardes" { - std.testing.refAllDecls(pardes); -} -- cgit v1.3 From a403340ec5c90238b136ca85fc64d65809b29a65 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 04:42:01 -0300 Subject: docs: the manual's library section is a consumer's build.zig lines and what each option selects, as build.zig's head comment says them, word for word (pardes-build-2 item 6): docs/typ/building.typ's "As a library" opens with the head comment's prose and its two shapes (`pardes-syntax` with `.grammars = .minimal`; `pardes-editor` with `.grammars = .none, .editor = true` and a grammar of the program's own), checked line by line against build.zig's `//!` lines; the syntax's table under its own heading, the paragraph after it no longer saying the option a second way; the editor's subsection no longer repeating a dependency line, saying instead what `.grammars = .none` means for it (a program links its grammar's parser and hands it in, as test/consumer does with C's); the head comment's dependency calls one field a line, as the manual's code blocks are, so a page's width holds them; `zig build book site cheatsheet` builds (138/138 steps) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- build.zig | 22 ++++++++++----- docs/typ/building.typ | 75 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 60 insertions(+), 37 deletions(-) (limited to 'build.zig') diff --git a/build.zig b/build.zig index 7ea167ff..125f4dcc 100644 --- a/build.zig +++ b/build.zig @@ -3,15 +3,25 @@ //! builds the editor; docs/typ/building.typ has the rest. //! //! A package that depends on pardes is given its syntax, `pardes-syntax` -//! (core/lib_syntax.zig): +//! (`core/lib_syntax.zig`): //! -//! const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .minimal }); +//! const pardes = b.dependency("pardes", .{ +//! .target = target, +//! .optimize = optimize, +//! .grammars = .minimal, +//! }); //! exe.root_module.addImport("pardes-syntax", pardes.module("pardes-syntax")); //! -//! and, when it asks, the text pane, `pardes-editor` (core/lib_editor.zig), -//! here with a grammar of its own handed in at run time: +//! and, when it asks, the text pane, `pardes-editor` +//! (`core/lib_editor.zig`), here with a grammar of its own handed in at +//! run time: //! -//! const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .grammars = .none, .editor = true }); +//! const pardes = b.dependency("pardes", .{ +//! .target = target, +//! .optimize = optimize, +//! .grammars = .none, +//! .editor = true, +//! }); //! exe.root_module.addImport("pardes-editor", pardes.module("pardes-editor")); //! //! `.grammars`: the tree-sitter grammars compiled in: `none` (the runtime @@ -22,7 +32,7 @@ //! Every dependency but tree-sitter's runtime is lazy in the manifest, so //! such a package fetches what it asked for and nothing else. //! `pardes-editor` has `pardes-syntax` in it as `.syntax`: a file belongs -//! to one module, so a package imports one of the two. test/consumer is +//! to one module, so a package imports one of the two. `test/consumer` is //! such a package, with both shapes; `lib-test`, which `unit-test` runs, //! builds and runs it. const std = @import("std"); diff --git a/docs/typ/building.typ b/docs/typ/building.typ index 8d14374d..d73543e1 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -92,15 +92,12 @@ moves an anchor fails that file's step. Each table says why, edit by edit. == As a library -A Zig package that depends on pardes is given one module and nothing of -the editor: `pardes-syntax` (`core/lib_syntax.zig`), for a program that -draws source text itself and wants it parsed and coloured as pardes has -it. +As `build.zig`'s head comment says it, word for word: + +A package that depends on pardes is given its syntax, `pardes-syntax` +(`core/lib_syntax.zig`): ``` -// build.zig.zon -.pardes = .{ .path = "../pardes" }, -// build.zig const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, @@ -109,6 +106,36 @@ const pardes = b.dependency("pardes", .{ exe.root_module.addImport("pardes-syntax", pardes.module("pardes-syntax")); ``` +and, when it asks, the text pane, `pardes-editor` +(`core/lib_editor.zig`), here with a grammar of its own handed in at +run time: + +``` +const pardes = b.dependency("pardes", .{ + .target = target, + .optimize = optimize, + .grammars = .none, + .editor = true, +}); +exe.root_module.addImport("pardes-editor", pardes.module("pardes-editor")); +``` + +`.grammars`: the tree-sitter grammars compiled in: `none` (the runtime +alone), `minimal` (C, C++ and Zig) or `full` (all of them; the default). +`.editor`: exports `pardes-editor` besides `pardes-syntax`, and with it +fetches uucode, mvzr and vaxis. + +Every dependency but tree-sitter's runtime is lazy in the manifest, so +such a package fetches what it asked for and nothing else. +`pardes-editor` has `pardes-syntax` in it as `.syntax`: a file belongs +to one module, so a package imports one of the two. `test/consumer` is +such a package, with both shapes; `lib-test`, which `unit-test` runs, +builds and runs it. + +`build.zig.zon` names pardes by its path: `.pardes = .{ .path = "../pardes" }`. + +=== The syntax + It is the editor's own code under the names it has there. A language is told by a path's extension, as in the editor, and `pathFor` gives a path for a language's name. @@ -122,35 +149,21 @@ for a language's name. [`start`, `stop`], [tree-sitter's allocator, and the end of the parsers kept per language], ) -The `grammars` option is the build's own: `none` (the runtime alone), -`minimal` (C, C++ and Zig) or `full` (all of them; the default). Every dependency but tree-sitter's -runtime is lazy in the manifest, so the package fetches the runtime and -its set's grammars and compiles those alone; pardes's own build asks for -what it is missing in one round. Zig still unpacks a lazy package whose -archive its global cache already holds; `--pkg-dir` (or `ZIG_LOCAL_PKG_DIR`) -at pardes's `zig-pkg` shares the one copy. What a pane measures stays -out: a cell's width is vaxis's and uucode's, and a tab is -`config.tab_width` cells wherever it stands. `test/consumer` is such a -package, and `zig build lib-test` builds and runs it. +pardes's own build takes `-Dgrammars` too, and asks for what it is +missing in one round. Zig still unpacks a lazy package whose archive its +global cache already holds; `--pkg-dir` (or `ZIG_LOCAL_PKG_DIR`) at +pardes's `zig-pkg` shares the one copy. What a pane measures stays out: a +cell's width is vaxis's and uucode's, and a tab is `config.tab_width` +cells wherever it stands. === The editor `pardes-editor` (`core/lib_editor.zig`) is the text pane as a library: helix's keys on a text kept in memory, no files, commands, builtins, -prompts or views. A package asks for it, and is then given uucode, mvzr -and vaxis's grapheme width besides; one that does not fetches nothing more. -It has `pardes-syntax` in it as `.syntax`: a file belongs to one module, so -a package imports one of the two. - -``` -const pardes = b.dependency("pardes", .{ - .target = target, - .optimize = optimize, - .grammars = .minimal, - .editor = true, -}); -exe.root_module.addImport("pardes-editor", pardes.module("pardes-editor")); -``` +prompts or views. Of vaxis it takes the grapheme width alone. With +`.grammars = .none` it has no grammar of pardes's: a program hands its +own in at run time (`Language`, below), linking the grammar's parser +itself, as `test/consumer` does with C's. The editor is a value of plain data, `Editor(capacity)`: no pointer, no allocator, every capacity in its type, so a program keeps it where it -- cgit v1.3