Skip to main content

0003: Rope text storage with char offsets as the canonical unit

  • Status: Proposed (implemented in the MVP)
  • Date: 2026-12-05

Context​

Edits must not copy whole documents, large files must stay responsive, and four coordinate systems (UI columns, UTF-8 bytes, Unicode scalars, LSP UTF-16) must never be confused.

Decision​

  • Store text in a ropey 1.6 rope. Use char (Unicode scalar) offsets as the canonical internal unit because ropey indexes them natively in O(log n).
  • Provide explicit conversions to byte offsets, TextPosition (line, char column) and Utf16Position, all clamping out-of-range input.
  • Normalise CRLF to LF in memory and restore it on save; remember a UTF-8 BOM; refuse binary, non-UTF-8 and files over 64 MiB.
  • Keep undo history as reversible change records with state ids, so dirty state is "current id ≠ saved id".

Alternatives considered​

  • Piece table: excellent for undo and large files, but no mature Rust crate with line indexing and UTF-16 metrics.
  • Gap buffer / String: simple, but O(n) line lookups and copies on large edits.
  • Byte offsets as canonical: matches Tree-sitter, but most UI operations are character-based and every caret move would need boundary checks.
  • ropey 2.0: still in beta when reviewed.

Consequences​

  • Each change reports itself in every coordinate system (AppliedChange), which keeps Tree-sitter and LSP updates exact.
  • Graphemes are not yet the caret unit (backlog); mixed line endings are normalised on save (flagged to the user).

Validation​

Tests cover multibyte text, empty files, invalid ranges, undo/redo sequences, coalescing and revisions. A keystroke in a 2 MB document costs ~2 µs in the text engine (performance).