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
struct LineEditorConforms to: Swift.Sendable, Swift.SendableMetatype.
A plain-text draft whose cursor and selection use extended grapheme clusters.
Source · LineEditor.swift:5text
var text: String { get }The stored text content.
Source · LineEditor.swift:6cursor
var cursor: Int { get }Cursor offset in grapheme clusters, not UTF-8 bytes or terminal cells.
Source · LineEditor.swift:9selectionAnchor
var selectionAnchor: Int? { get }Optional anchor offset in the editor’s grapheme-cluster coordinates.
Source · LineEditor.swift:10init()
init()Creates a value with the supplied arguments. Default arguments are shown in the declaration.
Source · LineEditor.swift:23selection
var selection: Range<Int>? { get }A half-open grapheme range, or nil when nothing is selected.
Source · LineEditor.swift:26reset()
mutating func reset()Clears text, cursor, selection, undo/redo history, insertion coalescing, and remembered vertical column.
Source · LineEditor.swift:31insert(_:)
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.
moveLeft(selecting:)
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.
moveRight(selecting:)
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.
moveWordLeft(selecting:)
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.
moveWordRight(selecting:)
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.
moveHome(selecting:)
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.
moveEnd(selecting:)
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.
moveCursor(to:selecting:)
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.
moveVertical(by:width:selecting:)
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.
selectAll()
mutating func selectAll()Selects the entire draft by setting the anchor to zero and cursor to text.count.
Source · LineEditor.swift:136deleteBackward()
mutating func deleteBackward()Deletes the selected text or the preceding grapheme.
Source · LineEditor.swift:144delete(in:)
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.
replacePreviousGrapheme(with:)
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.
deleteForward()
mutating func deleteForward()Deletes the selected text or the following grapheme.
Source · LineEditor.swift:176deleteWordBackward()
mutating func deleteWordBackward()Deletes the selection, or whitespace and the previous word/punctuation run.
Source · LineEditor.swift:181deleteWordForward()
mutating func deleteWordForward()Deletes the selection, or the following word/punctuation run and trailing whitespace.
Source · LineEditor.swift:185undo()
mutating func undo()Restores the draft before the most recent edit.
Source · LineEditor.swift:190redo()
mutating func redo()Reapplies the most recently undone edit.
Source · LineEditor.swift:197handle(_:)
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.
layout(width:)
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).