buffer

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 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) 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)

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

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

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

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 int) (rune, error)

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

	// 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