inputcontent

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

README

inputcontent

原子引用输入的纯内容层,由 el.InputDocument 使用。保存 UTF-8 提交文本、引用 ID/显示名称/范围,以及选区和撤销事务;不依赖 Gio 或其他 Keel 包。

Content 是独立草稿,引用范围必须匹配原文并落在字素边界。Presentation 映射提交文本与显示名称的字节坐标。Session 按明确编辑区间处理替换,避免文本 diff 漏掉“相同文字替换但移除引用”的操作;输入法组合事务只记一次撤销。

UI 层负责焦点、只读/禁用、平台事件、复制粘贴、命中与绘制。这里的组合输入测试只证明事务行为,不证明原生候选窗口或组合下划线显示正确。

LayoutPresentation 可为引用指定编辑器内部的显示替代串,例如用于占位字形的单个私用字符。它不修改提交文本和引用元数据;用 SourceRange 将显示编辑范围映射到原文,再调用 Session.ReplaceSource,可沿用原有组合输入和撤销事务。复制仍从 Session.SelectedText 取原文,不能复制布局占位串。已接入 Input/Textarea 的对象布局事件通路;平台 IME 使用可读标签投影,内部编辑器使用对象投影,两者分别映射到原文。

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