Repository navigation
add(editor)!: an edit is retain, insert and delete ops, and an undo step keeps their inverse - #18
Merged
Merged
Conversation
…tep keeps their inverse The splicer's one primitive is now apply(ops): Retain, Insert and Delete read from the start of the source, applied with one reparse and recorded as one undo step. A gesture still builds its replacement for a span, but replace_at_span records the fewest ops that turn the span's bytes into it (ast::ops::diff, Myers over characters, so no op ends inside a UTF-8 sequence, and one splice past 512 edits). Bold over a word is two inserts and the word retained, not the word deleted and written again, which is what a merge needs to keep a concurrent edit inside the word. edit_range, here and in the C ABI, records the span as asked. last_change keeps reporting the span asked. Editor gains Op, apply (several places in one edit; a position past the end or inside a character is InvalidArgument), last_ops (what the last edit, undo or redo did, in one canonical spelling), and set_history / keeps_history. An undo step holds the edit's inverse ops and its caret blob instead of a copy of the whole source; coalescing composes two steps' ops. History off records no steps, for a host such as leaf that keeps its own over last_ops. These are twig's tasks in leaf's draft proposal "An edit format shared by leaf and the core" (diaryx-org/leaf#23). BREAKING CHANGE: ast::splicer::HistoryEntry holds `ops: Vec<ast::ops::Op>` where it held `source: Vec<u8>`, and ast::splicer::Error has an OutOfRange variant. Behavioural-change: An undo or redo whose restored text the parser refuses now leaves its step on the stack, so it can be tried again. It used to drop the step.
adammharris
force-pushed
the
cc/youthful-hamilton-hid5lb
branch
from
October 24, 2026 18:40
6a76fae to
d81b0e6
Compare
…ps text and ops of its own Op::diff gives the fewest ops between two texts, cut between characters, as the gestures record theirs. Op::apply applies ops to a text with the checks Editor::apply makes. Op::compose folds a run of edits into one list, in the canonical spelling last_ops reads back. leaf composes each edit's last_ops into what it hands its host, and brings an open page to a changed file with a diff.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
twig's half of leaf's draft proposal An edit format shared by leaf and the core (diaryx-org/leaf#23), tasks 6 and 7: ops applied with one reparse, and history that can be turned off. leaf takes it in diaryx-org/leaf#25.
What changes
One primitive:
apply(ops).Splicer::applytakesRetain,InsertandDelete, read from the start of the source, and applies them with one reparse as one undo step. A failed reparse changes nothing, as before.A gesture records the fewest ops. Gestures still build a replacement for a span.
replace_at_spannow records the fewest ops that turn the span's bytes into that replacement (ast::ops::diff: Myers over characters, so no op ends inside a UTF-8 sequence, with one splice as the fallback past 512 edits). So Cmd-B overwordis two inserts with the word retained, not the word deleted and written again. That matters to a CRDT merge: a concurrent edit inside the word survives.edit_range, here and in the C ABI, records the span as the caller asked it.last_changestill reports the span asked.Public
EditorandOp:Op—Retain(usize),Insert(String),Delete(usize)Editor::apply(&[Op]) -> Result<Change, Error>— several places in one edit; a position past the end or inside a character isInvalidArgumentEditor::last_ops() -> Result<Vec<Op>, Error>— what the last edit, undo or redo did, in one canonical spelling (no zero-length op, no two ops of one kind side by side, an insert before a delete at one place, no retain at the end)Editor::set_history(bool)/keeps_history()Op::diff(old, new),Op::apply(text, ops)andOp::compose(first, then), for a host that keeps text and ops of its own; leaf uses all threeHistory is inverse ops. An undo step holds its edit's inverse ops and its caret blob, not a copy of the whole source (200 copies of the page before). Coalescing composes two steps' ops. With history off, edits record no steps, for a host such as leaf that keeps its own history over
last_ops.Breaking
ast::splicer::HistoryEntryhasops: Vec<ast::ops::Op>in place ofsource: Vec<u8>.ast::splicer::Errorhas a newOutOfRangevariant.This is a major version, as decision 5 of the leaf proposal expects. The first commit uses
!, so the changelog files it under Breaking. OneBehavioural-change:trailer: an undo or redo whose restored text the parser refuses now leaves its step on the stack, where it used to drop it.Not in this PR
@diaryx/twigget noapplyorlast_opsyet. leaf calls twig from Rust, and the proposal leaves leaf-ffi and leaf-wasm on whole-text hosts for now.Testing
cargo test --workspace: everything that passed before still passes, unchanged.ast/ops_test.rs: seeded randomized checks thatdiffapplies back to the new text on character boundaries and is minimal against an LCS, thatinvertrestores the source, and thatcomposeequals one edit then the other.applyas one step, coalescing, history off, and a refused undo keeping its step.tests/binding.rs, includingOp::diff,Op::applyandOp::compose, and the new signatures pinned intests/api_surface.rs.cargo test -p twig-doc --no-default-featurespasses.