buffer

package
v0.0.0-...-2ab8766 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	What   ActionType
	Data   []byte
	Cursor Cursor
	Lines  []*Line
}

An action is a single entity of undo/redo history. All changes to contents of a buffer must be initiated by an action.

func NewDeleteAction

func NewDeleteAction(c Cursor, numBytes int) *Action

NewDeleteAction creates a new action deleting numBytes bytes at c.

func NewInsertAction

func NewInsertAction(c Cursor, data []byte) *Action

NewInsertAction creates a new action inserting data bytes at c.

func (*Action) Apply

func (a *Action) Apply(buf *Buffer)

func (*Action) DeletedLines

func (a *Action) DeletedLines() (int, int)

returns the range of deleted lines, the first and the last one

func (*Action) LastLine

func (a *Action) LastLine() *Line

func (*Action) Revert

func (a *Action) Revert(buf *Buffer)

type ActionGroup

type ActionGroup struct {
	Actions []Action
	Next    *ActionGroup
	Prev    *ActionGroup
}

func (*ActionGroup) Append

func (ag *ActionGroup) Append(a *Action)

func (*ActionGroup) CursorAfter

func (ag *ActionGroup) CursorAfter() Cursor

CursorBefore returns cursor position after actions in the group are applied.

func (*ActionGroup) CursorBefore

func (ag *ActionGroup) CursorBefore() Cursor

CursorBefore returns cursor position before actions in the group are applied.

func (*ActionGroup) LastAction

func (ag *ActionGroup) LastAction() *Action

Valid only as long as no new actions were added to the action group.

type ActionType

type ActionType int
const (
	ActionInsert ActionType = 1
	ActionDelete ActionType = -1
)

type Buffer

type Buffer struct {
	FirstLine *Line
	LastLine  *Line
	NumLines  int

	History *ActionGroup

	// absoulte path of the file, if it's empty string, then the file has no
	// on-disk representation
	Path string

	// buffer name (displayed in the status line), must be unique,
	// uniqueness is maintained by godit methods
	Name string
	// contains filtered or unexported fields
}

func NewBuffer

func NewBuffer(r io.Reader) (*Buffer, error)

func NewEmptyBuffer

func NewEmptyBuffer() *Buffer

func (*Buffer) AddListener

func (b *Buffer) AddListener(c chan BufferEvent)

func (*Buffer) CleanupTrailingNewlines

func (b *Buffer) CleanupTrailingNewlines()

CleanupTrailingNewlines removes all but one trailing newlines.

func (*Buffer) CleanupTrailingSpaces

func (b *Buffer) CleanupTrailingSpaces()

CleanupTrailingSpaces removes trailing whitespace characters from every line in the buffer.

func (*Buffer) Delete

func (b *Buffer) Delete(c Cursor, numBytes int)

func (*Buffer) DeleteLine

func (b *Buffer) DeleteLine(line *Line)

func (*Buffer) DeleteRange

func (b *Buffer) DeleteRange(from Cursor, to Cursor)

func (*Buffer) DeleteRune

func (b *Buffer) DeleteRune(c Cursor)

If at the EOL, move contents of the next line to the end of the current line, erasing the next line after that. Otherwise, delete one character under the cursor.

func (*Buffer) DeleteRuneBackward

func (b *Buffer) DeleteRuneBackward(c Cursor)

If at the beginning of the line, move contents of the current line to the end of the previous line. Otherwise, erase one character backward.

func (*Buffer) Emit

func (b *Buffer) Emit(e BufferEvent)

func (*Buffer) EnsureTrailingEOL

func (b *Buffer) EnsureTrailingEOL()

EnsureTrailingEOL adds a newline at the end of the buffer, unless one exists already.

func (*Buffer) FinalizeActionGroup

func (b *Buffer) FinalizeActionGroup()

func (*Buffer) Insert

func (b *Buffer) Insert(c Cursor, data []byte)

func (*Buffer) InsertLine

func (b *Buffer) InsertLine(line *Line, prev *Line)

InsertLine inserts a line after prev in the buffer. If prev is nil then the line will be the new first line of the buffer.

func (*Buffer) InsertRune

func (b *Buffer) InsertRune(c Cursor, r rune)

InsertRune inserts 'r' at the cursor position 'c'

func (*Buffer) Redo

func (b *Buffer) Redo()

func (*Buffer) RemoveListener

func (b *Buffer) RemoveListener(c chan BufferEvent)

func (*Buffer) Save

func (b *Buffer) Save() error

func (*Buffer) SaveAs

func (b *Buffer) SaveAs(filename string) error

func (*Buffer) SyncedWithDisk

func (b *Buffer) SyncedWithDisk() bool

func (*Buffer) Undo

func (b *Buffer) Undo()

type BufferEvent

type BufferEvent struct {
	Type   BufferEventType
	Action *Action
}

type BufferEventType

type BufferEventType int
const (
	BufferEventInsert BufferEventType = iota
	BufferEventDelete
	BufferEventBOF
	BufferEventEOF
	BufferEventHistoryBack
	BufferEventHistoryStart
	BufferEventHistoryForward
	BufferEventHistoryEnd
	BufferEventSave
)

type BufferReader

type BufferReader struct {
	Line *Line
	// contains filtered or unexported fields
}

func (*BufferReader) Read

func (br *BufferReader) Read(data []byte) (int, error)

type Cursor

type Cursor struct {
	Line    *Line
	LineNum int
	Boffset int
}

A Cursor represents a position within a buffer.

func SortCursors

func SortCursors(c1, c2 Cursor) (r1, r2 Cursor)

SortCursors orders a pair of cursors, from closest to furthest from the beginning of the buffer.

func (Cursor) Above

func (c Cursor) Above(other Cursor) bool

Above reports whether the cursor is above other, regardless of column.

func (Cursor) After

func (c Cursor) After(other Cursor) bool

Before reports whether the cursor is after other.

func (*Cursor) BOF

func (c *Cursor) BOF() bool

BOF reports whether the cursor is at the beginning of the file.

func (*Cursor) BOL

func (c *Cursor) BOL() bool

BOL reports whether the cursor is at the beginning of the current line.

func (Cursor) Before

func (c Cursor) Before(other Cursor) bool

Before reports whether the cursor is before other.

func (Cursor) Below

func (c Cursor) Below(other Cursor) bool

Below reports whether the cursor is below other, regardless of column.

func (Cursor) Distance

func (a Cursor) Distance(b Cursor) int

Distance returns the distance between the cursor and another in bytes.

func (*Cursor) EOF

func (c *Cursor) EOF() bool

EOF reports whether the cursor is at the end of the file.

func (*Cursor) EOL

func (c *Cursor) EOL() bool

EOL reports whether the cursor is at the end of the current line.

func (*Cursor) EndWord

func (c *Cursor) EndWord() bool

EndWord moves cursor to the end of current word or seeks to the beginning of next word, if character under cursor is a whitespace.

func (Cursor) Equals

func (c Cursor) Equals(other Cursor) bool

Equals reports whether the cursor position equals that of other. This function avoids a check for pointer equality of the Line pointer.

func (*Cursor) ExtractBytes

func (c *Cursor) ExtractBytes(n int) []byte

ExtractBytes returns a slice of up to n bytes from the current cursor position.

func (*Cursor) FirstLine

func (c *Cursor) FirstLine() bool

FirstLine reports whether the cursor is at the first line of the buffer.

func (*Cursor) LastLine

func (c *Cursor) LastLine() bool

LastLine reports whether the cursor is at the last line of the buffer.

func (Cursor) LeftOf

func (c Cursor) LeftOf(other Cursor) bool

Left reports whether the cursor is to the left of other, regardless of line.

func (*Cursor) MoveBOL

func (c *Cursor) MoveBOL()

MoveBOL moves the cursor to the beginning of the current line.

func (*Cursor) MoveEOL

func (c *Cursor) MoveEOL()

MoveEOL moves the cursor to the end of the current line.

func (*Cursor) NextLine

func (c *Cursor) NextLine() bool

NextLine moves the cursor to the next line. It reports whether the motion succeeded.

func (*Cursor) NextRune

func (c *Cursor) NextRune(wrap bool) bool

NextRune moves cursor to the next rune. If wrap is true, wraps the cursor to the beginning of next line once the end of the current one is reached. Returns true if motion succeeded, false otherwise.

func (*Cursor) NextRuneFunc

func (c *Cursor) NextRuneFunc(f func(rune) bool) bool

Move cursor forward until current rune satisfies condition f. Returns true if the move was successful, false if EOF reached.

func (*Cursor) NextWord

func (c *Cursor) NextWord() bool

Move cursor forward to beginning of next word. Skips the rest of the current word, if any. Returns true if the move was successful, false if EOF reached.

func (*Cursor) OnDeleteAdjust

func (c *Cursor) OnDeleteAdjust(a *Action)

func (*Cursor) OnInsertAdjust

func (c *Cursor) OnInsertAdjust(a *Action)

func (*Cursor) PrevLine

func (c *Cursor) PrevLine() bool

PrevLine moves the cursor to the previous line. It reports whether the motion succeeded.

func (*Cursor) PrevRune

func (c *Cursor) PrevRune(wrap bool) bool

PrevRune moves cursor to the previous rune. If wrap is true, wraps the cursor to the end of next line once the beginning of the current one is reached. Returns true if motion succeeded, false otherwise.

func (*Cursor) PrevRuneFunc

func (c *Cursor) PrevRuneFunc(f func(rune) bool) bool

Move cursor backward until current rune satisfies condition f. Returns true if the move was successful, false if EOF reached.

func (*Cursor) PrevWord

func (c *Cursor) PrevWord() bool

Move cursor forward to beginning of the previous word. Skips the rest of the current word, if any, unless is located at its first character. Returns true if the move was successful, false if EOF reached.

func (Cursor) RightOf

func (c Cursor) RightOf(other Cursor) bool

Right reports whether the cursor is to the right of other, regardless of line.

func (*Cursor) RuneAfter

func (c *Cursor) RuneAfter() (rune, int)

RuneAfter return the rune after the current cursor and its width in bytes.

func (*Cursor) RuneBefore

func (c *Cursor) RuneBefore() (rune, int)

RuneUnder returns the rune before the current cursor and its width in bytes.

func (*Cursor) RuneUnder

func (c *Cursor) RuneUnder() (rune, int)

RuneUnder returns the rune under the current cursor and its width in bytes.

func (*Cursor) VoffsetCoffset

func (c *Cursor) VoffsetCoffset() (vo, co int)

VoffsetCoffset returns a visual and a character offset for a given cursor.

func (*Cursor) WordUnderCursor

func (c *Cursor) WordUnderCursor() []byte

type Line

type Line struct {
	Data []byte
	Next *Line
	Prev *Line
}

func (*Line) FindClosestOffsets

func (l *Line) FindClosestOffsets(voffset int) (bo, co, vo int)

Find a set of closest offsets for a given visual offset

func (Line) Len

func (l Line) Len() int

Len returns the length of the line in bytes

type Range

type Range struct {
	Start Cursor
	End   Cursor
}

type RangeFunc

type RangeFunc func(from Cursor, to Cursor)

Jump to

Keyboard shortcuts

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