Skip to content

enable undo cursor restoration for programmatic edits#1062

Closed
Third-Thing wants to merge 3 commits into
lapce:mainfrom
Third-Thing:editor-aware-edit
Closed

enable undo cursor restoration for programmatic edits#1062
Third-Thing wants to merge 3 commits into
lapce:mainfrom
Third-Thing:editor-aware-edit

Conversation

@Third-Thing
Copy link
Copy Markdown
Contributor

@Third-Thing Third-Thing commented Apr 4, 2026

Problem: Programmatic document edits currently bypass the editor context that undo cursor restoration depends on. Document::edit and edit_single mutate the buffer and emit deltas, but they do not record cursor_before / cursor_after, so undo lacks the initiating editor state needed to restore the cursor correctly. That left floem’s examples using raw document edits from button callbacks and compensating with workarounds like disabling undo in one shared editor.

Solution: Add additive editor-aware document edit APIs: edit_from and edit_single_from. TextDocument uses the initiating editor to capture cursor-before state, apply the edit, remap that editor’s cursor through the produced deltas, and record cursor-after state on the revision. Raw edit / edit_single remain unchanged as low-level document mutation APIs, while UI-driven edits can now preserve expected cursor and undo behavior without changing the broader document-centered reactive model.

Reasoning: This keeps the architecture aligned with floem’s existing update flow. Live cursor consistency across editors still comes from document deltas and update listeners; the new APIs only add what undo needs. The examples are moved to the editor-aware path, which removes the need to suppress undo in the secondary editor and makes the intended API split explicit.

Associated with issue #1058

This is the smallest change that fixes the crash using existing
document-delta plumbing, without taking on the larger API remodel.
@Third-Thing
Copy link
Copy Markdown
Contributor Author

Third-Thing commented Apr 4, 2026

This PR is stacked on top of #1061, because it relies on the test block added in that branch. That is why the first two commits are included. This is a consequence of using Gitbutler for the first time, not intentional.

Problem: Programmatic document edits currently bypass the editor context that 
undo cursor restoration depends on. `Document::edit` and `edit_single` 
mutate the buffer and emit deltas, but they do not record 
`cursor_before` / `cursor_after`, so undo lacks the initiating editor 
state needed to restore the cursor correctly. That left Floem’s examples 
using raw document edits from button callbacks and compensating with 
workarounds like disabling undo in one shared editor.

Solution: Add additive editor-aware document edit APIs: `edit_from` and 
`edit_single_from`. `TextDocument` uses the initiating editor to capture 
cursor-before state, apply the edit, remap that editor’s cursor through 
the produced deltas, and record cursor-after state on the revision. Raw 
`edit` / `edit_single` remain unchanged as low-level document mutation 
APIs, while UI-driven edits can now preserve expected cursor and undo 
behavior without changing the broader document-centered reactive model.

Reasoning: This keeps the architecture aligned with Floem’s existing update flow. 
Live cursor consistency across editors still comes from document deltas 
and update listeners; the new APIs only add what undo needs. The 
examples are moved to the editor-aware path, which removes the need to 
suppress undo in the secondary editor and makes the intended API split 
explicit.
@Third-Thing Third-Thing marked this pull request as draft April 4, 2026 06:08
@Third-Thing Third-Thing marked this pull request as ready for review April 22, 2026 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant