|
|
Donner 0.5.1
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"
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... | |
| 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 |
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. | |
| bool | needsRerender () const |
| True if the editor needs to be re-rendered. | |
| std::string | getText () const |
| Get all text in the editor. | |
| std::string | getSelectedText () const |
| Get currently selected text. | |
| bool | isFocused () const |
| bool | isTextChanged () const |
| bool | isCursorPositionChanged () const |
| void | resetTextChanged () |
| 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 | 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 () |
| 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 value) |
| 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 | 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 | 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.
|
strong |
Shortcut IDs for various editor actions.
Each can be bound to a keyboard combination.
|
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 |
| 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:
| 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 |
| 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. |
| 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 |