Text buffer abstractions to power your text editor, in UI or in memory.
- In-memory text mutations with an API similar to text views.
- Consistent behavior across platforms.
We operate on the abstraction of a Buffer to perform changes.
This enables usage of the declarative API on multiple buffers at once without having to put the text into a UI component to render.
A NSTextView is a buffer. You can use this declarative API to make changes to text views on screen.
You can also use purely in-memory buffers for text mutations of things you don't want to render. This allows you to read multiple files into buffers in your app and use the declarative API to change their contents, while only rendering a single selected file in a text view.
This is harnessed by DeclarativeTextKit.
Add TextBuffer as a Swift Package Manager dependency:
dependencies: [
.package(url: "https://github.com/CleanCocoa/TextBuffer", from: "0.4.0")
]Buffer— protocol for reading and mutating text with UTF-16 indexed ranges.MutableStringBuffer— lightweight in-memoryBufferbacked byNSMutableString, for off-screen mutations and tests.RopeBuffer— in-memoryBufferbacked byTextRope(B-tree rope), for large documents with O(log n) edits.NSTextViewBuffer—Bufferconformance forNSTextView, applying changes directly to the text view.Undoable— wraps aBufferwith FoundationUndoManagerintegration for AppKit undo/redo.TransferableUndoable— wraps aBufferwith anOperationLogfor portable undo history; supportssnapshot()andrepresent(_:)for state transfer.OperationLog— value-type undo history that recordsBufferOperations grouped intoUndoGroups. Inspectable vialog.history.
TextBuffer provides two undo wrappers:
-
Undoableuses Foundation'sUndoManager. Use this when you need AppKit undo/redo integration (e.g., responding to Edit menu actions in anNSTextView-based editor). -
TransferableUndoableuses anOperationLogvalue type instead. Use this when you need to transfer undo state between buffers — for example, swapping an in-memory buffer's state into a text view. Callsnapshot()to capture the current content and undo history, andrepresent(_:)to restore it into anotherTransferableUndoable.
Copyright © 2025 Christian Tietze. All rights reserved. Distributed under the MIT License.