NewEditor#
func NewEditor(initial string) *EditorNewEditor creates an editor with its caret at the end of initial text.
type Editor struct {
// accept and maxRunes constrain what an edit may leave in the text; see
// Restrict.
// lastReplaceStart is where the latest replace began; see replace.
// text caches String's conversion so a paint that reads the value every
// frame does not allocate; every mutation of runes clears textValid.
// visualStarts, visualOrder and visualOdd are the bidi caret walk's scratch,
// reused by every arrow key on a line that needs reordering.
}Editor retains text, selection, and an optional IME composition range. Like its mutators, String is not safe for concurrent use: it caches the converted text.
NewEditor#func NewEditor(initial string) *EditorNewEditor creates an editor with its caret at the end of initial text.
BreakUndoGroup#func (editor *Editor) BreakUndoGroup()BreakUndoGroup keeps the next edit from merging with adjacent typing or deletion. Movement, selection changes, paste and line breaks call it automatically; applications may call it at another semantic boundary.
CanRedo#func (editor *Editor) CanRedo() boolCanRedo reports whether Redo would reapply an undone edit.
CanUndo#func (editor *Editor) CanUndo() boolCanUndo reports whether Undo would restore an earlier edit.
Caret#func (editor *Editor) Caret() intCaret returns the rune index of the caret: the end of the selection that moves when it is extended, which is the Start of a selection made backwards.
ClearHistory#func (editor *Editor) ClearHistory()ClearHistory forgets every undo and redo unit without changing the text.
Composition#func (editor *Editor) Composition() (Range, bool)Composition returns the active marked-text range.
DeleteBackward#func (editor *Editor) DeleteBackward() boolDeleteBackward removes the selection or previous grapheme cluster.
DeleteForward#func (editor *Editor) DeleteForward() boolDeleteForward removes the selection or next grapheme cluster.
DeleteWord#func (editor *Editor) DeleteWord(forward bool)DeleteWord removes a selection or a word as one undo unit. Undo restores the original caret, rather than an artificial selection used to delete the word.
Insert#func (editor *Editor) Insert(value string)Insert replaces the current selection or composition and places the caret after inserted text.
MoveLeft#func (editor *Editor) MoveLeft(extend bool) boolMoveLeft moves or extends selection by one grapheme cluster to the left on the screen: the previous one, except in a line with right-to-left text, where the order is the one UAX #9 displays it in.
MoveLineBoundary#func (editor *Editor) MoveLineBoundary(end, extend bool)MoveLineBoundary implements Home/End for an unwrapped logical line.
MoveRight#func (editor *Editor) MoveRight(extend bool) boolMoveRight moves or extends selection by one grapheme cluster to the right on the screen (see MoveLeft).
MoveTo#func (editor *Editor) MoveTo(index int, extend bool)MoveTo puts the caret at rune index, or with extend moves it there from where it was and keeps the other end of the selection where it is. It is how an editor applies a movement the text itself does not know, such as one line up in text a view has wrapped. It ends active composition.
MoveWord#func (editor *Editor) MoveWord(forward, extend bool)MoveWord moves to the next/previous word boundary, optionally extending the selection.
Paste#func (editor *Editor) Paste(value string)Paste inserts text as one undo unit, separate from adjacent typing.
Redo#func (editor *Editor) Redo() boolRedo reapplies the next undone edit and restores its resulting selection.
Restrict#func (editor *Editor) Restrict(accept func(rune) bool, maxRunes int)Restrict limits what edits may put into the editor: accept reports whether a rune may be typed (nil accepts every rune) and maxRunes caps the length of the text (zero or less means no cap). It is how a one-time code takes only digits, six of them, or a port field only digits.
The limits apply to edits made after the call, whether typed, pasted or composed by an IME: unacceptable runes are dropped from the inserted text, and what does not fit is cut off the end, so a paste of "12ab3456789" into an empty six-digit editor leaves "123456". Text already present is left alone, and deleting is never restricted.
RuneCount#func (editor *Editor) RuneCount() intRuneCount returns the number of Unicode code points.
RuneIndexToUTF16#func (editor *Editor) RuneIndexToUTF16(index int) intRuneIndexToUTF16 converts a rune index to a native UTF-16 code-unit index.
Selection#func (editor *Editor) Selection() RangeSelection returns the normalized selection range.
SetMarkedText#func (editor *Editor) SetMarkedText(value string, selected Range)SetMarkedText replaces the current composition or selection with marked text. selected is relative to the marked text, matching native IME APIs.
SetOverwrite#func (editor *Editor) SetOverwrite(overwrite bool)SetOverwrite switches the editor between inserting and overwriting. When overwriting, text typed at a caret replaces the runes after it instead of pushing them along, and at the length cap set by Restrict it replaces the last rune instead of being refused: the behavior of a fixed-width field such as a one-time code, where the caret sits on a slot and the next digit fills it. A selection is replaced as always, and runes the restriction rejects delete nothing.
SetSelection#func (editor *Editor) SetSelection(selection Range)SetSelection updates the selection and ends active composition. A range whose Start is after its End is a selection made backwards: the caret is at the Start, as Caret reports, and Selection still returns it in order.
String#func (editor *Editor) String() stringString returns the current UTF-8 text. The conversion is cached until the text next changes, so repeated calls between edits allocate nothing.
UTF16ToRuneIndex#func (editor *Editor) UTF16ToRuneIndex(units int) intUTF16ToRuneIndex converts a native UTF-16 code-unit index to the nearest preceding rune boundary.
Undo#func (editor *Editor) Undo() boolUndo restores the text and selection before the latest undo unit.
Unmark#func (editor *Editor) Unmark()Unmark commits the current marked text without changing it.