diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 11:07:09 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 11:25:35 -0300 |
| commit | 6b3df3071d16153bd554462c4f54ebb88f3ed50e (patch) | |
| tree | 5428c2d4d6679fe0c34c0496cd58f94fad1b7486 | |
| parent | a953f560f648e91dda8dd3a8dafe60a7f75739d0 (diff) | |
| download | pardes-6b3df3071d16153bd554462c4f54ebb88f3ed50e.tar.gz pardes-6b3df3071d16153bd554462c4f54ebb88f3ed50e.zip | |
lib: pardes's syntax is a library, as ghostty-vt is ghostty's: a package that depends on pardes is given build.zig's `library` and nothing of the editor, one module, `pardes-syntax` (src/lib_syntax.zig), naming what a program that draws source text itself takes, the editor's own code under its own names (highlightFileRange and analyzeSource for a Syn a byte and the sticky headers' declarations, parse and walkNodes over the tree-sitter bindings, theme, page, ink and synInk for a named theme's colours, the line arithmetic, pathFor for a language's name); 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 alone, and pardes's own build asks for all it is missing in one round before its first b.dependency; build.zig's tree-sitter and theme-generator blocks become treeSitter and generatedThemes for both to call, a grammar not yet fetched asked for and left out of that round, and the scanners' -include of src/tree_sitter_heap.h is spelled from the root package's directory, where the compiler runs (pardes's own flags are as they were); File.synStyle's switch moves to colors.synInk, so the editor and the library colour a class from one place; test/lib is such a package, depending on pardes by path: it colours, outlines and walks a C and a Zig source in orchard's colours under the testing allocator, and `zig build lib-test`, which unit-test runs, is its own `zig build test` over this checkout's zig-pkg; ts_markdown is pinned by the commit its hash is of (a0a00f81): its split_parser branch moved to v0.5.4 and a fresh fetch no longer matched; building.typ and the changelog say so; syntax snapshots unchanged
| -rw-r--r-- | build.zig | 216 | ||||
| -rw-r--r-- | build.zig.zon | 46 | ||||
| -rw-r--r-- | docs/typ/building.typ | 49 | ||||
| -rw-r--r-- | src/CHANGELOG.md | 10 | ||||
| -rw-r--r-- | src/File.zig | 8 | ||||
| -rw-r--r-- | src/colors.zig | 11 | ||||
| -rw-r--r-- | src/lib_syntax.zig | 90 | ||||
| -rw-r--r-- | test/lib/build.zig | 18 | ||||
| -rw-r--r-- | test/lib/build.zig.zon | 10 | ||||
| -rw-r--r-- | test/lib/lib.zig | 107 |
10 files changed, 489 insertions, 76 deletions
@@ -59,6 +59,9 @@ const gui_shaders = [_][]const u8{ }; 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 library(b); const requested_platform = b.option(Platform, "platform", "which shell to build (tui, gui, macos); absent builds the tui cli and the SDL gui together"); const platform = requested_platform orelse .tui; const also_gui = requested_platform == null; @@ -233,6 +236,15 @@ pub fn build(b: *std.Build) void { core_mods_len += 1; }; const core_mods = core_mods_buf[0..core_mods_len]; + // The manifest marks every dependency lazy but tree-sitter's runtime, so + // that a package depending on pardes fetches what `library` takes and + // nothing else. pardes takes these whatever it builds: the missing ones + // are asked for here in one round, the grammars with them, where each + // `b.dependency` below would end the configure to fetch its own alone. + const ts = treeSitter(b, io, target, c_optimize, tree_sitter_grammars); + inline for (.{ "cloud9", "vaxis", "ghostty", "uucode", "zstbi", "mvzr", "zstd", "xitdb" }) |name| { + if (!std.Build.package_map.get(@field(zon.dependencies, name).hash).?.available) _ = b.lazyDependency(name, .{}); + } const cloud9 = b.dependency("cloud9", .{ .target = target, .optimize = optimize }).module("cloud9"); for (core_mods) |mod| mod.addImport("cloud9", cloud9); const core_shells = core_shells_buf[0..core_mods_len]; @@ -243,27 +255,12 @@ pub fn build(b: *std.Build) void { // The conditional dependencies, hoisted so one `wireCore` call at the end // can name them all. The unconditional ones are ordinary consts below. - var ts_mod: ?*std.Build.Module = null; - var ts_queries_opts: ?*std.Build.Step.Options = null; + const ts_mod = ts.mod; + const ts_queries_opts = ts.queries; var ghostty_core_vt: ?*std.Build.Module = null; - var ts_libs: std.ArrayList(*std.Build.Step.Compile) = .empty; + const ts_libs = ts.libs; - const theme_gen = b.addExecutable(.{ - .name = "pardes-gen-themes", - .root_module = b.createModule(.{ - .target = b.graph.host, - .optimize = .debug, - .root_source_file = b.path("tools/gen_themes.zig"), - }), - }); - const run_theme_gen = b.addRunArtifact(theme_gen); - const themes_out = run_theme_gen.addOutputDirectoryArg("themes"); - for (vendoredThemes(b, io)) |name| run_theme_gen.addFileArg(b.path(b.fmt("vendor/themes/{s}", .{name}))); - const themes_mod = b.createModule(.{ - .target = target, - .optimize = optimize, - .root_source_file = themes_out.path(b, "list.zig"), - }); + const themes_mod = generatedThemes(b, io, target, optimize); // forkpty: in libc proper on glibc>=2.34 and darwin; the BSDs keep it in libutil if (target.result.os.tag == .freebsd or target.result.os.tag == .netbsd or target.result.os.tag == .openbsd) @@ -314,53 +311,6 @@ pub fn build(b: *std.Build) void { } } - 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)) - { - const dep = b.dependency(g.dep, .{}); - const query = configureRead(b, io, dep.builder.root, g.query, 0x100000) catch @panic("read " ++ g.name ++ " highlights"); - ts_queries.addOption([]const u8, g.name ++ "_highlights", query); - // 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 src/tree_sitter_heap.h for the ones that call - // malloc themselves. - const ts_cflags = [_][]const u8{ - "-fno-sanitize=function", - "-DTREE_SITTER_REUSE_ALLOCATOR", - // Relative to the build root, the compiler's working directory. - "-include", - "src/tree_sitter_heap.h", - }; - 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; - } - const zstbi_dep = b.dependency("zstbi", .{ .target = target, .optimize = c_optimize }); const zstbi_mod = zstbi_dep.module("root"); // mvzr backtracks with no bound on its work (its own test "do not make @@ -1556,6 +1506,15 @@ pub fn build(b: *std.Build) void { syntax_test_step.dependOn(&run_syntax_test.step); if (testMatch(b, unit_profile, 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 + // 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.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)"); + lib_test_step.dependOn(&lib_test.step); + const history = b.addExecutable(.{ .name = "pardes-history", .root_module = b.createModule(.{ @@ -1592,6 +1551,7 @@ pub fn build(b: *std.Build) void { // The perf gate runs with every unit-test (a regression like a // contrast search per grapheme costs 100x, far past the gate). unit_step.dependOn(perf_gate_step); + unit_step.dependOn(lib_test_step); const units: Units = .{ .nonce = test_nonce, .build = test_build, .unit = unit_step, .match = testMatch(b, unit_profile, test_filters, unit_step), .profiler = unit_profile, .filters = test_filters }; // The perf and fs bench programs' own tests: they compile no editor. _ = units.add(b, perf_tests, null); @@ -2067,6 +2027,130 @@ fn vendoredThemes(b: *std.Build, io: std.Io) []const []const u8 { return names.items; } +/// The vendored palettes as the `generated_themes` module colors.zig folds +/// into its ring, written by tools/gen_themes.zig. +fn generatedThemes(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize) *std.Build.Module { + const theme_gen = b.addExecutable(.{ + .name = "pardes-gen-themes", + .root_module = b.createModule(.{ + .target = b.graph.host, + .optimize = .debug, + .root_source_file = b.path("tools/gen_themes.zig"), + }), + }); + const run_theme_gen = b.addRunArtifact(theme_gen); + const themes_out = run_theme_gen.addOutputDirectoryArg("themes"); + for (vendoredThemes(b, io)) |name| run_theme_gen.addFileArg(b.path(b.fmt("vendor/themes/{s}", .{name}))); + return b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = themes_out.path(b, "list.zig"), + }); +} + +const TreeSitter = struct { + mod: ?*std.Build.Module, + queries: ?*std.Build.Step.Options, + libs: std.ArrayList(*std.Build.Step.Compile), +}; + +/// tree-sitter's bindings, and for each grammar of the set its queries (the +/// `ts_queries` options syntax.zig reads) and its parser, a static library. +/// A grammar not fetched yet is asked for and left out: the configure runs +/// again once it is there. +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; + 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.lazyDependency(g.dep, .{}) orelse break :grammar; + const query = configureRead(b, io, dep.builder.root, g.query, 0x100000) catch @panic("read " ++ g.name ++ " highlights"); + ts_queries.addOption([]const u8, g.name ++ "_highlights", query); + // 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 src/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, "src/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 }; +} + +/// pardes as a library, all a package that depends on it is given: the +/// `pardes-syntax` module (src/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 +/// dependency, so nothing else is fetched (build.zig.zon). test/lib is such +/// a package, which `lib-test` builds. +fn library(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; + 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("src/lib_syntax.zig"), + .link_libc = true, + }); + mod.addOptions("pardes_config", shellOptions(b, .{ + .tree_sitter_grammars = tree_sitter_grammars, + .tracy = false, + .workspace_tag = null, + .prebuilt_shaders = false, + .embed_sources = false, + .commit = null, + .change = null, + .built = null, + }, .tui)); + const ts = treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, tree_sitter_grammars); + wireCore(mod, .{ + .themes = generatedThemes(b, io, target, optimize), + .tree_sitter = ts.mod, + .ts_libs = ts.libs.items, + .ts_queries = ts.queries, + .c_heap = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/c_heap.zig") }), + }); +} + fn wireCore(m: *std.Build.Module, d: struct { kind: ?*std.Build.Module = null, themes: ?*std.Build.Module = null, diff --git a/build.zig.zon b/build.zig.zon index 23efe9d0..360adbd9 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -2,6 +2,11 @@ .name = .pardes, .version = "0.24.0", .dependencies = .{ + // Every dependency but tree_sitter is lazy: a package that depends + // on pardes for its library (build.zig's `library`) fetches + // tree-sitter and the grammars it asks for, and none of the rest. + // pardes's own build asks for what it is missing in one round. + // 9P sessions and transports live in the published cloud9 package, so // the manifest pins the build: `zig build` fetches this commit into // zig-pkg/ like any other dependency. Push to @@ -11,6 +16,7 @@ .cloud9 = .{ .url = "git+https://git.sr.ht/~gbrls/cloud9#0702e33bcfadf0abd9ccbaa8b681a19a84c332fe", .hash = "cloud9-0.1.0-yt86qkExHgCRI7_eJsztRECamioWguBj2mTFsJ5N-AOn", + .lazy = true, }, // Pinned to the exact libvaxis commit ghostty is built against, so it // is compatible with ghostty's uucode configuration (the one we share @@ -18,10 +24,12 @@ .vaxis = .{ .url = "git+https://github.com/rockorager/libvaxis.git#6fd944a27fb3d6f596e981076381a3131f2448b4", .hash = "vaxis-0.6.0-BWNV_MafDAAEzkVUvrW9NFUqnnMUoeHqrs2mecnD6D7w", + .lazy = true, }, .ghostty = .{ .url = "git+https://github.com/ghostty-org/ghostty.git#83fb95624b0439ee660c2679b43b38c8b63769cd", .hash = "ghostty-1.4.0-5UdBC-qohwWqLS0BImOz05t4hVyTwbaKslyTPvKYvAtE", + .lazy = true, }, // Same uucode commit ghostty and vaxis both depend on. We build it // ourselves (with uucode_config.zig) so ghostty-vt and vaxis can share @@ -29,12 +37,14 @@ .uucode = .{ .url = "git+https://github.com/jacobsandlund/uucode#ea62149739404a73c202b48a33bf6dd2af4bd9b0", .hash = "uucode-0.2.0-ZZjBPoGFVgCiflJNFc6Io8xzC67B6PA5bUkk4LT0kCxg", + .lazy = true, }, // Pivok7's fork: zig-gamedev/zstbi PR #16, "Upgrade to zig 0.17.0" // (two lines of build.zig). Back to upstream once it is merged. .zstbi = .{ .url = "git+https://github.com/Pivok7/zstbi#bc96f28d341f9c7fb9cbc5dc65aa4c5cf0c8f007", .hash = "zstbi-0.11.0-dev-L0Ea_3GWBwCpNHVwGU4MPR_oDd0FoQOYNrGodwvJ6Z45", + .lazy = true, }, // SDL3 (castholm/SDL zig bindings, its release-3.4.x branch for Zig // 0.17; david-vanderson's fork, pinned before, has no 0.17 build) — only @@ -58,118 +68,150 @@ .ts_rust = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-rust#77a3747266f4d621d0757825e6b11edcbf991ca5", .hash = "N-V-__8AAHJYbgAipcanYj2A6rnj9wtGzjGzANkrOsqiFAxm", + .lazy = true, }, .ts_c = .{ .url = "git+https://github.com/pierrelgol/tree-sitter-c#67949e4c8dc630c18a522919f2bde2e025a0d025", .hash = "tree_sitter_c-0.24.2-y5boS-htQACe3pz6jHq4tpS8dB6WIo4-zk_ct7lAjvQT", + .lazy = true, }, .ts_typst = .{ .url = "git+https://github.com/uben0/tree-sitter-typst#13863ddcbaa7b68ee6221cea2e3143415e64aea4", .hash = "N-V-__8AANAJVQBOabRzU-zDAdM20_2B2W2qk3-6Gvm1p1ZS", + .lazy = true, }, .ts_zig = .{ .url = "git+https://github.com/maxxnino/tree-sitter-zig#a80a6e9be81b33b182ce6305ae4ea28e29211bd5", .hash = "N-V-__8AADLAWgBszUZPv5YTGyO5ZNyxNcuuR8MB3mIMJrZf", + .lazy = true, }, .ts_python = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-python#26855eabccb19c6abf499fbc5b8dc7cc9ab8bc64", .hash = "N-V-__8AAOiqPgC9tH5tYSAYNsMWLoPiSxcmkHvIhMnezomx", + .lazy = true, }, .ts_javascript = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-javascript#58404d8cf191d69f2674a8fd507bd5776f46cb11", .hash = "N-V-__8AANlfOQDpOAP-6YMQeJ2nQetX1OpUshQr903bnIHU", + .lazy = true, }, .ts_go = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-go#2346a3ab1bb3857b48b29d779a1ef9799a248cd7", .hash = "N-V-__8AAM9cIgDttbrvbwLWFB0JPplu_2KtCWu7__79EjIT", + .lazy = true, }, .ts_json = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-json#001c28d7a29832b06b0e831ec77845553c89b56d", .hash = "N-V-__8AAHNaAgD6kjK3HX6lAf89chPqS5zIm0tLpvBQLOtX", + .lazy = true, }, .ts_html = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-html#73a3947324f6efddf9e17c0ea58d454843590cc0", .hash = "N-V-__8AAEmOAwDdgRRlet5t1CEdpX1YMCbfhFiIbj_vsBH9", + .lazy = true, }, .ts_css = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-css#dda5cfc5722c429eaba1c910ca32c2c0c5bb1a3f", .hash = "N-V-__8AAKwvEACP3YMpMDoRyyZPOdDN1n-zdTZRWTc3uufz", + .lazy = true, }, .ts_bash = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-bash#a06c2e4415e9bc0346c6b86d401879ffb44058f7", .hash = "N-V-__8AANjenwCy_cXlIbU8nqBzGxMlEHVokLZJYrmIXtoz", + .lazy = true, }, .ts_cpp = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-cpp#8b5b49eb196bec7040441bee33b2c9a4838d6967", .hash = "N-V-__8AAMePmwGeojPhBaBC10aGwaQIOU4vg1H17IWakWs-", + .lazy = true, }, .ts_java = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-java#e10607b45ff745f5f876bfa3e94fbcc6b44bdc11", .hash = "N-V-__8AAGrhLgBgxGFnDv-tHxMarGDRT4VTig1IIX1rcBdc", + .lazy = true, }, .ts_c_sharp = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-c-sharp#af29416d729b7a6603101b513604392d8f675e3b", .hash = "N-V-__8AALpc-AFDJNotrh7YgiXce1oXC9ub9SuD-Enwt7rN", + .lazy = true, }, .ts_php = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-php#38216983c07bf9e1b56e16acde53b25adaeab61c", .hash = "N-V-__8AABgQ6QCTufRtmuKAuAvX1ty8ZouX1jZ17wGFcx6I", + .lazy = true, }, .ts_ruby = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-ruby#ad907a69da0c8a4f7a943a7fe012712208da6dee", .hash = "N-V-__8AAB_S8QDTw_FIWZfx_eKtcAvrPJuTP12QEMVN2LDS", + .lazy = true, }, .ts_scala = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-scala#4d081d98670ff6e98ca42c085294fc75eec15e1d", .hash = "N-V-__8AAJlN0gFxPUfpXqmIgQLEXkmXsrvo3caVWINVJdTb", + .lazy = true, }, .ts_kotlin = .{ .url = "git+https://github.com/fwcd/tree-sitter-kotlin#c8ac3d2627240160b999a2c100de3babbdb8f419", .hash = "N-V-__8AAKSpGALvICLMOMcl4S_ypQBib_euVyf_B_9DP08A", + .lazy = true, }, .ts_haskell = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-haskell#0975ef72fc3c47b530309ca93937d7d143523628", .hash = "N-V-__8AALWgQgGf5l7eg-Dwi24SJszCffzN4_mWEtv3e3oI", + .lazy = true, }, .ts_ocaml = .{ .url = "git+https://github.com/tree-sitter/tree-sitter-ocaml#527d62ef0f24ce0d97fbedf004921d75d0a7e086", .hash = "N-V-__8AAGaGxwK7r2vy00a8b4MDEm0eudoNzkzu1WM01h7Q", + .lazy = true, }, .ts_erlang = .{ .url = "git+https://github.com/WhatsApp/tree-sitter-erlang#836aa2b6c3af2c7cef3f84049b0ed6d44485a870", .hash = "N-V-__8AAAVWKgC44vrL1kvl8SLUnqNkFU4guLAaXK73RmYK", + .lazy = true, }, .ts_elixir = .{ .url = "git+https://github.com/elixir-lang/tree-sitter-elixir#7937d3b4d65fa574163cfa59394515d3c1cf16f4", .hash = "N-V-__8AAHFBzAB5XFJyJvMulqqafphEQOoizic5m6WFfy6j", + .lazy = true, }, .ts_clojure = .{ .url = "git+https://github.com/sogaiu/tree-sitter-clojure#e43eff80d17cf34852dcd92ca5e6986d23a7040f", .hash = "N-V-__8AAAnoDwDKLr63cSQ_IHEojhLIanmbGIkrZNQSUM5_", + .lazy = true, }, .ts_fortran = .{ .url = "git+https://github.com/stadelmanma/tree-sitter-fortran#7edacd2b21aa80057d9725384a1304a1c758e0f8", .hash = "N-V-__8AAJ0zQgKKtzb0-wDQjY1pewE7RXmpAHVsKuwLT0e3", + .lazy = true, }, .ts_ada = .{ .url = "git+https://github.com/briot/tree-sitter-ada#6b58259a08b1a22ba0247a7ce30be384db618da6", .hash = "N-V-__8AAHNJMgAgTa051RgoAs48ohsOgCTsM34tTxoj80CE", + .lazy = true, }, .ts_pascal = .{ .url = "git+https://github.com/Isopod/tree-sitter-pascal#042119eca2e18a60e56317fb06ee3ba5c32cb447", .hash = "N-V-__8AAMmnUgA4hx9Tv_Vl5uDuzQ8r4gSH6J1CuwlZ5YOm", + .lazy = true, }, .ts_powershell = .{ .url = "git+https://github.com/airbus-cert/tree-sitter-powershell#d398441825243b00e317e87e1829b9d6a3e54ce0", .hash = "N-V-__8AAKzlUACji5nMCtCC_dgDtF1rEg1qoduDm3MT5B5H", + .lazy = true, }, + // The split_parser branch's commit this hash is of: the branch has + // moved since (v0.5.4), and a fresh fetch of its name no longer + // matches. .ts_markdown = .{ - .url = "git+https://github.com/tree-sitter-grammars/tree-sitter-markdown#split_parser", + .url = "git+https://github.com/tree-sitter-grammars/tree-sitter-markdown#a0a00f817d02412bd92c54d316f164d827b57b5c", .hash = "N-V-__8AAPRhUwBHS53YVU2fqpXuZ8VMvL_vIKp2AQo4Ja7V", + .lazy = true, }, .mvzr = .{ .url = "git+https://github.com/mnemnion/mvzr#3efcfe339bd32e122c36b8f1db25b237f93a7ec2", .hash = "mvzr-0.3.9-ZSOky6t2AQCA7efmm5EYbPb5sOdbXa4EFsUgYbQ6hZv9", + .lazy = true, }, // MuPDF for the pdf addon (mupdf.zig). Lazy: -Dpdf fetches it, once. .mupdf = .{ @@ -200,10 +242,12 @@ .zstd = .{ .url = "https://github.com/facebook/zstd/releases/download/v1.5.7/zstd-1.5.7.tar.gz", .hash = "N-V-__8AAPZ7fwBg4JoCzM_0o2A8wxH2hsUUeiU1iuZv53L5", + .lazy = true, }, .xitdb = .{ .url = "https://github.com/xit-vcs/xitdb/archive/6b93af1af3a260399fa0a0b1b268ded2cbf5eec5.tar.gz", .hash = "xitdb-0.0.0-EEFkxhwmBwDz1N8H9oHhVhd1Al5YZp8iDhaMLm2_79pS", + .lazy = true, }, }, .minimum_zig_version = "0.17.0", diff --git a/docs/typ/building.typ b/docs/typ/building.typ index f78f83a9..31019116 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -1,5 +1,5 @@ -// Building pardes, for contributors: platforms, options, tests, the -// release gates and sending patches, then how the pieces fit: the core +// Building pardes, for contributors: platforms, options, the library, tests, +// the release gates and sending patches, then how the pieces fit: the core // and its shells, the threads, the daemon and its sessions, the 9P engine, the // listeners and mounts. #import "style.typ": key, keys, btn, chord, word, tag, addr, file, cmd, doc, pairs @@ -71,6 +71,49 @@ must share with its session; `pardes --help` lists every flag. [`-Dhelix-harness`], [reference executable for live differential tests; `HX_HARNESS`, else `hx-harness` on `PATH`], ) +== As a library <library> + +A Zig package that depends on pardes is given one module and nothing of +the editor: `pardes-syntax` (`src/lib_syntax.zig`), for a program that +draws source text itself and wants it parsed and coloured as pardes has +it. + +``` +// build.zig.zon +.pardes = .{ .path = "../pardes" }, +// build.zig +const pardes = b.dependency("pardes", .{ + .target = target, + .optimize = optimize, + .@"tree-sitter" = .minimal, +}); +exe.root_module.addImport("pardes-syntax", pardes.module("pardes-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. + +#pairs( + [`highlightFileRange`, `analyzeSource`], [a style class for each byte (`Syn`: keyword, string, number, comment or none) and the declarations the sticky headers show, each with its bytes and lines], + [`parse`, `walkNodes`, `ts`], [a text's syntax tree, helix's walks from a byte range to the node about it, its siblings and children, and the tree-sitter bindings the nodes are read through], + [`theme`, `themes`, `page`, `ink`, `synInk`], [a theme by name and what it draws a page, its text and each class in], + [`lineCount`, `lineStartOffset`, `lineSlice`], [a line's bytes from its number], + [`grammars`, `supportsPath`, `pathFor`], [every grammar, and whether this build has a path's], + [`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 +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/lib` is such a +package, and `zig build lib-test` builds and runs it. + = Tests ``` @@ -85,6 +128,8 @@ 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); + unit-test runs it too zig build behaviour-test behaviour through the 9P tree, in process (test/behaviour/), and the T1 runner's short run of random 9P requests zig build process-test real sessions over their sockets: mounts, flags, shells (test/process/), diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index fdc14c43..750b3744 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,16 @@ ## Unreleased +- pardes's syntax is a library: a Zig package that depends on pardes is + given the `pardes-syntax` module (`src/lib_syntax.zig`), with the + 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 + 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. + - Enter, `o` and `O` give the new line helix's indent where the file's grammar has an indent query (bash, C, C#, C++, Clojure, CSS, Elixir, Fortran, Go, HTML, Java, JavaScript, JSON, Kotlin, OCaml, PHP, Python, diff --git a/src/File.zig b/src/File.zig index 05e5271c..dc7b4cf8 100644 --- a/src/File.zig +++ b/src/File.zig @@ -1830,13 +1830,7 @@ pub const DiffTints = struct { }; fn synStyle(p: *Pardes, sy: syntax.Syn) ?SynStyle { - return switch (sy) { - .none => null, - .keyword => .{ .fg = p.theme().kw, .bold = false }, - .string => .{ .fg = p.theme().str, .bold = false }, - .number => .{ .fg = p.theme().num, .bold = false }, - .comment => .{ .fg = p.theme().comment, .bold = false }, - }; + return .{ .fg = Colors.synInk(p.theme(), sy) orelse return null, .bold = false }; } /// syntax colors: recolor each content cell from its tree-sitter style byte; diff --git a/src/colors.zig b/src/colors.zig index bc59c334..e679f4a5 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -281,6 +281,17 @@ pub const Theme = struct { rail_px: ?u8 = null, }; +/// The ink a syntax class is drawn in; a byte of none keeps its text's own. +pub fn synInk(th: *const Theme, sy: @import("syntax.zig").Syn) ?[3]u8 { + return switch (sy) { + .none => null, + .keyword => th.kw, + .string => th.str, + .number => th.num, + .comment => th.comment, + }; +} + /// One family, its twelve backgrounds a theme each (`curated`). const seoul256 = @import("themes/seoul256.zig"); diff --git a/src/lib_syntax.zig b/src/lib_syntax.zig new file mode 100644 index 00000000..395a02f0 --- /dev/null +++ b/src/lib_syntax.zig @@ -0,0 +1,90 @@ +//! pardes's syntax as a library: the `pardes-syntax` module a package that +//! depends on pardes imports (build.zig's `library`, docs/typ/building.typ), +//! for a program that draws source text itself and wants it parsed and +//! 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 +//! 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` +//! gives a path for a language's name. One thread: each language's parser +//! is kept and shared between calls. +const syntax = @import("syntax.zig"); +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 +/// compiled in, and `supportsPath` answers for this build. +pub const grammars = @import("grammar_manifest.zig"); +pub const supportsPath = syntax.supportsPath; + +/// A path that selects the language `name` (a grammar's name or an alias, +/// in any case: "c", "py", "Zig"); null when no grammar goes by it. +pub fn pathFor(name: []const u8) ?[]const u8 { + const i = grammars.byName(name) orelse return null; + inline for (grammars.all, 0..) |g, at| { + if (g.exts.len > 0 and at == i) return "a" ++ g.exts[0]; + } + return null; +} + +/// tree-sitter and the grammars' scanners allocate from `start`'s allocator +/// from then on, and from libc's malloc without it; before any other call +/// here, and once. `stop` frees the parsers and queries kept per language, +/// after a `start` or without one. +pub const start = syntax.start; +pub const stop = syntax.stop; + +/// What a byte is drawn as. pardes has one colour each for keywords, +/// strings, numbers and comments (`synInk`); none is the text's own. +pub const Syn = syntax.Syn; +/// One `Syn` for each byte of `content[start..end]`, that range parsed as a +/// text of its own, the caller's to free; empty where the path has no +/// grammar. +pub const highlightFileRange = syntax.highlightFileRange; +/// A whole text in one parse: its `colors`, a `Syn` a byte, and its +/// `declarations`, the functions, types and tests of more than a line (the +/// editor's sticky headers), in source order, an outer one before its inner. +pub const SourceAnalysis = syntax.SourceAnalysis; +pub const ContextDeclaration = syntax.ContextDeclaration; +pub const analyzeSource = syntax.analyzeSource; + +/// The tree-sitter bindings a tree is read through: `tree.rootNode()`, and +/// a node's `kind()`, `startByte()`, `endByte()`, `child(i)`, `parent()`. +pub const ts = syntax.ts; +/// `content` parsed by its path's grammar, the caller's to `destroy()`; null +/// without a grammar. +pub fn parse(path: []const u8, content: []const u8) ?*ts.Tree { + return @ptrCast(@alignCast(syntax.parseTree(path, content, null))); +} +/// helix's walks from a byte range to the nodes about it: the node that +/// holds it (`expand_node`), a sibling, its children. +pub const NodeWalk = syntax.NodeWalk; +pub const Span = syntax.Span; +pub const walkNodes = syntax.walkNodes; + +pub const Theme = colors.Theme; +/// Every theme: pardes's own, then those imported from helix and zed. The +/// first is the default. +pub const themes: []const Theme = &colors.themes; +/// The theme named `name`, whatever its case. +pub fn theme(name: []const u8) ?*const Theme { + return &colors.themes[colors.themeIndex(name) orelse return null]; +} +/// A theme's page and its text's ink as a file pane has them: its own, or +/// its tag's for the one theme with none (`dark`, a terminal's own). +pub fn page(th: *const Theme) [3]u8 { + return th.bg orelse th.tag_bg; +} +pub fn ink(th: *const Theme) [3]u8 { + return th.fg orelse th.tag_fg; +} +pub const synInk = colors.synInk; + +/// A text's lines by its newlines: how many, the byte line `row` (from 0) +/// starts at, and its bytes. +pub const lineCount = modal.lineCount; +pub const lineStartOffset = modal.lineStartOffset; +pub const lineSlice = modal.lineSlice; diff --git a/test/lib/build.zig b/test/lib/build.zig new file mode 100644 index 00000000..6cd8d6e5 --- /dev/null +++ b/test/lib/build.zig @@ -0,0 +1,18 @@ +//! 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. +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 }); + const tests = b.addTest(.{ .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("lib.zig"), + .imports = &.{.{ .name = "pardes-syntax", .module = pardes.module("pardes-syntax") }}, + }) }); + b.step("test", "colour, outline and walk a C and a Zig source through pardes's library").dependOn(&b.addRunArtifact(tests).step); +} diff --git a/test/lib/build.zig.zon b/test/lib/build.zig.zon new file mode 100644 index 00000000..5bb45dae --- /dev/null +++ b/test/lib/build.zig.zon @@ -0,0 +1,10 @@ +.{ + .name = .pardes_lib_test, + .version = "0.0.0", + .dependencies = .{ + .pardes = .{ .path = "../.." }, + }, + .minimum_zig_version = "0.17.0", + .paths = .{""}, + .fingerprint = 0x3f9948734dd377c7, +} diff --git a/test/lib/lib.zig b/test/lib/lib.zig new file mode 100644 index 00000000..a3f99b56 --- /dev/null +++ b/test/lib/lib.zig @@ -0,0 +1,107 @@ +//! 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 <stdio.h> + \\ + \\// 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, "<stdio.h>", .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 "every name the library has compiles outside pardes" { + std.testing.refAllDecls(pardes); +} |
