Skip to main content

Overview

The EditBuffer class extends EventEmitter and provides a complete text editing solution with cursor management, undo/redo history, and grapheme-aware text operations. It’s designed for building text editors and interactive text input components.

Constructor

RenderLib
required
The render library instance.
Pointer
required
Pointer to the native buffer.

Static Methods

create

Creates a new EditBuffer instance.
WidthMethod
required
The width calculation method for character rendering.
EditBuffer
A new EditBuffer instance.

Properties

id

number
Unique identifier for this buffer instance.

ptr

Pointer
The native pointer to the buffer.

Text Operations

setText

Sets text and completely resets the buffer state (clears history).
string
required
The text content to set.

setTextOwned

Sets text using owned memory and resets buffer state. Native code takes ownership.
string
required
The text content to set.

replaceText

Replaces text while preserving undo history (creates an undo point).
string
required
The replacement text content.

replaceTextOwned

Replaces text using owned memory while preserving undo history.
string
required
The replacement text content.

getText

Retrieves the current text content.
string
The current text content.

getTextRange

Retrieves a range of text by character offsets.
number
required
The starting character offset.
number
required
The ending character offset.
string
The text in the specified range.

getTextRangeByCoords

Retrieves a range of text by line/column coordinates.
number
required
The starting row (0-based).
number
required
The starting column (0-based).
number
required
The ending row (0-based).
number
required
The ending column (0-based).
string
The text in the specified range.

getLineCount

Gets the number of lines in the buffer.
number
The number of lines.

Editing Operations

insertChar

Inserts a character at the current cursor position.
string
required
The character to insert.

insertText

Inserts text at the current cursor position.
string
required
The text to insert.

deleteChar

Deletes the character at the cursor position (forward delete).

deleteCharBackward

Deletes the character before the cursor position (backspace).

deleteRange

Deletes a range of text by coordinates.
number
required
The starting line (0-based).
number
required
The starting column (0-based).
number
required
The ending line (0-based).
number
required
The ending column (0-based).

newLine

Inserts a new line at the cursor position.

deleteLine

Deletes the current line.

Cursor Operations

getCursorPosition

Gets the current cursor position.
LogicalCursor
Object containing row, col, and offset properties.

setCursor

Sets the cursor to a specific line and column.
number
required
The line number (0-based).
number
required
The column number (0-based).

setCursorToLineCol

Sets the cursor to a specific line and column.
number
required
The line number (0-based).
number
required
The column number (0-based).

setCursorByOffset

Sets the cursor by character offset.
number
required
The character offset (0-based).

moveCursorLeft

Moves the cursor one grapheme left.

moveCursorRight

Moves the cursor one grapheme right.

moveCursorUp

Moves the cursor one line up.

moveCursorDown

Moves the cursor one line down.

gotoLine

Moves the cursor to the start of a specific line.
number
required
The line number (0-based).

getNextWordBoundary

Finds the next word boundary from the cursor.
LogicalCursor
The position of the next word boundary.

getPrevWordBoundary

Finds the previous word boundary from the cursor.
LogicalCursor
The position of the previous word boundary.

getEOL

Gets the end-of-line position for the current line.
LogicalCursor
The end-of-line position.

Position Conversion

offsetToPosition

Converts a character offset to a line/column position.
number
required
The character offset.
{ row: number; col: number } | null
The row/column position or null if invalid offset.

positionToOffset

Converts a line/column position to a character offset.
number
required
The row number (0-based).
number
required
The column number (0-based).
number
The character offset.

getLineStartOffset

Gets the character offset of the start of a line.
number
required
The row number (0-based).
number
The character offset of the line start.

Undo/Redo

undo

Undoes the last edit operation.
string | null
Metadata about the undo operation or null if nothing to undo.

redo

Redoes the last undone operation.
string | null
Metadata about the redo operation or null if nothing to redo.

canUndo

Checks if there are operations available to undo.
boolean
True if undo is available.

canRedo

Checks if there are operations available to redo.
boolean
True if redo is available.

clearHistory

Clears the entire undo/redo history.

Styling

setDefaultFg

Sets the default foreground color.
RGBA | null
required
The foreground color or null to clear.

setDefaultBg

Sets the default background color.
RGBA | null
required
The background color or null to clear.

setDefaultAttributes

Sets default text attributes (bold, italic, etc.).
number | null
required
Bitfield of text attributes or null to clear.

resetDefaults

Resets all default styling to original values.

setSyntaxStyle

Sets the syntax highlighting style.
SyntaxStyle | null
required
The syntax style or null to disable.

getSyntaxStyle

Gets the current syntax highlighting style.
SyntaxStyle | null
The current syntax style or null if none is set.

Highlights

addHighlight

Adds a highlight to a specific line by column positions.
number
required
The line index (0-based).
Highlight
required
The highlight definition with start/end columns.

addHighlightByCharRange

Adds a highlight using absolute character offsets.
Highlight
required
The highlight definition with start/end character offsets.

removeHighlightsByRef

Removes all highlights with a specific reference ID.
number
required
The highlight reference ID.

clearLineHighlights

Clears all highlights on a specific line.
number
required
The line index (0-based).

clearAllHighlights

Removes all highlights from the buffer.

getLineHighlights

Retrieves all highlights for a specific line.
number
required
The line index (0-based).
Array<Highlight>
Array of highlights on the line.

Memory Management

clear

Clears the buffer content.

destroy

Destroys the buffer and frees native resources.

debugLogRope

Logs the internal rope data structure for debugging.

Types

LogicalCursor

number
required
The row position (0-based).
number
required
The column position (0-based).
number
required
The character offset from start of buffer.

Events

EditBuffer extends EventEmitter and emits native events. Events are prefixed with eb_ internally but exposed without the prefix.

Example