undo

package
v0.0.0-...-e21ce11 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2020 License: MIT Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

func NewBuffer(content []byte) *Buffer

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

func (b *Buffer) Clean()

Clean marks the buffer as non-dirty.

func (*Buffer) Commit

func (b *Buffer) Commit()

Commit commits the currently performed changes and creates an undo/redo point.

func (*Buffer) Delete

func (b *Buffer) Delete(off, length int64) error

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

func (b *Buffer) Dirty() bool

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

func (b *Buffer) Insert(off int64, data []byte) error

Insert inserts the data at the given offset in the buffer. An error is return when the given offset is invalid.

func (*Buffer) ReadAt

func (b *Buffer) ReadAt(data []byte, off int64) (n int, err error)

func (*Buffer) Redo

func (b *Buffer) Redo() (off, n int64)

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.

func (*Buffer) Size

func (b *Buffer) Size() int64

Size returns the size of the buffer in the current state. Size is the number of bytes available for reading via ReadAt. Operations like Insert, Delete, Undo and Redo modify the size.

func (*Buffer) Undo

func (b *Buffer) Undo() (off, n int64)

Undo reverts the last performed action. It returns the offset in bytes at which the first change of the action occured and the number of bytes the change added at off. If there is no action to undo, Undo returns -1 as the offset.

Jump to

Keyboard shortcuts

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