buffer

package
v0.0.0-...-25dfc2e Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp

func Clamp(v, a, b int) int

Clamp keeps `v` within `a` and `b` numerically. `a` must be smaller than `b`. Returns clamped `v`.

func Max

func Max(a, b int) int

Max returns the larger integer.

func Min

func Min(a, b int) int

Min returns the smaller integer.

Types

type Buffer

type Buffer interface {
	// Line returns a slice of the data at the given line, including the ending line-
	// delimiter. line starts from zero. Data returned may or may not be a copy: do not
	// write to it.
	Line(line int) []byte

	// Returns a slice of the buffer from startLine, startCol, to endLine, endCol,
	// inclusive bounds. The returned value may or may not be a copy of the data,
	// so do not write to it.
	Slice(startLine, startCol, endLine, endCol int) []byte

	// RuneAtPos returns the UTF-8 rune at the byte position `pos` of the buffer. The
	// position must be a correct position, otherwise zero is returned.
	RuneAtPos(pos int) rune

	// EachRuneAtPos executes the function `f` at each rune after byte position `pos`.
	// This function should be used as opposed to performing a "per character" operation
	// manually, as it enables caching buffer operations and safety checks. The function
	// returns when the end of the buffer is met or `f` returns true.
	EachRuneAtPos(pos int, f func(pos int, r rune) bool)

	// Bytes returns all of the bytes in the buffer. This function is very likely
	// to copy all of the data in the buffer. Use sparingly. Try using other methods,
	// where possible.
	Bytes() []byte

	// Insert copies a byte slice (inserting it) into the position at line, col.
	Insert(line, col int, value []byte)

	// Remove deletes any characters between startLine, startCol, and endLine,
	// endCol, inclusive bounds.
	Remove(startLine, startCol, endLine, endCol int)

	// Returns the number of occurrences of 'sequence' in the buffer, within the range
	// of start line and col, to end line and col. [start, end) (exclusive end).
	Count(startLine, startCol, endLine, endCol int, sequence []byte) int

	// Len returns the number of bytes in the buffer.
	Len() int

	// Lines returns the number of lines in the buffer. If the buffer is empty,
	// 1 is returned, because there is always at least one line. This function
	// basically counts the number of newline ('\n') characters in a buffer.
	Lines() int

	// RunesInLine returns the number of runes in the given line. That is, the
	// number of Utf-8 codepoints in the line, not bytes. Includes the line delimiter
	// in the count. If that line delimiter is CRLF ('\r\n'), then it adds two.
	RunesInLineWithDelim(line int) int

	// RunesInLine returns the number of runes in the given line. That is, the
	// number of Utf-8 codepoints in the line, not bytes. Excludes line delimiters.
	RunesInLine(line int) int

	// ClampLineCol is a utility function to clamp any provided line and col to
	// only possible values within the buffer, pointing to runes. It first clamps
	// the line, then clamps the column. The column is clamped between zero and
	// the last rune before the line delimiter.
	ClampLineCol(line, col int) (int, int)

	// LineColToPos returns the index of the byte at line, col. If line is less than
	// zero, or more than the number of available lines, the function will panic. If
	// col is less than zero, the function will panic. If col is greater than the
	// length of the line, the position of the last byte of the line is returned,
	// instead.
	LineColToPos(line, col int) int

	// PosToLineCol converts a byte offset (position) of the buffer's bytes, into
	// a line and column. Unless you are working with the Bytes() function, this
	// is unlikely to be useful to you. Position will be clamped.
	PosToLineCol(pos int) (int, int)

	WriteTo(w io.Writer) (int64, error)

	// RegisterCursor adds the Cursor to a slice which the Buffer uses to update
	// each Cursor based on changes that occur in the Buffer. Various functions are
	// called on the Cursor depending upon where the edits occurred and how it should
	// modify the Cursor's position. Unregister a Cursor before deleting it from
	// memory, or forgetting it, with UnregisterPosition.
	RegisterCursor(cursor *Cursor)

	// UnregisterCursor will remove the cursor from the list of watched Cursors.
	// It is mandatory that a Cursor be unregistered before being freed from memory,
	// or otherwise being forgotten.
	UnregisterCursor(cursor *Cursor)
}

A Buffer is wrapper around any buffer data structure like ropes or a gap buffer that can be used for text editors. One way this interface helps is by making all API function parameters line and column indexes, so it is simple and easy to index and use like a text editor. All lines and columns start at zero, and all "end" ranges are inclusive.

Any bounds out of range are panics! If you are unsure your position or range may be out of bounds, use ClampLineCol() or compare with Lines() or ColsInLine().

type ByCol

type ByCol []Match

ByCol implements sort.Interface for []Match based on the Col field.

func (ByCol) Len

func (c ByCol) Len() int

func (ByCol) Less

func (c ByCol) Less(i, j int) bool

func (ByCol) Swap

func (c ByCol) Swap(i, j int)

type Colorscheme

type Colorscheme map[Syntax]tcell.Style

func (*Colorscheme) GetStyle

func (c *Colorscheme) GetStyle(s Syntax) tcell.Style

Gets the tcell.Style from the Colorscheme map for the given Syntax. If the Syntax cannot be found in the map, either the `Default` Syntax is used, or `tcell.DefaultStyle` is returned if the Default is not assigned.

type Cursor

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

A Cursor's functions emulate common cursor actions. To have a Cursor be automatically updated when the buffer has text prepended or appended -- one should register the Cursor with the Buffer's function `RegisterCursor()` which makes the Cursor "anchored" to the Buffer.

func NewCursor

func NewCursor(in *Buffer) Cursor

func (Cursor) Down

func (c Cursor) Down() Cursor

func (Cursor) Eq

func (c Cursor) Eq(other Cursor) bool

func (Cursor) GetLineCol

func (c Cursor) GetLineCol() (line, col int)

func (Cursor) Left

func (c Cursor) Left() Cursor

func (Cursor) NextWordBoundaryEnd

func (c Cursor) NextWordBoundaryEnd() Cursor

NextWordBoundaryEnd proceeds to the position after the last character of the next word boundary to the right of the Cursor. A word boundary is the beginning or end of any sequence of similar or same-classed characters. Whitespace is skipped.

func (Cursor) PrevWordBoundaryStart

func (c Cursor) PrevWordBoundaryStart() Cursor

func (Cursor) Right

func (c Cursor) Right() Cursor

func (Cursor) SetLineCol

func (c Cursor) SetLineCol(line, col int) Cursor

SetLineCol sets the line and col of the Cursor to those provided. `line` is clamped within the range (0, lines in buffer). `col` is then clamped within the range (0, line length in runes).

func (Cursor) Up

func (c Cursor) Up() Cursor

type Highlighter

type Highlighter struct {
	Buffer      Buffer
	Language    *Language
	Colorscheme *Colorscheme
	// contains filtered or unexported fields
}

A Highlighter can answer how to color any part of a provided Buffer. It does so by applying regular expressions over a region of the buffer.

func NewHighlighter

func NewHighlighter(buffer Buffer, lang *Language, colorscheme *Colorscheme) *Highlighter

func (*Highlighter) GetLineMatches

func (h *Highlighter) GetLineMatches(line int) []Match

func (*Highlighter) GetStyle

func (h *Highlighter) GetStyle(match Match) tcell.Style

func (*Highlighter) HasInvalidatedLines

func (h *Highlighter) HasInvalidatedLines(startLine, endLine int) bool

func (*Highlighter) InvalidateLines

func (h *Highlighter) InvalidateLines(startLine, endLine int)

func (*Highlighter) UpdateInvalidatedLines

func (h *Highlighter) UpdateInvalidatedLines(startLine, endLine int)

UpdateInvalidatedLines only updates the highlighting for lines that are invalidated between lines startLine and endLine, inclusively.

func (*Highlighter) UpdateLines

func (h *Highlighter) UpdateLines(startLine, endLine int)

UpdateLines forces the highlighting matches for lines between startLine to endLine, inclusively, to be updated. It is more efficient to mark lines as invalidated when changes occur and call UpdateInvalidatedLines(...).

type Language

type Language struct {
	Name      string
	Filetypes []string // .go, .c, etc.
	Rules     map[*RegexpRegion]Syntax
}

type Match

type Match struct {
	Col     int
	EndLine int // Inclusive
	EndCol  int // Inclusive
	Syntax  Syntax
}

type RegexpRegion

type RegexpRegion struct {
	Start    *regexp.Regexp
	End      *regexp.Regexp   // Should be "$" by default
	Skip     *regexp.Regexp   // Optional
	Error    *regexp.Regexp   // Optional
	Specials []*regexp.Regexp // Optional (nil or zero len)
}

type Region

type Region struct {
	Start Cursor
	End   Cursor
}

A Selection represents a region of the buffer to be selected for text editing purposes. It is asserted that the start position is less than the end position. The start and end are inclusive. If the EndCol of a Region is one more than the last column of a line, then it points to the line delimiter at the end of that line. It is understood that as a Region spans multiple lines, those connecting line-delimiters are included in the selection, as well.

func NewRegion

func NewRegion(in *Buffer) Region

type RopeBuffer

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

func NewRopeBuffer

func NewRopeBuffer(contents []byte) *RopeBuffer

func (*RopeBuffer) Bytes

func (b *RopeBuffer) Bytes() []byte

Bytes returns all of the bytes in the buffer. This function is very likely to copy all of the data in the buffer. Use sparingly. Try using other methods, where possible.

func (*RopeBuffer) ClampLineCol

func (b *RopeBuffer) ClampLineCol(line, col int) (int, int)

ClampLineCol is a utility function to clamp any provided line and col to only possible values within the buffer, pointing to runes. It first clamps the line, then clamps the column. The column is clamped between zero and the last rune before the line delimiter.

func (*RopeBuffer) Count

func (b *RopeBuffer) Count(startLine, startCol, endLine, endCol int, sequence []byte) int

Returns the number of occurrences of 'sequence' in the buffer, within the range of start line and col, to end line and col. End is exclusive.

func (*RopeBuffer) EachRuneAtPos

func (b *RopeBuffer) EachRuneAtPos(pos int, f func(pos int, r rune) bool)

EachRuneAtPos executes the function `f` at each rune after byte position `pos`. This function should be used as opposed to performing a "per character" operation manually, as it enables caching buffer operations and safety checks. The function returns when the end of the buffer is met or `f` returns true.

func (*RopeBuffer) Insert

func (b *RopeBuffer) Insert(line, col int, value []byte)

Insert copies a byte slice (inserting it) into the position at line, col.

func (*RopeBuffer) Len

func (b *RopeBuffer) Len() int

Len returns the number of bytes in the buffer.

func (*RopeBuffer) Line

func (b *RopeBuffer) Line(line int) []byte

Line returns a slice of the data at the given line, including the ending line- delimiter. line starts from zero. Data returned may or may not be a copy: do not write it.

func (*RopeBuffer) LineColToPos

func (b *RopeBuffer) LineColToPos(line, col int) int

LineColToPos returns the index of the byte at line, col. If line is less than zero, or more than the number of available lines, the function will panic. If col is less than zero, the function will panic. If col is greater than the length of the line, the position of the last byte of the line is returned, instead.

func (*RopeBuffer) Lines

func (b *RopeBuffer) Lines() int

Lines returns the number of lines in the buffer. If the buffer is empty, 1 is returned, because there is always at least one line. This function basically counts the number of newline ('\n') characters in a buffer.

func (*RopeBuffer) PosToLineCol

func (b *RopeBuffer) PosToLineCol(pos int) (int, int)

PosToLineCol converts a byte offset (position) of the buffer's bytes, into a line and column. Unless you are working with the Bytes() function, this is unlikely to be useful to you. Position will be clamped.

func (*RopeBuffer) RegisterCursor

func (b *RopeBuffer) RegisterCursor(cursor *Cursor)

RegisterCursor adds the Cursor to a slice which the Buffer uses to update each Cursor based on changes that occur in the Buffer. Various functions are called on the Cursor depending upon where the edits occurred and how it should modify the Cursor's position. Unregister a Cursor before deleting it from memory, or forgetting it, with UnregisterPosition.

func (*RopeBuffer) Remove

func (b *RopeBuffer) Remove(startLine, startCol, endLine, endCol int)

Remove deletes any characters between startLine, startCol, and endLine, endCol, inclusive bounds.

func (*RopeBuffer) RuneAtPos

func (b *RopeBuffer) RuneAtPos(pos int) (val rune)

RuneAtPos returns the UTF-8 rune at the byte position `pos` of the buffer. The position must be a correct position, otherwise zero is returned.

func (*RopeBuffer) RunesInLine

func (b *RopeBuffer) RunesInLine(line int) int

RunesInLine returns the number of runes in the given line. That is, the number of Utf-8 codepoints in the line, not bytes. Excludes line delimiters.

func (*RopeBuffer) RunesInLineWithDelim

func (b *RopeBuffer) RunesInLineWithDelim(line int) int

RunesInLineWithDelim returns the number of runes in the given line. That is, the number of Utf-8 codepoints in the line, not bytes. Includes the line delimiter in the count. If that line delimiter is CRLF ('\r\n'), then it adds two.

func (*RopeBuffer) Slice

func (b *RopeBuffer) Slice(startLine, startCol, endLine, endCol int) []byte

Returns a slice of the buffer from startLine, startCol, to endLine, endCol, inclusive bounds. The returned value may or may not be a copy of the data, so do not write to it.

func (*RopeBuffer) UnregisterCursor

func (b *RopeBuffer) UnregisterCursor(cursor *Cursor)

UnregisterCursor will remove the cursor from the list of watched Cursors. It is mandatory that a Cursor be unregistered before being freed from memory, or otherwise being forgotten.

func (*RopeBuffer) WriteTo

func (b *RopeBuffer) WriteTo(w io.Writer) (int64, error)

type Syntax

type Syntax uint8
const (
	Default Syntax = iota
	Column         // Not necessarily a Syntax; useful for Colorscheming editor column
	Keyword
	String
	Special
	Type
	Number
	Builtin
	Comment
	DocComment
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳