Manages a collection of lines (the raw text) plus related text operations like insert, delete, and substring extraction. More...
#include "donner/editor/TextBuffer.h"
Classes | |
| struct | LineColumnCursor |
| Progress through a line when mapping nondecreasing UTF-8 byte indices. More... | |
Public Types | |
| using | Lines = std::vector<Line> |
| Ordered line storage for the text buffer. | |
Public Member Functions | |
| TextBuffer () | |
| Create an empty text buffer. | |
| void | setText (std::string_view text) |
| Replace the entire buffer with new text. | |
| std::string | getText () const |
| Get the entire text buffer as a single string, joined by newlines. | |
| std::size_t | byteLength () const |
| Byte count of the joined text without constructing a copy of it. | |
| std::string | getText (const Coordinates &start, const Coordinates &end) const |
| Get the text in [start, end), inclusive of start and exclusive of end. | |
| const Line & | getLineGlyphs (int line) const |
| Get the text in a given line. | |
| Line & | getLineGlyphsMutable (int line) |
| Get the text in a given line. | |
| int | insertTextAt (Coordinates &where, std::string_view text, bool indent=false) |
| Insert text at a given position. | |
| void | deleteRange (const Coordinates &start, const Coordinates &end) |
| Remove text in [start, end). | |
| Line & | insertLine (int index, int column=0) |
| Insert an empty line at index, or optionally split at a column in an existing line. | |
| void | removeLine (int index) |
| Remove a single line at the given index if valid. | |
| void | removeLine (int start, int end) |
| Remove lines in the range [start, end). | |
| int | getLineMaxColumn (int line) const |
| Query the max column index of a given line (i.e. | |
| int | getLineCharacterCount (int line) const |
| Query how many characters (glyphs) are on a given line. | |
| int | getCharacterIndex (const Coordinates &coords) const |
| Convert (line, column) to the internal character index within its line. | |
| int | getCharacterColumn (int line, int index, LineColumnCursor &cursor) const |
| Resolve a byte index using a cursor for this line. Reset the cursor when changing lines. | |
| int | getCharacterColumn (int line, int index) const |
| Resolve a single byte index to its editor column. | |
| std::size_t | getByteOffset (const Coordinates &coords) const |
| Resolve a coordinate to a byte offset in the full buffer text. | |
| Coordinates | getCoordinatesAtByteOffset (std::size_t offset) const |
| Resolve a full-buffer byte offset to editor coordinates. | |
| int | getTotalLines () const |
| The total number of lines in the buffer. | |
Static Public Member Functions | |
| static int | glyphByteLength (char c) |
Byte length of the glyph that starts with c: its UTF-8 sequence length, or 1 for a byte that cannot start a sequence (a stray continuation byte or 0xF8-0xFF), so every walk over glyph bytes advances. | |
Manages a collection of lines (the raw text) plus related text operations like insert, delete, and substring extraction.
| struct donner::editor::TextBuffer::LineColumnCursor |
|
inline |
Remove text in [start, end).
Merges lines if needed.
Example:
If you remove 'Line0' entirely, you'd pass start.column=0 and end.column=5. If you want to remove multi-line, for example removing 0Lin, pass start (0,4) and end (1,3).
| start | The start of the range to remove (inclusive). |
| end | The end of the range to remove (exclusive). |
|
inline |
Resolve a coordinate to a byte offset in the full buffer text.
| coords | Coordinate to resolve. |
|
inline |
Resolve a byte index using a cursor for this line. Reset the cursor when changing lines.
| line | Zero-based line index. |
| index | UTF-8 byte index, nondecreasing between calls using the same cursor. |
| cursor | Current byte and column position, advanced by this call. |
|
inline |
Resolve a full-buffer byte offset to editor coordinates.
Offsets are interpreted against getText(), where line breaks are represented by single \n bytes between lines. Offsets that point at a line break resolve to the end of the preceding line; offsets after a line break resolve to the start of the next line.
| offset | Byte offset in the full buffer text. |
|
inline |
Get the text in a given line.
| line | The line number to get. |
|
inline |
Get the text in a given line.
| line | The line number to get. |
|
inline |
Query the max column index of a given line (i.e.
length of that line).
|
inline |
Get the text in [start, end), inclusive of start and exclusive of end.
Columns are visual columns, as everywhere else in this buffer, so they are converted to glyph indices before the glyphs are read.
|
inlinestatic |
Byte length of the glyph that starts with c: its UTF-8 sequence length, or 1 for a byte that cannot start a sequence (a stray continuation byte or 0xF8-0xFF), so every walk over glyph bytes advances.
| c | First byte of the glyph. |
|
inline |
Insert an empty line at index, or optionally split at a column in an existing line.
Returns a reference to the newly inserted line.
|
inline |
Insert text at a given position.
Splits lines, handles newlines, etc.
| where | Coordinates where the text should be inserted. |
| text | The text to insert (can include newlines). |
| indent | Whether to apply special indentation logic if desired. |
|
inline |
Replace the entire buffer with new text.
| text | The new text to load into the buffer. |