Documentation
¶
Overview ¶
Package document implements Gap Buffer based text storage and editing.
Design: Gap Buffer is one of the classic in-memory structures for text editors. It splits the buffer into three parts:
[left half] [gap] [right half]
The cursor position corresponds to the gap start. Inserting at the cursor writes characters into the gap and advances the gap pointer without moving large amounts of data. Deleting simply expands the gap.
Complexity: - Insert/Delete: amortized O(1) - Line-based access: requires scanning, O(n), but can be optimized with line start caching
Package document provides the data structures for editor documents, including encoding detection and conversion support.
Package document's undo.go implements the undo/redo system.
Design: Uses the Command Pattern — each edit operation is recorded as an UndoStep. The stack capacity is configurable (default 100 steps); oldest records are dropped when exceeded. Merge strategy: consecutive insert operations are merged into one step to reduce redundancy.
Index ¶
- func EncodeFromUTF8(text string, enc CommonEncoding) ([]byte, error)
- func GBKToUTF8(data []byte) (string, error)
- func StripBOM(data []byte) []byte
- func UTF8ToGBK(text string) ([]byte, error)
- type CommonEncoding
- type Document
- func (d *Document) Content() string
- func (d *Document) DeleteRange(pos, length int) []rune
- func (d *Document) DeleteRuneAt(pos int) rune
- func (d *Document) GetIndent(line int) string
- func (d *Document) InsertRune(pos int, ch rune)
- func (d *Document) InsertText(pos int, text string)
- func (d *Document) Len() int
- func (d *Document) Line(n int) string
- func (d *Document) LineColToPos(line, col int) int
- func (d *Document) LineCount() int
- func (d *Document) LineLen(line int) int
- func (d *Document) PosToLineCol(pos int) (int, int)
- type GapBuffer
- func (gb *GapBuffer) BytePos(runePos int) int
- func (gb *GapBuffer) Cap() int
- func (gb *GapBuffer) Content() string
- func (gb *GapBuffer) Delete(pos, length int)
- func (gb *GapBuffer) Indentation(line int) string
- func (gb *GapBuffer) Insert(pos int, text []rune)
- func (gb *GapBuffer) Len() int
- func (gb *GapBuffer) Line(line int) string
- func (gb *GapBuffer) LineColToPos(line, col int) int
- func (gb *GapBuffer) LineCount() int
- func (gb *GapBuffer) LineEnd(line int) int
- func (gb *GapBuffer) LineLen(line int) int
- func (gb *GapBuffer) LineStart(line int) int
- func (gb *GapBuffer) PosToLineCol(pos int) (int, int)
- func (gb *GapBuffer) RuneAt(pos int) rune
- func (gb *GapBuffer) Slice(start, end int) []rune
- func (gb *GapBuffer) String() string
- type Position
- type UndoKind
- type UndoStack
- func (us *UndoStack) CanRedo() bool
- func (us *UndoStack) CanUndo() bool
- func (us *UndoStack) Clear()
- func (us *UndoStack) Push(kind UndoKind, pos int, text, deleted []rune)
- func (us *UndoStack) Redo() (UndoStep, bool)
- func (us *UndoStack) RedoCount() int
- func (us *UndoStack) Undo() (UndoStep, bool)
- func (us *UndoStack) UndoCount() int
- type UndoStep
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EncodeFromUTF8 ¶
func EncodeFromUTF8(text string, enc CommonEncoding) ([]byte, error)
EncodeFromUTF8 encodes a UTF-8 string back to the given encoding.
Types ¶
type CommonEncoding ¶
type CommonEncoding int
CommonEncoding represents a human-readable encoding name.
const ( EncUnknown CommonEncoding = iota EncUTF8 EncUTF16LE EncUTF16BE EncGBK EncBig5 EncShiftJIS EncEUCJP EncEUCKR EncLatin1 )
func DecodeToUTF8 ¶
func DecodeToUTF8(data []byte) (string, CommonEncoding, error)
DecodeToUTF8 decodes raw bytes to a UTF-8 string. Returns the decoded string, detected encoding, and any error.
func DetectEncoding ¶
func DetectEncoding(data []byte) CommonEncoding
DetectEncoding detects the character encoding of raw byte data.
func (CommonEncoding) BOM ¶
func (e CommonEncoding) BOM() []byte
BOM returns an ASCII/UTF-8 BOM sequence for UTF-16 encodings (empty for others).
func (CommonEncoding) String ¶
func (e CommonEncoding) String() string
type Document ¶
type Document struct {
Buffer *GapBuffer // underlying gap buffer
FilePath string // file path
Modified bool // modified flag
Encoding CommonEncoding // detected file encoding
}
Document is the high-level document structure wrapping GapBuffer.
func NewDocumentFromString ¶
NewDocumentFromString creates a document from a string.
func (*Document) DeleteRange ¶
DeleteRange deletes 'length' characters starting at pos, returning the deleted text.
func (*Document) DeleteRuneAt ¶
DeleteRuneAt deletes the character at position pos, returning the deleted rune.
func (*Document) InsertRune ¶
InsertRune inserts a single rune at position pos.
func (*Document) InsertText ¶
InsertText inserts a string at position pos.
func (*Document) LineColToPos ¶
LineColToPos converts (line, column) to a global position.
type GapBuffer ¶
type GapBuffer struct {
// contains filtered or unexported fields
}
GapBuffer is the gap buffer data structure.
func NewGapBuffer ¶
NewGapBuffer creates a gap buffer with the specified initial capacity.
func NewGapBufferFromRunes ¶
NewGapBufferFromRunes creates a GapBuffer from a rune slice.
func (*GapBuffer) Indentation ¶
Indentation returns the whitespace indentation string for the given line.
func (*GapBuffer) LineColToPos ¶
LineColToPos converts (line, column) to a global position. Clamps out-of-range values.
func (*GapBuffer) LineCount ¶
LineCount returns the total number of lines. An empty buffer counts as 1 line. Content ending with a newline counts the trailing newline as starting an additional empty line.
func (*GapBuffer) LineEnd ¶
LineEnd returns the position of the first character after the given line, excluding the newline character. Returns the buffer length if the line has no trailing newline.
func (*GapBuffer) LineStart ¶
LineStart returns the position of the first character on the given line. Returns -1 if the line is out of range.
func (*GapBuffer) PosToLineCol ¶
PosToLineCol converts a global position to (line, column).
type UndoStack ¶
type UndoStack struct {
// contains filtered or unexported fields
}
UndoStack manages the undo/redo stacks.
func NewUndoStack ¶
NewUndoStack creates an undo stack with the given step limit.
func (*UndoStack) Push ¶
Push records an operation. Consecutive same-type inserts are merged to reduce steps.
type UndoStep ¶
type UndoStep struct {
Kind UndoKind // Operation type
Pos int // Global position where the operation occurred
Text []rune // Inserted text (Insert) or replaced text (Replace)
Deleted []rune // Deleted text (Delete) or pre-replacement text (Replace)
}
UndoStep records a single undoable operation step.