fix(desktop): keep the editor selection valid when external content replaces the document
Problem
A renderer event on release 1.22.0 reported CodeMirror rejecting a Backspace with an invalid-change-range error where the range ran from the end of the document to offset 0. The keystroke is dropped and the error is unhandled. DESKTOP-QF (release 1.12.1, archived in August) is the same failure reached through paste, with a different document length in the message, so each occurrence groups as a fresh Sentry issue and archiving does not stick.
Root cause
Both code editors (the v1 file viewer/diff editor under screens/ and the v2 file pane editor under routes/) sync an externally changed file by dispatching a whole-document replace with no explicit selection. CodeMirror maps a non-empty selection through a change by mapping its start with assoc = 1 and its end with assoc = -1. For a replace that covers the whole selection that sends the start to the end of the inserted text and the end to the start of it, leaving an inverted range (newLength, 0). The very next edit command (deleteCharBackward, replaceSelection for paste or typing) builds a change from that range and ChangeSet.of throws.
This is not fixed by the CodeMirror bump main already took (state 6.6.0 → 6.7.1): SelectionRange.map is byte-identical in 6.6.0, 6.7.1 and the current upstream 6.7.5.
Control run against the current code path (state 6.7.1, no fix):
$ bun run control.ts
old replace path: RangeError: Invalid change range 2840 to 0 (in doc of length 2840)
Fix
One helper, replaceEditorDocument(state, value), builds the replace transaction and sets an explicit cursor computed against the document the change produces (CodeMirror normalizes CRLF on insert, so the string length is not the document length), clamped to its end and snapped to a grapheme-cluster boundary so it can never land between the halves of an astral character. All four whole-document replace sites (the two value-sync effects and the two adapter setValues) use it. The selection can no longer be inverted, and the user keeps their offset in the refreshed file instead of the mapped-to-zero position the old dispatch produced.
Verification
$ bunx biome check apps/desktop/src/renderer/lib/replaceEditorDocument <3 changed editor files>
Checked 24 files in 13ms. No fixes applied.
(re-run after each revision on the helper folder: Checked 3 files in 29ms. No fixes applied.)
$ cd apps/desktop && bun run typecheck
$ tsc --noEmit
typecheck exit=0
$ cd apps/desktop && bun test src/renderer/lib/replaceEditorDocument
6 pass
0 fail
11 expect() calls
Ran 6 tests across 1 file. [89.00ms]
Refs DESKTOP-19H
Summary by CodeRabbit
-
Bug Fixes
- Improved code editor document replacement to preserve valid cursor positions when content changes.
- Prevented cursors from landing beyond the new document, inside grapheme clusters, or at positions affected by line-ending normalization.
- Ensured selected ranges remain editable after replacing content.
- Multiple selections now collapse to a single cursor when documents are replaced.
-
Tests
- Added coverage for cursor positioning, selections, Unicode characters, shorter documents, and normalized line endings.