Code Editor Widget
The code editor widget provides a full-featured text editing experience with syntax highlighting, line numbers, selections, search, and undo/redo support.Basic Usage
Syntax Highlighting
Built-in Languages
The code editor includes tokenizers for multiple languages:typescriptjavascriptjsongorustc/cpp/c++csharp/c#javapythonbashplain(no highlighting)
Token Types
Syntax tokens are classified into semantic categories:keyword- Language keywords (if, function, class)type- Type names (string, number, boolean)string- String literalsnumber- Numeric literalscomment- Commentsoperator- Operators (+, -, *, =)punctuation- Punctuation (., ,, ;, :)function- Function namesvariable- Variable names (uppercase constants)plain- Plain text
Custom Tokenizer
Provide your own tokenizer for custom highlighting:Editing Operations
Text Insertion
The editor supports multi-line paste:Deletion
Indentation
Cursor Movement
Selections
Selection Range
Get Selected Text
Undo/Redo
- Max stack size: 1000 entries (configurable via
MAX_UNDO_STACK) - Grouping window: 300ms (edits within this window are grouped)
Search and Replace
Diagnostics
Show inline error/warning markers:error- Red underlinewarning- Yellow underlineinfo- Blue underlinehint- Gray underline
Configuration
Line Numbers
Tab Size
Word Wrap
Read-Only Mode
Keyboard Shortcuts
Built-in shortcuts:- Arrow keys: Move cursor
- Ctrl/Cmd+Arrow: Move by word
- Home/End: Line start/end
- Ctrl/Cmd+Home/End: Document start/end
- Shift+Arrow: Select
- Ctrl/Cmd+A: Select all
- Ctrl/Cmd+C: Copy
- Ctrl/Cmd+V: Paste
- Ctrl/Cmd+X: Cut
- Ctrl/Cmd+Z: Undo
- Ctrl/Cmd+Shift+Z: Redo
- Tab: Indent (or insert tab)
- Shift+Tab: Dedent
- Backspace: Delete before cursor
- Delete: Delete after cursor
Props Reference
CodeEditorProps
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | Required | Widget identifier |
lines | readonly string[] | Required | Document content |
cursor | CursorPosition | Required | Cursor position |
selection | EditorSelection | null | Required | Selection range |
scrollTop | number | Required | Vertical scroll |
scrollLeft | number | Required | Horizontal scroll |
onChange | (lines: readonly string[], cursor: CursorPosition) => void | Required | Content change callback |
onSelectionChange | (selection: EditorSelection | null) => void | Required | Selection change callback |
onScroll | (scrollTop: number, scrollLeft: number) => void | Required | Scroll callback |
tabSize | number | 2 | Tab size in spaces |
insertSpaces | boolean | true | Use spaces for tabs |
lineNumbers | boolean | true | Show line numbers |
wordWrap | boolean | false | Wrap long lines |
readOnly | boolean | false | Read-only mode |
searchQuery | string | — | Search query |
searchMatches | readonly SearchMatch[] | — | Search match positions |
currentMatchIndex | number | — | Highlighted match index |
diagnostics | readonly CodeEditorDiagnostic[] | — | Error/warning markers |
syntaxLanguage | CodeEditorSyntaxLanguage | "plain" | Built-in syntax language |
tokenizeLine | CodeEditorLineTokenizer | — | Custom tokenizer |
highlightActiveCursorCell | boolean | true | Highlight cursor cell |
onUndo | () => void | — | Undo callback |
onRedo | () => void | — | Redo callback |
focusable | boolean | true | Include in tab order |
accessibleLabel | string | — | Accessibility label |
focusConfig | FocusConfig | — | Focus appearance |
scrollbarVariant | "minimal" | "classic" | "modern" | "dots" | "thin" | "minimal" | Scrollbar style |
scrollbarStyle | TextStyle | — | Scrollbar color |
Related Widgets
- Diff Viewer - Side-by-side diff display
- Input - Single-line text input
- Textarea - Multi-line plain text
Location in Source
- Implementation:
packages/core/src/widgets/codeEditor.ts - Syntax:
packages/core/src/widgets/codeEditorSyntax.ts - Types:
packages/core/src/widgets/types.ts:1856-2012 - Factory:
packages/core/src/widgets/ui.ts:codeEditor()