loom & weft
Made for Swift.

Unreleased APIs from PR #2. Not in 0.2.1. Stable 0.2.1 reference.

LOOM Unreleased (PR #2) / TEXT, MARKDOWN, AND EDITING

LineEditor

Cursor and selection offsets count extended grapheme clusters, not bytes or cells. The draft is capped at 65,536 UTF-8 bytes after replacing a selection; insertions that exceed the limit are ignored. Controls are removed except newline/tab. Consecutive insertions coalesce into undo history; movement and other edits break coalescing. Undo history retains at most 100 saved states. handle does not insert .character keys or process paste: route those to insert yourself.

Standard inherited Swift protocol operations such as synthesized equality and OptionSet set operations are not repeated. The public declarations below are extracted from the exact source revision listed in the coverage manifest.

Example

var editor = LineEditor()
editor.insert("Hello, 世界")
editor.moveLeft(selecting: true)
editor.deleteBackward()
editor.undo()
_ = editor.layout(width: 40)
Members on this page

Feature guides and release status

LineEditor

Structure
struct LineEditor

Conforms to: Swift.Sendable, Swift.SendableMetatype.

A plain-text draft whose cursor and selection use extended grapheme clusters.

Source · LineEditor.swift:5

text

Instance Property
var text: String { get }

The stored text content.

Source · LineEditor.swift:6

cursor

Instance Property
var cursor: Int { get }

Cursor offset in grapheme clusters, not UTF-8 bytes or terminal cells.

Source · LineEditor.swift:9

selectionAnchor

Instance Property
var selectionAnchor: Int? { get }

Optional anchor offset in the editor’s grapheme-cluster coordinates.

Source · LineEditor.swift:10

init()

Initializer
init()

Creates a value with the supplied arguments. Default arguments are shown in the declaration.

Source · LineEditor.swift:23

selection

Instance Property
var selection: Range<Int>? { get }

A half-open grapheme range, or nil when nothing is selected.

Source · LineEditor.swift:26

reset()

Instance Method
mutating func reset()

Clears text, cursor, selection, undo/redo history, insertion coalescing, and remembered vertical column.

Source · LineEditor.swift:31

insert(_:)

Instance Method
mutating func insert(_ value: String)

Inserts sanitized text, replacing the current selection if present.

Parameters (1)
value
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · LineEditor.swift:42

moveLeft(selecting:)

Instance Method
mutating func moveLeft(selecting: Bool = false)

Moves by one grapheme, optionally extending the selection.

Parameters (1)
selecting
Extend the current selection when true; otherwise clear it.
Source · LineEditor.swift:62

moveRight(selecting:)

Instance Method
mutating func moveRight(selecting: Bool = false)

Moves by one grapheme, optionally extending the selection.

Parameters (1)
selecting
Extend the current selection when true; otherwise clear it.
Source · LineEditor.swift:71

moveWordLeft(selecting:)

Instance Method
mutating func moveWordLeft(selecting: Bool = false)

Moves across whitespace and one word or punctuation run.

Parameters (1)
selecting
Extend the current selection when true; otherwise clear it.
Source · LineEditor.swift:80

moveWordRight(selecting:)

Instance Method
mutating func moveWordRight(selecting: Bool = false)

Moves across one word or punctuation run and trailing whitespace.

Parameters (1)
selecting
Extend the current selection when true; otherwise clear it.
Source · LineEditor.swift:85

moveHome(selecting:)

Instance Method
mutating func moveHome(selecting: Bool = false)

Moves to the beginning of the entire draft, not just the current displayed row. selecting extends the selection instead of clearing it.

Parameters (1)
selecting
Extend the current selection when true; otherwise clear it.
Source · LineEditor.swift:89

moveEnd(selecting:)

Instance Method
mutating func moveEnd(selecting: Bool = false)

Moves to the end of the entire draft. selecting extends the selection instead of clearing it.

Parameters (1)
selecting
Extend the current selection when true; otherwise clear it.
Source · LineEditor.swift:93

moveCursor(to:selecting:)

Instance Method
mutating func moveCursor(to offset: Int, selecting: Bool = false)

Moves to a grapheme offset, optionally extending the current selection.

Parameters (2)
to
Argument type and any default are shown in the declaration; see the behavior notes above.
selecting
Extend the current selection when true; otherwise clear it.
Source · LineEditor.swift:98

moveVertical(by:width:selecting:)

Instance Method
mutating func moveVertical(by rows: Int, width: Int, selecting: Bool = false)

Moves between displayed rows, retaining the desired terminal column across short rows.

Parameters (3)
by
Argument type and any default are shown in the declaration; see the behavior notes above.
width
Available columns in terminal cells.
selecting
Extend the current selection when true; otherwise clear it.
Source · LineEditor.swift:103

selectAll()

Instance Method
mutating func selectAll()

Selects the entire draft by setting the anchor to zero and cursor to text.count.

Source · LineEditor.swift:136

deleteBackward()

Instance Method
mutating func deleteBackward()

Deletes the selected text or the preceding grapheme.

Source · LineEditor.swift:144

delete(in:)

Instance Method
mutating func delete(in range: Range<Int>)

Deletes a grapheme-offset range as one edit, preserving the original cursor and selection for undo.

Empty and out-of-bounds ranges do not modify the draft or its undo history.

Parameters (1)
in
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · LineEditor.swift:152

replacePreviousGrapheme(with:)

Instance Method
mutating func replacePreviousGrapheme(with value: String)

Replaces the preceding grapheme as one undoable edit.

Parameters (1)
with
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · LineEditor.swift:158

deleteForward()

Instance Method
mutating func deleteForward()

Deletes the selected text or the following grapheme.

Source · LineEditor.swift:176

deleteWordBackward()

Instance Method
mutating func deleteWordBackward()

Deletes the selection, or whitespace and the previous word/punctuation run.

Source · LineEditor.swift:181

deleteWordForward()

Instance Method
mutating func deleteWordForward()

Deletes the selection, or the following word/punctuation run and trailing whitespace.

Source · LineEditor.swift:185

undo()

Instance Method
mutating func undo()

Restores the draft before the most recent edit.

Source · LineEditor.swift:190

redo()

Instance Method
mutating func redo()

Reapplies the most recently undone edit.

Source · LineEditor.swift:197

handle(_:)

Instance Method
mutating func handle(_ key: Key)

Handles Left, Right, Home, End, Backspace, Delete, and newline. All other keys are ignored. Route character text/paste to insert, and app-defined shortcuts to movement, undo, or submission.

Parameters (1)
key
Logical key to handle.
Source · LineEditor.swift:203

layout(width:)

Instance Method
func layout(width: Int) -> (TextLayout, Point)

Returns a wrapped TextLayout with an extra trailing space for cursor placement, plus the cursor’s local terminal-cell Point.

Parameters (1)
width
Available columns in terminal cells.

Returns (TextLayout, Point).

Source · LineEditor.swift:216