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 /src | |
| 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
Diffstat (limited to 'src')
| -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 |
4 files changed, 112 insertions, 7 deletions
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; |
