inputcontent

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 7 Imported by: 0

README

ui/internal/inputcontent

English | 简体中文

Pure content layer for atomic reference inputs, used by el.InputDocument. Saves UTF-8 commit text, reference IDs/display names/ranges, and selections and undo transactions; no dependencies on Gio or other Keel packages.

Content is an independent draft, and the reference range must match the original text and fall on grapheme boundaries. Presentation maps the presentation text to the byte coordinates of the display name. Session processes substitutions according to clear editing intervals to prevent text diff from missing the operation of "replacing the same text but removing the reference"; only one undo of the input method combination transaction is recorded.

The UI layer is responsible for focus, read-only/disabled, platform events, copy-paste, hit and draw. The combined input test here only proves the transaction behavior, not the native candidate window or the correct display of the combined underline.

LayoutPresentation specifies an editor-internal display substitution string for a reference, such as a single private character for a placeholder glyph. It does not modify the commit text and reference metadata; use SourceRange to map the display editing range to the original text, and then call Session.ReplaceSource to continue inputting and undoing transactions using the original combination. The copy still takes the original text from Session.SelectedText, and the layout placeholder string cannot be copied. The object layout event path of Input/Textarea has been connected; the platform IME uses readable label projection, and the internal editor uses object projection, both of which are mapped to the original text.

Documentation

Overview

Package inputcontent keeps editable text and atomic references together. Offsets are UTF-8 bytes; all token boundaries must also be grapheme boundaries.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrToken    = errors.New("inputcontent: invalid token")
	ErrBoundary = errors.New("inputcontent: invalid grapheme boundary")
	ErrOverlap  = errors.New("inputcontent: overlapping tokens")
	ErrText     = errors.New("inputcontent: text mismatch or invalid UTF-8")
)
View Source
var ErrCompositionActive = errors.New("inputcontent: composition is active")

Functions

func Equal

func Equal(a, b Content) bool

Equal compares both text and reference metadata.

Types

type Content

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

Content is immutable. Accessors return copies, including when no edit occurs.

func New

func New(text string, spans ...Span) (Content, error)

func (Content) Presentation

func (c Content) Presentation() Presentation

func (Content) Replace

func (c Content) Replace(r Range, text string, token *Token) (Content, Range, error)

Replace makes one atomic edit. Partial token selections expand to whole tokens; insertion inside a token is rejected. A plain replacement removes references in the replaced interval. References after it shift by the byte delta.

func (Content) Selection

func (c Content) Selection(start, end int) (int, int)

Selection expands intersected tokens, preserving anchor/focus direction.

func (Content) Snap

func (c Content) Snap(at, bias int) int

Snap keeps carets outside token interiors. Negative/positive bias chooses the beginning/end; zero chooses the nearest side, with ties going to the end.

func (Content) Text

func (c Content) Text() string

func (Content) Tokens

func (c Content) Tokens() []Span

func (Content) WithToken

func (c Content) WithToken(s Span) (Content, error)

WithToken annotates existing text without editing it. Plain text that merely matches a token's Text never becomes a reference implicitly.

type DisplaySpan

type DisplaySpan struct {
	Source, Display Range
	Token           Token
}

type History

type History struct {
	Current Snapshot
	// contains filtered or unexported fields
}

func (*History) Commit

func (h *History) Commit(value Snapshot)

func (*History) Redo

func (h *History) Redo() bool

func (*History) Set

func (h *History) Set(value Snapshot)

func (*History) Undo

func (h *History) Undo() bool

type Presentation

type Presentation struct {
	Text  string
	Spans []DisplaySpan
}

Presentation replaces reference text with display labels and provides the corresponding source/display byte ranges for hit testing and clipboard maps.

func LayoutPresentation

func LayoutPresentation(c Content, replacements []string) (Presentation, error)

LayoutPresentation supplies editor-only replacements for atomic references, for example a single private-use rune shaped as an inline object. It never changes the stored text or token metadata. Map edits with SourceRange before passing them to Session.ReplaceSource; clipboard text stays source-based.

func (Presentation) DisplayOffset

func (p Presentation) DisplayOffset(source, bias int) int

DisplayOffset maps a source caret to the visible label, snapping token interiors using the same bias as SourceOffset.

func (Presentation) SourceOffset

func (p Presentation) SourceOffset(display, bias int) int

func (Presentation) SourceRange

func (p Presentation) SourceRange(r Range) (Range, error)

SourceRange expands a nonempty display selection to whole references while preserving direction. A caret inside a display label snaps to its nearest edge.

type Range

type Range struct{ Start, End int }

func ByteRange

func ByteRange(text string, r Range) (Range, error)

func RuneRange

func RuneRange(text string, r Range) (Range, error)

RuneRange and ByteRange bridge platform/editor rune coordinates and the content API's byte coordinates without silently accepting out-of-range values.

type Session

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

Session owns editing transactions. UI adapters must pass explicit edit ranges: a text diff loses reference removal when an edit leaves the characters intact. All ranges are UTF-8 bytes. The platform's rune offsets must be converted before calling these methods. UI focus and read-only policies belong to the adapter.

func (*Session) BeginComposition

func (s *Session) BeginComposition()

BeginComposition groups all subsequent plain edits into one undo transaction. Repeated begin notifications from a platform do not reset the saved snapshot.

func (*Session) CancelComposition

func (s *Session) CancelComposition()

CancelComposition restores both the reference metadata and original selection.

func (*Session) Composing

func (s *Session) Composing() bool

func (*Session) EndComposition

func (s *Session) EndComposition()

func (*Session) Redo

func (s *Session) Redo() bool

func (*Session) ReplaceDisplay

func (s *Session) ReplaceDisplay(r Range, text string) error

ReplaceDisplay handles typing, plain paste and deletion. Selecting a label copies/removes the whole reference, not its display name. No text matching is used to infer references. Replacing a label with itself still removes its ID.

func (*Session) ReplaceSource

func (s *Session) ReplaceSource(r Range, text string) error

ReplaceSource handles an edit already mapped out of an editor-specific presentation. It keeps the same composition and undo transaction semantics.

func (*Session) ReplaceToken

func (s *Session) ReplaceToken(token Token) error

ReplaceToken replaces the current source selection with an atomic reference. Applications must not insert references in a platform's active composition.

func (*Session) SelectDisplay

func (s *Session) SelectDisplay(r Range) error

func (*Session) SelectSource

func (s *Session) SelectSource(r Range) error

func (*Session) SelectedText

func (s *Session) SelectedText() string

func (*Session) Set

func (s *Session) Set(content Content)

Set restores a draft silently and discards undo and active composition state.

func (*Session) SetText

func (s *Session) SetText(text string) error

SetText always clears references and history, even when text is unchanged.

func (*Session) Snapshot

func (s *Session) Snapshot() Snapshot

func (*Session) Undo

func (s *Session) Undo() bool

type Snapshot

type Snapshot struct {
	Content   Content
	Selection Range
}

Snapshot stores references alongside text and selection, including edits that change a reference ID without changing any characters.

type Span

type Span struct {
	Range Range
	Token Token
}

type Token

type Token struct{ ID, Text, Label string }

func (Token) Display

func (t Token) Display() string

func (Token) Validate

func (t Token) Validate() error

Jump to

Keyboard shortcuts

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