editor

package
v0.0.0-...-794ee3b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

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

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

func New(content []byte, name string) *Editor

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) Bytes

func (e *Editor) Bytes() []byte

Bytes returns the current buffer contents.

func (*Editor) CursorLineCol

func (e *Editor) CursorLineCol() (line, col int)

CursorLineCol returns the cursor's zero-based line and rune column.

func (*Editor) CursorPos

func (e *Editor) CursorPos() int

CursorPos returns the cursor as a rune index.

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) Dirty

func (e *Editor) Dirty() bool

Dirty reports whether there are unsaved changes.

func (*Editor) Height

func (e *Editor) Height() int

Height returns the viewport height in rows.

func (*Editor) Insert

func (e *Editor) Insert(text string)

Insert inserts text at the cursor and advances the cursor past it.

func (*Editor) InsertNewline

func (e *Editor) InsertNewline()

InsertNewline inserts a line break at the cursor.

func (*Editor) InsertRune

func (e *Editor) InsertRune(r rune)

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) Line

func (e *Editor) Line(n int) string

Line returns the contents of the given line (no trailing newline).

func (*Editor) LineCount

func (e *Editor) LineCount() int

LineCount returns the number of lines.

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

func (e *Editor) LineStartPos(line int) int

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) Name

func (e *Editor) Name() string

Name returns the file path backing this editor (possibly empty).

func (*Editor) PosToLineCol

func (e *Editor) PosToLineCol(pos int) (line, col int)

PosToLineCol converts a rune index to a zero-based line and column.

func (*Editor) Replace

func (e *Editor) Replace(start, end int, text string)

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

func (e *Editor) SetCursor(pos int)

SetCursor moves the cursor to a rune index (clamped) and keeps it visible.

func (*Editor) SetName

func (e *Editor) SetName(name string)

SetName sets the file path used for saving.

func (*Editor) SetSize

func (e *Editor) SetSize(width, height int)

SetSize sets the viewport dimensions (in text rows/columns) and keeps the cursor visible.

func (*Editor) SetTabStops

func (e *Editor) SetTabStops(width int, expand bool)

SetTabStops configures tab handling used by Insert when a tab is typed.

func (*Editor) SetText

func (e *Editor) SetText(s string)

SetText replaces the entire document, clamping the cursor into the new bounds. Used by replace-all.

func (*Editor) Text

func (e *Editor) Text() string

Text returns the whole document as a string.

func (*Editor) Top

func (e *Editor) Top() int

Top returns the first visible line index.

func (*Editor) Width

func (e *Editor) Width() int

Width returns the viewport width in columns.

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

func BuildLineIndex(b *buffer.Buffer) *LineIndex

BuildLineIndex records the start of every line by walking the buffer's piece table (without copying the whole document).

func (*LineIndex) Count

func (li *LineIndex) Count() int

Count returns the number of lines (always >= 1).

func (*LineIndex) LineCol

func (li *LineIndex) LineCol(pos int) (line, col int)

LineCol converts a rune index to a (line, col) pair. pos is clamped to [0, total].

func (*LineIndex) LineLen

func (li *LineIndex) LineLen(line int) int

LineLen returns the number of runes on line, excluding the trailing newline.

func (*LineIndex) RuneIndex

func (li *LineIndex) RuneIndex(line, col int) int

RuneIndex converts a (line, col) pair to a rune index, clamping line to the document and col to the end of that line.

func (*LineIndex) Start

func (li *LineIndex) Start(line int) int

Start returns the rune index at which line begins. line is clamped.

func (*LineIndex) Total

func (li *LineIndex) Total() int

Total returns the total number of runes in the indexed buffer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL