|
|
Donner 0.8.0-pre
Embeddable browser-grade SVG2 engine
|
A text editor widget for Dear ImGui that supports syntax highlighting, undo & redo, and more. More...
#include "donner/editor/TextEditor.h"
Classes | |
| struct | SourceStyleDecoration |
| Source decoration for style/cascade annotations in the editor. More... | |
| struct | AutocompleteRequest |
| Request payload for structured autocomplete providers. More... | |
| struct | AutocompleteSuggestion |
| A single autocomplete suggestion. More... | |
| struct | AutocompleteResponse |
| Response from a structured autocomplete provider. More... | |
Public Types | |
| enum class | ShortcutId { Undo , Redo , MoveUp , SelectUp , MoveDown , SelectDown , MoveLeft , SelectLeft , MoveWordLeft , SelectWordLeft , MoveRight , SelectRight , MoveWordRight , SelectWordRight , MoveUpBlock , SelectUpBlock , MoveDownBlock , SelectDownBlock , MoveTop , SelectTop , MoveBottom , SelectBottom , MoveStartLine , SelectStartLine , MoveEndLine , SelectEndLine , ForwardDelete , ForwardDeleteWord , DeleteRight , BackwardDelete , BackwardDeleteWord , DeleteLeft , Copy , Paste , Cut , SelectAll , AutocompleteOpen , AutocompleteSelect , AutocompleteSelectActive , AutocompleteUp , AutocompleteDown , NewLine , Indent , Unindent , Find , Replace , FindNext , DuplicateLine , CommentLines , UncommentLines , Count } |
| Shortcut IDs for various editor actions. More... | |
| enum class | SourceStyleChipKind { SelectorMatchCount , ReferenceCount } |
| Visual role for a rendered source style chip. More... | |
| using | SelectionMode = ::donner::editor::SelectionMode |
| using | Identifier = ::donner::editor::Identifier |
| using | Identifiers = ::donner::editor::Identifiers |
| using | Keywords = ::donner::editor::Keywords |
| using | ErrorMarkers = ::donner::editor::ErrorMarkers |
| using | Palette = ::donner::editor::Palette |
| using | LanguageDefinition = ::donner::editor::LanguageDefinition |
| using | String = RcString |
| using | AutocompleteProvider |
| Provider callback for syntax-aware autocomplete. | |
Public Member Functions | |
| TextEditor (const TextEditor &)=delete | |
| TextEditor & | operator= (const TextEditor &)=delete |
| TextEditor (TextEditor &&)=delete | |
| TextEditor & | operator= (TextEditor &&)=delete |
| void | setLanguageDefinition (const LanguageDefinition &langDef) |
| void | setPalette (const Palette &value) |
| void | setErrorMarkers (const ErrorMarkers &markers) |
| void | processFind (const std::string &findWord, bool findNext) |
| Process find operation to locate text in editor. | |
| void | processReplace (const std::string &findWord, const std::string &replaceWord, bool replaceAll=false) |
| Process replace operation to replace text in editor. | |
| void | render (std::string_view title, const ImVec2 &size=ImVec2(), bool showBorder=false) |
| void | setText (std::string_view text, bool preserveScroll=false) |
| Replace the entire buffer with new text. | |
| void | applyExternalSourceEdit (std::size_t offset, std::size_t removedLength, std::string_view replacement) |
| Apply source bytes that changed outside the editor without recording a user edit. | |
| bool | needsRerender () const |
| True if the editor needs to be re-rendered. | |
| std::string | getText () const |
| Get all text in the editor. | |
| Coordinates | getCoordinatesAtByteOffset (std::size_t offset) const |
| Resolve a full-buffer byte offset to editor coordinates. | |
| std::size_t | getByteOffsetAtCoordinates (const Coordinates &coordinates) const |
| Resolve editor coordinates to a full-buffer byte offset. | |
| std::string | getSelectedText () const |
| Get currently selected text. | |
| bool | isFocused () const |
| bool | isTextChanged () const |
| bool | isCursorPositionChanged () const |
| bool | didMouseChangeCursorPosition () const |
| Whether mouse input moved the cursor during the current render frame. | |
| std::optional< Coordinates > | hoveredTextPosition () const |
| Source coordinates currently hovered by the mouse, if the text area is hovered. | |
| void | resetTextChanged () |
| bool | hasPendingSourceEditIntents () const |
| True if user-facing edits have pending byte-level source intents. | |
| std::vector< SourceEditIntent > | takePendingSourceEditIntents () |
| Consume pending byte-level source intents captured from user-facing edits. | |
| const LanguageDefinition & | getLanguageDefinition () const |
| const Palette & | getPalette () const |
| bool | isColorizerEnabled () const |
| Returns true if syntax highlighting is enabled. | |
| void | setColorizerEnabled (bool enabled) |
| Enable or disable syntax highlighting. | |
| Coordinates | getCorrectCursorPosition () |
| Get the cursor position adjusted for tab characters. | |
| Coordinates | getCursorPosition () const |
| Get the current cursor position in the editor. | |
| void | setCursorPosition (const Coordinates &position) |
| Set the cursor position and ensure it's visible. | |
| void | setHandleMouseInputs (bool value) |
| bool | isHandleMouseInputsEnabled () const |
| void | setHandleKeyboardInputs (bool value) |
| bool | isHandleKeyboardInputsEnabled () const |
| void | setImGuiChildIgnored (bool value) |
| bool | isImGuiChildIgnored () const |
| void | setSourceFocusModeContextMenu (bool checked) |
| Show a source-focus toggle in the editor context menu. | |
| void | clearSourceFocusModeContextMenu () |
| Hide the source-focus toggle from the editor context menu. | |
| bool | takeSourceFocusModeContextMenuToggleRequest () |
| Consume a source-focus toggle requested from the editor context menu. | |
| void | setShowWhitespaces (bool value) |
| Show or hide whitespace characters in the editor. | |
| bool | isShowingWhitespaces () const |
| void | insertText (std::string_view text, bool indent=false) |
| Insert text at current cursor position. | |
| void | moveUp (int amount=1, bool select=false) |
| Move cursor up by specified number of lines. | |
| void | moveDown (int amount=1, bool select=false) |
| Move cursor down by specified number of lines. | |
| void | moveLeft (int amount=1, bool select=false, bool wordMode=false) |
| void | moveRight (int amount=1, bool select=false, bool wordMode=false) |
| void | moveTop (bool select=false) |
| void | moveBottom (bool select=false) |
| void | moveHome (bool select=false) |
| void | moveEnd (bool select=false) |
| void | setSelectionStart (const Coordinates &position) |
| Set the start position of the current selection. | |
| void | setSelectionEnd (const Coordinates &position) |
| Set the end position of the current selection. | |
| void | selectAndFocus (const Coordinates &start, const Coordinates &end) |
| Set a new selection and scroll to it. | |
| void | setSelection (const Coordinates &start, const Coordinates &end, SelectionMode mode=SelectionMode::Normal) |
| Set the current text selection with specified mode. | |
| void | selectWordUnderCursor () |
| Select the word at the current cursor position. | |
| void | selectAll () |
| Select all text in the editor. | |
| bool | hasSelection () const |
| Check if there is currently selected text. | |
| void | copy () |
| Copy current selection or current line to clipboard. | |
| void | cut () |
| Cut current selection to clipboard. | |
| void | paste () |
| Paste clipboard contents at cursor position. | |
| void | handleEndOfLineDelete (Coordinates pos, UndoRecord &undo) |
| Handles deletion of a line ending at the given coordinates. | |
| void | handleMidLineDelete (Coordinates pos, UndoRecord &undo) |
| Handles deletion of a character in the middle of a line. | |
| void | delete_ () |
| Handles deletion of text at the current cursor position or selection. | |
| bool | canUndo () const |
| Check if undo operation is available. | |
| bool | canRedo () const |
| Check if redo operation is available. | |
| void | undo (int steps=1) |
| Undo specified number of operations. | |
| void | redo (int steps=1) |
| Redo specified number of operations. | |
| void | setHighlightedLines (const std::vector< int > &lines) |
| void | clearHighlightedLines () |
| bool | setHoverSourceRanges (std::vector< SourceByteRange > ranges) |
| Set subtle source ranges to highlight while the source mouse hover is active. | |
| bool | clearHoverSourceRanges () |
| Clear source-hover highlights. | |
| const std::vector< SourceByteRange > & | hoverSourceRanges () const |
| Active source-hover ranges. | |
| bool | setSourceStyleDecorations (std::vector< SourceStyleDecoration > decorations) |
| Set source style/cascade decorations. | |
| bool | clearSourceStyleDecorations () |
| Clear source style/cascade decorations. | |
| const std::vector< SourceStyleDecoration > & | sourceStyleDecorations () const |
| Active source style/cascade decorations. | |
| std::optional< std::size_t > | takeClickedSourceStyleChipId () |
| Return and clear the last clicked source style decoration chip id. | |
| void | setFocusPartition (const FocusPartition &partition) |
| Install a source-focus partition. Hidden lines are folded from the view only. | |
| void | clearFocusPartition () |
| Clear source-focus mode and reveal every line. | |
| bool | hasFocusPartition () const |
| Return true if a source-focus partition is currently active. | |
| bool | isPositionInsideFocusRange (const Coordinates &position) const |
Return true if position is inside the active source-focus range. | |
| bool | isCursorInsideFocusRange () const |
| Return true if the current cursor is inside the active source-focus range. | |
| void | flashSourceRange (SourceByteRange byteRange) |
Add a transient source flash over byteRange. | |
| std::optional< float > | nextFlashWakeSeconds () const |
| Return the next flash wake interval, or nullopt if no flash is active. | |
| std::optional< float > | nextRopeAnimationWakeSeconds () const |
| Return the next rope-animation wake interval, or nullopt if every rope is idle. | |
| const FrameCostBreakdown::SourceRopes & | lastSourceRopeCost () const |
| Latest source-focus reference rope cost counters. | |
| void | tickSourceFlashes () |
| Remove expired flashes using the current steady-clock time. | |
| void | setTabSize (int size) |
| int | getTabSize () const |
| void | setInsertSpaces (bool value) |
| bool | getInsertSpaces () const |
| void | setSmartIndent (bool value) |
| void | setAutoIndentOnPaste (bool value) |
| void | setHighlightLine (bool value) |
| void | setCompleteBraces (bool value) |
| void | setHorizontalScroll (bool) |
| void | setSmartPredictions (bool value) |
| void | setFunctionDeclarationTooltip (bool value) |
| void | setFunctionTooltips (bool value) |
| void | setActiveAutocomplete (bool value) |
| void | setScrollbarMarkers (bool value) |
| void | setSidebarVisible (bool value) |
| void | setSearchEnabled (bool value) |
| void | setHighlightBrackets (bool value) |
| void | setFoldEnabled (bool value) |
| void | setWordWrapEnabled (bool value) |
| bool | wordWrapEnabled () const |
| void | setUIScale (float scale) |
| void | setUIFontSize (float size) |
| void | setEditorFontSize (float size) |
| void | setShortcut (ShortcutId id, Shortcut shortcut) |
| Bind a keyboard shortcut to an editor action. | |
| void | setShowLineNumbers (bool show) |
| Configure line number display. | |
| float | getTextStart () const |
| Get the horizontal offset where text content starts. | |
| void | colorize (int fromLine=0, int count=-1) |
| Update color range for syntax highlighting. | |
| void | colorizeRange (int fromLine=0, int toLine=0) |
| void | colorizeInternal () |
| void | clearAutocompleteData () |
| void | clearAutocompleteEntries () |
| void | addAutocompleteEntry (std::string_view searchTerm, std::string_view displayText, std::string_view insertText) |
| Add an entry to the autocomplete suggestions. | |
| void | setAutocompleteProvider (AutocompleteProvider provider) |
| Install a structured autocomplete provider. | |
| void | deleteRange (const Coordinates &start, const Coordinates &end) |
| int | insertTextAt (Coordinates &where, std::string_view text, bool indent=false) |
| Inserts text at the given coordinates, handling auto-indentation and fold markers. | |
Static Public Member Functions | |
| static std::vector< Shortcut > | getDefaultShortcuts () |
| Returns the default set of keyboard shortcuts for the text editor. | |
| static const Palette & | getDarkPalette () |
Public Attributes | |
| std::function< void(TextEditor *, std::string_view)> | onIdentifierHover |
| std::function< bool(TextEditor *, std::string_view)> | hasIdentifierHover |
| std::function< void(TextEditor *, std::string_view)> | onExpressionHover |
| std::function< bool(TextEditor *, std::string_view)> | hasExpressionHover |
| std::function< void(TextEditor *, std::string_view, Coordinates)> | onCtrlAltClick |
| std::function< void(TextEditor *)> | onContentUpdate |
Static Public Attributes | |
| static constexpr int | kLineNumberSpace = 20 |
| Width of line number margin in pixels. | |
A text editor widget for Dear ImGui that supports syntax highlighting, undo & redo, and more.
| struct donner::editor::TextEditor::AutocompleteRequest |
| struct donner::editor::TextEditor::AutocompleteSuggestion |
| struct donner::editor::TextEditor::AutocompleteResponse |
Response from a structured autocomplete provider.
| Class Members | ||
|---|---|---|
| size_t | replaceEndOffset = 0 | Exclusive source byte offset to replace. |
| size_t | replaceStartOffset = 0 | Inclusive source byte offset to replace. |
| vector< AutocompleteSuggestion > | suggestions | |
Provider callback for syntax-aware autocomplete.
Return std::nullopt when the provider does not handle the current source. Return an empty response to suppress generic word autocomplete for handled contexts that have no suggestions.
|
strong |
Shortcut IDs for various editor actions.
Each can be bound to a keyboard combination.
|
strong |
|
inline |
Add an entry to the autocomplete suggestions.
| searchTerm | Term to match against when filtering |
| displayText | Text to show in the autocomplete popup |
| insertText | Text to insert when selected |
| void donner::editor::TextEditor::applyExternalSourceEdit | ( | std::size_t | offset, |
| std::size_t | removedLength, | ||
| std::string_view | replacement ) |
Apply source bytes that changed outside the editor without recording a user edit.
| offset | Byte offset in the current buffer. |
| removedLength | Number of bytes to remove. |
| replacement | Replacement bytes to insert. |
| bool donner::editor::TextEditor::canRedo | ( | ) | const |
Check if redo operation is available.
| bool donner::editor::TextEditor::canUndo | ( | ) | const |
Check if undo operation is available.
| void donner::editor::TextEditor::colorize | ( | int | fromLine = 0, |
| int | count = -1 ) |
Update color range for syntax highlighting.
| fromLine | Starting line number |
| lines | Number of lines to colorize (-1 for all remaining lines) |
| void donner::editor::TextEditor::delete_ | ( | ) |
Handles deletion of text at the current cursor position or selection.
If there is a selection, deletes the selected text. Otherwise, deletes either:
The operation is recorded for undo history tracking. After deletion:
|
inline |
Resolve editor coordinates to a full-buffer byte offset.
| coordinates | Coordinates in getText(). |
|
inline |
Resolve a full-buffer byte offset to editor coordinates.
| offset | Byte offset in getText(). |
| Coordinates donner::editor::TextEditor::getCorrectCursorPosition | ( | ) |
Get the cursor position adjusted for tab characters.
Returns cursor position with the column adjusted to account for tab size.
|
inline |
Get the current cursor position in the editor.
|
static |
Returns the default set of keyboard shortcuts for the text editor.
These shortcuts map various editor actions (like navigation, editing, and search) to specific key combinations. The returned shortcuts can be further customized at runtime.
| std::string donner::editor::TextEditor::getSelectedText | ( | ) | const |
Get currently selected text.
| std::string donner::editor::TextEditor::getText | ( | ) | const |
Get all text in the editor.
| void donner::editor::TextEditor::handleEndOfLineDelete | ( | Coordinates | pos, |
| UndoRecord & | undo ) |
Handles deletion of a line ending at the given coordinates.
This merges the current line with the next line when the delete key is pressed at the end of a line. The operation is recorded in the undo record for history tracking.
| pos | The coordinates where the end-of-line deletion occurs |
| undo | The undo record to store the deletion information |
| void donner::editor::TextEditor::handleMidLineDelete | ( | Coordinates | pos, |
| UndoRecord & | undo ) |
Handles deletion of a character in the middle of a line.
Deletes a single character (which may be a multi-byte UTF-8 sequence) at the given position and updates any affected fold markers. The operation is recorded in the undo record for history tracking.
| pos | The coordinates where the deletion occurs |
| undo | The undo record to store the deletion information |
| bool donner::editor::TextEditor::hasSelection | ( | ) | const |
Check if there is currently selected text.
| void donner::editor::TextEditor::insertText | ( | std::string_view | text, |
| bool | indent = false ) |
Insert text at current cursor position.
Handles selection replacement and auto-indentation.
| text | Text to insert |
| indent | Whether to apply auto-indentation |
| int donner::editor::TextEditor::insertTextAt | ( | Coordinates & | where, |
| std::string_view | text, | ||
| bool | indent = false ) |
Inserts text at the given coordinates, handling auto-indentation and fold markers.
This method handles:
| where | The coordinates where text should be inserted. Modified to point to the end of insertion. |
| text | The text to insert |
| indent | Whether to apply auto-indentation for newlines |
|
nodiscard |
Return true if position is inside the active source-focus range.
| position | Source editor coordinates to test. |
| void donner::editor::TextEditor::moveDown | ( | int | amount = 1, |
| bool | select = false ) |
Move cursor down by specified number of lines.
Updates selection if shift is held.
| amount | Number of lines to move down |
| select | Whether to extend selection while moving |
| void donner::editor::TextEditor::moveUp | ( | int | amount = 1, |
| bool | select = false ) |
Move cursor up by specified number of lines.
Updates selection if shift is held.
| amount | Number of lines to move up |
| select | Whether to extend selection while moving |
| void donner::editor::TextEditor::processFind | ( | const std::string & | findWord, |
| bool | findNext ) |
Process find operation to locate text in editor.
Updates cursor position and selection to found text.
| findWord | Text to search for |
| findNext | Whether this is a "find next" operation |
| void donner::editor::TextEditor::processReplace | ( | const std::string & | findWord, |
| const std::string & | replaceWord, | ||
| bool | replaceAll = false ) |
Process replace operation to replace text in editor.
Can replace single occurrence or all occurrences.
| findWord | Text to search for |
| replaceWord | Text to replace with |
| replaceAll | Whether to replace all occurrences |
| void donner::editor::TextEditor::redo | ( | int | steps = 1 | ) |
Redo specified number of operations.
| steps | Number of operations to redo |
|
inline |
Set a new selection and scroll to it.
| start | Selection start |
| end | Selection end. |
|
inline |
Install a structured autocomplete provider.
| provider | Provider callback, or empty to use only generic word autocomplete. |
| void donner::editor::TextEditor::setColorizerEnabled | ( | bool | enabled | ) |
Enable or disable syntax highlighting.
| enabled | Whether to enable the colorizer |
| void donner::editor::TextEditor::setCursorPosition | ( | const Coordinates & | position | ) |
Set the cursor position and ensure it's visible.
Updates cursor position and scrolls view if needed.
| position | New cursor coordinates |
| void donner::editor::TextEditor::setSelection | ( | const Coordinates & | start, |
| const Coordinates & | end, | ||
| SelectionMode | mode = SelectionMode::Normal ) |
Set the current text selection with specified mode.
Updates selection range and handles word/line selection modes.
| start | Start coordinates of selection |
| end | End coordinates of selection |
| mode | Selection mode (Normal, Word, or Line) |
| void donner::editor::TextEditor::setSelectionEnd | ( | const Coordinates & | position | ) |
Set the end position of the current selection.
Ensures selection range remains valid by swapping start/end if needed.
| position | Selection end coordinates |
| void donner::editor::TextEditor::setSelectionStart | ( | const Coordinates & | position | ) |
Set the start position of the current selection.
Ensures selection range remains valid by swapping start/end if needed.
| position | Selection start coordinates |
| void donner::editor::TextEditor::setShortcut | ( | ShortcutId | id, |
| Shortcut | shortcut ) |
Bind a keyboard shortcut to an editor action.
| id | The action to bind |
| shortcut | The keyboard shortcut to bind to the action |
|
inline |
Configure line number display.
| show | Whether to show line numbers |
| void donner::editor::TextEditor::setText | ( | std::string_view | text, |
| bool | preserveScroll = false ) |
Replace the entire buffer with new text.
| text | The new text to load into the buffer. |
| preserveScroll | If true, the current scroll position is kept. Use this for in-place edits (e.g. canvas→text writeback) so the view doesn't snap back to the top. Default false matches the "load a fresh document" behavior. |
| void donner::editor::TextEditor::undo | ( | int | steps = 1 | ) |
Undo specified number of operations.
| steps | Number of operations to undo |