Skip to content
This repository was archived by the owner on Nov 12, 2026. It is now read-only.

feat(core): every change to the text as twig's edit ops, for a host to send on - #25

Merged
adammharris merged 6 commits into
mainfrom
cc/youthful-hamilton-hid5lb
Oct 26, 2026
Merged

adammharris merged 6 commits into
mainfrom
cc/youthful-hamilton-hid5lb

Conversation

@adammharris

@adammharris adammharris commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Takes twig's edit ops (diaryx-org/twig#18) into leaf. This is the part of task 9 in An edit format shared by leaf and the core (#23) that needs no event graph: the ops a local edit makes, out.

What changes

Doc::take_ops() / Doc::has_ops(). Every change since the host last asked, as one list of twig Ops (retain, insert, delete) against the text it last took. A host sends this where it sent the whole body, and a replica holding the same text applies it with twig::Op::apply.

  • refresh, the funnel every change comes through, folds in twig's last_ops for each edit. A gesture is the fewest ops that make it: bold over a word is two inserts with the word kept. A keystroke is the span typed.
  • Undo, redo, replace_source and reload are changes like any other.
  • Where more than one edit landed between refreshes, or the editor is a new one, the change is twig's diff of the two texts.

replace_source through ops. It applies twig's diff of the two texts as one edit, not the one span between their common prefix and suffix, and carries the caret and selection through every place that changed. A caret between two changes far apart now stays on its text; it used to fall inside the one span and go to its end. Still one undo step.

Reload through ops. A reload applies the diff the same way, so the host's ops and the undo step are the change and not the whole file. Its caret rule (keep the offset, clamped) is unchanged, since leaf-tui and leaf-core pin it in tests.

Taking twig after 6.0.0

The twig crates come from the twig PR's branch by git, until it ships as twig 7. All five lockfiles are updated, and they change only the twig entries. Until twig 7 is on crates.io, leaf-core cannot be published, because crates.io takes no git dependency.

Twig main also carries an unreleased change (d4bcd44) that refuses a bare directive attribute key and a value holding a quote. So the first commit makes Doc::insert_directive write a bare attribute as key="" in every format, as it already did for djot. leaf-ffi and leaf-wasm now pass an empty value through instead of turning it into a bare key; without that, every Swift or web catalogue item with a flag would fail. Docs and tests in Rust, Swift and TypeScript now say key="". The generated Swift binding's doc comment is edited to match what regenerating it would give.

Behavioural changes (trailers on the commits)

  • insert_directive writes a bare attribute, or an empty value through leaf-ffi and leaf-wasm, as key="" in Markdown, where it wrote {key}. Both read back as an empty value.
  • replace_source leaves a caret or selection that lies between two changed places on its own text.

Not in this PR

  • Taking another replica's ops in. Applying ops while local ones are in flight needs the Eg-walker merge crate (tasks 1–5 of the proposal). Until then a host brings a merged text in through replace_source, as now.
  • Undo in the merge layer (task 10). twig still keeps the history, now as inverse ops rather than whole-page copies.
  • Carrying the caret through a reload. Easy now, but it changes a rule leaf pins on purpose, so it's left for a separate decision.

Testing

  • cargo test --workspace --exclude xtask --no-fail-fast: all green.
  • New tests in crates/leaf-core/src/doc/tests/ops.rs:
    • typing, a gesture, undo and redo, edits that cancel out, and a mix of every kind of change, each checked by applying the taken ops to the text last taken;
    • replace_source carrying a selection between two changes;
    • a reload as ops;
    • the carry rule itself.
  • cargo fmt --all --check is clean.
  • The excluded crates (leaf-dioxus, leaf-gpui, the apps) and the Swift package were not built here.

twig now refuses a bare attribute key in insert_directive (twig d4bcd44): no format reads one back as written, as djot does not read `{wide}` as an attribute at all and Markdown reads it back as `wide=""`. Doc::insert_directive already wrote a bare attribute as `key=""` for djot; it now does so for every format, so a `None` value keeps working. leaf-ffi and leaf-wasm pass an empty value through as `Some("")` rather than turning it into a bare key, and their docs, the Swift and TypeScript docs and the tests say `key=""`. twig also refuses a value holding a quote, so the round-trip test no longer writes one.

Behavioural-change: insert_directive writes a bare attribute, or an empty
  value through leaf-ffi and leaf-wasm, as `key=""` in Markdown, where it
  wrote `{key}`. Both read back as an empty value.
… released

diaryx-org/twig#18 makes every twig edit retain, insert and delete ops, applied with one reparse and read back with Editor::last_ops. leaf takes the twig crates from that branch by git until it ships as twig 7, and goes back to a crates.io version then. Until then leaf-core cannot be published to crates.io, which takes no git dependency.
…o send on

Doc::take_ops hands a host every change since it last asked, as one list of twig retain, insert and delete ops against the text it last took: what a host sends instead of the whole body, and what a replica holding the same text applies with twig::Op::apply. refresh, the funnel every change comes through, folds in twig's last_ops for each edit, so a gesture is the fewest ops that make it (bold over a word is two inserts and the word kept) and a keystroke is the span typed. Where more than one edit landed between refreshes, or the editor is a new one, the change is twig's diff of the two texts.

replace_source applies twig's diff of the two texts as one edit instead of the one span between their common prefix and suffix, and carries the caret and selection through every place that changed. A caret between two changes far apart stays on its text; it used to fall inside the one span and go to its end. A reload applies the diff the same way, and keeps its caret rule.

This is the part of leaf's task 9 in "An edit format shared by leaf and the core" that needs no event graph: the ops a local edit makes, out. Taking another replica's ops in while local ones are in flight needs the Eg-walker merge crate.

Behavioural-change: replace_source leaves a caret or selection that lies
  between two changed places on its own text. It used to move one inside the
  span from the first change to the last to that span's end.
twig 7.0.0 ships the edit ops leaf#25 was built on, so leaf goes back from the PR branch to a crates.io version, and every lockfile that reaches twig takes 7.0.0. twig 7's Op::compose takes the text its first op list reads; Doc passes the text the host last took.
@adammharris
adammharris merged commit a92330c into main Oct 26, 2026
@adammharris
adammharris deleted the cc/youthful-hamilton-hid5lb branch October 30, 2026 01:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant