Skip to main content

Crate twrite

Crate twrite 

Source
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 interactive Editor view.

§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 (markdown module) and task widgets.
  • wayland, x11, font-kit: platform-backend passthrough to gpui (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§

AutoPairsHook
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.
CachedInput
GPUI canvas rendering, theming, configuration, and editor view. Owned per-line inputs shared by prepaint and hit-testing.
ConcealedLine
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.
ContextMenuCaps
Core buffer, syntax, movement, and hook primitives. Frontend-supplied capabilities the core cannot observe headlessly.
ContextMenuContext
Core buffer, syntax, movement, and hook primitives. Read-only snapshot passed to hooks contributing menu items.
ContextMenuItem
Core buffer, syntax, movement, and hook primitives. One row in the right-click context menu.
ContextMenuState
Core buffer, syntax, movement, and hook primitives. Headless open/closed menu state + resolved item list.
DisplayPad
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.
EditorBuffer
Core buffer, syntax, movement, and hook primitives. A text buffer that manages document contents, cursor position, and undo/redo history.
EditorCanvas
GPUI canvas rendering, theming, configuration, and editor view. The GPUI canvas element responsible for shaping and rendering text runs and quads.
EditorConfig
GPUI canvas rendering, theming, configuration, and editor view. Layout and visual settings for the editor canvas.
EditorTheme
GPUI canvas rendering, theming, configuration, and editor view. Complete theme configuration for the editor.
FaceAvailability
GPUI canvas rendering, theming, configuration, and editor view. Availability of bold/italic font faces for the editor’s base font.
FrameStats
GPUI canvas rendering, theming, configuration, and editor view. Rolling frame-time statistics sampled once per rendered frame.
HookContext
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.
LayoutCache
GPUI canvas rendering, theming, configuration, and editor view. Viewport input cache keyed by buffer version + highlighter revision.
LineMetrics
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.
PromptBar
GPUI canvas rendering, theming, configuration, and editor view. Interactive renderer for the shared headless PromptState.
PromptItem
Core buffer, syntax, movement, and hook primitives. One selectable row in a palette-style prompt (commands, files, matches).
PromptSpec
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.
PromptState
Core buffer, syntax, movement, and hook primitives. Headless single-line input + item list + history.
ResolvedTokenStyle
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.
SearchHook
Core buffer, syntax, movement, and hook primitives. A stock hook connecting the headless search engine to the shared prompt.
SearchQuery
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::Regex on use.
SearchSnapshot
Core buffer, syntax, movement, and hook primitives. Owned snapshot of a hook’s search panel for renderers.
SearchState
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.
StyleSpan
Core buffer, syntax, movement, and hook primitives. A styled region of text on a single line.
StyledSegment
Core buffer, syntax, movement, and hook primitives. A contiguous segment of text on a line with its resolved style and selection status.
SyntaxTheme
GPUI canvas rendering, theming, configuration, and editor view. Color configuration for syntax elements.
TextStyle
Core buffer, syntax, movement, and hook primitives. Direct styling attributes for a span of text.
VisibleLineLayout
GPUI canvas rendering, theming, configuration, and editor view. Layout metrics and screen coordinates for a visible line, cached during prepaint for instant hit testing.
VisibleLink
GPUI canvas rendering, theming, configuration, and editor view. A hyperlink visible on screen with its screen pixel bounds and target URL.

Enums§

CharKind
Core buffer, syntax, movement, and hook primitives. Character classification used for word-boundary detection.
CursorStyle
Core buffer, syntax, movement, and hook primitives. The visual style of the text cursor.
EditorError
Core buffer, syntax, movement, and hook primitives. Error type for editor buffer validation, range operations, and file I/O.
HighlightTag
Core buffer, syntax, movement, and hook primitives. Semantic categorization for syntax tokens.
HookEffect
Core buffer, syntax, movement, and hook primitives. App-level requests a hook cannot fulfil on its own (file I/O, quit).
HookOutcome
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::Char variant absorbs all printable characters instead of per-letter variants.
PromptAction
Core buffer, syntax, movement, and hook primitives. What PromptState::handle_key decided for a keystroke.
PromptPlacement
Core buffer, syntax, movement, and hook primitives. Where a frontend should render an open prompt.
SearchAction
Core buffer, syntax, movement, and hook primitives. Synthetic search-panel actions from pointer chrome (buttons, boxes).
SelectionGranularity
GPUI canvas rendering, theming, configuration, and editor view. Selection granularity when mouse-drag selecting text.
StyleValue
Core buffer, syntax, movement, and hook primitives. The style applied to a token, either via a semantic tag or direct visual attributes.
UnderlineDecoration
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§

EditorHook
Core buffer, syntax, movement, and hook primitives. Trait for intercepting input events, mutating buffer state, and extending editor behaviors.
SyntaxHighlighter
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 for query in text, 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 s for 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\n or \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 (\n or \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 query in buffer, 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 when wrap is set. Returns Ok(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 when wrap is set. Returns Ok(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 FrameStats as 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 candidate against query as a case-insensitive subsequence.
replace_all_query
Core buffer, syntax, movement, and hook primitives. Replaces all matches of query with replacement as a single undoable transaction. Supports $1 / $name capture expansion in regex mode; literal mode uses replacement verbatim. Returns the number of replacements applied.
replace_one_query
Core buffer, syntax, movement, and hook primitives. Replaces the single match range with replacement (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§

EditorResult
Core buffer, syntax, movement, and hook primitives. Specialized Result type for editor operations.