diff --git a/.cargo/config.toml b/.cargo/config.toml index c3fb16ae..e73e5a39 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,16 +1,9 @@ -# NOTE: this file deliberately does *not* set TWIG_SYS_FORCE_SOURCE. twig-sys -# links the prebuilt libtwig.a shipped by its `twig-sys-` payload crate, -# which needs no Zig toolchain — that is what a consumer of leaf gets, so it is -# what CI and everyday builds should exercise too. Forcing the source build here -# was a leftover from when leaf pinned twig through its own [patch.crates-io]; -# it made every machine, CI included, shell out to `zig`, and it is why the -# Swift-bindings job failed on a runner with no Zig on PATH. -# -# To build against the sibling twig checkout instead, use the org-wide switch: -# copy ~/diaryx/.cargo/patches.toml to ~/diaryx/.cargo/config.toml, uncomment -# `twig-doc`, and set TWIG_SYS_FORCE_SOURCE=1 in that (untracked) file or in the -# environment. Cargo merges config from every ancestor directory, so it reaches -# this checkout without a tracked file changing. +# NOTE: this file deliberately patches nothing. To build against the sibling +# twig checkout instead, use the org-wide switch: copy +# ~/diaryx/.cargo/patches.toml to ~/diaryx/.cargo/config.toml and uncomment +# `twig-doc` and the language crates (that untracked file). Cargo merges config +# from every ancestor directory, so it reaches this checkout without a tracked +# file changing. # `cargo xtask ` — the workspace task runner (xtask/). It builds and runs # the frontends whose build leaves Rust (the Apple app over packages/leaf-swift, diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3793b762..787f2cd1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,15 +35,10 @@ jobs: # Rehearse the release build. homebrew.yml cross-compiles `leaf-tui` to these # four targets on a `v*.*.*` tag, and until this job existed none of it was # exercised until the tag was already pushed — the repo's Rust code was never - # built in CI at all. Two things here are only ever wrong on a cross target: - # - # - The Zig fallback. twig-doc -> twig-sys ships prebuilt payloads for - # aarch64-apple-darwin, x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu, - # x86_64-pc-windows-msvc, and wasm32. `macos-x86_64` is not on that list, so - # that leg alone compiles libtwig.a from vendored Zig source. - # - The toolchain. leaf pins 1.98.1 in rust-toolchain.toml, and a toolchain - # file outranks whatever `rustup default` was set to — so targets have to be - # added to the *resolved* toolchain, not to `stable`. + # built in CI at all. The thing here that is only ever wrong on a cross target + # is the toolchain: leaf pins 1.98.1 in rust-toolchain.toml, and a toolchain + # file outranks whatever `rustup default` was set to — so targets have to be + # added to the *resolved* toolchain, not to `stable`. # # Which is why this installs the target with a bare `rustup target add` from # inside the checkout rather than naming a version: rustup reads @@ -78,10 +73,6 @@ jobs: cross-linker: aarch64-linux-gnu-gcc steps: - uses: actions/checkout@v7 - # Matches homebrew.yml's `zig-version`, and the version twig-sys's vendored - # build.zig expects. - - uses: mlugg/setup-zig@v2 - with: { version: "0.17.0" } - name: Add the target to the pinned toolchain run: rustup target add ${{ matrix.target }} - name: Install cross linker diff --git a/.github/workflows/homebrew.yml b/.github/workflows/homebrew.yml index e280612d..bda28212 100644 --- a/.github/workflows/homebrew.yml +++ b/.github/workflows/homebrew.yml @@ -18,13 +18,6 @@ name: Homebrew # until a tag is pushed. Keep this in step with rust-toolchain.toml; ci.yml # builds with the file, so a drift shows up there first. # -# `needs-zig: true` because leaf depends on twig-doc -> twig-sys, which ships -# prebuilt payload crates for aarch64-apple-darwin, x86_64-unknown-linux-gnu, -# aarch64-unknown-linux-gnu, x86_64-pc-windows-msvc, and wasm32 — and for -# nothing else. The `macos-x86_64` leg of the matrix is not on that list, so its -# build.rs falls back to compiling libtwig.a from the vendored Zig source. One -# of the four legs needs a Zig toolchain; the other three ignore it. -# # No `wasi:` — leaf is a terminal editor that wants a tty, a clipboard, and a # graphics protocol. A wasm32-wasip1 build would compile and be useless. # @@ -45,9 +38,6 @@ jobs: language: rust cargo-package: leaf-tui rust-toolchain: "1.98.1" - needs-zig: true - # Matches ci.yml's setup-zig: twig 4.0.1 builds from source only with 0.17.0. - zig-version: "0.17.0" secrets: TAP_APP_ID: ${{ secrets.TAP_APP_ID }} TAP_APP_KEY: ${{ secrets.TAP_APP_KEY }} diff --git a/Cargo.lock b/Cargo.lock index 085c76c6..c1e1a6aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1076,7 +1076,12 @@ dependencies = [ "anyhow", "fig-css", "syntect", + "twig-asciidoc", + "twig-djot", "twig-doc", + "twig-html", + "twig-markdown", + "twig-xml", "two-face", "unicode-segmentation", "unicode-width 0.2.0", @@ -1116,7 +1121,9 @@ dependencies = [ "ratex-parser", "ratex-svg", "ratex-types", + "twig-djot", "twig-doc", + "twig-markdown", "wasm-bindgen", ] @@ -2631,56 +2638,70 @@ dependencies = [ ] [[package]] -name = "twig-doc" -version = "4.1.0" +name = "twig-asciidoc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e56ba403fc20370917aba95abd9f55767e759f6c37a4589e89277ec4db9c212" +checksum = "3bbaea0529c2c7e65273c20116a4c6a0730c31e3b3fe49dc741a0d085d40e9ea" dependencies = [ - "twig-sys", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys" -version = "4.1.0" +name = "twig-djot" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12fe9573219cc8938498936db35ea33136a4cfed701e82580b58a9535f34d886" +checksum = "d8619b5a7e9b37e5f25463b25d43424c5afea59b0d0478f0bc45e99744af4203" dependencies = [ - "twig-sys-linux-arm64-gnu", - "twig-sys-linux-x64-gnu", - "twig-sys-macos-arm64", - "twig-sys-wasm32", - "twig-sys-windows-x64-msvc", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys-linux-arm64-gnu" -version = "4.1.0" +name = "twig-doc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8009a919f9996c456578b1b92f4b4c2c662967d70e0fe2585829629d380e8536" +checksum = "0cd6cc20c4f31dd31e5e56dfa7d01f3dbdf5692695b1007e8c54640f9766193f" +dependencies = [ + "twig-kit", +] [[package]] -name = "twig-sys-linux-x64-gnu" -version = "4.1.0" +name = "twig-html" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6f3c05d0632ccc214df9c0ca1c9c37dc75e5ad0409a9b74a03e48839ed9a8b" +checksum = "1beb6c0999dc04d89a5a0da40e585cb05a9a7c13ecb19c2e5a65085a147f62ca" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] -name = "twig-sys-macos-arm64" -version = "4.1.0" +name = "twig-kit" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac61d2868353e8d37d5c0b05949da2f7800d2298f52076ef2dfc23489e74c269" +checksum = "1bc21949975c1e3c5ab858d0d104e9d4f4d6000e1dcfa9e46bb7dcc460bba24f" [[package]] -name = "twig-sys-wasm32" -version = "4.1.0" +name = "twig-markdown" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d052dd11d1a9ccb25294ef5546c3063ef72fb7caa99704397665745c01db00b9" +checksum = "60ffcae233483874bec0f3c39f8a2480a087d71beb038049a9ee287e0e570745" +dependencies = [ + "twig-doc", + "twig-html", + "twig-kit", +] [[package]] -name = "twig-sys-windows-x64-msvc" -version = "4.1.0" +name = "twig-xml" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfca715feba0b62dfb5473104c0b0a9fc5276323efd36c31f1d73ef07ba9be57" +checksum = "8634fdd8e854401a1f21aef62ef80e3a87d1723ad59e810f6a633c66bf2c42c6" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] name = "two-face" diff --git a/Cargo.toml b/Cargo.toml index 82d5f74f..a0549e80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,8 +40,19 @@ repository = "https://github.com/diaryx-org/leaf" readme = "README.md" [workspace.dependencies] -# The document AST + offset-addressed span-splice editor. -twig-doc = "4.1.0" +# The document AST + offset-addressed span-splice editor. twig is pure Rust +# since 6.0, and its core names no language: each one leaf opens is a crate +# that registers itself with twig on first use (`twig_markdown::format()`), and +# leaf's own `leaf_core::Format` is what picks among them. Without twig-doc's +# default `fig` feature: that is `twig::meta` (frontmatter read and edited +# through fig, and a document's `twig:` key obeyed), which leaf never asks for, +# and it would bring fig's YAML, TOML, JSON and figl parsers with it. +twig-doc = { version = "6", default-features = false } +twig-markdown = "6" +twig-djot = "6" +twig-html = "6" +twig-asciidoc = "6" +twig-xml = "6" # The CSS inside a `style` attribute: the presentation vocabulary is written # there, so another app renders it, and every edit to it keeps the declarations # leaf did not write. Shared with thorn, which edits an SVG's `style` the same way. diff --git a/apps/leaf-dioxus/Cargo.lock b/apps/leaf-dioxus/Cargo.lock index 5857a154..112c7e54 100644 --- a/apps/leaf-dioxus/Cargo.lock +++ b/apps/leaf-dioxus/Cargo.lock @@ -2650,7 +2650,12 @@ dependencies = [ "anyhow", "fig-css", "syntect", + "twig-asciidoc", + "twig-djot", "twig-doc", + "twig-html", + "twig-markdown", + "twig-xml", "two-face", "unicode-segmentation", "unicode-width", @@ -5132,56 +5137,70 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31" [[package]] -name = "twig-doc" -version = "4.1.0" +name = "twig-asciidoc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e56ba403fc20370917aba95abd9f55767e759f6c37a4589e89277ec4db9c212" +checksum = "3bbaea0529c2c7e65273c20116a4c6a0730c31e3b3fe49dc741a0d085d40e9ea" dependencies = [ - "twig-sys", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys" -version = "4.1.0" +name = "twig-djot" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12fe9573219cc8938498936db35ea33136a4cfed701e82580b58a9535f34d886" +checksum = "d8619b5a7e9b37e5f25463b25d43424c5afea59b0d0478f0bc45e99744af4203" dependencies = [ - "twig-sys-linux-arm64-gnu", - "twig-sys-linux-x64-gnu", - "twig-sys-macos-arm64", - "twig-sys-wasm32", - "twig-sys-windows-x64-msvc", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys-linux-arm64-gnu" -version = "4.1.0" +name = "twig-doc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8009a919f9996c456578b1b92f4b4c2c662967d70e0fe2585829629d380e8536" +checksum = "0cd6cc20c4f31dd31e5e56dfa7d01f3dbdf5692695b1007e8c54640f9766193f" +dependencies = [ + "twig-kit", +] [[package]] -name = "twig-sys-linux-x64-gnu" -version = "4.1.0" +name = "twig-html" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6f3c05d0632ccc214df9c0ca1c9c37dc75e5ad0409a9b74a03e48839ed9a8b" +checksum = "1beb6c0999dc04d89a5a0da40e585cb05a9a7c13ecb19c2e5a65085a147f62ca" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] -name = "twig-sys-macos-arm64" -version = "4.1.0" +name = "twig-kit" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac61d2868353e8d37d5c0b05949da2f7800d2298f52076ef2dfc23489e74c269" +checksum = "1bc21949975c1e3c5ab858d0d104e9d4f4d6000e1dcfa9e46bb7dcc460bba24f" [[package]] -name = "twig-sys-wasm32" -version = "4.1.0" +name = "twig-markdown" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d052dd11d1a9ccb25294ef5546c3063ef72fb7caa99704397665745c01db00b9" +checksum = "60ffcae233483874bec0f3c39f8a2480a087d71beb038049a9ee287e0e570745" +dependencies = [ + "twig-doc", + "twig-html", + "twig-kit", +] [[package]] -name = "twig-sys-windows-x64-msvc" -version = "4.1.0" +name = "twig-xml" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfca715feba0b62dfb5473104c0b0a9fc5276323efd36c31f1d73ef07ba9be57" +checksum = "8634fdd8e854401a1f21aef62ef80e3a87d1723ad59e810f6a633c66bf2c42c6" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] name = "two-face" diff --git a/apps/leaf-editor/ci_scripts/ci_post_clone.sh b/apps/leaf-editor/ci_scripts/ci_post_clone.sh index d33205d1..4415842a 100755 --- a/apps/leaf-editor/ci_scripts/ci_post_clone.sh +++ b/apps/leaf-editor/ci_scripts/ci_post_clone.sh @@ -16,7 +16,6 @@ set -euo pipefail ROOT="${CI_PRIMARY_REPOSITORY_PATH:?ci_post_clone.sh must run under Xcode Cloud}" -ZIG_VERSION="0.16.0" # twig's minimum_zig_version echo "▸ Repository: $ROOT" echo "▸ Platform: ${CI_PRODUCT_PLATFORM:-unknown}" @@ -43,30 +42,6 @@ rustc --version echo "▸ Adding Rust targets…" rustup target add aarch64-apple-darwin aarch64-apple-ios aarch64-apple-ios-sim -# Zig. twig-sys ships a prebuilt library for macOS but not for iOS, and -# compiles twig's Zig core from source there. Installed under $HOME/.local/bin, -# which the pre-build phase puts on PATH. -echo "▸ Installing Zig ${ZIG_VERSION}…" -ZIG_PREFIX="$HOME/.local/zig-$ZIG_VERSION" -if [ ! -x "$ZIG_PREFIX/zig" ]; then - ZIG_URL="$(curl -fsSL https://ziglang.org/download/index.json | /usr/bin/python3 -c " -import json, sys -release = json.load(sys.stdin)['$ZIG_VERSION'] -for key in ('aarch64-macos', 'macos-aarch64'): - if key in release: - print(release[key]['tarball']) - break -else: - sys.exit('no aarch64 macOS tarball for Zig $ZIG_VERSION') -")" - mkdir -p "$ZIG_PREFIX" - curl -fsSL "$ZIG_URL" | tar -xJ --strip-components=1 -C "$ZIG_PREFIX" -fi -mkdir -p "$HOME/.local/bin" -ln -sf "$ZIG_PREFIX/zig" "$HOME/.local/bin/zig" -export PATH="$HOME/.local/bin:$PATH" -zig version - # CFBundleVersion. project.yml carries 1 for local builds; App Store Connect # refuses a second upload under one number, so a cloud build takes Xcode # Cloud's own counter, which the iOS and macOS workflows share. The clone is diff --git a/apps/leaf/Cargo.lock b/apps/leaf/Cargo.lock index b8c04575..b717a0ba 100644 --- a/apps/leaf/Cargo.lock +++ b/apps/leaf/Cargo.lock @@ -1617,6 +1617,38 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "fig" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1912585cea7b84fd1cbacdd0ee85c57b12f54cc46a917145d97a62b95c3eafab" +dependencies = [ + "fig-core", +] + +[[package]] +name = "fig-core" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "310da8e88299b38abe3aadfa1aecce5c46a93dc767ee38900f23e9f630e05290" +dependencies = [ + "fig-kit", +] + +[[package]] +name = "fig-css" +version = "0.1.0" +source = "git+https://github.com/diaryx-org/fig-css?branch=main#d2400882084ed83510f452ded0603cf1d05fc611" +dependencies = [ + "fig", +] + +[[package]] +name = "fig-kit" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0005cbc0579d07474d9a452c76f23cbf55ba6c82dd84c7e7ed5c38ace903a7a" + [[package]] name = "find-msvc-tools" version = "0.1.14" @@ -2979,8 +3011,14 @@ name = "leaf-core" version = "0.5.1" dependencies = [ "anyhow", + "fig-css", "syntect", + "twig-asciidoc", + "twig-djot", "twig-doc", + "twig-html", + "twig-markdown", + "twig-xml", "two-face", "unicode-segmentation", "unicode-width", @@ -5926,56 +5964,70 @@ dependencies = [ ] [[package]] -name = "twig-doc" -version = "4.1.0" +name = "twig-asciidoc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e56ba403fc20370917aba95abd9f55767e759f6c37a4589e89277ec4db9c212" +checksum = "3bbaea0529c2c7e65273c20116a4c6a0730c31e3b3fe49dc741a0d085d40e9ea" dependencies = [ - "twig-sys", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys" -version = "4.1.0" +name = "twig-djot" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12fe9573219cc8938498936db35ea33136a4cfed701e82580b58a9535f34d886" +checksum = "d8619b5a7e9b37e5f25463b25d43424c5afea59b0d0478f0bc45e99744af4203" dependencies = [ - "twig-sys-linux-arm64-gnu", - "twig-sys-linux-x64-gnu", - "twig-sys-macos-arm64", - "twig-sys-wasm32", - "twig-sys-windows-x64-msvc", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys-linux-arm64-gnu" -version = "4.1.0" +name = "twig-doc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8009a919f9996c456578b1b92f4b4c2c662967d70e0fe2585829629d380e8536" +checksum = "0cd6cc20c4f31dd31e5e56dfa7d01f3dbdf5692695b1007e8c54640f9766193f" +dependencies = [ + "twig-kit", +] [[package]] -name = "twig-sys-linux-x64-gnu" -version = "4.1.0" +name = "twig-html" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6f3c05d0632ccc214df9c0ca1c9c37dc75e5ad0409a9b74a03e48839ed9a8b" +checksum = "1beb6c0999dc04d89a5a0da40e585cb05a9a7c13ecb19c2e5a65085a147f62ca" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] -name = "twig-sys-macos-arm64" -version = "4.1.0" +name = "twig-kit" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac61d2868353e8d37d5c0b05949da2f7800d2298f52076ef2dfc23489e74c269" +checksum = "1bc21949975c1e3c5ab858d0d104e9d4f4d6000e1dcfa9e46bb7dcc460bba24f" [[package]] -name = "twig-sys-wasm32" -version = "4.1.0" +name = "twig-markdown" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d052dd11d1a9ccb25294ef5546c3063ef72fb7caa99704397665745c01db00b9" +checksum = "60ffcae233483874bec0f3c39f8a2480a087d71beb038049a9ee287e0e570745" +dependencies = [ + "twig-doc", + "twig-html", + "twig-kit", +] [[package]] -name = "twig-sys-windows-x64-msvc" -version = "4.1.0" +name = "twig-xml" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfca715feba0b62dfb5473104c0b0a9fc5276323efd36c31f1d73ef07ba9be57" +checksum = "8634fdd8e854401a1f21aef62ef80e3a87d1723ad59e810f6a633c66bf2c42c6" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] name = "two-face" diff --git a/crates/leaf-core/Cargo.toml b/crates/leaf-core/Cargo.toml index 792156c8..40e40e6b 100644 --- a/crates/leaf-core/Cargo.toml +++ b/crates/leaf-core/Cargo.toml @@ -27,6 +27,12 @@ syntax = ["dep:syntect", "dep:two-face"] [dependencies] twig-doc.workspace = true +# The languages a `Format` names, each registered with twig on first use. +twig-markdown.workspace = true +twig-djot.workspace = true +twig-html.workspace = true +twig-asciidoc.workspace = true +twig-xml.workspace = true fig-css.workspace = true anyhow.workspace = true unicode-segmentation = "1.13.3" diff --git a/crates/leaf-core/examples/bench.rs b/crates/leaf-core/examples/bench.rs index 94a5827d..b3b5a596 100644 --- a/crates/leaf-core/examples/bench.rs +++ b/crates/leaf-core/examples/bench.rs @@ -1,10 +1,10 @@ //! Where does leaf's per-keystroke and per-paint time actually go? //! //! `cargo run --release -p leaf-core --example bench` -use leaf_core::{Doc, View, source, wysiwyg}; +use leaf_core::{Doc, Format, View, source, wysiwyg}; use std::hash::{Hash, Hasher}; use std::time::Instant; -use twig::{Editor, Format}; +use twig::Editor; fn body(bytes: usize) -> String { let mut s = String::new(); @@ -35,7 +35,7 @@ fn main() { let src = body(kb * 1024); println!("=== {} KB ===", src.len() / 1024); - let mut ed = Editor::new_str(&src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(&src, Format::Markdown.twig()).unwrap(); let nodes = ed.nodes().unwrap(); let map = wysiwyg::build( &nodes, @@ -59,7 +59,7 @@ fn main() { // cost leaf pays. Skipping it used to end every run in a slice panic // (`push_escaped_text`, walking a span past the end of a shorter source) // and would otherwise have quietly measured a build over a mismatch. - let mut ed = Editor::new_str(&src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(&src, Format::Markdown.twig()).unwrap(); time("twig nodes() FFI marshal", 5, || ed.nodes().unwrap().len()); time("wysiwyg::build", 5, || { wysiwyg::build( diff --git a/crates/leaf-core/src/doc/mod.rs b/crates/leaf-core/src/doc/mod.rs index 1a53bb30..1bf2241b 100644 --- a/crates/leaf-core/src/doc/mod.rs +++ b/crates/leaf-core/src/doc/mod.rs @@ -33,11 +33,12 @@ use std::path::PathBuf; use anyhow::Context; use anyhow::{Result, anyhow}; use twig::{ - Alignment, BlockContainerKind, BlockKind, Change, Editor, FlatNode, Format, Gesture, - InlineKind, Kind, MarkdownExtensions, NodeId, QueryMatch, + Alignment, BlockContainerKind, BlockKind, Change, Editor, FlatNode, Gesture, InlineKind, Kind, + NodeId, QueryMatch, }; use unicode_segmentation::GraphemeCursor; +use crate::Format; use crate::counts::{self, TextCounts}; use crate::html; use crate::source::{self, SourceMap}; @@ -822,24 +823,29 @@ pub struct Doc { /// twig's own rule that a dollar followed by whitespace never opens math, so /// `$5 and $6` stays prose. /// -/// Every flag is inert for non-Markdown formats, so it's safe to pass them -/// unconditionally. Threading this through every constructor (not just `open`) -/// keeps `from_source`, `blank`, and `reload` parsing the same document the same -/// way — twig reparses with these same flags after each edit. -pub(crate) fn parse_extensions() -> MarkdownExtensions { - MarkdownExtensions { - html_elements: true, - directives: true, - highlight: true, - highlight_colors: true, - math: true, - } +/// They are Markdown's features; no other language leaf opens declares any, so +/// every other format's mask is empty. Threading this through every +/// constructor (not just `open`) keeps `from_source`, `blank`, and `reload` +/// parsing the same document the same way — twig reparses with these same +/// flags after each edit. +pub(crate) fn parse_extensions(format: Format) -> u32 { + const FEATURES: &[&str] = &[ + "html_elements", + "directives", + "highlight", + "highlight_colors", + "math", + ]; + // `UnsupportedFormat` is a language with no features at all — the empty + // mask is exactly what it parses with. + format.twig().feature_flags(FEATURES).unwrap_or(0) } /// Build an editor over `bytes` in `format` with leaf's [`parse_extensions`], /// mapping twig's error into the `anyhow` context every constructor shares. fn new_editor(bytes: &[u8], format: Format) -> Result { - Editor::new_ext(bytes, format, parse_extensions()).map_err(|e| anyhow!("twig parse: {e}")) + Editor::new_with_flags(bytes, format.twig(), parse_extensions(format)) + .map_err(|e| anyhow!("twig parse: {e}")) } /// Does `format` spell a table as a **pipe table** — the one grid twig's table @@ -1018,8 +1024,8 @@ impl Capabilities { /// caller makes: every leaf document is parsed with [`parse_extensions`], /// so the format is the whole of what varies. pub fn of(format: Format) -> Self { - let exts = parse_extensions(); - let supports = |g| format.supports_with(exts, g); + let flags = parse_extensions(format); + let supports = |g| format.supports_with(flags, g); let inline = |k| supports(Gesture::ToggleInline(k)); let container = |k| supports(Gesture::ToggleBlockContainer(k)); Self { diff --git a/crates/leaf-core/src/doc/state.rs b/crates/leaf-core/src/doc/state.rs index 60eae046..e7a7e230 100644 --- a/crates/leaf-core/src/doc/state.rs +++ b/crates/leaf-core/src/doc/state.rs @@ -685,9 +685,9 @@ impl Doc { } pub fn format_name(&self) -> &'static str { - // `Format` is `#[non_exhaustive]` as of twig 3.0, so the wildcard is - // required. It also covers `Asciidoc`, which twig parses but cannot - // serialize — leaf never opens a document in it (see `Doc::open`). + // The wildcard is `Asciidoc`, which twig parses but leaf never opens a + // document in (see `Doc::open`) — and any language `Format` learns + // later, before this learns its name. match self.format { Format::Djot => "djot", Format::Markdown => "markdown", @@ -723,7 +723,8 @@ impl Doc { /// `UnsupportedFormat`. Gray out on `false`; don't read `true` as "this /// will work here". pub fn supports(&self, gesture: Gesture) -> bool { - self.format.supports_with(parse_extensions(), gesture) + self.format + .supports_with(parse_extensions(self.format), gesture) } /// Every control's enabled state in one read — what a toolbar builds itself diff --git a/crates/leaf-core/src/doc/tests/enter.rs b/crates/leaf-core/src/doc/tests/enter.rs index 2403711b..73b64428 100644 --- a/crates/leaf-core/src/doc/tests/enter.rs +++ b/crates/leaf-core/src/doc/tests/enter.rs @@ -219,8 +219,12 @@ fn enter_and_backspace_anywhere_show_what_they_write_and_undo_in_one_step() { // edited map differing from it is a view that changes on the // next full rebuild, seconds after the key, with no key. let fresh = |source: &str| { - let mut ed = - twig::Editor::new_ext(source.as_bytes(), format, parse_extensions()).unwrap(); + let mut ed = twig::Editor::new_with_flags( + source.as_bytes(), + format.twig(), + parse_extensions(format), + ) + .unwrap(); let nodes = ed.nodes().unwrap(); crate::wysiwyg::build( &nodes, diff --git a/crates/leaf-core/src/doc/tests/mod.rs b/crates/leaf-core/src/doc/tests/mod.rs index b6c6fca1..f9d396d5 100644 --- a/crates/leaf-core/src/doc/tests/mod.rs +++ b/crates/leaf-core/src/doc/tests/mod.rs @@ -181,8 +181,12 @@ fn reference_map_revealing(source: &str, reveal: Option) -> crate::wysiw // sides disagree on what the *document* is before the renderer is even // reached — a bare `:word` is a text directive to one and prose to the // other — and the mismatch reads as a splice bug that isn't one. - let mut ed = - twig::Editor::new_ext(source.as_bytes(), Format::Markdown, parse_extensions()).unwrap(); + let mut ed = twig::Editor::new_with_flags( + source.as_bytes(), + Format::Markdown.twig(), + parse_extensions(Format::Markdown), + ) + .unwrap(); let nodes = ed.nodes().unwrap(); crate::wysiwyg::build( &nodes, diff --git a/crates/leaf-core/src/doc/tests/presentation.rs b/crates/leaf-core/src/doc/tests/presentation.rs index d152bf68..aa66c84a 100644 --- a/crates/leaf-core/src/doc/tests/presentation.rs +++ b/crates/leaf-core/src/doc/tests/presentation.rs @@ -774,7 +774,9 @@ fn directives_are_offered_only_where_the_walker_reads_them_back() { (Format::Asciidoc, "hello\n"), ] { // What twig writes for a host's name, read back by the walker. - let mut ed = twig::Editor::new_ext(src.as_bytes(), fmt, parse_extensions()).unwrap(); + let mut ed = + twig::Editor::new_with_flags(src.as_bytes(), fmt.twig(), parse_extensions(fmt)) + .unwrap(); ed.insert_directive(src.len() - 1, "x-card", None, &[("k", Some("v"))]) .unwrap_or_else(|e| panic!("{fmt:?}: twig spells it: {e}")); let written = String::from_utf8(ed.source().unwrap()).unwrap(); diff --git a/crates/leaf-core/src/format.rs b/crates/leaf-core/src/format.rs new file mode 100644 index 00000000..0d1a5d75 --- /dev/null +++ b/crates/leaf-core/src/format.rs @@ -0,0 +1,78 @@ +//! The document languages leaf opens. +//! +//! Up to twig 4, `twig::Format` was an enum naming every language twig +//! parsed, and leaf passed it through as its own. Since 6.0 twig's core names +//! none: a language is a crate (`twig-markdown`, `twig-djot`, …) that +//! registers itself on first use and hands back an opaque `twig::Format` +//! handle. [`Format`] is leaf's own enum over the languages it links, under +//! the variant names it always had, and [`Format::twig`] is the handle twig +//! is called with. + +use twig::Gesture; + +/// A language a leaf document is written in. +/// +/// `#[non_exhaustive]` as `twig::Format` was before it: a language leaf +/// learns later is not a breaking change to a host that matches on this. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum Format { + Markdown, + Djot, + Html, + /// Parsed, and authorable in part; leaf never opens a file in it by + /// extension (see `Doc::open`). + Asciidoc, + /// Parse-only: every gesture refuses. + Xml, +} + +impl Format { + /// twig's handle for this language, registering it on first use. + pub fn twig(self) -> twig::Format { + match self { + Format::Markdown => twig_markdown::format(), + Format::Djot => twig_djot::format(), + Format::Html => twig_html::format(), + Format::Asciidoc => twig_asciidoc::format(), + Format::Xml => twig_xml::format(), + } + } + + /// twig's name for this language (`"markdown"`, `"djot"`, …). + pub fn name(self) -> &'static str { + self.twig().name() + } + + /// Whether this format can spell `gesture` with no features laid over it. + /// What a leaf document can spell is [`Format::supports_with`] under + /// leaf's own flags — [`Capabilities::of`](crate::Capabilities::of) + /// answers that per control. + pub fn supports(self, gesture: Gesture) -> bool { + self.twig().supports(gesture) + } + + /// Whether this format can spell `gesture` under the feature mask `flags` + /// (twig's `Format::feature_flags`). + pub fn supports_with(self, flags: u32, gesture: Gesture) -> bool { + self.twig().supports_flags(flags, gesture) + } + + /// Whether this format can be authored into at all — `false` for a + /// parse-only format (XML). + pub fn is_authorable(self) -> bool { + self.twig().is_authorable() + } +} + +impl From for twig::Format { + fn from(format: Format) -> Self { + format.twig() + } +} + +impl std::fmt::Display for Format { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.name()) + } +} diff --git a/crates/leaf-core/src/html.rs b/crates/leaf-core/src/html.rs index f40ccb92..551e25b9 100644 --- a/crates/leaf-core/src/html.rs +++ b/crates/leaf-core/src/html.rs @@ -34,7 +34,8 @@ //! over the input nodes and the output text. twig 3.0 exposes the library's own //! measured answer, so all three are gone; see [`parse_fragment`]. -use twig::{Document, Fidelity, Format, Kind, Target, Warning}; +use crate::Format; +use twig::{Document, Fidelity, Kind, Target, Warning}; /// Render a `format` source fragment to HTML, for the clipboard's `text/html`. /// @@ -42,7 +43,7 @@ use twig::{Document, Fidelity, Format, Kind, Target, Warning}; /// document is a real possibility and not an error worth reporting: the caller /// still has the plain flavor, which is what the user copied. pub(crate) fn render_fragment(source: &str, format: Format) -> Option { - let mut doc = Document::parse_str(source, format).ok()?; + let mut doc = Document::parse_str(source, format.twig()).ok()?; let html = String::from_utf8(doc.render_html().ok()?).ok()?; let html = html.trim(); (!html.is_empty()).then(|| html.to_string()) @@ -52,7 +53,7 @@ pub(crate) fn render_fragment(source: &str, format: Format) -> Option { /// not survive the trip well enough to paste — see the diagnostics gate below. pub(crate) fn parse_fragment(html: &str, format: Format) -> Option { let cleaned = sanitize(html); - let mut doc = Document::parse_str(&cleaned, Format::Html).ok()?; + let mut doc = Document::parse_str(&cleaned, Format::Html.twig()).ok()?; // Would this conversion lose anything? twig 3.0 answers that directly, and // it is the whole of the faithfulness test: a paste that degrades is worse @@ -85,12 +86,12 @@ pub(crate) fn parse_fragment(html: &str, format: Format) -> Option { // plain flavor's cue. Note that arboard wraps leaf's *own* copy in // `…`, so a leaf→leaf paste is a section every // time. - let warnings = doc.diagnostics(Target::from(format)).ok()?; + let warnings = doc.diagnostics(Target::from(format.twig())).ok()?; if warnings.iter().any(would_leak) { return None; } - let source = String::from_utf8(doc.serialize(format).ok()?).ok()?; + let source = String::from_utf8(doc.serialize(format.twig()).ok()?).ok()?; // twig's serializers terminate a block with a newline, because a document // ends in one. A paste is not a document — it lands at a caret, usually diff --git a/crates/leaf-core/src/lib.rs b/crates/leaf-core/src/lib.rs index c48fcbae..c0d3f8d3 100644 --- a/crates/leaf-core/src/lib.rs +++ b/crates/leaf-core/src/lib.rs @@ -29,6 +29,7 @@ pub mod counts; pub mod doc; +pub mod format; pub mod frame; mod html; pub mod source; @@ -64,4 +65,5 @@ pub use wysiwyg::{ // `Gesture` names one authoring capability for `Doc::supports`, the finer-grained // half of `Capabilities` — a frontend wanting a mark leaf's own toolbar doesn't // offer asks with one of these. -pub use twig::{Alignment, BlockContainerKind, BlockKind, Format, Gesture, InlineKind}; +pub use format::Format; +pub use twig::{Alignment, BlockContainerKind, BlockKind, Gesture, InlineKind}; diff --git a/crates/leaf-core/src/source.rs b/crates/leaf-core/src/source.rs index 05aad27f..80f0e7fb 100644 --- a/crates/leaf-core/src/source.rs +++ b/crates/leaf-core/src/source.rs @@ -416,12 +416,13 @@ fn style_of(node: &FlatNode, base: Style) -> Style { #[cfg(test)] mod tests { use super::*; - use twig::{Editor, Format}; + use crate::Format; + use twig::Editor; /// Build a map the way `Doc` does, and hand back the source alongside it so /// assertions can name bytes by the text they cover rather than by offset. fn map(src: &str, format: Format) -> SourceMap { - let mut ed = Editor::new_str(src, format).unwrap(); + let mut ed = Editor::new_str(src, format.twig()).unwrap(); let nodes = ed.nodes().unwrap(); build(&nodes, src) } @@ -486,10 +487,10 @@ mod tests { // Markdown *extension*, and a map built without them would show the // literal text this test would then be asserting nothing about. let src = "a ==🔴 red== b\n"; - let mut ed = twig::Editor::new_ext( + let mut ed = twig::Editor::new_with_flags( src.as_bytes(), - Format::Markdown, - crate::doc::parse_extensions(), + Format::Markdown.twig(), + crate::doc::parse_extensions(Format::Markdown), ) .unwrap(); let m = build(&ed.nodes().unwrap(), src); @@ -776,7 +777,7 @@ mod tests { /// place to find out. #[test] fn a_span_past_the_end_of_the_source_is_clipped_not_panicked() { - let mut ed = Editor::new_str("# H\n", Format::Markdown).unwrap(); + let mut ed = Editor::new_str("# H\n", Format::Markdown.twig()).unwrap(); let nodes = ed.nodes().unwrap(); let m = build(&nodes, "# "); for run in m.runs() { diff --git a/crates/leaf-core/src/wysiwyg/builder/blocks.rs b/crates/leaf-core/src/wysiwyg/builder/blocks.rs index 04f86949..d5d16eba 100644 --- a/crates/leaf-core/src/wysiwyg/builder/blocks.rs +++ b/crates/leaf-core/src/wysiwyg/builder/blocks.rs @@ -460,7 +460,7 @@ impl Builder<'_> { // `diaryx_core::visibility`'s hand-rolled publish-time filter and // apps/web's directive serializer both write; twig parses each // bare word as its own attribute with an empty value, per - // `languages/markdown/attributes.zig`). Reading only `.class` + // twig-markdown's `attributes.rs`). Reading only `.class` // would leave every *existing* diaryx `:::vis{...}` block // unlabeled. // Only the *container* form is the panel below. A `text` directive diff --git a/crates/leaf-core/src/wysiwyg/tests/block_boundaries.rs b/crates/leaf-core/src/wysiwyg/tests/block_boundaries.rs index 878d7e98..b857e2ad 100644 --- a/crates/leaf-core/src/wysiwyg/tests/block_boundaries.rs +++ b/crates/leaf-core/src/wysiwyg/tests/block_boundaries.rs @@ -88,7 +88,7 @@ fn the_lines_above_the_first_block_draw_as_the_lines_under_the_last_do() { // Preserve flow draws every line, but the frontmatter's. let preserve = |src: &str| { - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); build( &ed.nodes().unwrap(), src, diff --git a/crates/leaf-core/src/wysiwyg/tests/directives.rs b/crates/leaf-core/src/wysiwyg/tests/directives.rs index efd59bb8..61acffb4 100644 --- a/crates/leaf-core/src/wysiwyg/tests/directives.rs +++ b/crates/leaf-core/src/wysiwyg/tests/directives.rs @@ -321,14 +321,10 @@ fn a_container_directive_is_still_a_panel_not_a_placeholder() { /// Every `container` node in `src`, parsed the way production does (both /// extensions on), paired with what [`container_is_directive`] makes of it. fn containers(src: &str) -> Vec<(String, bool, Option)> { - let mut ed = Editor::new_ext( + let mut ed = Editor::new_with_features( src.as_bytes(), - Format::Markdown, - twig::MarkdownExtensions { - directives: true, - html_elements: true, - ..Default::default() - }, + Format::Markdown.twig(), + &["directives", "html_elements"], ) .unwrap(); ed.nodes() diff --git a/crates/leaf-core/src/wysiwyg/tests/display_columns.rs b/crates/leaf-core/src/wysiwyg/tests/display_columns.rs index 34770e6f..3042511a 100644 --- a/crates/leaf-core/src/wysiwyg/tests/display_columns.rs +++ b/crates/leaf-core/src/wysiwyg/tests/display_columns.rs @@ -33,7 +33,7 @@ fn a_cell_wrapped_narrow_never_breaks_inside_a_character() { // where a click or Down can reach it and the next Backspace takes the // cluster apart from the middle. let src = "| A |\n|---|\n| 👨‍👩‍👧👨‍👩‍👧 |\n"; - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let m = build_t(&ed.nodes().unwrap(), src, Some(8)); let boundaries: Vec = src .grapheme_indices(true) @@ -55,7 +55,7 @@ fn a_wrapped_cell_keeps_every_line_inside_its_column() { // on the border or in the next cell — and it is a promise about cells, // which is not what a count of glyphs measures. let src = "| A |\n|---|\n| 你好世界漢字 |\n"; - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let m = build_t(&ed.nodes().unwrap(), src, Some(14)); for r in &m.rows { assert_eq!(r.width(), 14, "{:?} is not drawn to the grid", rendered(&m)); diff --git a/crates/leaf-core/src/wysiwyg/tests/footnotes.rs b/crates/leaf-core/src/wysiwyg/tests/footnotes.rs index 136593ed..ce302900 100644 --- a/crates/leaf-core/src/wysiwyg/tests/footnotes.rs +++ b/crates/leaf-core/src/wysiwyg/tests/footnotes.rs @@ -139,7 +139,7 @@ fn a_footnote_definition_wearing_a_long_label_indents_its_wrapped_body() { /// [`map`] on a surface that raises a superscript — a pixel frontend's. fn map_raised(src: &str, wrap: Option) -> VisualMap { - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let surface = Surface { raised_superscripts: true, ..Default::default() @@ -185,7 +185,7 @@ fn a_plain_surface_keeps_the_brackets_a_terminal_needs() { #[test] fn a_surface_that_raises_keeps_dropping_a_verses_number() { let src = "{#v20}\n*20* And they did plead[^20a] wept[^20].\n\n[^20a]: note.\n\n[^20]: note.\n"; - let mut ed = Editor::new_str(src, Format::Djot).unwrap(); + let mut ed = Editor::new_str(src, Format::Djot.twig()).unwrap(); let surface = Surface { raised_superscripts: true, ..Default::default() diff --git a/crates/leaf-core/src/wysiwyg/tests/hidden_blocks.rs b/crates/leaf-core/src/wysiwyg/tests/hidden_blocks.rs index b0b6a48b..cbecbeab 100644 --- a/crates/leaf-core/src/wysiwyg/tests/hidden_blocks.rs +++ b/crates/leaf-core/src/wysiwyg/tests/hidden_blocks.rs @@ -95,7 +95,7 @@ fn the_cached_build_does_not_spell_the_document_out_as_blank_rows_after_a_commen .collect::>() .join("\n\n"); let src = format!("intro\n\n\n{body}\n"); - let mut ed = Editor::new_str(&src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(&src, Format::Markdown.twig()).unwrap(); let mut cache = BlockCache::default(); let (plain, cached) = render_both(&mut ed, &src, Some(80), &mut cache); assert_maps_eq(&plain, &cached, "comment then 200 paragraphs"); @@ -302,7 +302,7 @@ fn the_incremental_walk_labels_boundaries_like_the_full_one() { // build, a query match's on the cached one — against a document with one // of every boundary in it. let src = "one\n\n# Head\n\ntwo\n\n- a\n- b\n\n> q\n\n```\nc\n```\n\npara\n"; - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let mut cache = BlockCache::default(); let (full, cached) = render_both(&mut ed, src, Some(80), &mut cache); assert_maps_eq(&full, &cached, "boundary labelling"); diff --git a/crates/leaf-core/src/wysiwyg/tests/mapping.rs b/crates/leaf-core/src/wysiwyg/tests/mapping.rs index 08491a76..e4dd3b63 100644 --- a/crates/leaf-core/src/wysiwyg/tests/mapping.rs +++ b/crates/leaf-core/src/wysiwyg/tests/mapping.rs @@ -18,7 +18,7 @@ fn a_source_shorter_than_the_arena_built_over_it_renders_rather_than_panicking() // An escape puts the run on `push_escaped_text`'s slow path — the fast // path is a length comparison that a truncated source fails anyway. let src = "alpha \\*beta\\* gamma delta epsilon\n"; - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let nodes = ed.nodes().unwrap(); // Every truncation of it, so the cut lands before, inside and after the @@ -150,7 +150,7 @@ fn build_cached_matches_build() { for wrap in [None, Some(80usize), Some(20)] { for src in docs { let ctx = format!("wrap={wrap:?} src={src:?}"); - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let mut cache = BlockCache::default(); // 1) Fresh cache equals the cache-free build. @@ -195,7 +195,7 @@ fn a_block_running_past_the_last_byte_still_keys_the_cache_by_its_own_bytes() { // served the first's rows, and the definition rendered as a copy of the // heading. let src = "A claim[^1] worth checking.\n\n# A heading with a reference[^1] in it\n\n[^1]: The first note.\n[^note]: A note with a word for a label."; - let mut ed = Editor::new_str(src, Format::Djot).unwrap(); + let mut ed = Editor::new_str(src, Format::Djot.twig()).unwrap(); let (plain, cached) = render_both(&mut ed, src, Some(80), &mut BlockCache::default()); assert_maps_eq(&plain, &cached, "a definition ending the file"); let text = rendered(&cached); @@ -215,12 +215,12 @@ fn a_block_running_past_the_last_byte_still_keys_the_cache_by_its_own_bytes() { // types in it. let mut cache = BlockCache::default(); let first = "first para\n\n# A heading\n\nlast para with no newline"; - let mut ed = Editor::new_str(first, Format::Djot).unwrap(); + let mut ed = Editor::new_str(first, Format::Djot.twig()).unwrap(); let (_, warm) = render_both(&mut ed, first, Some(80), &mut cache); assert!(rendered(&warm).ends_with("last para with no newline")); let second = "first para\n\n# A heading\n\nDIFFERENT text without a newline"; - let mut ed = Editor::new_str(second, Format::Djot).unwrap(); + let mut ed = Editor::new_str(second, Format::Djot.twig()).unwrap(); let (plain, cached) = render_both(&mut ed, second, Some(80), &mut cache); assert_maps_eq(&plain, &cached, "edited last block, warm cache"); let text = rendered(&cached); diff --git a/crates/leaf-core/src/wysiwyg/tests/math.rs b/crates/leaf-core/src/wysiwyg/tests/math.rs index 876cfbaa..31f813c2 100644 --- a/crates/leaf-core/src/wysiwyg/tests/math.rs +++ b/crates/leaf-core/src/wysiwyg/tests/math.rs @@ -5,7 +5,12 @@ use super::*; /// [`map_leaf`] on a chosen surface and reveal — the whole of what a math /// rendering turns on. fn map_math(src: &str, format: Format, surface: &Surface, reveal: Option) -> VisualMap { - let mut ed = Editor::new_ext(src.as_bytes(), format, crate::doc::parse_extensions()).unwrap(); + let mut ed = Editor::new_with_flags( + src.as_bytes(), + format.twig(), + crate::doc::parse_extensions(format), + ) + .unwrap(); build(&ed.nodes().unwrap(), src, Some(80), false, surface, reveal) } @@ -106,10 +111,10 @@ fn an_atom_follows_its_glyph_across_a_wrap() { // Fifteen words, then a formula that wraps onto the second row: the // mark is drained onto the row the glyph landed on, at its index there. let src = format!("{}$x$ end\n", "word ".repeat(15)); - let mut ed = Editor::new_ext( + let mut ed = Editor::new_with_flags( src.as_bytes(), - Format::Markdown, - crate::doc::parse_extensions(), + Format::Markdown.twig(), + crate::doc::parse_extensions(Format::Markdown), ) .unwrap(); let m = build( @@ -315,10 +320,10 @@ fn a_display_block_on_the_reveal_line_is_its_source_lines_in_the_code_style() { #[test] fn a_block_that_holds_a_formula_says_so_to_the_cache() { let src = "plain\n\nwith $x$ in it\n\n$$\ny\n$$\n"; - let mut ed = Editor::new_ext( + let mut ed = Editor::new_with_flags( src.as_bytes(), - Format::Markdown, - crate::doc::parse_extensions(), + Format::Markdown.twig(), + crate::doc::parse_extensions(Format::Markdown), ) .unwrap(); let mut cache = BlockCache::default(); @@ -362,10 +367,10 @@ fn incremental_builds_agree_with_the_reference_on_math() { "$$\n\\int\n$$\n", ] { for surface in [Surface::default(), pictures()] { - let mut ed = Editor::new_ext( + let mut ed = Editor::new_with_flags( src.as_bytes(), - Format::Markdown, - crate::doc::parse_extensions(), + Format::Markdown.twig(), + crate::doc::parse_extensions(Format::Markdown), ) .unwrap(); let all = ed.nodes().unwrap(); diff --git a/crates/leaf-core/src/wysiwyg/tests/mod.rs b/crates/leaf-core/src/wysiwyg/tests/mod.rs index 9688fb65..31c2d279 100644 --- a/crates/leaf-core/src/wysiwyg/tests/mod.rs +++ b/crates/leaf-core/src/wysiwyg/tests/mod.rs @@ -1,6 +1,7 @@ use super::*; +use crate::Format; use crate::style::{FontFamily, LineSpacing, SizeStep}; -use twig::{Editor, Format, NodeId}; +use twig::{Editor, NodeId}; mod block_boundaries; mod code_blocks; @@ -17,14 +18,14 @@ mod tables; mod verse; fn map(src: &str) -> VisualMap { - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); build_t(&ed.nodes().unwrap(), src, Some(80)) } /// [`map`] over a Djot source. Djot is the format that spells superscript /// and subscript at all — Markdown has no syntax for either. fn map_djot(src: &str) -> VisualMap { - let mut ed = Editor::new_str(src, Format::Djot).unwrap(); + let mut ed = Editor::new_str(src, Format::Djot.twig()).unwrap(); build_t(&ed.nodes().unwrap(), src, Some(80)) } @@ -42,7 +43,7 @@ fn baselines_of(m: &VisualMap, ch: char) -> Vec { /// [`map`] at a chosen wrap width. fn map_at(src: &str, wrap: Option) -> VisualMap { - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); build_t(&ed.nodes().unwrap(), src, wrap) } @@ -50,15 +51,9 @@ fn map_at(src: &str, wrap: Option) -> VisualMap { /// default) — the `:::name{.class}` fenced-div containers leaf-core's /// `"directive"` wysiwyg arm renders. fn map_directives(src: &str) -> VisualMap { - let mut ed = Editor::new_ext( - src.as_bytes(), - Format::Markdown, - twig::MarkdownExtensions { - directives: true, - ..Default::default() - }, - ) - .unwrap(); + let mut ed = + Editor::new_with_features(src.as_bytes(), Format::Markdown.twig(), &["directives"]) + .unwrap(); build_t(&ed.nodes().unwrap(), src, Some(80)) } @@ -67,13 +62,18 @@ fn map_directives(src: &str) -> VisualMap { /// pairs a Markdown `
` with its `
` into a container and makes /// `::page-break` a directive rather than a paragraph of colons. fn map_leaf(src: &str, format: Format) -> VisualMap { - let mut ed = Editor::new_ext(src.as_bytes(), format, crate::doc::parse_extensions()).unwrap(); + let mut ed = Editor::new_with_flags( + src.as_bytes(), + format.twig(), + crate::doc::parse_extensions(format), + ) + .unwrap(); build_t(&ed.nodes().unwrap(), src, Some(80)) } /// [`map`] with soft breaks preserved (`LineFlow::Preserve`). fn map_preserve(src: &str, wrap: Option) -> VisualMap { - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); build( &ed.nodes().unwrap(), src, diff --git a/crates/leaf-core/src/wysiwyg/tests/rows.rs b/crates/leaf-core/src/wysiwyg/tests/rows.rs index 016574e3..7158ff24 100644 --- a/crates/leaf-core/src/wysiwyg/tests/rows.rs +++ b/crates/leaf-core/src/wysiwyg/tests/rows.rs @@ -5,7 +5,7 @@ fn unwrapped_mode_emits_one_row_per_paragraph() { // A long paragraph that would wrap under a column budget stays a single // row when wrap is None (the GUI wraps it at pixel width instead). let long = "one two three four five six seven eight nine ten eleven twelve\n"; - let mut ed = Editor::new_str(long, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(long, Format::Markdown.twig()).unwrap(); let wrapped = build_t(&ed.nodes().unwrap(), long, Some(12)); let unwrapped = build_t(&ed.nodes().unwrap(), long, None); assert!(wrapped.num_rows() > 1, "narrow column should wrap"); @@ -37,7 +37,7 @@ fn preserve_lays_each_soft_break_on_its_own_row() { // A soft break (a bare newline inside a paragraph) folds into a space by // default — the whole paragraph is one reflowed row... let src = "one two\nthree four\n"; - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let folded = build_t(&ed.nodes().unwrap(), src, None); assert_eq!(folded.num_rows(), 1, "fold: one reflowed row"); assert_eq!( diff --git a/crates/leaf-core/src/wysiwyg/tests/tables.rs b/crates/leaf-core/src/wysiwyg/tests/tables.rs index 248cdcc8..5aaf23fb 100644 --- a/crates/leaf-core/src/wysiwyg/tests/tables.rs +++ b/crates/leaf-core/src/wysiwyg/tests/tables.rs @@ -93,7 +93,7 @@ fn a_wide_table_is_cut_to_fit_and_its_cells_wrap() { // an alignment holds on every line of a wrapped cell, not just the first. let src = "| Ingredient | Notes |\n|---|---:|\n\ | flour milled coarse | sift it twice |\n| salt | a pinch |\n"; - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let m = build_t(&ed.nodes().unwrap(), src, Some(30)); let text = rendered(&m); assert_eq!( @@ -121,7 +121,7 @@ fn a_column_too_narrow_for_a_word_breaks_it_rather_than_spilling() { // A paragraph lets an overlong word trail off the end of the line; a // table column can't — a glyph past the border lands on the border. let src = "| A | B |\n|---|---|\n| antidisestablishmentarianism | x |\n"; - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let m = build_t(&ed.nodes().unwrap(), src, Some(20)); for (r, row) in m.rows.iter().enumerate() { assert!( @@ -255,7 +255,7 @@ fn every_hidden_inline_mark_gives_its_content_end_a_home() { assert!(m.stops.contains(&6)); // Revealed, the delimiter is glyphs of its own and the end is an // ordinary glyph stop: nothing to add. - let mut ed = Editor::new_str("a **bold** b\n", Format::Markdown).unwrap(); + let mut ed = Editor::new_str("a **bold** b\n", Format::Markdown.twig()).unwrap(); let src = "a **bold** b\n"; let revealed = build( &ed.nodes().unwrap(), @@ -580,7 +580,7 @@ fn structural_cells_are_unwrapped_even_when_the_picture_wraps_them() { // in pixels needs the text as the document spells it, before that // decision. Narrow enough that the drawn cell must break. let src = "| Name |\n|------|\n| alpha beta gamma |\n"; - let mut ed = Editor::new_str(src, Format::Markdown).unwrap(); + let mut ed = Editor::new_str(src, Format::Markdown.twig()).unwrap(); let m = build_t(&ed.nodes().unwrap(), src, Some(12)); let drawn = rendered(&m); let cell: String = m.tables[0].grid[1].cells[0] diff --git a/crates/leaf-core/src/wysiwyg/tests/verse.rs b/crates/leaf-core/src/wysiwyg/tests/verse.rs index 0ea41286..e9acc9cc 100644 --- a/crates/leaf-core/src/wysiwyg/tests/verse.rs +++ b/crates/leaf-core/src/wysiwyg/tests/verse.rs @@ -1,8 +1,13 @@ use super::*; -use twig::Format; +use crate::Format; fn map(src: &str, format: Format, wrap: Option) -> VisualMap { - let mut ed = Editor::new_ext(src.as_bytes(), format, crate::doc::parse_extensions()).unwrap(); + let mut ed = Editor::new_with_flags( + src.as_bytes(), + format.twig(), + crate::doc::parse_extensions(format), + ) + .unwrap(); build( &ed.nodes().unwrap(), src, diff --git a/crates/leaf-ffi/src/lib.rs b/crates/leaf-ffi/src/lib.rs index aba013cb..b79c9ba0 100644 --- a/crates/leaf-ffi/src/lib.rs +++ b/crates/leaf-ffi/src/lib.rs @@ -1671,14 +1671,15 @@ struct Inner { frame: u32, } -// SAFETY: `Doc` embeds a `twig::Editor`, which holds a `NonNull` and -// is therefore `!Send`. UniFFI hands `LeafDoc` to Swift as a reference-counted +// SAFETY: `Doc` embeds a `twig::Editor`, which holds its reparse as a boxed +// `dyn Fn` with no `Send` bound and is therefore `!Send`. UniFFI hands `LeafDoc` to Swift as a reference-counted // handle that must be `Send + Sync`, so `Inner` must be `Send`. This is sound // because: // 1. Every access goes through `LeafDoc::lock()` — the `Mutex` serializes all // reads and mutations, so there is never concurrent access to the handle. -// 2. twig's editor handle owns a plain heap allocation with no thread-affinity -// (no thread-locals, no per-thread state) — moving the pointer between +// 2. twig's editor owns plain heap allocations with no thread-affinity: the +// closure captures only the format and its parse config, and the language +// registry it reads is process-wide, not thread-local — moving it between // threads is fine as long as use is serialized, which (1) guarantees. // The intended usage is still main-thread-driven; this impl only permits the // handle to cross threads safely, it does not invite concurrent use. diff --git a/crates/leaf-gpui/Cargo.lock b/crates/leaf-gpui/Cargo.lock index cd2e85d1..c25e151d 100644 --- a/crates/leaf-gpui/Cargo.lock +++ b/crates/leaf-gpui/Cargo.lock @@ -1626,6 +1626,38 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "fig" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1912585cea7b84fd1cbacdd0ee85c57b12f54cc46a917145d97a62b95c3eafab" +dependencies = [ + "fig-core", +] + +[[package]] +name = "fig-core" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "310da8e88299b38abe3aadfa1aecce5c46a93dc767ee38900f23e9f630e05290" +dependencies = [ + "fig-kit", +] + +[[package]] +name = "fig-css" +version = "0.1.0" +source = "git+https://github.com/diaryx-org/fig-css?branch=main#d2400882084ed83510f452ded0603cf1d05fc611" +dependencies = [ + "fig", +] + +[[package]] +name = "fig-kit" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0005cbc0579d07474d9a452c76f23cbf55ba6c82dd84c7e7ed5c38ace903a7a" + [[package]] name = "find-msvc-tools" version = "0.1.14" @@ -2979,8 +3011,14 @@ name = "leaf-core" version = "0.5.1" dependencies = [ "anyhow", + "fig-css", "syntect", + "twig-asciidoc", + "twig-djot", "twig-doc", + "twig-html", + "twig-markdown", + "twig-xml", "two-face", "unicode-segmentation", "unicode-width", @@ -5985,56 +6023,70 @@ dependencies = [ ] [[package]] -name = "twig-doc" -version = "4.1.0" +name = "twig-asciidoc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e56ba403fc20370917aba95abd9f55767e759f6c37a4589e89277ec4db9c212" +checksum = "3bbaea0529c2c7e65273c20116a4c6a0730c31e3b3fe49dc741a0d085d40e9ea" dependencies = [ - "twig-sys", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys" -version = "4.1.0" +name = "twig-djot" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12fe9573219cc8938498936db35ea33136a4cfed701e82580b58a9535f34d886" +checksum = "d8619b5a7e9b37e5f25463b25d43424c5afea59b0d0478f0bc45e99744af4203" dependencies = [ - "twig-sys-linux-arm64-gnu", - "twig-sys-linux-x64-gnu", - "twig-sys-macos-arm64", - "twig-sys-wasm32", - "twig-sys-windows-x64-msvc", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys-linux-arm64-gnu" -version = "4.1.0" +name = "twig-doc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8009a919f9996c456578b1b92f4b4c2c662967d70e0fe2585829629d380e8536" +checksum = "0cd6cc20c4f31dd31e5e56dfa7d01f3dbdf5692695b1007e8c54640f9766193f" +dependencies = [ + "twig-kit", +] [[package]] -name = "twig-sys-linux-x64-gnu" -version = "4.1.0" +name = "twig-html" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6f3c05d0632ccc214df9c0ca1c9c37dc75e5ad0409a9b74a03e48839ed9a8b" +checksum = "1beb6c0999dc04d89a5a0da40e585cb05a9a7c13ecb19c2e5a65085a147f62ca" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] -name = "twig-sys-macos-arm64" -version = "4.1.0" +name = "twig-kit" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac61d2868353e8d37d5c0b05949da2f7800d2298f52076ef2dfc23489e74c269" +checksum = "1bc21949975c1e3c5ab858d0d104e9d4f4d6000e1dcfa9e46bb7dcc460bba24f" [[package]] -name = "twig-sys-wasm32" -version = "4.1.0" +name = "twig-markdown" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d052dd11d1a9ccb25294ef5546c3063ef72fb7caa99704397665745c01db00b9" +checksum = "60ffcae233483874bec0f3c39f8a2480a087d71beb038049a9ee287e0e570745" +dependencies = [ + "twig-doc", + "twig-html", + "twig-kit", +] [[package]] -name = "twig-sys-windows-x64-msvc" -version = "4.1.0" +name = "twig-xml" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfca715feba0b62dfb5473104c0b0a9fc5276323efd36c31f1d73ef07ba9be57" +checksum = "8634fdd8e854401a1f21aef62ef80e3a87d1723ad59e810f6a633c66bf2c42c6" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] name = "two-face" diff --git a/crates/leaf-math-edit/Cargo.toml b/crates/leaf-math-edit/Cargo.toml index 7082cff2..cb5d81ad 100644 --- a/crates/leaf-math-edit/Cargo.toml +++ b/crates/leaf-math-edit/Cargo.toml @@ -11,6 +11,8 @@ description = "Spike: a structural math editor over TeX formulas found by twig [dependencies] # Only `doc` and the demo read documents; the editor itself needs none. twig-doc = { workspace = true, optional = true } +# The demo's documents are Markdown. +twig-markdown = { workspace = true, optional = true } ratex-parser = "0.1.14" ratex-layout = "0.1.14" ratex-types = "0.1.14" @@ -24,7 +26,11 @@ console_error_panic_hook = { version = "0.1", optional = true } default = ["doc"] # Finding formulas in a document, and applying their splices to it, through twig. doc = ["dep:twig-doc"] -wasm = ["doc", "dep:wasm-bindgen", "dep:console_error_panic_hook"] +wasm = ["doc", "dep:twig-markdown", "dep:wasm-bindgen", "dep:console_error_panic_hook"] + +[dev-dependencies] +twig-markdown.workspace = true +twig-djot.workspace = true [lib] crate-type = ["cdylib", "rlib"] diff --git a/crates/leaf-math-edit/src/doc.rs b/crates/leaf-math-edit/src/doc.rs index 0993f89f..945b70b8 100644 --- a/crates/leaf-math-edit/src/doc.rs +++ b/crates/leaf-math-edit/src/doc.rs @@ -9,7 +9,7 @@ use crate::edit::Splice; use std::ops::Range; -use twig::{Editor, Format, MarkdownExtensions}; +use twig::{Editor, Format}; /// A formula's place in a document. #[derive(Clone, Debug, PartialEq)] @@ -21,16 +21,11 @@ pub struct FormulaRef { pub display: bool, } -/// An editor over `src` with the math extension on. +/// An editor over `src` with the math extension on, where `format` has one +/// (Markdown's `math` feature; djot reads formulas without being asked). pub fn editor(src: &str, format: Format) -> Result { - Editor::new_ext( - src.as_bytes(), - format, - MarkdownExtensions { - math: true, - ..Default::default() - }, - ) + let flags = format.feature_flags(&["math"]).unwrap_or(0); + Editor::new_with_flags(src.as_bytes(), format, flags) } /// Every formula in the document, in source order. @@ -85,7 +80,7 @@ mod tests { #[test] fn formulas_are_found_in_markdown_and_djot() { let md = "Euler: $e^{i\\pi}+1=0$.\n\n$$\n\\int_0^1 x\\,dx\n$$\n"; - let mut ed = editor(md, Format::Markdown).unwrap(); + let mut ed = editor(md, twig_markdown::format()).unwrap(); let fs = formulas(&mut ed).unwrap(); assert_eq!(fs.len(), 2); assert_eq!(&md[fs[0].tex.clone()], "e^{i\\pi}+1=0"); @@ -94,7 +89,7 @@ mod tests { assert_eq!(md[fs[1].tex.clone()].trim(), "\\int_0^1 x\\,dx"); let dj = "Euler: $`e^{i\\pi}`.\n"; - let mut ed = editor(dj, Format::Djot).unwrap(); + let mut ed = editor(dj, twig_djot::format()).unwrap(); let fs = formulas(&mut ed).unwrap(); assert_eq!(&dj[fs[0].tex.clone()], "e^{i\\pi}"); } @@ -102,7 +97,7 @@ mod tests { #[test] fn keystrokes_edit_the_formula_in_place_and_nothing_else() { let md = "# Notes\n\nThe area is $r^2$, roughly.\n\n- item, *kept* \n"; - let mut ed = editor(md, Format::Markdown).unwrap(); + let mut ed = editor(md, twig_markdown::format()).unwrap(); let f = formulas(&mut ed).unwrap().remove(0); let mut tex = f.tex.clone(); let mut s = Session::new(&md[tex.clone()]).unwrap(); diff --git a/crates/leaf-math-edit/src/wasm.rs b/crates/leaf-math-edit/src/wasm.rs index 5d793a9b..10647104 100644 --- a/crates/leaf-math-edit/src/wasm.rs +++ b/crates/leaf-math-edit/src/wasm.rs @@ -4,7 +4,6 @@ use crate::doc; use crate::edit::{Key, Session}; use std::ops::Range; -use twig::Format; use wasm_bindgen::prelude::*; /// Pixels per em the demo draws at. @@ -27,8 +26,8 @@ impl Demo { #[wasm_bindgen(constructor)] pub fn new(markdown: &str, index: usize) -> Result { console_error_panic_hook::set_once(); - let mut ed = - doc::editor(markdown, Format::Markdown).map_err(|e| JsError::new(&e.to_string()))?; + let mut ed = doc::editor(markdown, twig_markdown::format()) + .map_err(|e| JsError::new(&e.to_string()))?; let f = doc::formulas(&mut ed) .map_err(|e| JsError::new(&e.to_string()))? .into_iter() @@ -46,7 +45,7 @@ impl Demo { /// How many formulas `markdown` holds. pub fn count(markdown: &str) -> usize { - doc::editor(markdown, Format::Markdown) + doc::editor(markdown, twig_markdown::format()) .ok() .and_then(|mut ed| doc::formulas(&mut ed).ok()) .map_or(0, |f| f.len()) diff --git a/flake.nix b/flake.nix index 71480cdd..981f198e 100644 --- a/flake.nix +++ b/flake.nix @@ -4,8 +4,8 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; - # The org's Rust and Zig pins, and the shell that carries them. leaf is the - # repository that sets the Rust one: gpui uses library features stabilised in + # The org's Rust pin, and the shell that carries it. leaf is the + # repository that sets it: gpui uses library features stabilised in # 1.95, so `versions.rust` there and `rust-toolchain.toml` here are the same # number by construction. diaryx-nix.url = "github:diaryx-org/nix"; @@ -16,8 +16,6 @@ flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; - zig = diaryx-nix.lib.${system}.zig; - # The workspace version (single source of truth in [workspace.package]). # Parse it so the flake reports the same number as `leaf --version`. version = @@ -34,45 +32,23 @@ # workspace. `buildRustPackage` vendors from Cargo.lock and demands an # output hash for every git-sourced entry there whatever `-p` it is # told to build — so while leaf-gpui was a member, building the TUI - # meant fetching the Zed monorepo. That lockfile now has no git - # entries at all; see the `exclude` note in Cargo.toml. + # meant fetching the Zed monorepo; see the `exclude` note in + # Cargo.toml. What git entries it still has are small (fig-css, the + # mermaid renderer's fork, resvg-swift), pinned by rev in Cargo.lock. leaf = pkgs.rustPlatform.buildRustPackage { pname = "leaf"; inherit version; src = ./.; - cargoLock.lockFile = ./Cargo.lock; - - # zig for twig-sys's build.rs. It only actually runs on a target with - # no prebuilt payload crate — of the four systems this flake covers, - # that is x86_64-darwin alone; the other three link twig-sys's - # shipped archive and never invoke it. Unconditional anyway, because - # a toolchain that is present and unused costs a closure entry, and - # one that is absent costs a build failure on one system only, found - # by whoever happens to be on it. On Apple targets that build script - # also repacks Zig's static archive with `libtool` (ld64 rejects - # Zig's alignment); cctools provides that `libtool`, which is not - # otherwise on the sandbox PATH. - nativeBuildInputs = [ zig ] - ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isDarwin [ pkgs.cctools ]; - - # Those Zig builds want a writable HOME + cache dirs, which the - # read-only Nix store will not provide. - preBuild = '' - export HOME="$TMPDIR" - export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-global-cache" - export ZIG_LOCAL_CACHE_DIR="$TMPDIR/zig-local-cache" - ''; + cargoLock = { + lockFile = ./Cargo.lock; + # The git entries are fetched by the rev Cargo.lock pins, which + # needs no hash written here. + allowBuiltinFetchGit = true; + }; - # The twig-sys build script repacks its Zig archive with - # `libtool`/`ar`, which leaves an unreadable `__.SYMDEF` in the build - # script's `out/repack` dir. buildRustPackage's install hook then - # does a bulk `cp -r` of the release dir and fails on it. This runs - # before that hook (the postBuild attr precedes postBuildHooks), so - # make the tree readable first. - postBuild = '' - chmod -R u+rwX target - ''; + # twig (6) is pure Rust, so the build needs nothing but the + # toolchain: no Zig, and no `libtool` to repack its archive. # Build/test only the CLI crate; leaf-core and leaf-ratatui come in # as path dependencies. leaf-ffi and leaf-wasm are not in this graph. @@ -94,9 +70,8 @@ program = "${self.packages.${system}.leaf}/bin/leaf"; }; - # The shared rust+zig shell: the pinned Rust toolchain rather than - # nixpkgs' `cargo`/`rustc`, the Zig twig-sys falls back to, and git-cliff - # for `ship changelog`. - devShells.default = diaryx-nix.devShells.${system}.rust-zig; + # The shared Rust shell: the pinned Rust toolchain rather than + # nixpkgs' `cargo`/`rustc`, and git-cliff for `ship changelog`. + devShells.default = diaryx-nix.devShells.${system}.rust; }); } diff --git a/packages/leaf-android/README.md b/packages/leaf-android/README.md index 02677e0f..abfa6543 100644 --- a/packages/leaf-android/README.md +++ b/packages/leaf-android/README.md @@ -124,6 +124,7 @@ field would clash with `Throwable.message`, so in Kotlin alone it is called `reason` (`[bindings.kotlin.rename]` in `crates/leaf-ffi/uniffi.toml`): `LeafException.Parse.reason` is what Swift calls `LeafError.Parse(message:)`. -twig-sys builds for Android from its Zig source (bionic's thread-locals need -API 29, which is `minSdk`); twig 3.11.1 is the first release that knows the two -Android targets. +twig is pure Rust since 6.0, so the library cross-compiles for both Android +targets with nothing but cargo-ndk. `minSdk` is 29, the floor bionic's ELF +thread-locals set for the Zig library twig used to link; nothing needs it now, +and it stays until a lower floor has been tried on a device. diff --git a/packages/leaf-android/leaf-ffi/build.gradle.kts b/packages/leaf-android/leaf-ffi/build.gradle.kts index acc98025..b400d589 100644 --- a/packages/leaf-android/leaf-ffi/build.gradle.kts +++ b/packages/leaf-android/leaf-ffi/build.gradle.kts @@ -12,8 +12,10 @@ android { compileSdk = 37 defaultConfig { - // 29 is the floor for ELF thread-locals in bionic; below it the Zig - // static library inside libleaf_ffi.so (twig) cannot load. + // 29 was the floor for ELF thread-locals in bionic, which the Zig static + // library twig used to link into libleaf_ffi.so needed. twig is pure + // Rust since 6.0, so nothing forces it any more; it stays until a lower + // floor has been tried on a device. minSdk = 29 } diff --git a/packages/leaf-swift/README.md b/packages/leaf-swift/README.md index 09d33e9e..753873d9 100644 --- a/packages/leaf-swift/README.md +++ b/packages/leaf-swift/README.md @@ -569,7 +569,7 @@ is the reference event-loop for a proportional renderer — the same wiring in J `DocView` is a rich nested value returned from every call; UniFFI maps records/enums/`Vec`/`Option` to native Swift structs, so there's no JSON round trip. Because a UniFFI object is a thread-safe `Arc`, the `Doc` lives behind a -`Mutex`, and `Inner` carries one `unsafe impl Send` — twig's editor handle is a -`NonNull` (hence `!Send`), but every access is serialized through that mutex and -the handle has no thread-affinity. Drive it from the main thread. See the safety +`Mutex`, and `Inner` carries one `unsafe impl Send` — twig's editor holds its +reparse as a boxed closure with no `Send` bound (hence `!Send`), but every access +is serialized through that mutex and the editor has no thread-affinity. Drive it from the main thread. See the safety comment in `src/lib.rs`. diff --git a/spikes/dioxus-widget/Cargo.lock b/spikes/dioxus-widget/Cargo.lock index 0086a030..37204c86 100644 --- a/spikes/dioxus-widget/Cargo.lock +++ b/spikes/dioxus-widget/Cargo.lock @@ -2248,7 +2248,12 @@ dependencies = [ "anyhow", "fig-css", "syntect", + "twig-asciidoc", + "twig-djot", "twig-doc", + "twig-html", + "twig-markdown", + "twig-xml", "two-face", "unicode-segmentation", "unicode-width", @@ -4216,7 +4221,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix", "windows-sys 0.61.2", @@ -4496,56 +4501,70 @@ dependencies = [ ] [[package]] -name = "twig-doc" -version = "4.1.0" +name = "twig-asciidoc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e56ba403fc20370917aba95abd9f55767e759f6c37a4589e89277ec4db9c212" +checksum = "3bbaea0529c2c7e65273c20116a4c6a0730c31e3b3fe49dc741a0d085d40e9ea" dependencies = [ - "twig-sys", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys" -version = "4.1.0" +name = "twig-djot" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12fe9573219cc8938498936db35ea33136a4cfed701e82580b58a9535f34d886" +checksum = "d8619b5a7e9b37e5f25463b25d43424c5afea59b0d0478f0bc45e99744af4203" dependencies = [ - "twig-sys-linux-arm64-gnu", - "twig-sys-linux-x64-gnu", - "twig-sys-macos-arm64", - "twig-sys-wasm32", - "twig-sys-windows-x64-msvc", + "twig-doc", + "twig-kit", ] [[package]] -name = "twig-sys-linux-arm64-gnu" -version = "4.1.0" +name = "twig-doc" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8009a919f9996c456578b1b92f4b4c2c662967d70e0fe2585829629d380e8536" +checksum = "0cd6cc20c4f31dd31e5e56dfa7d01f3dbdf5692695b1007e8c54640f9766193f" +dependencies = [ + "twig-kit", +] [[package]] -name = "twig-sys-linux-x64-gnu" -version = "4.1.0" +name = "twig-html" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6f3c05d0632ccc214df9c0ca1c9c37dc75e5ad0409a9b74a03e48839ed9a8b" +checksum = "1beb6c0999dc04d89a5a0da40e585cb05a9a7c13ecb19c2e5a65085a147f62ca" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] -name = "twig-sys-macos-arm64" -version = "4.1.0" +name = "twig-kit" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac61d2868353e8d37d5c0b05949da2f7800d2298f52076ef2dfc23489e74c269" +checksum = "1bc21949975c1e3c5ab858d0d104e9d4f4d6000e1dcfa9e46bb7dcc460bba24f" [[package]] -name = "twig-sys-wasm32" -version = "4.1.0" +name = "twig-markdown" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d052dd11d1a9ccb25294ef5546c3063ef72fb7caa99704397665745c01db00b9" +checksum = "60ffcae233483874bec0f3c39f8a2480a087d71beb038049a9ee287e0e570745" +dependencies = [ + "twig-doc", + "twig-html", + "twig-kit", +] [[package]] -name = "twig-sys-windows-x64-msvc" -version = "4.1.0" +name = "twig-xml" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfca715feba0b62dfb5473104c0b0a9fc5276323efd36c31f1d73ef07ba9be57" +checksum = "8634fdd8e854401a1f21aef62ef80e3a87d1723ad59e810f6a633c66bf2c42c6" +dependencies = [ + "twig-doc", + "twig-kit", +] [[package]] name = "two-face" diff --git a/xtask/src/android.rs b/xtask/src/android.rs index 004167c1..66e8f33b 100644 --- a/xtask/src/android.rs +++ b/xtask/src/android.rs @@ -18,8 +18,10 @@ use std::path::{Path, PathBuf}; const APPLICATION_ID: &str = "org.diaryx.leaf"; const ACTIVITY: &str = "org.diaryx.leaf.app.MainActivity"; -/// The lowest API the library loads on: bionic has ELF thread-locals from 29, -/// and the Zig static library inside the .so (twig) uses them. +/// The lowest API the app is built for: bionic has ELF thread-locals from 29, +/// which the Zig static library twig used to link into the .so needed. twig is +/// pure Rust since 6.0; this stays at 29 until a lower floor is tried, and +/// moves with `minSdk` in `packages/leaf-android/leaf-ffi/build.gradle.kts`. const MIN_API: &str = "29"; #[derive(clap::Args)] @@ -50,7 +52,7 @@ pub struct Args { /// Extra arguments for the cargo build and the bindgen run, in the /// `LEAF_ANDROID_CARGO_ARGS` spelling — a `--config - /// 'patch.crates-io.twig-sys.path="…"'` while a dependency's Android + /// 'patch.crates-io.twig-doc.path="…"'` while a dependency's Android /// support is not yet released, say. Both commands take the same ones, or a /// patch the build wrote into the lockfile re-resolves differently for the /// host. @@ -292,8 +294,8 @@ mod tests { #[test] fn a_patch_argument_splits_as_a_shell_would() { assert_eq!( - split_args(r#"--config 'patch.crates-io.twig-sys.path="../twig/x"'"#), - ["--config", r#"patch.crates-io.twig-sys.path="../twig/x""#] + split_args(r#"--config 'patch.crates-io.twig-doc.path="../twig/x"'"#), + ["--config", r#"patch.crates-io.twig-doc.path="../twig/x""#] ); assert!(split_args(" ").is_empty()); }