Documentation
¶
Overview ¶
Package editor holds chiquito's framework-agnostic editing state: the text buffer, the cursor, the viewport, and the commands that mutate them. It has no knowledge of Bubble Tea or any terminal — the TUI layer (internal/ui) drives it — so every command here is directly unit-testable.
Index ¶
- type Editor
- func (e *Editor) Bytes() []byte
- func (e *Editor) CursorLineCol() (line, col int)
- func (e *Editor) CursorPos() int
- func (e *Editor) DeleteBackward()
- func (e *Editor) DeleteForward()
- func (e *Editor) Dirty() bool
- func (e *Editor) Height() int
- func (e *Editor) Insert(text string)
- func (e *Editor) InsertNewline()
- func (e *Editor) InsertRune(r rune)
- func (e *Editor) KillLine()
- func (e *Editor) Line(n int) string
- func (e *Editor) LineCount() int
- func (e *Editor) LineEnd()
- func (e *Editor) LineStart()
- func (e *Editor) LineStartPos(line int) int
- func (e *Editor) MarkSaved()
- func (e *Editor) MoveDown()
- func (e *Editor) MoveLeft()
- func (e *Editor) MoveRight()
- func (e *Editor) MoveUp()
- func (e *Editor) Name() string
- func (e *Editor) PosToLineCol(pos int) (line, col int)
- func (e *Editor) Replace(start, end int, text string)
- func (e *Editor) SetCursor(pos int)
- func (e *Editor) SetName(name string)
- func (e *Editor) SetSize(width, height int)
- func (e *Editor) SetTabStops(width int, expand bool)
- func (e *Editor) SetText(s string)
- func (e *Editor) Text() string
- func (e *Editor) Top() int
- func (e *Editor) Width() int
- type LineIndex
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Editor ¶
type Editor struct {
// contains filtered or unexported fields
}
Editor is the editing model: a buffer plus a cursor and viewport over it. The cursor is stored as a single rune index (pos); line/column are derived from the cached LineIndex. The zero value is not usable — construct with New.
func New ¶
New returns an Editor over the given contents, with the cursor at the start. name is the file path used for saving (may be empty for a scratch buffer).
func (*Editor) CursorLineCol ¶
CursorLineCol returns the cursor's zero-based line and rune column.
func (*Editor) DeleteBackward ¶
func (e *Editor) DeleteBackward()
DeleteBackward deletes the rune before the cursor (Backspace).
func (*Editor) DeleteForward ¶
func (e *Editor) DeleteForward()
DeleteForward deletes the rune at the cursor (Emacs C-d / Delete).
func (*Editor) InsertNewline ¶
func (e *Editor) InsertNewline()
InsertNewline inserts a line break at the cursor.
func (*Editor) InsertRune ¶
InsertRune inserts a single rune, expanding tabs to spaces when configured.
func (*Editor) KillLine ¶
func (e *Editor) KillLine()
KillLine deletes from the cursor to the end of the line; if the cursor is already at the end of the line, it deletes the newline, joining the next line (Emacs C-k).
func (*Editor) LineEnd ¶
func (e *Editor) LineEnd()
LineEnd moves to the end of the current line (Emacs C-e).
func (*Editor) LineStart ¶
func (e *Editor) LineStart()
LineStart moves to the first column of the current line (Emacs C-a).
func (*Editor) LineStartPos ¶
LineStartPos returns the rune index at which line begins.
func (*Editor) MarkSaved ¶
func (e *Editor) MarkSaved()
MarkSaved clears the dirty flag after a successful save.
func (*Editor) MoveDown ¶
func (e *Editor) MoveDown()
MoveDown moves to the next line, keeping the remembered goal column.
func (*Editor) MoveLeft ¶
func (e *Editor) MoveLeft()
MoveLeft moves the cursor one rune left, wrapping to the previous line.
func (*Editor) MoveRight ¶
func (e *Editor) MoveRight()
MoveRight moves the cursor one rune right, wrapping to the next line.
func (*Editor) MoveUp ¶
func (e *Editor) MoveUp()
MoveUp moves to the previous line, keeping the remembered goal column.
func (*Editor) PosToLineCol ¶
PosToLineCol converts a rune index to a zero-based line and column.
func (*Editor) Replace ¶
Replace replaces the rune range [start, end) with text and places the cursor just after the inserted text. The range is clamped to the document.
func (*Editor) SetCursor ¶
SetCursor moves the cursor to a rune index (clamped) and keeps it visible.
func (*Editor) SetSize ¶
SetSize sets the viewport dimensions (in text rows/columns) and keeps the cursor visible.
func (*Editor) SetTabStops ¶
SetTabStops configures tab handling used by Insert when a tab is typed.
type LineIndex ¶
type LineIndex struct {
// contains filtered or unexported fields
}
LineIndex caches, for a snapshot of a buffer, the rune offset at which each line begins. It turns line/column ↔ rune-index conversions and cursor movement into O(log n) (binary search) or O(1) lookups, which a raw piece table cannot offer cheaply.
The index is immutable; rebuild it with BuildLineIndex after any edit. (The editor rebuilds on edit; incremental maintenance is a Phase 5 optimization.)
func BuildLineIndex ¶
BuildLineIndex records the start of every line by walking the buffer's piece table (without copying the whole document).
func (*LineIndex) LineCol ¶
LineCol converts a rune index to a (line, col) pair. pos is clamped to [0, total].
func (*LineIndex) LineLen ¶
LineLen returns the number of runes on line, excluding the trailing newline.
func (*LineIndex) RuneIndex ¶
RuneIndex converts a (line, col) pair to a rune index, clamping line to the document and col to the end of that line.