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 ¶
- Variables
- type Document
- func (d *Document) Content() string
- func (d *Document) DeleteBackward(p Position) Position
- func (d *Document) DeleteForward(p Position) Position
- func (d *Document) DeleteRange(from, to Position) Position
- func (d *Document) Dirty() bool
- func (d *Document) Insert(p Position, text string) Position
- func (d *Document) Line(i int) string
- func (d *Document) LineCount() int
- func (d *Document) Lines() []string
- func (d *Document) Path() string
- func (d *Document) Redo() (Position, bool)
- func (d *Document) Save() error
- func (d *Document) SaveForce() error
- func (d *Document) TextRange(from, to Position) string
- func (d *Document) Undo() (Position, bool)
- func (d *Document) Version() int
- type Position
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Insert inserts text (which may contain "\n") at p and returns the cursor position immediately after the inserted text.
func (*Document) Lines ¶
Lines returns a shallow copy of the buffer; callers must not mutate the document through it.
func (*Document) Path ¶
Path returns the file path associated with the document, or "" if the document was created with NewFromString and never saved.
func (*Document) Redo ¶
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 ¶
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 ¶
SaveForce writes the document to Path unconditionally, ignoring any external modification.
func (*Document) TextRange ¶ added in v0.3.0
TextRange returns the text in the half-open range [from, to). Endpoints are clamped and order does not matter. An empty range yields "".