Donner 0.5.1
Embeddable browser-grade SVG2 engine
Loading...
Searching...
No Matches
donner::editor::TextEditor Class Reference

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< ShortcutgetDefaultShortcuts ()
 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.

Friends

class TextEditorTests
class TextEditorTests_UndoReversesInsertion_Test
class TextEditorTests_RedoRestoresAfterUndo_Test
class TextEditorTests_MultipleUndoStepsBackthrough_Test
class TextEditorTests_RedoIsClearedAfterNewEdit_Test
class TextEditorTests_CanUndoReturnsTrueAfterEdit_Test

Detailed Description

A text editor widget for Dear ImGui that supports syntax highlighting, undo & redo, and more.

Member Enumeration Documentation

◆ ShortcutId

Shortcut IDs for various editor actions.

Each can be bound to a keyboard combination.

Member Function Documentation

◆ addAutocompleteEntry()

void donner::editor::TextEditor::addAutocompleteEntry ( std::string_view searchTerm,
std::string_view displayText,
std::string_view insertText )
inline

Add an entry to the autocomplete suggestions.

Parameters
searchTermTerm to match against when filtering
displayTextText to show in the autocomplete popup
insertTextText to insert when selected

◆ canRedo()

bool donner::editor::TextEditor::canRedo ( ) const

Check if redo operation is available.

Returns
true if redo is possible

◆ canUndo()

bool donner::editor::TextEditor::canUndo ( ) const

Check if undo operation is available.

Returns
true if undo is possible

◆ colorize()

void donner::editor::TextEditor::colorize ( int fromLine = 0,
int count = -1 )

Update color range for syntax highlighting.

Parameters
fromLineStarting line number
linesNumber of lines to colorize (-1 for all remaining lines)

◆ delete_()

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:

  • A single character at the cursor position if mid-line
  • Merges with the next line if at the end of a line

The operation is recorded for undo history tracking. After deletion:

  • Change tracking is updated
  • Text changed flag is set
  • Content update callback is triggered if set
  • Syntax highlighting is updated for the affected line

◆ getCorrectCursorPosition()

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.

Returns
Adjusted cursor coordinates

◆ getCursorPosition()

Coordinates donner::editor::TextEditor::getCursorPosition ( ) const
inline

Get the current cursor position in the editor.

Returns
Current cursor coordinates

◆ getDefaultShortcuts()

std::vector< Shortcut > donner::editor::TextEditor::getDefaultShortcuts ( )
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.

Returns
A vector of Shortcut objects representing the default shortcuts.

◆ getSelectedText()

std::string donner::editor::TextEditor::getSelectedText ( ) const

Get currently selected text.

Returns
String containing selected text, or empty if no selection

◆ getText()

std::string donner::editor::TextEditor::getText ( ) const

Get all text in the editor.

Returns
String containing entire editor contents

◆ handleEndOfLineDelete()

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.

Parameters
posThe coordinates where the end-of-line deletion occurs
undoThe undo record to store the deletion information

◆ handleMidLineDelete()

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.

Parameters
posThe coordinates where the deletion occurs
undoThe undo record to store the deletion information

◆ hasSelection()

bool donner::editor::TextEditor::hasSelection ( ) const

Check if there is currently selected text.

Returns
true if there is a selection

◆ insertText()

void donner::editor::TextEditor::insertText ( std::string_view text,
bool indent = false )

Insert text at current cursor position.

Handles selection replacement and auto-indentation.

Parameters
textText to insert
indentWhether to apply auto-indentation

◆ insertTextAt()

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:

  • Multi-line text insertion with proper indentation
  • UTF-8 character sequences
  • Fold marker updates for braces
  • Tab expansion and space-based indentation
  • Snippet tag position updates
Parameters
whereThe coordinates where text should be inserted. Modified to point to the end of insertion.
textThe text to insert
indentWhether to apply auto-indentation for newlines
Returns
The number of new lines created

◆ moveDown()

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.

Parameters
amountNumber of lines to move down
selectWhether to extend selection while moving

◆ moveUp()

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.

Parameters
amountNumber of lines to move up
selectWhether to extend selection while moving

◆ processFind()

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.

Parameters
findWordText to search for
findNextWhether this is a "find next" operation

◆ processReplace()

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.

Parameters
findWordText to search for
replaceWordText to replace with
replaceAllWhether to replace all occurrences

◆ redo()

void donner::editor::TextEditor::redo ( int steps = 1)

Redo specified number of operations.

Parameters
stepsNumber of operations to redo

◆ selectAndFocus()

void donner::editor::TextEditor::selectAndFocus ( const Coordinates & start,
const Coordinates & end )
inline

Set a new selection and scroll to it.

Parameters
startSelection start
endSelection end.

◆ setColorizerEnabled()

void donner::editor::TextEditor::setColorizerEnabled ( bool enabled)

Enable or disable syntax highlighting.

Parameters
enabledWhether to enable the colorizer

◆ setCursorPosition()

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.

Parameters
positionNew cursor coordinates

◆ setSelection()

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.

Parameters
startStart coordinates of selection
endEnd coordinates of selection
modeSelection mode (Normal, Word, or Line)

◆ setSelectionEnd()

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.

Parameters
positionSelection end coordinates

◆ setSelectionStart()

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.

Parameters
positionSelection start coordinates

◆ setShortcut()

void donner::editor::TextEditor::setShortcut ( ShortcutId id,
Shortcut shortcut )

Bind a keyboard shortcut to an editor action.

Parameters
idThe action to bind
shortcutThe keyboard shortcut to bind to the action

◆ setShowLineNumbers()

void donner::editor::TextEditor::setShowLineNumbers ( bool show)
inline

Configure line number display.

Parameters
showWhether to show line numbers

◆ setText()

void donner::editor::TextEditor::setText ( std::string_view text,
bool preserveScroll = false )

Replace the entire buffer with new text.

Parameters
textThe new text to load into the buffer.
preserveScrollIf 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.

◆ undo()

void donner::editor::TextEditor::undo ( int steps = 1)

Undo specified number of operations.

Parameters
stepsNumber of operations to undo

The documentation for this class was generated from the following file: