Skip to main content

Text representation

Each document is a ropey rope plus an undo history. A rope makes insertions, deletions and line lookups O(log n) without copying the document, which keeps typing cost independent of file size (the measured core edit is microseconds even in a 2 MB file; see performance).

Coordinates​

Four coordinate systems meet in an editor. They are separate types or explicitly named functions and are never converted implicitly.

NameUnitUsed by
char offsetUnicode scalar values from document startCanonical: selections, edits, decorations
byte offsetUTF-8 bytesTree-sitter, regex decorations
TextPositionline + column in scalar valuesUI and status bar (shown 1-based)
Utf16Positionline + column in UTF-16 code unitsLanguage Server Protocol

Conversions clamp out-of-range input instead of panicking, so positions from a stale UI state or a misbehaving server cannot crash the editor.

Grapheme clusters

Caret movement and columns count scalar values, so a flag emoji or an accented letter built from combining characters takes several caret steps. Grapheme-aware movement is a backlog item.

Line endings and encoding​

  • Files are decoded as UTF-8. A UTF-8 byte-order mark is remembered and written back.
  • CRLF files are normalised to \n in memory and converted back on save. Files with mixed endings are flagged when opened; saving normalises them to the dominant style.
  • Binary and non-UTF-8 files are refused with a message. Other encodings are not supported yet.

Edits, undo and dirty state​

  • An edit is a list of (char range, replacement) applied as one transaction. All ranges are validated before anything changes.
  • Every change returns an AppliedChange describing it in char, byte, line/byte-column and UTF-16 terms, which is exactly what Tree-sitter and LSP need.
  • Consecutive single-character typing is merged into one undo step until whitespace, a caret move or a save; consecutive backspaces merge likewise. History is capped at 10 000 steps.
  • Every state has an id. The document is dirty when the current id differs from the id at the last save, so undoing back to the saved text makes it clean again.
  • A revision counter increases with every change (including undo/redo). Language-server versions are derived from it, and stale diagnostics are discarded.