Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
text_document.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/model_position.hpp>
6#include <portals/text_history.hpp>
7
8#include <string_view>
9
10namespace portals {
15struct TextSplice {
16 std::uint64_t offset{};
17 std::uint64_t erased{};
18 std::uint64_t inserted{};
19};
20
23 std::size_t maxChanges{128};
24};
25
31
40class TextChangeMap final {
41public:
49 static std::expected<std::unique_ptr<TextChangeMap>, SourceError> create(ModelResource resource, std::uint64_t extent,
50 TextAnchorOptions options = {},
51 std::uint64_t initialRevision = 1,
52 std::uint64_t initialContentState = 1);
58 std::expected<void, SourceError> edit(TextSplice change, std::uint64_t contentState) noexcept;
63 std::expected<void, SourceError> traverse(std::uint64_t contentState, std::uint64_t extent) noexcept;
68 std::expected<ModelBookmark, SourceError> bookmark(std::uint64_t offset,
75 std::expected<TextMappedPosition, SourceError> map(const ModelBookmark &bookmark, std::stop_token stop = {}) const;
77 std::uint64_t revision() const noexcept;
79 std::uint64_t extent() const noexcept;
80
81private:
82 struct State;
83 explicit TextChangeMap(std::unique_ptr<State> state) noexcept;
84 std::unique_ptr<State> state_;
85};
86
99
102 std::size_t cursor{};
103 std::size_t anchor{};
105 bool operator==(const TextSelection &other) const noexcept = default;
106};
107
115
121public:
122 TextSaveTicket() = default;
124 std::string_view target() const noexcept;
126 std::uint64_t revision() const noexcept;
127
128private:
129 friend class FiniteTextDocument;
130 struct State;
131 std::shared_ptr<const State> state_;
132};
133
141class FiniteTextDocument final : public PositionResolver {
142public:
147 static std::expected<std::unique_ptr<FiniteTextDocument>, TextDocumentError>
148 create(std::string_view text, FiniteTextDocumentOptions options = {});
151 const GraphemeText &text() const noexcept;
153 ModelResource resource() const noexcept;
155 SourceStamp session() const noexcept;
157 std::uint64_t revision() const noexcept;
159 TextSelection selection() const noexcept;
170 std::expected<void, TextDocumentError> replace(std::size_t begin, std::size_t end, std::string_view replacement,
171 TextHistoryKind kind = TextHistoryKind::PROGRAMMATIC);
173 std::expected<void, TextDocumentError> undo();
175 std::expected<void, TextDocumentError> redo();
177 bool canUndo() const noexcept;
179 bool canRedo() const noexcept;
184 std::expected<ModelBookmark, SourceError> bookmark(std::size_t byteOffset,
185 PositionAffinity affinity = PositionAffinity::FORWARD) const;
191 std::expected<PositionReply, SourceError> resolve(const PositionRequest &request,
192 std::stop_token stop = {}) const override;
196 std::expected<TextSaveTicket, TextDocumentError> beginSave(std::string_view target);
201 std::expected<void, TextDocumentError> acknowledgeSave(const TextSaveTicket &ticket) noexcept;
203 bool dirty() const noexcept;
205 std::string_view saveTarget() const noexcept;
209 bool conflicted() const noexcept;
214 std::expected<void, TextDocumentError> acceptExternalReplacement(std::string_view text);
215
216private:
217 struct State;
218 explicit FiniteTextDocument(std::unique_ptr<State> state) noexcept;
219 std::expected<void, TextDocumentError> traverse(bool undo);
220 std::unique_ptr<State> state_;
221};
222} // namespace portals
std::expected< void, TextDocumentError > redo()
std::expected< void, TextDocumentError > replace(std::size_t begin, std::size_t end, std::string_view replacement, TextHistoryKind kind=TextHistoryKind::PROGRAMMATIC)
bool canUndo() const noexcept
std::uint64_t revision() const noexcept
TextSelection selection() const noexcept
const GraphemeText & text() const noexcept
std::expected< void, TextDocumentError > setSelection(TextSelection selection) noexcept
std::expected< void, TextDocumentError > acceptExternalReplacement(std::string_view text)
std::expected< ModelBookmark, SourceError > bookmark(std::size_t byteOffset, PositionAffinity affinity=PositionAffinity::FORWARD) const
bool conflicted() const noexcept
std::string_view saveTarget() const noexcept
static std::expected< std::unique_ptr< FiniteTextDocument >, TextDocumentError > create(std::string_view text, FiniteTextDocumentOptions options={})
SourceStamp session() const noexcept
bool canRedo() const noexcept
std::expected< void, TextDocumentError > undo()
bool dirty() const noexcept
ModelResource resource() const noexcept
std::expected< PositionReply, SourceError > resolve(const PositionRequest &request, std::stop_token stop={}) const override
~FiniteTextDocument() override
Release only this session's own text, history and metadata.
void observeExternalReplacement() noexcept
Report external replacement without accepting unrelated bytes or clearing history.
std::expected< void, TextDocumentError > acknowledgeSave(const TextSaveTicket &ticket) noexcept
std::expected< TextSaveTicket, TextDocumentError > beginSave(std::string_view target)
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
Copyable bookmark owning metadata only; never retains model, snapshot, view or page....
Definition model_position.hpp:66
Bounded provider extension independent of views, schedulers, and total extent. Implementations must n...
Definition model_position.hpp:163
std::uint64_t revision() const noexcept
std::uint64_t extent() const noexcept
std::expected< void, SourceError > edit(TextSplice change, std::uint64_t contentState) noexcept
~TextChangeMap()
Release metadata; no provider, view or text is retained.
std::expected< ModelBookmark, SourceError > bookmark(std::uint64_t offset, PositionAffinity affinity=PositionAffinity::FORWARD) const
std::expected< void, SourceError > traverse(std::uint64_t contentState, std::uint64_t extent) noexcept
static std::expected< std::unique_ptr< TextChangeMap >, SourceError > create(ModelResource resource, std::uint64_t extent, TextAnchorOptions options={}, std::uint64_t initialRevision=1, std::uint64_t initialContentState=1)
std::expected< TextMappedPosition, SourceError > map(const ModelBookmark &bookmark, std::stop_token stop={}) const
Owning save acknowledgement qualification; retains target metadata, never document bytes....
Definition text_document.hpp:120
std::uint64_t revision() const noexcept
std::string_view target() const noexcept
TextSaveTicket()=default
Empty invalid ticket.
Public Portals API.
Definition action.hpp:8
PositionAffinity
Preferred surviving side when a position is deleted or text is inserted beside a mark.
Definition model_position.hpp:42
@ FORWARD
Prefer the following item; an end boundary falls back to the preceding item.
Definition model_position.hpp:43
@ EMPTY
Successful content has zero logical rows.
Definition data_presentation.hpp:14
TextDocumentError
Finite document error; rejected operations preserve published state.
Definition text_document.hpp:88
@ 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
PositionResolution
Successful protocol outcomes; absence of support differs from missing history.
Definition model_position.hpp:123
@ UNAVAILABLE
Supported position history or storage is no longer available.
Definition model_position.hpp:130
@ 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
@ WOULD_BLOCK
Active resume, invocation, registration or queue capacity rejects the operation.
Definition async.hpp:30
@ INVALID_ARGUMENT
Empty task, callback or immutable data owner, or invalid policy.
Definition async.hpp:26
@ CONFLICT
The model revision changed since this draft began.
Definition line_edit.hpp:86
SourceError
Source protocol and admission failures, independent of a provider's storage format.
Definition source_budget.hpp:14
@ NOT_BOUNDARY
A cursor offset is outside the text or inside a cluster.
Definition text.hpp:83
Explicit finite-buffer adapter configuration; not a windowed editor.
Definition text_document.hpp:109
TextHistoryOptions history
Existing bounded Undo/Redo; content tokens are enabled by this adapter.
Definition text_document.hpp:110
WidthPolicy widthPolicy
Existing Unicode/display validation policy.
Definition text_document.hpp:112
TextLimits limits
Existing finite whole-buffer admission limits.
Definition text_document.hpp:113
TextAnchorOptions anchors
Metadata proof capacity independent of text/history size.
Definition text_document.hpp:111
Owning scalar position; offset is meaningful only in the declared data revision.
Definition model_position.hpp:48
Logical resource namespace, distinct from its current delivery session. Reuse an existing SourceStamp...
Definition model_position.hpp:21
Owning resolution receipt; meaningful only for its matching live request.
Definition model_position.hpp:142
Immutable qualified navigation request; does not start work by itself.
Definition model_position.hpp:134
Immutable source namespace; replacement sessions must share their old budget root.
Definition window_source.hpp:27
Finite metadata retention, independent of logical document size.
Definition text_document.hpp:22
std::size_t maxChanges
Positive maximum retained splice edges and revision receipts.
Definition text_document.hpp:23
Configurable bounds and explicit-boundary grouping for text history.
Definition text_history.hpp:22
Owning limits checked without silently truncating input or clusters.
Definition text.hpp:57
Intermediate scalar mapping; not a boundary-validated PositionReply.
Definition text_document.hpp:27
PositionResolution state
Mapping proof outcome.
Definition text_document.hpp:28
ModelPosition position
Scalar hint requiring the provider's final grapheme-boundary validation.
Definition text_document.hpp:29
Byte-boundary selection owned by the document session, independent of a view.
Definition text_document.hpp:101
bool operator==(const TextSelection &other) const noexcept=default
std::size_t cursor
Active byte-boundary end.
Definition text_document.hpp:102
std::size_t anchor
Fixed byte-boundary end.
Definition text_document.hpp:103
Scalar text replacement proof; does not read or retain document bytes. Units are chosen by the enclos...
Definition text_document.hpp:15
std::uint64_t erased
Number of removed units.
Definition text_document.hpp:17
std::uint64_t inserted
Number of inserted units.
Definition text_document.hpp:18
std::uint64_t offset
First replaced scalar offset in the provider's text units.
Definition text_document.hpp:16
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42