Expand description
TWrite is a fast, modular text editor engine for GPUI.
It provides rope-backed text editing, multi-span syntax highlighting, a versatile hook system for modal editing and smart pairs, and GPU-accelerated rendering.
§Layout
Most users need only this facade crate: Editor plus the hook traits
and batteries. It re-exports two layers:
twrite-core: headless buffer, movement, syntax, hooks, and structured keys (KeyCode,KeyHint). Usable without GPUI (servers, CLIs, tests). See https://docs.rs/twrite-core.twrite-gpui: canvas rendering, theming, configuration, and the interactiveEditorview.
§Quick start
[dependencies]
gpui = "0.2"
twrite = "0.9"use gpui::*;
use twrite::Editor;
Application::new().run(|cx: &mut App| {
cx.open_window(WindowOptions::default(), |_, cx| {
cx.new(|cx| Editor::new("Hello from TWrite!", cx))
})
.unwrap();
cx.activate(true);
});§Features
markdown: CommonMark/GFM highlighter (markdownmodule) and task widgets.wayland,x11,font-kit: platform-backend passthrough togpui(its defaults already include all three).
Modules§
- batteries
- Core buffer, syntax, movement, and hook primitives. Battery registry: optional, feature-gated editor batteries (see it for the contract and checklist for adding new batteries). Battery registry: optional, feature-gated editor batteries.
- buffer
- Core buffer, syntax, movement, and hook primitives. Text buffer implementation backed by a Rope.
- canvas
- GPUI canvas rendering, theming, configuration, and editor view. Canvas element handling prepaint, layout, and GPU quad rendering.
- config
- GPUI canvas rendering, theming, configuration, and editor view. Configuration settings for font size, line height, wrapping, and gutters.
- context_
menu - Core buffer, syntax, movement, and hook primitives. Headless right-click context menu state and item merging.
- coordinates
- Core buffer, syntax, movement, and hook primitives. 2D text coordinates (row and column).
- editor
- GPUI canvas rendering, theming, configuration, and editor view. Main editor entity, keybindings, selections, and hook executions.
- effect
- Core buffer, syntax, movement, and hook primitives. App-level hook effects (save, load, quit, messages) for headless frontends.
- error
- Core buffer, syntax, movement, and hook primitives. Strongly-typed error types and results for editor operations.
- fps
- GPUI canvas rendering, theming, configuration, and editor view. Frame-rate HUD for interactive performance testing. Frame-rate HUD for interactive performance testing.
- history
- Core buffer, syntax, movement, and hook primitives. Granular undo/redo transaction history.
- hook
- Core buffer, syntax, movement, and hook primitives. Extensible hook system and input interceptors.
- input
- GPUI canvas rendering, theming, configuration, and editor view. Translation helpers from GPUI key events to normalized twrite key events.
- keycode
- Core buffer, syntax, movement, and hook primitives. Structured key identity for hooks, hints, and keymaps. Structured key identity for hooks, hints, and keymaps.
- layout_
cache - GPUI canvas rendering, theming, configuration, and editor view. Per-version viewport cache for highlight/conceal/link inputs. Per-version viewport cache for expensive per-line inputs.
- movement
- Core buffer, syntax, movement, and hook primitives. Text movement and boundary calculation primitives.
- prompt
- Core buffer, syntax, movement, and hook primitives. Headless prompt / input-box primitive (bottom bar, command palette).
- prompt_
bar - GPUI canvas rendering, theming, configuration, and editor view. Dumb renderer for the shared headless prompt (bottom bar, palette).
- search
- Core buffer, syntax, movement, and hook primitives. Headless find & replace engine (literal and regex search, single-undo batch replace).
- search_
hook - Core buffer, syntax, movement, and hook primitives. Stock search/replace hook binding the engine to the shared prompt.
- selection
- Core buffer, syntax, movement, and hook primitives. Text selection ranges and anchor/head management.
- syntax
- Core buffer, syntax, movement, and hook primitives. Headless syntax highlighting, styling tokens, and interval splitting.
- theme
- GPUI canvas rendering, theming, configuration, and editor view. Color palettes, Catppuccin themes, and syntax token style resolution.
Structs§
- Auto
Pairs Hook - Core buffer, syntax, movement, and hook primitives. Built-in hook that automatically inserts closing quotes, brackets, and braces, wraps selected text, and steps over closing pairs.
- Cached
Input - GPUI canvas rendering, theming, configuration, and editor view. Owned per-line inputs shared by prepaint and hit-testing.
- Concealed
Line - Core buffer, syntax, movement, and hook primitives. A rendered visual line where concealed syntax tokens have been collapsed, maintaining exact bidirectional mapping to source buffer byte offsets.
- Context
Menu Caps - Core buffer, syntax, movement, and hook primitives. Frontend-supplied capabilities the core cannot observe headlessly.
- Context
Menu Context - Core buffer, syntax, movement, and hook primitives. Read-only snapshot passed to hooks contributing menu items.
- Context
Menu Item - Core buffer, syntax, movement, and hook primitives. One row in the right-click context menu.
- Context
Menu State - Core buffer, syntax, movement, and hook primitives. Headless open/closed menu state + resolved item list.
- Display
Pad - Core buffer, syntax, movement, and hook primitives.
Display-only padding spliced into a
ConcealedLine’s display text. - Editor
- GPUI canvas rendering, theming, configuration, and editor view. The main GPUI text editor view and controller.
- Editor
Buffer - Core buffer, syntax, movement, and hook primitives. A text buffer that manages document contents, cursor position, and undo/redo history.
- Editor
Canvas - GPUI canvas rendering, theming, configuration, and editor view. The GPUI canvas element responsible for shaping and rendering text runs and quads.
- Editor
Config - GPUI canvas rendering, theming, configuration, and editor view. Layout and visual settings for the editor canvas.
- Editor
Theme - GPUI canvas rendering, theming, configuration, and editor view. Complete theme configuration for the editor.
- Face
Availability - GPUI canvas rendering, theming, configuration, and editor view. Availability of bold/italic font faces for the editor’s base font.
- Frame
Stats - GPUI canvas rendering, theming, configuration, and editor view. Rolling frame-time statistics sampled once per rendered frame.
- Hook
Context - Core buffer, syntax, movement, and hook primitives. The mutable editing context passed to editor hooks during events.
- KeyEvent
- Core buffer, syntax, movement, and hook primitives. A normalized keyboard event passed to editor hooks.
- KeyHint
- Core buffer, syntax, movement, and hook primitives. Structured keybinding hint for menu rows.
- Layout
Cache - GPUI canvas rendering, theming, configuration, and editor view. Viewport input cache keyed by buffer version + highlighter revision.
- Line
Metrics - GPUI canvas rendering, theming, configuration, and editor view. Visual layout metrics and block-level decorations for a single rendered line.
- Modifiers
- Core buffer, syntax, movement, and hook primitives. Keyboard modifier keys state.
- Point
- Core buffer, syntax, movement, and hook primitives. A position within a text document.
- Prompt
Bar - GPUI canvas rendering, theming, configuration, and editor view.
Interactive renderer for the shared headless
PromptState. - Prompt
Item - Core buffer, syntax, movement, and hook primitives. One selectable row in a palette-style prompt (commands, files, matches).
- Prompt
Spec - Core buffer, syntax, movement, and hook primitives. How a prompt was opened. Semantics belong to hooks; this is only the view-model a frontend renders.
- Prompt
State - Core buffer, syntax, movement, and hook primitives. Headless single-line input + item list + history.
- Resolved
Token Style - GPUI canvas rendering, theming, configuration, and editor view. Fully resolved style ready for canvas text run construction.
- Rgba
- Core buffer, syntax, movement, and hook primitives. An 8-bit per channel RGBA color representation.
- RunFonts
- GPUI canvas rendering, theming, configuration, and editor view. Fonts used when building styled text runs for a line.
- Search
Hook - Core buffer, syntax, movement, and hook primitives. A stock hook connecting the headless search engine to the shared prompt.
- Search
Query - Core buffer, syntax, movement, and hook primitives.
A headless find query: literal substring or regex, with case and
whole-word toggles. Compiled to a
regex::Regexon use. - Search
Snapshot - Core buffer, syntax, movement, and hook primitives. Owned snapshot of a hook’s search panel for renderers.
- Search
State - Core buffer, syntax, movement, and hook primitives.
Version-cached search state for interactive find (prompt, vim
/). - Selection
- Core buffer, syntax, movement, and hook primitives. Represents a text selection using an anchor and a head.
- Style
Span - Core buffer, syntax, movement, and hook primitives. A styled region of text on a single line.
- Styled
Segment - Core buffer, syntax, movement, and hook primitives. A contiguous segment of text on a line with its resolved style and selection status.
- Syntax
Theme - GPUI canvas rendering, theming, configuration, and editor view. Color configuration for syntax elements.
- Text
Style - Core buffer, syntax, movement, and hook primitives. Direct styling attributes for a span of text.
- Visible
Line Layout - GPUI canvas rendering, theming, configuration, and editor view. Layout metrics and screen coordinates for a visible line, cached during prepaint for instant hit testing.
- Visible
Link - GPUI canvas rendering, theming, configuration, and editor view. A hyperlink visible on screen with its screen pixel bounds and target URL.
Enums§
- Char
Kind - Core buffer, syntax, movement, and hook primitives. Character classification used for word-boundary detection.
- Cursor
Style - Core buffer, syntax, movement, and hook primitives. The visual style of the text cursor.
- Editor
Error - Core buffer, syntax, movement, and hook primitives. Error type for editor buffer validation, range operations, and file I/O.
- Highlight
Tag - Core buffer, syntax, movement, and hook primitives. Semantic categorization for syntax tokens.
- Hook
Effect - Core buffer, syntax, movement, and hook primitives. App-level requests a hook cannot fulfil on its own (file I/O, quit).
- Hook
Outcome - Core buffer, syntax, movement, and hook primitives. The outcome of an editor hook handling an event.
- KeyCode
- Core buffer, syntax, movement, and hook primitives.
A single key, crossterm-style: one
KeyCode::Charvariant absorbs all printable characters instead of per-letter variants. - Prompt
Action - Core buffer, syntax, movement, and hook primitives.
What
PromptState::handle_keydecided for a keystroke. - Prompt
Placement - Core buffer, syntax, movement, and hook primitives. Where a frontend should render an open prompt.
- Search
Action - Core buffer, syntax, movement, and hook primitives. Synthetic search-panel actions from pointer chrome (buttons, boxes).
- Selection
Granularity - GPUI canvas rendering, theming, configuration, and editor view. Selection granularity when mouse-drag selecting text.
- Style
Value - Core buffer, syntax, movement, and hook primitives. The style applied to a token, either via a semantic tag or direct visual attributes.
- Underline
Decoration - Core buffer, syntax, movement, and hook primitives. Visual style for underlined text.
Constants§
- COPY_ID
- Core buffer, syntax, movement, and hook primitives. Well-known id for the built-in Copy action.
- CUT_ID
- Core buffer, syntax, movement, and hook primitives. Well-known context menu item ids for the built-in edit actions.
- DELETE_
ID - Core buffer, syntax, movement, and hook primitives. Well-known id for the built-in Delete action.
- PASTE_
ID - Core buffer, syntax, movement, and hook primitives. Well-known id for the built-in Paste action.
- REDO_ID
- Core buffer, syntax, movement, and hook primitives. Well-known id for the built-in Redo action.
- REPLACE_
PROMPT_ ID - Core buffer, syntax, movement, and hook primitives.
Prompt ids owned by
SearchHook. - SEARCH_
PROMPT_ ID - Core buffer, syntax, movement, and hook primitives.
Prompt ids owned by
SearchHook. - SELECT_
ALL_ ID - Core buffer, syntax, movement, and hook primitives. Well-known id for the built-in Select All action.
- UNDO_ID
- Core buffer, syntax, movement, and hook primitives. Well-known id for the built-in Undo action.
Traits§
- Editor
Hook - Core buffer, syntax, movement, and hook primitives. Trait for intercepting input events, mutating buffer state, and extending editor behaviors.
- Syntax
Highlighter - Core buffer, syntax, movement, and hook primitives. Trait implemented by language tokenizers and syntax highlighters.
Functions§
- build_
line_ text_ runs - GPUI canvas rendering, theming, configuration, and editor view. Builds styled text runs for a single line by blending syntax spans and active selection.
- classify_
char - Core buffer, syntax, movement, and hook primitives.
Classifies a character into
CharKind. - collect_
context_ items - Core buffer, syntax, movement, and hook primitives. Merges defaults with hook-contributed rows.
- collect_
replacements - Core buffer, syntax, movement, and hook primitives.
Collects
(range, expanded_text)pairs forqueryintext, with 0-based ascending ranges. - default_
context_ items - Core buffer, syntax, movement, and hook primitives. Builds the built-in edit rows for the given capabilities.
- display_
width - Core buffer, syntax, movement, and hook primitives.
Display-column width of
sfor monospace alignment. - find_
line_ end - Core buffer, syntax, movement, and hook primitives.
Returns the byte offset of the end of the line containing
cursor_byte, excluding trailing newline characters (\r\nor\n). - find_
line_ range_ at - Core buffer, syntax, movement, and hook primitives.
Returns the byte range of the full line containing
cursor_byte, including any trailing line terminator (\nor\r\n). - find_
line_ start - Core buffer, syntax, movement, and hook primitives.
Returns the byte offset of the beginning of the line containing
cursor_byte. - find_
matches - Core buffer, syntax, movement, and hook primitives.
Returns all match byte ranges for
queryinbuffer, in ascending order. - find_
next - Core buffer, syntax, movement, and hook primitives.
Returns the first match at or after
from_offset, wrapping to the start whenwrapis set. ReturnsOk(None)when there are no matches. - find_
next_ word_ end - Core buffer, syntax, movement, and hook primitives.
Finds the end offset of the current word or next word moving forward from
cursor_byte. - find_
prev - Core buffer, syntax, movement, and hook primitives.
Returns the last match at or before
from_offset, wrapping to the end whenwrapis set. ReturnsOk(None)when there are no matches. - find_
prev_ word_ start - Core buffer, syntax, movement, and hook primitives.
Finds the start offset of the previous word (or punctuation token) moving backward from
cursor_byte. - find_
word_ range_ at - Core buffer, syntax, movement, and hook primitives.
Finds the byte range of the word, punctuation token, or whitespace run containing
cursor_byte. - fps_
badge - GPUI canvas rendering, theming, configuration, and editor view.
Renders
FrameStatsas a color-coded status-bar chip. - fuzzy_
filter - Core buffer, syntax, movement, and hook primitives.
Filters item labels against
query, returning(index, score)pairs sorted by score descending, index ascending. - fuzzy_
score - Core buffer, syntax, movement, and hook primitives.
Scores
candidateagainstqueryas a case-insensitive subsequence. - replace_
all_ query - Core buffer, syntax, movement, and hook primitives.
Replaces all matches of
querywithreplacementas a single undoable transaction. Supports$1/$namecapture expansion in regex mode; literal mode usesreplacementverbatim. Returns the number of replacements applied. - replace_
one_ query - Core buffer, syntax, movement, and hook primitives.
Replaces the single match
rangewithreplacement(undoable). - split_
line_ intervals - Core buffer, syntax, movement, and hook primitives. Computes disjoint, sorted styled segments for a line by splitting across syntax span and selection boundaries.
Type Aliases§
- Editor
Result - Core buffer, syntax, movement, and hook primitives. Specialized Result type for editor operations.