DocumentState

@Stable
class DocumentState : RememberObserver

A hoistable state holder for a text component that owns the Document the component renders. The state and the bound component share one document, so an edit made through this state is what the component displays, and text the user types into the component is what this state reports - there is no value to keep in sync and no round-trip per keystroke. A caller who needs a document of their own supplies it at rememberDocumentState; content is otherwise read through text and changed through edit.

text and selection are snapshot-observable: reading them inside a composable (or a snapshotFlow collector) subscribes to later edits, so the reader recomposes when the document or the caret changes. text is materialized on demand from the document, so typing does not pay for a full read until a caller actually asks for the whole text.

See also

Properties

Link copied to clipboard

Whether a redo is currently available; snapshot-observable.

Link copied to clipboard

Whether an undo is currently available; snapshot-observable.

Link copied to clipboard

The current selection, a directional TextRange over the caret. Reading registers a snapshot subscription to caret changes; assigning moves the caret and selection of the bound component (or, while unmounted, stores the value to apply when a component binds).

Link copied to clipboard
var text: @Nls CharSequence

The current text of the document. Reading registers a snapshot subscription to later edits; assigning diffs the new value against the current content and applies only the changed span through the document, leaving the surrounding text untouched.

Functions

Link copied to clipboard
fun edit(block: DocumentEditScope.() -> Unit)

Applies a batch of edits to the document as one compound change. Insertions, replacements and deletions made on the DocumentEditScope are committed together, then the caret is placed as the block requested (its default rests after the last insertion).

Link copied to clipboard
open override fun onAbandoned()
Link copied to clipboard
open override fun onForgotten()
Link copied to clipboard
open override fun onRemembered()
Link copied to clipboard
fun redo()

Reapplies the most recently undone edit, if any.

Link copied to clipboard
fun undo()

Reverts the most recent edit, if any.