buffer

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDebug

func SetDebug(enable bool)

Set debug mode or not. In debug mode, the internal states of PieceTable is printed to console.

Types

type CursorPos

type CursorPos struct {
	// start rune offset
	Start int
	// end rune offset
	End int
}

CursorPos keep track of the previous cursor position of undo/redo.

type PieceTable

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

PieceTable implements a piece table data structure. See the following resources for more information:

https://en.wikipedia.org/wiki/Piece_table

https://www.cs.unm.edu/~crowley/papers/sds.pdf

This implementation is heavily inspired by the design described in James Brown's Piece Chain(http://www.catch22.net/tuts/neatpad/piece-chains).

func NewPieceTable

func NewPieceTable(text []byte) *PieceTable

func (*PieceTable) Changed

func (pt *PieceTable) Changed() bool

func (*PieceTable) Erase

func (pt *PieceTable) Erase(startOff, endOff int) bool

func (*PieceTable) GroupOp

func (pt *PieceTable) GroupOp()

Group operations such as insert, earase or replace in a batch. Nested call share the same single batch.

func (*PieceTable) Insert

func (pt *PieceTable) Insert(runeIndex int, text string) bool

Insert insert text at the logical position specifed by runeIndex. runeIndex is measured by rune. There are 2 scenarios need to be handled:

  1. Insert in the middle of a piece.
  2. Insert at the boundary of two pieces.

func (*PieceTable) Inspect

func (pt *PieceTable) Inspect()

Inspect prints the internal of the piece table. For debug purpose only.

func (*PieceTable) Len

func (pt *PieceTable) Len() int

Size returns the total length of the document data in runes.

func (*PieceTable) Lines

func (li *PieceTable) Lines() []lineInfo

func (*PieceTable) Rebuild

func (li *PieceTable) Rebuild(pt *PieceTable)

func (*PieceTable) Redo

func (pt *PieceTable) Redo() ([]CursorPos, bool)

func (*PieceTable) Replace

func (pt *PieceTable) Replace(startOff, endOff int, text string) bool

Replace removes text from startOff to endOff(exclusive), and insert text at the position of startOff.

func (*PieceTable) SetText

func (pt *PieceTable) SetText(text []byte)

func (*PieceTable) UnGroupOp

func (pt *PieceTable) UnGroupOp()

Ungroup a batch. Latter insert, earase or replace operations outside of a group is not batched.

func (*PieceTable) Undo

func (pt *PieceTable) Undo() ([]CursorPos, bool)

func (*PieceTable) UpdateOnDelete

func (li *PieceTable) UpdateOnDelete(runeIndex int, length int)

func (*PieceTable) UpdateOnInsert

func (li *PieceTable) UpdateOnInsert(runeIndex int, text []byte)

type PieceTableReader

type PieceTableReader struct {
	*PieceTable
	// contains filtered or unexported fields
}

PieceTableReader implements a TextSource.

func NewTextSource

func NewTextSource() *PieceTableReader

func (*PieceTableReader) Lines

func (r *PieceTableReader) Lines() int

func (*PieceTableReader) Read

func (r *PieceTableReader) Read(p []byte) (int, error)

Read implements io.Reader.

func (*PieceTableReader) ReadAt

func (r *PieceTableReader) ReadAt(p []byte, offset int64) (total int, err error)

ReadAt implements io.ReaderAt.

func (*PieceTableReader) ReadLine

func (r *PieceTableReader) ReadLine(lineNum int) (line []byte, runeOff int, err error)

func (*PieceTableReader) ReadRuneAt

func (r *PieceTableReader) ReadRuneAt(runeOff int64) (rune, error)

Need optimization

func (*PieceTableReader) ReadRuneAtBytes

func (r *PieceTableReader) ReadRuneAtBytes(off int64) (rune, int, error)

ReadRuneAt reads the rune starting at the given byte offset, if any.

func (*PieceTableReader) ReadRuneBeforeBytes

func (r *PieceTableReader) ReadRuneBeforeBytes(off int64) (rune, int, error)

ReadRuneAt reads the run prior to the given byte offset, if any.

func (PieceTableReader) Rebuild

func (li PieceTableReader) Rebuild(pt *PieceTable)

func (*PieceTableReader) RuneOffset

func (r *PieceTableReader) RuneOffset(runeOff int) int

RuneOffset returns the byte offset for the rune at position runeOff.

func (*PieceTableReader) Seek

func (r *PieceTableReader) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker.

func (*PieceTableReader) Text

func (r *PieceTableReader) Text(buf []byte) []byte

func (PieceTableReader) UpdateOnDelete

func (li PieceTableReader) UpdateOnDelete(runeIndex int, length int)

func (PieceTableReader) UpdateOnInsert

func (li PieceTableReader) UpdateOnInsert(runeIndex int, text []byte)

type TextSource

type TextSource interface {
	io.Seeker
	io.Reader
	io.ReaderAt

	// ReadRuneAt reads the rune starting at the given rune offset, if any.
	ReadRuneAt(runeOff int64) (rune, error)

	// ReadRuneAtBytes reads the rune starting at the given byte offset, if any.
	// It returns the rune and its width in bytes. If there is an error reading
	// from the buffer, it is also returned.
	ReadRuneAtBytes(off int64) (rune, int, error)

	// ReadRuneBeforeBytes reads the run prior to the given byte offset, if any.
	// It returns the rune and its width in bytes. If there is an error reading
	// from the buffer, it is also returned.
	ReadRuneBeforeBytes(off int64) (rune, int, error)

	// RuneOffset returns the byte offset for the rune at position runeIndex.
	RuneOffset(runeIndex int) int

	//ReadLine reads a line of text from the source. It returns the line as bytes
	// ,the start rune offset and an optional error if there's any.
	ReadLine(lineNum int) ([]byte, int, error)
	// Lines returns the total number of lines/paragraphs of the source.
	Lines() int

	//Text returns the contents of the editor.
	Text(buf []byte) []byte

	// Len is the length of the editor contents, in runes.
	Len() int

	// SetText reset the buffer and replace the content of the buffer with the provided text.
	SetText(text []byte)

	// Insert insert text at the logical position specifed by runeIndex measured by rune.
	Insert(runeIndex int, text string) bool
	// Delete text from startOff to endOff(exclusive).
	Erase(startOff, endOff int) bool
	// Replace replace text from startOff to endOff(exclusive) with text.
	Replace(startOff, endOff int, text string) bool

	// Undo the last insert, erase, or replace, or a group of operations.
	// It returns all the cursor positions after undo.
	Undo() ([]CursorPos, bool)
	// Redo the last insert, erase, or replace, or a group of operations.
	// It returns all the cursor positions after undo.
	Redo() ([]CursorPos, bool)

	// Group operations such as insert, earase or replace in a batch.
	// Nested call share the same single batch.
	GroupOp()

	// Ungroup a batch. Latter insert, earase or replace operations outside of
	// a group is not batched.
	UnGroupOp()

	// Changed report whether the contents have changed since the last call to Changed.
	Changed() bool
}

TextSource provides data for editor.

Basic editing operations, such as insert, delete, replace, undo/redo are supported. If used with GroupOp and UnGroupOp, the undo and redo operations can be batched.

Jump to

Keyboard shortcuts

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