Documentation
¶
Overview ¶
Package undo provides methods for undoable/redoable text manipulation. Modifications are made by two operations: insert or delete.
The package is based on the text manipulation in the vis editor. (Some parts are pure ports.) For further information please visit
https://github.com/martanne/vis.
Insertation ¶
When inserting new data there are 2 cases to consider:
1. the insertion point falls into the middle of an exisiting piece which is replaced by three new pieces:
/-+ --> +---------------+ --> +-\ | | | existing text | | | \-+ <-- +---------------+ <-- +-/ ^ insertion point for "demo " /-+ --> +---------+ --> +-----+ --> +-----+ --> +-\ | | | existing| |demo | |text | | | \-+ <-- +---------+ <-- +-----+ <-- +-----+ <-- +-/
2. it falls at a piece boundary:
/-+ --> +---------------+ --> +-\ | | | existing text | | | \-+ <-- +---------------+ <-- +-/ ^ insertion point for "short" /-+ --> +-----+ --> +---------------+ --> +-\ | | |short| | existing text | | | \-+ <-- +-----+ <-- +---------------+ <-- +-/
Deletion ¶
The delete operation can either start/stop midway through a piece or at a boundary. In the former case a new piece is created to represent the remaining text before/after the modification point.
/-+ --> +---------+ --> +-----+ --> +-----+ --> +-\ | | | existing| |demo | |text | | | \-+ <-- +---------+ <-- +-----+ <-- +-----+ <-- +-/ ^ ^ |------ delete range -----| /-+ --> +----+ --> +--+ --> +-\ | | | exi| |t | | | \-+ <-- +----+ <-- +--+ <-- +-/
Changes ¶
Undoing and redoing works with actions (action is a group of changes: insertations and deletations). An action is represented by any operations between two calls of Commit method. Anything that happens between these two calls is a part of that particular action.
Index ¶
- Variables
- type Buffer
- func (b *Buffer) Clean()
- func (b *Buffer) Commit()
- func (b *Buffer) Delete(off, length int64) error
- func (b *Buffer) Dirty() bool
- func (b *Buffer) Insert(off int64, data []byte) error
- func (b *Buffer) ReadAt(data []byte, off int64) (n int, err error)
- func (b *Buffer) Redo() (off, n int64)
- func (b *Buffer) Size() int64
- func (b *Buffer) Undo() (off, n int64)
Constants ¶
This section is empty.
Variables ¶
var ErrWrongOffset = errors.New("offset is greater than buffer size")
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
A Buffer is a structure capable of two operations: inserting or deleting. All operations could be unlimitedly undone or redone.
func NewBuffer ¶
NewBuffer initializes a new buffer with the given content as a starting point. To start with an empty buffer pass nil as a content.
func (*Buffer) Commit ¶
func (b *Buffer) Commit()
Commit commits the currently performed changes and creates an undo/redo point.
func (*Buffer) Delete ¶
Delete deletes the portion of the length at the given offset. An error is returned if the portion isn't in the range of the buffer size. If the length exceeds the size of the buffer, the portions from off to the end of the buffer will be deleted.
func (*Buffer) Dirty ¶
Dirty reports whether the current state of the buffer is different from the initial state or from the one in the time of calling Clean.
func (*Buffer) Insert ¶
Insert inserts the data at the given offset in the buffer. An error is return when the given offset is invalid.
func (*Buffer) Redo ¶
Redo repeats the last undone action. It returns the offset in bytes at which the last change of the action occured and the number of bytes the change added at off. If there is no action to redo, Redo returns -1 as the offset.