GGUI::TextBuffer class

Editable UTF-8 text with anchors, undo, and a line index. See the file description.

Constructors, destructors, conversion operators

TextBuffer()
Empty.
TextBuffer(std::string_view text) explicit
Holding text, with no undo history.

Public functions

void SetText(std::string_view text)
Replace everything. Anchors move to the start and undo history is cleared.
auto Size() const -> TextPos
Its length in bytes.
auto IsEmpty() const -> bool
Whether it has no text.
auto Text() const -> std::string
A copy of its text.
void Read(TextPos start, TextPos end, std::string& out) const
Append [start, end) to out.
auto ByteAt(TextPos position) const -> char
The byte at position.
auto IsCharBoundary(TextPos position) const -> bool
True if position is not in the middle of a UTF-8 sequence.
auto NextChar(TextPos position) const -> TextPos
The next code point boundary after position.
auto PrevChar(TextPos position) const -> TextPos
The previous code point boundary before position.
auto LineCount() const -> uint32_t
How many lines it has: at least 1, as an empty buffer has one empty line.
auto LineStart(uint32_t line) const -> TextPos
Where line line starts.
auto LineEnd(uint32_t line) const -> TextPos
End of the line's text, before its line break.
auto LineAt(TextPos position) const -> uint32_t
The line position is on.
void Line(uint32_t line, std::string& out) const
The line without its line break. Replaces out.
auto AddAnchor(TextPos position, AnchorBias bias = AnchorBias::After) -> TextAnchor
A new anchor at position.
void RemoveAnchor(TextAnchor anchor)
Remove anchor. Its handle reads as dead from then on.
auto IsAlive(TextAnchor anchor) const -> bool
Whether anchor hasn't been removed.
auto PositionOf(TextAnchor anchor) const -> TextPos
Where anchor is now; 0 for a dead one.
void MoveAnchor(TextAnchor anchor, TextPos position)
Put anchor at position.
auto Apply(std::span<const TextEdit> edits, TextEditKind kind = TextEditKind::Other) -> bool
Apply edits as one undo step.
auto Replace(TextPos start, TextPos end, std::string_view text, TextEditKind kind = TextEditKind::Other) -> bool
Replace [start, end) with text, as one undo step.
auto Insert(TextPos at, std::string_view text, TextEditKind kind = TextEditKind::Other) -> bool
Insert text at at.
auto Erase(TextPos start, TextPos end, TextEditKind kind = TextEditKind::Other) -> bool
Remove [start, end).
auto CanUndo() const -> bool
Whether there is a step to undo.
auto CanRedo() const -> bool
Whether there is a step to redo.
auto Undo() -> bool
Undo the last step. False if there is none.
auto Redo() -> bool
Redo the step last undone. False if there is none.
auto UndoAndForget() -> bool
Undo the last step and discard it (no redo). For provisional text, e.g. an IME composition being replaced by its next form.
void BreakUndo()
Force the next transaction to start a new undo step (caret moved, time passed).
void ClearUndo()
Forget every undo and redo step.
void SetUndoLimit(uint32_t steps)
The most steps kept; past it the oldest are dropped. 1024 by default.
auto UndoCount() const -> uint32_t
How many steps can be undone.
auto Changed() const -> const GGRevision&
Bumped on every text change.
auto LastChange() const -> const TextChange&
Lines touched by the last change, so only those are re-shaped.
auto PieceCount() const -> size_t
How many pieces the text is in now. For tests and tools.

Function documentation

bool GGUI::TextBuffer::Apply(std::span<const TextEdit> edits, TextEditKind kind = TextEditKind::Other)

Apply edits as one undo step.

All positions refer to the text before any of them. Edits must not overlap or be reversed; if they are, nothing changes and false is returned. Positions past the end are clamped.