Documentation
¶
Overview ¶
Package piecetable provides an editable text buffer with undo/redo support.
Index ¶
- type PieceTable
- func (p *PieceTable) AppendHistoryEntry(selectionStart, selectionEnd int)
- func (p *PieceTable) CanRedo() bool
- func (p *PieceTable) CanUndo() bool
- func (p *PieceTable) FindLineBounds(selStart, selEnd int) (lineStart, lineEnd int)
- func (p *PieceTable) HasText() bool
- func (p *PieceTable) Len() int
- func (p *PieceTable) ReadFrom(r io.Reader) (int64, error)
- func (p *PieceTable) Redo() (selectionStart, selectionEnd int, state *RangedState, ok bool)
- func (p *PieceTable) Replace(text string, start, end int)
- func (p *PieceTable) Reset(text string)
- func (p *PieceTable) SetCurrentRangedState(state *RangedState)
- func (p *PieceTable) Undo() (selectionStart, selectionEnd int, state *RangedState, ok bool)
- func (p *PieceTable) UpdateByIME(text string, start, end int)
- func (p *PieceTable) WriteRangeTo(w io.Writer, start, end int) (int64, error)
- func (p *PieceTable) WriteRangeToWithInsertion(w io.Writer, text string, insertStart, insertEnd, rangeStart, rangeEnd int) (int64, error)
- type RangedState
- type TextRange
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PieceTable ¶
type PieceTable struct {
// contains filtered or unexported fields
}
PieceTable is an editable text buffer backed by a piece table. The zero value is empty and ready for use.
func (*PieceTable) AppendHistoryEntry ¶
func (p *PieceTable) AppendHistoryEntry(selectionStart, selectionEnd int)
AppendHistoryEntry appends a new undo history entry with the text unchanged: undo and redo crossing it restore the selection to [selectionStart, selectionEnd]. The ranged states on either side are recorded via PieceTable.SetCurrentRangedState.
func (*PieceTable) CanRedo ¶
func (p *PieceTable) CanRedo() bool
CanRedo reports whether the piece table can redo.
func (*PieceTable) CanUndo ¶
func (p *PieceTable) CanUndo() bool
CanUndo reports whether the piece table can undo.
func (*PieceTable) FindLineBounds ¶
func (p *PieceTable) FindLineBounds(selStart, selEnd int) (lineStart, lineEnd int)
FindLineBounds returns the byte offsets bounding the line that contains the selection [selStart, selEnd]. lineStart is the position right after the previous line break (or 0 if none), and lineEnd is the position of the next line break (or Len() if none). The line break bytes themselves are excluded from both ends.
Line breaks that fall within [selStart, selEnd) are ignored, so a selection crossing line breaks yields a single combined line view.
func (*PieceTable) HasText ¶
func (p *PieceTable) HasText() bool
HasText reports whether the piece table has any text.
func (*PieceTable) Len ¶
func (p *PieceTable) Len() int
Len returns the length of the current text in bytes.
func (*PieceTable) ReadFrom ¶
func (p *PieceTable) ReadFrom(r io.Reader) (int64, error)
ReadFrom resets the piece table by reading bytes from r until EOF. Unlike bytes.Buffer.ReadFrom, ReadFrom does not append: any prior content is discarded.
The return value is the number of bytes read. On non-EOF error, the piece table is left in an empty state and the error is returned.
func (*PieceTable) Redo ¶
func (p *PieceTable) Redo() (selectionStart, selectionEnd int, state *RangedState, ok bool)
Redo re-applies the last undone operation and returns the selection and the ranged state to restore. The returned state is the one recorded for the position being returned to. The boolean is false when there is nothing to redo.
func (*PieceTable) Replace ¶
func (p *PieceTable) Replace(text string, start, end int)
Replace replaces the bytes in [start, end) with text. The change is recorded in the undo history.
func (*PieceTable) Reset ¶
func (p *PieceTable) Reset(text string)
Reset replaces the current text with text and clears the undo history.
func (*PieceTable) SetCurrentRangedState ¶
func (p *PieceTable) SetCurrentRangedState(state *RangedState)
SetCurrentRangedState records state as the ranged text state of the current history position: Undo and Redo arriving back at this position hand it back. Record the current state before every mutation and before Undo, so the position being left restores correctly.
func (*PieceTable) Undo ¶
func (p *PieceTable) Undo() (selectionStart, selectionEnd int, state *RangedState, ok bool)
Undo reverts the last operation and returns the selection and the ranged state to restore. The returned state is the one recorded for the position being returned to. The boolean is false when there is nothing to undo.
func (*PieceTable) UpdateByIME ¶
func (p *PieceTable) UpdateByIME(text string, start, end int)
UpdateByIME replaces the bytes in [start, end) with text, recording the change in the undo history with IME-merge semantics — consecutive IME edits whose ranges touch the previous redo selection collapse into one undo entry.
func (*PieceTable) WriteRangeTo ¶
WriteRangeTo writes the bytes of the current text in [start, end) to w. start and end are clamped to [0, Len()]; if start >= end after clamping, nothing is written.
func (*PieceTable) WriteRangeToWithInsertion ¶
func (p *PieceTable) WriteRangeToWithInsertion(w io.Writer, text string, insertStart, insertEnd, rangeStart, rangeEnd int) (int64, error)
WriteRangeToWithInsertion writes the bytes of the rendering text in [rangeStart, rangeEnd) to w. The rendering text is the conceptual stream
committed[:insertStart] ++ text ++ committed[insertEnd:]
where committed is the current piece-table content. rangeStart and rangeEnd are clamped to [0, renderingLength] where renderingLength = Len() - (insertEnd - insertStart) + len(text). If the clamped start is not less than the clamped end, nothing is written.
type RangedState ¶
type RangedState struct {
// OverrideStyleRuns is the ranged style overrides of the text.
OverrideStyleRuns textstyle.Runs
// HotspotRanges is the hotspot ranges of the text.
HotspotRanges []TextRange
}
RangedState is the ranged text state at a history position: the ranged style overrides and the hotspot ranges of the text.