Skip to content

crash fix: sync stale editor cursors through document updates#1061

Open
Third-Thing wants to merge 2 commits intolapce:mainfrom
Third-Thing:stale-cursor-crash-fix
Open

crash fix: sync stale editor cursors through document updates#1061
Third-Thing wants to merge 2 commits intolapce:mainfrom
Third-Thing:stale-cursor-crash-fix

Conversation

@Third-Thing
Copy link
Copy Markdown
Contributor

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

Problem: When multiple editors share a TextDocument, or when a document is edited programmatically, only the initiating editor updates its cursor. Other editors keep stale byte offsets. That can leave a cursor past the end of the buffer or inside a multibyte character, which then panics on the next keypress. In practice this affects both shared editors and single editors after raw Document::edit / edit_single calls.

Solution: Register a TextDocument update listener for each Editor and remap non-originating cursors through incoming RopeDeltas with Cursor::apply_delta. The originating editor is skipped to avoid double-transforming its cursor. The listener is registered when the editor is created and when its document changes, and it uses try_update plus a document identity check so disposed editors or stale listeners fail closed instead of panicking.

Reasoning: This is the smallest change that restores cursor validity using floem’s existing document update flow and eliminates the crashes mentioned in #934 & #693. TextDocument already emits deltas for every edit, and Cursor already knows how to transform itself through those deltas. Hooking editors into that path keeps cursor synchronization reactive and document-driven, without introducing new edit APIs or a larger coordination layer. The document identity guard is necessary because add_on_update is currently append-only, so old listeners cannot yet be removed when an editor switches documents.

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.
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