textview

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 27 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Region

type Region = lt.Region

Region describes the position and baseline of an area of interest within shaped text.

type SelectionAction

type SelectionAction int
const (
	SelectionExtend SelectionAction = iota
	SelectionClear
)

type TextView

type TextView struct {
	// Font set the font used to draw the text.
	Font font.Font
	// TextSize set the size of both the main text and line number.
	TextSize unit.Sp
	// Alignment controls the alignment of text within the editor.
	Alignment text.Alignment
	// LineHeight controls the distance between the baselines of lines of text.
	// If zero, the font size will be used.
	LineHeight unit.Sp
	// LineHeightScale applies a scaling factor to the LineHeight. If zero, a default
	// value 1.2 will be used.
	LineHeightScale float32

	// CaretWidth set the visual width of a caret.
	CaretWidth unit.Dp

	// SoftTab controls the behaviour when user try to insert a Tab character.
	// If set to true, the editor will insert the amount of space characters specified by
	// TabWidth, else the editor insert a \t character.
	SoftTab bool

	// TabWidth set how many spaces to represent a tab character. In the case of
	// soft tab, this determines the number of space characters to insert into the editor.
	// While for hard tab, this controls the maximum width of the 'tab' glyph to expand to.
	TabWidth int

	// CornerRadius set the radius when drawing selection polygons and other corners that apply.
	CornerRadius unit.Dp

	// WrapLine configures whether the displayed text will be broken into lines or not.
	WrapLine bool

	// WordSeperators configures a set of characters that will be used as word separators
	// when doing word related operations, like navigating or deleting by word.
	WordSeperators string
	// Brackets and quote pairs that can be auto-completed when the left half is entered.
	BracketsQuotes *bracketsQuotes
	// contains filtered or unexported fields
}

TextView provides efficient shaping and indexing of interactive text. When provided with a TextSource, TextView will shape and cache the runes within that source. It provides methods for configuring a viewport onto the shaped text which can be scrolled, and for configuring and drawing text selection boxes.

func NewTextView

func NewTextView() *TextView

func (*TextView) AddDecorations

func (e *TextView) AddDecorations(styles ...decoration.Decoration) error

func (*TextView) ByteOffset

func (e *TextView) ByteOffset(runeOffset int) int64

ByteOffset returns the start byte of the rune at the given rune offset.

func (*TextView) CaretCoords

func (e *TextView) CaretCoords() f32.Point

CaretCoords returns the coordinates of the caret, relative to the editor itself.

func (*TextView) CaretInfo

func (e *TextView) CaretInfo() (pos image.Point, ascent, descent int)

func (*TextView) CaretPos

func (e *TextView) CaretPos() (line, col int)

CaretPos returns the line & column numbers of the caret.

func (*TextView) Changed

func (e *TextView) Changed() bool

func (*TextView) ClearDecorations

func (e *TextView) ClearDecorations(source string) error

func (*TextView) ClearSelection

func (e *TextView) ClearSelection()

ClearSelection clears the selection, by setting the selection end equal to the selection start.

func (*TextView) ConvertPos

func (e *TextView) ConvertPos(line, col int) int

ConvertPos convert a line/col position to rune offset. line is counted by paragrah, and col is counted by rune.

func (*TextView) Dimensions

func (e *TextView) Dimensions() layout.Dimensions

Dimensions returns the dimensions of the visible text.

func (*TextView) FindAllTextOccurrences added in v0.5.0

func (e *TextView) FindAllTextOccurrences(start, end int) [][2]int

FindAllTextOccurrences returns the start and end rune offsets of all occurrences of the text spanning from start to end (exclusive). This implementation scans the document once with O(n) complexity. It matches exact rune sequences regardless of word boundaries.

func (*TextView) FindAllWordOccurrences added in v0.5.0

func (e *TextView) FindAllWordOccurrences(start, end int, bySpace bool) [][2]int

FindAllWordOccurrences returns the start and end rune offsets of all occurrences of the word spanning from start to end (exclusive). The bySpace parameter controls whether only spaces are considered separators (true) or custom word separators are used (false). This implementation scans the document once with O(n) complexity.

func (*TextView) FindParagraph

func (e *TextView) FindParagraph(runeIdx int) (int, lt.Paragraph)

find a paragraph by rune index, returning the line number(starting from zero) and the paragraph itself.

func (*TextView) FullDimensions

func (e *TextView) FullDimensions() layout.Dimensions

FullDimensions returns the dimensions of all shaped text, including text that isn't visible within the current viewport.

func (*TextView) GetLineHeight added in v0.5.0

func (e *TextView) GetLineHeight() fixed.Int26_6

GetLineHeight returns the calculated line height.

func (*TextView) HighlightMatchingBrackets

func (e *TextView) HighlightMatchingBrackets(gtx layout.Context, material op.CallOp)

func (*TextView) IndentLines

func (e *TextView) IndentLines(dedent bool) int

IndentLines indent or dedent each of the selected non-empty lines with one tab(soft tab or hard tab). If there is no selection, the current line is indented or dedented.

func (*TextView) IndentOnBreak

func (e *TextView) IndentOnBreak(s string) int

IndentOnBreak insert a line break at the the current caret position, and if there is any indentation of the previous line, it indent the new inserted line with the same size. Furthermore, if the newline if between a pair of brackets, it also insert indented lines between them.

This is mainly used as the line break handler when Enter or Return is pressed.

func (*TextView) Indentation

func (e *TextView) Indentation() string

Indentation returns the text sequence used to indent the lines(paragraphs).

func (*TextView) IsWordSeperator

func (e *TextView) IsWordSeperator(r rune) bool

IsWordSeperator check r to see if it is a word seperator. A word seperator set the boundary when navigating by words, or deleting by words. TODO: does it make sence to use unicode space definition here?

func (*TextView) Layout

func (e *TextView) Layout(gtx layout.Context, lt *text.Shaper)

Layout the text, reshaping it as necessary.

func (*TextView) Len

func (e *TextView) Len() int

Len is the length of the editor contents, in runes.

func (*TextView) MoveCaret

func (e *TextView) MoveCaret(startDelta, endDelta int)

MoveCaret moves the caret (aka selection start) and the selection end relative to their current positions. Positive distances moves forward, negative distances moves backward. Distances are in grapheme clusters which better match the expectations of users than runes.

func (*TextView) MoveCoord

func (e *TextView) MoveCoord(pos image.Point)

MoveCoord moves the caret to the position closest to the provided point that is aligned to a grapheme cluster boundary.

func (*TextView) MoveLineEnd

func (e *TextView) MoveLineEnd(selAct SelectionAction)

MoveLineEnd moves the caret to the end of the current line, ensuring that the resulting cursor position is on a grapheme cluster boundary.

func (*TextView) MoveLineStart

func (e *TextView) MoveLineStart(selAct SelectionAction)

MoveLineStart moves the caret to the start of the current line, ensuring that the resulting cursor position is on a grapheme cluster boundary.

func (*TextView) MoveLines

func (e *TextView) MoveLines(distance int, selAct SelectionAction)

MaxLines moves the cursor the specified number of lines vertically, ensuring that the resulting position is aligned to a grapheme cluster.

func (*TextView) MovePages

func (e *TextView) MovePages(pages int, selAct SelectionAction)

MovePages moves the caret position by vertical pages of text, ensuring that the final position is aligned to a grapheme cluster boundary.

func (*TextView) MoveTextEnd

func (e *TextView) MoveTextEnd(selAct SelectionAction)

MoveTextEnd moves the caret to the end of the text.

func (*TextView) MoveTextStart

func (e *TextView) MoveTextStart(selAct SelectionAction)

MoveTextStart moves the caret to the start of the text.

func (*TextView) MoveWords

func (e *TextView) MoveWords(distance int, selAct SelectionAction)

MoveWord moves the caret to the next few words in the specified direction. Positive is forward, negative is backward. The final caret position will be aligned to a grapheme cluster boundary.

func (*TextView) NearestMatchingBrackets

func (e *TextView) NearestMatchingBrackets() (left int, right int)

NearestMatchingBrackets finds the nearest matching brackets of the caret.

func (*TextView) PaintCaret

func (e *TextView) PaintCaret(gtx layout.Context, material op.CallOp)

PaintCaret clips and paints the caret rectangle, adding material immediately before painting to set the appropriate paint material.

func (*TextView) PaintOverlay

func (e *TextView) PaintOverlay(gtx layout.Context, offset image.Point, overlay layout.Widget)

func (*TextView) PaintSelection

func (e *TextView) PaintSelection(gtx layout.Context, material op.CallOp)

PaintSelection clips and paints the visible text selection rectangles using the provided material to fill the rectangles.

func (*TextView) PaintText

func (e *TextView) PaintText(gtx layout.Context, material op.CallOp)

PaintText clips and paints the visible text glyph outlines using the provided material to fill the glyphs.

func (*TextView) Paragraphs added in v0.4.1

func (e *TextView) Paragraphs() int

Paragraphs returns the total number of rendered paragraphs(or logical lines).

func (*TextView) Params added in v0.5.0

func (e *TextView) Params() text.Parameters

Params returns the current text parameters.

func (*TextView) PartialLineSelected

func (e *TextView) PartialLineSelected() bool

partialLineSelected checks if the current selection is a partial single line.

func (*TextView) QueryPos

func (e *TextView) QueryPos(pos image.Point) (line, col int, runeOff int)

QueryPos querys the line/column and rune offset of the passed position. If pos is outside of the line boundary, it returns zero line and col, and a negative runeOff.

func (*TextView) ReadRuneAt

func (e *TextView) ReadRuneAt(runeOff int) (rune, error)

ReadRuneAt reads a rune at the rune offset runeOff. It returns an error while reading from the underlying buffer.

func (*TextView) ReadUntil

func (e *TextView) ReadUntil(direction int, seperator func(r rune) bool) string

ReadUntil reads in the specified direction from the current caret position until the seperator returns false. It returns the read text.

func (*TextView) ReadWord

func (e *TextView) ReadWord(bySpace bool) (string, int)

ReadWord tries to read one word nearby the caret, returning the word if there's one, and the offset of the caret in the word.

The word boundary is checked using the word boundary characters or just spaces.

func (*TextView) Redo

func (e *TextView) Redo() ([]buffer.CursorPos, bool)

Redo revert the last undo operation(s) and mark the textview invalid.

func (*TextView) Regions

func (e *TextView) Regions(start, end int, regions []Region) []Region

Regions returns visible regions covering the rune range [start,end).

func (*TextView) Replace

func (e *TextView) Replace(start, end int, s string) int

Replace the text between start and end with s. Indices are in runes. It returns the number of runes inserted.

func (*TextView) RuneCoords added in v0.4.3

func (e *TextView) RuneCoords(runeIdx int) f32.Point

RuneCoords returns the coordinates of the rune at runeIdx, relative to the editor itself.

func (*TextView) ScrollBounds

func (e *TextView) ScrollBounds() image.Rectangle

func (*TextView) ScrollOff

func (e *TextView) ScrollOff() image.Point

ScrollOff returns the scroll offset of the text viewport.

func (*TextView) ScrollRel

func (e *TextView) ScrollRel(dx, dy int)

func (*TextView) ScrollToCaret

func (e *TextView) ScrollToCaret()

func (*TextView) SelectedLineRange

func (e *TextView) SelectedLineRange() (start, end int)

SelectedLineRange returns the start and end rune index of the paragraphs selected by the caret. If there is no selection, the range of current paragraph the caret is in is returned.

func (*TextView) SelectedLineText

func (e *TextView) SelectedLineText(buf []byte) ([]byte, int, int)

SelectedLine returns the text of the selected lines and the rune range. An empty selection is treated as a single line selection.

func (*TextView) SelectedText

func (e *TextView) SelectedText(buf []byte) []byte

SelectedText returns the currently selected text (if any) from the editor, filling the provided byte slice if it is large enough or allocating and returning a new byte slice if the provided one is insufficient. Callers can guarantee that the buf is large enough by providing a buffer with capacity e.SelectionLen()*utf8.UTFMax.

func (*TextView) Selection

func (e *TextView) Selection() (start, end int)

Selection returns the start and end of the selection, as rune offsets. start can be > end.

func (*TextView) SelectionLen

func (e *TextView) SelectionLen() int

SelectionLen returns the length of the selection, in runes; it is equivalent to utf8.RuneCountInString(e.SelectedText()).

func (*TextView) SetCaret

func (e *TextView) SetCaret(start, end int)

SetCaret moves the caret to start, and sets the selection end to end. Then the two ends are clamped to the nearest grapheme cluster boundary. start and end are in runes, and represent offsets into the editor text.

func (*TextView) SetColorScheme

func (e *TextView) SetColorScheme(scheme *syntax.ColorScheme)

func (*TextView) SetSyntaxTokens

func (e *TextView) SetSyntaxTokens(tokens ...syntax.Token)

func (*TextView) SetText

func (e *TextView) SetText(s string) int

Set the text of the buffer. It returns the number of runes inserted.

func (*TextView) SetWrapLine

func (e *TextView) SetWrapLine(enabled bool)

func (*TextView) Source

func (e *TextView) Source() buffer.TextSource

func (*TextView) TextLayout added in v0.5.0

func (e *TextView) TextLayout() *lt.TextLayout

TextLayout returns the internal text layout for accessing paragraph data. This is used by the gutter system to render line numbers and other gutter content.

func (*TextView) Undo

func (e *TextView) Undo() ([]buffer.CursorPos, bool)

Undo revert the last operation(s) and mark the textview invalid.

func (*TextView) UpdateSyntaxTokensOffset added in v0.5.0

func (e *TextView) UpdateSyntaxTokensOffset(start, end, newEnd int)

UpdateSyntaxTokensOffset adjusts existing syntax token offsets after a text edit. Parameters mirror Editor.replace: start and end are the old replaced range (runes), newEnd is start + (number of runes inserted).

This method is necessary when code highlighting occurs in an async way, during the short time window we need to keep the highlighting visually stable. When the async full highlighting completes, it replaces the shifted tokens with fully correct ones.

func (*TextView) Viewport added in v0.5.0

func (e *TextView) Viewport() image.Rectangle

Viewport returns the currently visible viewport as a rectangle in document coordinates.

func (*TextView) WordBoundariesAt added in v0.5.0

func (e *TextView) WordBoundariesAt(caret int, bySpace bool) (start, end int)

WordBoundariesAt returns the start and end rune offsets of the word at the given caret position. If caret is on a word separator, start and end will both equal caret (empty word). The bySpace parameter controls whether only spaces are considered separators (true) or custom word separators are used (false).

Jump to

Keyboard shortcuts

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