Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
text_history.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3
4#pragma once
5
6#include <portals/text.hpp>
7
8#include <cstddef>
9#include <cstdint>
10#include <expected>
11#include <memory>
12#include <string>
13
14namespace portals {
15
17inline constexpr std::size_t DEFAULT_TEXT_HISTORY_OPERATIONS = 128;
19inline constexpr std::size_t DEFAULT_TEXT_HISTORY_MAX_BYTES = 1048576;
20
36
54
58 // Count construction propagates MSVC Debug iterator-proxy allocation failures.
59 std::string bytes = std::string(0, '\0');
61 std::size_t cursor{};
63 std::size_t anchor{};
64};
65
83
84class TextHistory;
85
94public:
106
110 [[nodiscard]] bool changed() const noexcept;
112 [[nodiscard]] std::size_t retainedBytes() const noexcept;
116 [[nodiscard]] std::size_t cursor() const noexcept;
120 [[nodiscard]] std::size_t anchor() const noexcept;
124 [[nodiscard]] std::uint64_t contentState() const noexcept;
128 [[nodiscard]] const GraphemeText *text() const noexcept;
134 void takeText(GraphemeText &destination) noexcept;
135
136private:
137 friend class TextHistory;
138 struct State;
139 explicit PreparedTextHistory(std::unique_ptr<State> state) noexcept;
140 std::unique_ptr<State> state_;
141};
142
157public:
161 [[nodiscard]] std::uint64_t contentState() const noexcept;
167 explicit TextHistory(TextHistoryOptions options = {}, WidthPolicy policy = {}, TextLimits limits = {});
170 TextHistory(const TextHistory &) = delete;
171 TextHistory &operator=(const TextHistory &) = delete;
172
179 [[nodiscard]] std::expected<PreparedTextHistory, TextHistoryError>
180 prepareEdit(const TextHistorySnapshot &before, const TextHistorySnapshot &after, TextHistoryKind kind) const;
184 [[nodiscard]] std::expected<PreparedTextHistory, TextHistoryError> prepareUndo() const;
188 [[nodiscard]] std::expected<PreparedTextHistory, TextHistoryError> prepareRedo() const;
193 [[nodiscard]] std::expected<PreparedTextHistory, TextHistoryError>
199 [[nodiscard]] std::expected<void, TextHistoryError> checkPrepared(const PreparedTextHistory &prepared) const noexcept;
204 [[nodiscard]] std::expected<void, TextHistoryError> commit(PreparedTextHistory &&prepared) noexcept;
206 void stopGrouping() noexcept;
210 [[nodiscard]] bool canUndo() const noexcept;
214 [[nodiscard]] bool canRedo() const noexcept;
218 [[nodiscard]] std::size_t operationCount() const noexcept;
222 [[nodiscard]] std::size_t retainedBytes() const noexcept;
223
224private:
225 struct State;
226 [[nodiscard]] std::expected<PreparedTextHistory, TextHistoryError> prepareTraversal(bool undo) const;
227 std::unique_ptr<State> state_;
228 TextHistoryOptions options_;
229 WidthPolicy policy_;
230 TextLimits limits_;
231};
232
233} // namespace portals
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
Owning, move-only staged history and already segmented restoration text.
Definition text_history.hpp:93
std::uint64_t contentState() const noexcept
const GraphemeText * text() const noexcept
Borrow the already segmented target before publication.
std::size_t retainedBytes() const noexcept
std::size_t anchor() const noexcept
Return the validated target selection anchor.
PreparedTextHistory & operator=(PreparedTextHistory &&other) noexcept
Replace this handle without allocating.
void takeText(GraphemeText &destination) noexcept
Transfer staged text into existing storage without allocating.
std::size_t cursor() const noexcept
Return the validated target cursor.
PreparedTextHistory(PreparedTextHistory &&other) noexcept
Transfer a prepared value without allocating.
bool changed() const noexcept
Report whether preparation changes recorded history.
~PreparedTextHistory()
Release abandoned staging without changing the owner.
bool canUndo() const noexcept
Query local undo availability; editor eligibility belongs to the external owner.
void stopGrouping() noexcept
End the open editing group; invalidates outstanding staging without allocation.
std::size_t retainedBytes() const noexcept
Query retained logical memory, excluding documented overhead.
~TextHistory()
Release all owning history storage.
std::expected< void, TextHistoryError > checkPrepared(const PreparedTextHistory &prepared) const noexcept
Check that a prepared value can be committed before publishing its text.
std::expected< PreparedTextHistory, TextHistoryError > prepareUndo() const
Stage one undo with restoration segmentation under captured settings.
TextHistory(TextHistoryOptions options={}, WidthPolicy policy={}, TextLimits limits={})
Create an empty history under immutable text validation settings.
std::expected< void, TextHistoryError > commit(PreparedTextHistory &&prepared) noexcept
Publish prepared history without allocation or callbacks.
std::uint64_t contentState() const noexcept
std::expected< PreparedTextHistory, TextHistoryError > prepareRedo() const
Stage one redo with restoration segmentation under captured settings.
std::expected< PreparedTextHistory, TextHistoryError > prepareResetBaseline(const TextHistorySnapshot &baseline) const
Validate a new external baseline and stage clearing every group, even for equal bytes.
bool canRedo() const noexcept
Query local redo availability.
std::expected< PreparedTextHistory, TextHistoryError > prepareEdit(const TextHistorySnapshot &before, const TextHistorySnapshot &after, TextHistoryKind kind) const
Prepare a whole edit without changing this history.
std::size_t operationCount() const noexcept
Count all retained undo and redo groups.
Public Portals API.
Definition action.hpp:8
TextHistoryError
Technical history failure; no state is changed on failure.
Definition text_history.hpp:67
@ PASTE
Paste category without clipboard or payload.
Definition diagnostics.hpp:65
@ EMPTY
Successful content has zero logical rows.
Definition data_presentation.hpp:14
@ STALE
Receipt belongs to another document or superseded save request.
Definition text_document.hpp:95
@ EXHAUSTED
A monotonic counter cannot advance.
Definition text_document.hpp:96
@ INVALID_TEXT
Projected row is not admitted UTF-8 under the scene text policy.
Definition find_bar.hpp:53
@ NO_MEMORY
Allocation failed at a runner exception boundary.
Definition application.hpp:33
TextHistoryKind
Semantic editing categories; only the first three can form groups.
Definition text_history.hpp:38
@ CUT
A separate clipboard removal.
Definition text_history.hpp:48
@ FORWARD_DELETE
Deletion immediately after the cursor.
Definition text_history.hpp:44
@ TYPING
Plain insertion without replacing a selection.
Definition text_history.hpp:40
@ REPLACEMENT
A separate selection replacement.
Definition text_history.hpp:50
@ WOULD_BLOCK
Active resume, invocation, registration or queue capacity rejects the operation.
Definition async.hpp:30
constexpr std::size_t DEFAULT_TEXT_HISTORY_OPERATIONS
Default maximum combined undo and redo groups.
Definition text_history.hpp:17
constexpr std::size_t DEFAULT_TEXT_HISTORY_MAX_BYTES
Default logical record and UTF-8 capacity budget, in bytes.
Definition text_history.hpp:19
@ PROGRAMMATIC
Explicit semantic action, the default for caller-created ActionEvent.
Definition action.hpp:25
@ NOT_BOUNDARY
A cursor offset is outside the text or inside a cluster.
Definition text.hpp:83
Configurable bounds and explicit-boundary grouping for text history.
Definition text_history.hpp:22
bool enabled
Record edits when true; disabled history accepts edits without retaining them.
Definition text_history.hpp:24
std::size_t maxBytes
Maximum retained entry objects and UTF-8 string capacities, in bytes; zero disables retention.
Definition text_history.hpp:28
bool groupDeletion
Combine adjacent deletions of the same direction until a boundary.
Definition text_history.hpp:32
std::size_t maxOperations
Maximum combined undo and redo groups; zero disables retention.
Definition text_history.hpp:26
bool trackContentStates
Retain opaque content-state tokens for model anchors; default history has no per-entry token storage.
Definition text_history.hpp:34
bool groupTyping
Combine adjacent typing edits until an explicit boundary.
Definition text_history.hpp:30
Owning UTF-8 bytes and byte offsets validated during preparation.
Definition text_history.hpp:56
std::size_t cursor
Active end of selection, at an extended grapheme boundary.
Definition text_history.hpp:61
std::size_t anchor
Fixed end of selection, at an extended grapheme boundary.
Definition text_history.hpp:63
std::string bytes
Original UTF-8 text; no normalization is performed.
Definition text_history.hpp:59
Owning limits checked without silently truncating input or clusters.
Definition text.hpp:57
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42