buffer

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BiasForward sets the rule that:
	//
	// - when the marker is at the start of the edit, marker moves
	// to the end of the inserted text.
	//
	// - when the marker is at the end of the edit, it is pushed to
	// the end of the inserted text
	BiasForward = iota

	// BiasBackward sets the rule that:
	//
	// - when the marker is at the start of the edit, it keeps staying
	// at the begining.
	//
	// - when the marker is at the end of the edit, it gets pulled to
	// the start of the inserted text.
	BiasBackward
)

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 Marker added in v0.4.0

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

Marker is a text buffer annotation that tracks a logical location in the buffer over time. It tries to remain logically stationary even when the content changes.

func (*Marker) Offset added in v0.4.0

func (m *Marker) Offset() int

Offset returns the rune offset of the marker in the document.

type MarkerBias added in v0.4.0

type MarkerBias uint8

The specific rule that resolves ambiguity when an edit happens exactly at a marker's position.

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 NewTextSource

func NewTextSource() *PieceTable

func (*PieceTable) Changed

func (pt *PieceTable) Changed() bool

func (*PieceTable) CreateMarker added in v0.4.0

func (pt *PieceTable) CreateMarker(runeOff int, bais MarkerBias) (*Marker, error)

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

func (pt *PieceTable) Len() int

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

func (*PieceTable) Lines added in v0.3.0

func (pt *PieceTable) Lines() int

func (*PieceTable) ReadAt added in v0.3.0

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

ReadAt implements io.ReaderAt

func (*PieceTable) ReadRuneAt added in v0.3.0

func (pt *PieceTable) ReadRuneAt(runeOff int) (rune, error)

func (*PieceTable) Redo

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

func (*PieceTable) RemoveMarker added in v0.4.0

func (pt *PieceTable) RemoveMarker(m *Marker)

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) RuneOffset added in v0.3.0

func (pt *PieceTable) RuneOffset(runeOff int) int

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

func (*PieceTable) SetText

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

func (*PieceTable) Size added in v0.3.0

func (pt *PieceTable) Size() int

func (*PieceTable) UnGroupOp

func (pt *PieceTable) UnGroupOp()

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

This results in global batch op if writes are accessed from concurrent goroutine, may be not what the user want. But in our scenarios most of the time it is safe to do so.

func (*PieceTable) Undo

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

type TextReader added in v0.3.0

type TextReader interface {
	io.Seeker
	io.Reader
	//ReadAll returns the contents of the editor.
	ReadAll(buf []byte) []byte
}

func NewReader added in v0.3.0

func NewReader(src TextSource) TextReader

type TextSource

type TextSource interface {
	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

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

	// Size returns the size of the editor content in bytes.
	Size() int

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

	// Replace replace text from startOff to endOff(exclusive) with text.
	Replace(startOff, endOff int, text string) bool

	// CreateMarker adds a new marker at position runeOff, with the specified bais. A bais
	// controlls how the markers move when the insertion/deletion happens at the boundary location
	// of the marker.
	CreateMarker(runeOff int, bais MarkerBias) (*Marker, error)
	// RemoveMarker removes a marker from the text source.
	RemoveMarker(m *Marker)

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