doc

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package doc implements the line-buffer document model for mdit: rune-aware edits, undo/redo with coalescing, and safe (mtime-checked) saves. It has no dependency on any TUI library — it is consumed by the editor widget and UI layers built on top of it.

Index

Constants

This section is empty.

Variables

View Source
var ErrExternalChange = errors.New("file changed on disk")

ErrExternalChange is returned by Save when the file's mtime on disk no longer matches the mtime recorded at Load or at the last successful Save.

Functions

This section is empty.

Types

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document is a mutable line buffer with undo/redo history and safe-save support. The zero value is not usable; construct one with Load or NewFromString.

func Load

func Load(path string) (*Document, error)

Load reads path into a new Document. A nonexistent path is not an error: it yields an empty document (one empty line) with Path set to path; the file is created on the first Save.

func NewFromString

func NewFromString(s string) *Document

NewFromString creates a document from in-memory text with no backing file. Save/SaveForce return an error until the document has a path (this task's interface has no SaveAs yet).

func (*Document) Content

func (d *Document) Content() string

Content renders the buffer back to text: lines joined by "\n" with a trailing newline. An empty document (a single empty line) renders as the empty string so Save writes a true 0-byte file.

func (*Document) DeleteBackward

func (d *Document) DeleteBackward(p Position) Position

DeleteBackward deletes the rune before p (like backspace), joining with the previous line if p is at column 0. It is a no-op at the start of the document.

func (*Document) DeleteForward

func (d *Document) DeleteForward(p Position) Position

DeleteForward deletes the rune at p (like the delete key), joining with the next line if p is at the end of the line. It is a no-op at the end of the document.

func (*Document) DeleteRange

func (d *Document) DeleteRange(from, to Position) Position

DeleteRange removes the half-open range [from, to), joining lines if the range crosses a "\n". from/to are normalized so order doesn't matter. It returns the (now smaller) position, the cursor position after deletion.

func (*Document) Dirty

func (d *Document) Dirty() bool

Dirty reports whether the document has unsaved changes. It compares the current content against the content at the last Load/NewFromString/Save, not version counters: undoing back to the saved state must report clean even though Version() has moved on.

func (*Document) Insert

func (d *Document) Insert(p Position, text string) Position

Insert inserts text (which may contain "\n") at p and returns the cursor position immediately after the inserted text.

func (*Document) Line

func (d *Document) Line(i int) string

Line returns line i (0-indexed).

func (*Document) LineCount

func (d *Document) LineCount() int

LineCount returns the number of lines in the buffer.

func (*Document) Lines

func (d *Document) Lines() []string

Lines returns a shallow copy of the buffer; callers must not mutate the document through it.

func (*Document) Path

func (d *Document) Path() string

Path returns the file path associated with the document, or "" if the document was created with NewFromString and never saved.

func (*Document) Redo

func (d *Document) Redo() (Position, bool)

Redo reapplies the most recently undone group, returning the cursor position to restore and true. It returns false if the redo stack is empty (including whenever a subsequent edit has invalidated it).

func (*Document) Save

func (d *Document) Save() error

Save writes the document to Path if the file on disk has not changed since Load or the last successful Save (compared by mtime). If it has, Save returns ErrExternalChange without writing anything. Use SaveForce to overwrite unconditionally.

func (*Document) SaveForce

func (d *Document) SaveForce() error

SaveForce writes the document to Path unconditionally, ignoring any external modification.

func (*Document) TextRange added in v0.3.0

func (d *Document) TextRange(from, to Position) string

TextRange returns the text in the half-open range [from, to). Endpoints are clamped and order does not matter. An empty range yields "".

func (*Document) Undo

func (d *Document) Undo() (Position, bool)

Undo reverts the most recent undo group, returning the cursor position to restore and true. It returns false if the undo stack is empty.

func (*Document) Version

func (d *Document) Version() int

Version increases by one on every mutation (edit, undo, or redo) and is intended as a cheap cache-invalidation key for renderers.

type Position

type Position struct {
	Line, Col int
}

Position identifies a location in the document buffer. Col is measured in runes, not bytes, so unicode text addresses correctly.

Jump to

Keyboard shortcuts

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