Visual Studio Code, rebuilt in Rust
Editor and text model
The editor is the text model, the cursors and edits on it, the tokens, and the view that draws them. This file goes one level under ARCHITECTURE.md. Upstream is src/vs/editor.
Model, editor, group
Three types in app/src/editor.rs carry the state:
app/src/editor.rs::TextDocument: the text of one resource as lines (lines), its line ending, encoding and language, and its tokens.app/src/editor.rs::TextModel: a document plus what belongs to the resource and not to one editor: the version id, the undo stack, indent guides, marker decorations, the edit log. Every editor of a resource shares one model throughapp/src/editor.rs::SharedModel.app/src/editor.rs::OpenEditor: one editor of a model in one group, with its own view state: selections, folding, scroll position, word wrap, view zones, injected text, decorations.
app/src/editor.rs::EditorGroup holds the editors of the active group and the other groups (workbench.md, "Editor groups").
Invariant: text changes go through the edit functions of app/src/editor/edit.rs (execute_edits, execute_commands). They update the version id, the undo stack, the tokens and the edit log, and they return a app/src/editor/edit.rs::ContentChanged. Code that writes lines directly leaves all of those stale.
Invariant: the caller of an edit hands the returned ContentChanged to app/src/workbench/model_events.rs::model_content_changed, and a cursor move without an edit goes to app/src/workbench/model_events.rs::cursor_position_changed. The host hears the change first, then the listeners of the model and of the active code editor run. An edit the host does not see breaks every language feature of the document.
An editor that shares its model with another editor follows the other's edits from the model's edit log: app/src/editor/edit.rs::sync_with_model moves its cursors and folds to the new version.
Positions and units
A position is a 0-based line and a column counted in chars. Upstream counts 1-based lines and UTF-16 units. Each module header in app/src/editor states its unit. Folding and sticky scroll keep upstream's 1-based lines because their tests do.
Conversion to UTF-16 happens where a position leaves for the extension host or arrives from it (app/src/host_sync.rs, app/src/workbench/host_requests.rs). Do not convert inside the editor.
Editing features
Each editor feature is one file in app/src/editor, named after its upstream contribution, with upstream's tests beside it: app/src/editor/cursor.rs, app/src/editor/word_operations.rs, app/src/editor/multicursor.rs, app/src/editor/auto_closing.rs, app/src/editor/auto_indent.rs, app/src/editor/folding.rs, app/src/editor/word_wrap.rs, app/src/editor/snippet_session.rs, app/src/editor/find.rs. Almost none of these files import GPUI. A feature takes the editor and returns edits or selections.
The commands that bind these features to upstream's command ids are in app/src/workbench/editor_commands.rs and app/src/editor/commands.rs.
Undo is one service for the window (app/src/undo_redo.rs). A model pushes its edit stack elements into it (app/src/editor/edit_stack.rs), beside the file operations of the Explorer, so one undo order covers text and files.
Tokens and colors
- Syntax tokens come from the
textmatecrate:textmate/src/grammar.rs::Grammartokenizes a line from the state at the end of the line before. app/src/syntax.rs::Tokenizationkeeps the tokens per line. Lines are tokenized as far as the view reads them, and the rest in the background, as upstream'stextModelTokens.ts. After an edit, a line keeps its old tokens until it is tokenized again.- Semantic tokens from extensions are in
app/src/semantic_tokens.rs. - Bracket pair colors and matching are in
app/src/brackets. - A token's color comes from the theme:
textmate/src/theme.rs::Themeresolves scopes to styles, andapp/src/theme.rsloads the theme file with upstream's color defaults.
Invariant: tokenization never runs for the whole file on the main thread. A command that needs tokens for a far line uses the cheap paths upstream uses (tokenizeIfCheap). Comment at line 49000 of a 52000-line file took 5.3 s before this rule and 13 ms after (port/notes.md, "Scheduling differences").
The view
app/src/workbench/editor_view.rs draws the editor part: tabs, breadcrumbs and the code editor's view parts in upstream's order (margin, current line, selections, indent guides, text, cursors, overview ruler). app/src/editor/view.rs computes the viewport: which lines are in view, the scroll size, reveal. The minimap is app/src/editor/minimap.rs.
- The view shapes and draws only the lines in view (
app/src/workbench/editor_view.rs::editor_lines). - Injected text (inlay hints, ghost text, color boxes) moves every later x of its line. Every column-to-x conversion goes through the line layout of
app/src/editor/injected_text.rs. - Text input from the OS (IME, dead keys) arrives through GPUI's input handler in
app/src/workbench/text_input.rs, after keybindings. - A scroll of the editor notifies only the editor part view (workbench.md, "Layout").
Language features
Hover, completion, code actions, rename and the others are requests to the extension host. The workbench asks through app/src/host_sync.rs::call and draws the answer: app/src/workbench/content_hover.rs, app/src/workbench/suggest_controller.rs, app/src/workbench/code_actions.rs, app/src/workbench/rename.rs. Request timing follows upstream's debounce (app/src/language_feature_debounce.rs).
A late answer is handled as the feature's upstream controller handles it: dropped after a version check, or moved through the model's edit log. A request made with app/src/host_sync.rs::call_cancellable is cancelled in the host when its receiver is dropped.
The budget
Typing in a 52000-line file must take under 16 ms per edit at p95. The check is app/src/syntax.rs::typing_latency_on_a_large_file. A scroll frame of the editor must take under 8 ms: app/src/workbench/scroll_frame_time.rs::editor_scroll_frame_time. performance.md has the commands.