ycpp 0.1.0__cp315-cp315-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. ycpp/__init__.py +110 -0
  2. ycpp/_array.py +318 -0
  3. ycpp/_awareness.py +268 -0
  4. ycpp/_doc.py +718 -0
  5. ycpp/_event.py +188 -0
  6. ycpp/_id_map.py +620 -0
  7. ycpp/_map.py +267 -0
  8. ycpp/_provider.py +180 -0
  9. ycpp/_snapshot.py +28 -0
  10. ycpp/_sticky_index.py +59 -0
  11. ycpp/_stream.py +91 -0
  12. ycpp/_sync.py +192 -0
  13. ycpp/_text.py +239 -0
  14. ycpp/_transaction.py +287 -0
  15. ycpp/_typed.py +122 -0
  16. ycpp/_undo.py +201 -0
  17. ycpp/_update.py +18 -0
  18. ycpp/_xml.py +452 -0
  19. ycpp/_ycpp.cp315-win_amd64.lib +0 -0
  20. ycpp/_ycpp.cp315-win_amd64.pyd +0 -0
  21. ycpp-0.1.0.data/headers/fast_float/ascii_number.h +766 -0
  22. ycpp-0.1.0.data/headers/fast_float/bigint.h +638 -0
  23. ycpp-0.1.0.data/headers/fast_float/constexpr_feature_detect.h +53 -0
  24. ycpp-0.1.0.data/headers/fast_float/decimal_to_binary.h +212 -0
  25. ycpp-0.1.0.data/headers/fast_float/digit_comparison.h +455 -0
  26. ycpp-0.1.0.data/headers/fast_float/fast_float.h +91 -0
  27. ycpp-0.1.0.data/headers/fast_float/fast_table.h +708 -0
  28. ycpp-0.1.0.data/headers/fast_float/float_common.h +1432 -0
  29. ycpp-0.1.0.data/headers/fast_float/parse_number.h +488 -0
  30. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_arithmetic.hpp +267 -0
  31. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_bit.hpp +230 -0
  32. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_cast.hpp +43 -0
  33. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_complex.hpp +112 -0
  34. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_details.hpp +386 -0
  35. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_logical.hpp +240 -0
  36. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_math.hpp +2601 -0
  37. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_memory.hpp +1006 -0
  38. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_rounding.hpp +72 -0
  39. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_swizzle.hpp +260 -0
  40. ycpp-0.1.0.data/headers/xsimd/arch/common/xsimd_common_trigo.hpp +996 -0
  41. ycpp-0.1.0.data/headers/xsimd/arch/utils/shifts.hpp +82 -0
  42. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx.hpp +2111 -0
  43. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx2.hpp +1591 -0
  44. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512bw.hpp +724 -0
  45. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512cd.hpp +28 -0
  46. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512dq.hpp +270 -0
  47. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512er.hpp +17 -0
  48. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512f.hpp +2919 -0
  49. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512ifma.hpp +17 -0
  50. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512pf.hpp +17 -0
  51. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vbmi.hpp +76 -0
  52. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vbmi2.hpp +130 -0
  53. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vnni_avx512bw.hpp +17 -0
  54. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avx512vnni_avx512vbmi2.hpp +17 -0
  55. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_avxvnni.hpp +17 -0
  56. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_common.hpp +26 -0
  57. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_common_fwd.hpp +127 -0
  58. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_constants.hpp +393 -0
  59. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_emulated.hpp +787 -0
  60. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma3_avx.hpp +93 -0
  61. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma3_avx2.hpp +46 -0
  62. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma3_sse.hpp +97 -0
  63. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_fma4.hpp +92 -0
  64. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_i8mm_neon64.hpp +17 -0
  65. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_isa.hpp +146 -0
  66. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_neon.hpp +3580 -0
  67. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_neon64.hpp +1614 -0
  68. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_rvv.hpp +1515 -0
  69. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_scalar.hpp +1210 -0
  70. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse2.hpp +2333 -0
  71. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse3.hpp +66 -0
  72. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse4_1.hpp +510 -0
  73. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sse4_2.hpp +44 -0
  74. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_ssse3.hpp +186 -0
  75. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_sve.hpp +1186 -0
  76. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_vsx.hpp +916 -0
  77. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_vxe.hpp +787 -0
  78. ycpp-0.1.0.data/headers/xsimd/arch/xsimd_wasm.hpp +1819 -0
  79. ycpp-0.1.0.data/headers/xsimd/config/xsimd_arch.hpp +242 -0
  80. ycpp-0.1.0.data/headers/xsimd/config/xsimd_config.hpp +620 -0
  81. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features.hpp +50 -0
  82. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_arm.hpp +132 -0
  83. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_ppc.hpp +54 -0
  84. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_riscv.hpp +95 -0
  85. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_s390x.hpp +56 -0
  86. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpu_features_x86.hpp +1044 -0
  87. ycpp-0.1.0.data/headers/xsimd/config/xsimd_cpuid.hpp +144 -0
  88. ycpp-0.1.0.data/headers/xsimd/config/xsimd_getauxval.hpp +159 -0
  89. ycpp-0.1.0.data/headers/xsimd/config/xsimd_macros.hpp +76 -0
  90. ycpp-0.1.0.data/headers/xsimd/math/xsimd_rem_pio2.hpp +707 -0
  91. ycpp-0.1.0.data/headers/xsimd/memory/xsimd_aligned_allocator.hpp +349 -0
  92. ycpp-0.1.0.data/headers/xsimd/memory/xsimd_alignment.hpp +102 -0
  93. ycpp-0.1.0.data/headers/xsimd/types/xsimd_all_registers.hpp +57 -0
  94. ycpp-0.1.0.data/headers/xsimd/types/xsimd_api.hpp +3106 -0
  95. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx2_register.hpp +44 -0
  96. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512bw_register.hpp +51 -0
  97. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512cd_register.hpp +51 -0
  98. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512dq_register.hpp +51 -0
  99. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512er_register.hpp +51 -0
  100. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512f_register.hpp +77 -0
  101. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512ifma_register.hpp +51 -0
  102. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512pf_register.hpp +51 -0
  103. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vbmi2_register.hpp +51 -0
  104. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vbmi_register.hpp +51 -0
  105. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vnni_avx512bw_register.hpp +54 -0
  106. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx512vnni_avx512vbmi2_register.hpp +53 -0
  107. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avx_register.hpp +64 -0
  108. ycpp-0.1.0.data/headers/xsimd/types/xsimd_avxvnni_register.hpp +44 -0
  109. ycpp-0.1.0.data/headers/xsimd/types/xsimd_batch.hpp +1665 -0
  110. ycpp-0.1.0.data/headers/xsimd/types/xsimd_batch_constant.hpp +571 -0
  111. ycpp-0.1.0.data/headers/xsimd/types/xsimd_batch_fwd.hpp +41 -0
  112. ycpp-0.1.0.data/headers/xsimd/types/xsimd_common_arch.hpp +49 -0
  113. ycpp-0.1.0.data/headers/xsimd/types/xsimd_emulated_register.hpp +80 -0
  114. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma3_avx2_register.hpp +50 -0
  115. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma3_avx_register.hpp +50 -0
  116. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma3_sse_register.hpp +50 -0
  117. ycpp-0.1.0.data/headers/xsimd/types/xsimd_fma4_register.hpp +50 -0
  118. ycpp-0.1.0.data/headers/xsimd/types/xsimd_i8mm_neon64_register.hpp +55 -0
  119. ycpp-0.1.0.data/headers/xsimd/types/xsimd_neon64_register.hpp +55 -0
  120. ycpp-0.1.0.data/headers/xsimd/types/xsimd_neon_register.hpp +130 -0
  121. ycpp-0.1.0.data/headers/xsimd/types/xsimd_register.hpp +96 -0
  122. ycpp-0.1.0.data/headers/xsimd/types/xsimd_rvv_register.hpp +491 -0
  123. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse2_register.hpp +59 -0
  124. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse3_register.hpp +49 -0
  125. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse4_1_register.hpp +48 -0
  126. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sse4_2_register.hpp +48 -0
  127. ycpp-0.1.0.data/headers/xsimd/types/xsimd_ssse3_register.hpp +48 -0
  128. ycpp-0.1.0.data/headers/xsimd/types/xsimd_sve_register.hpp +216 -0
  129. ycpp-0.1.0.data/headers/xsimd/types/xsimd_traits.hpp +408 -0
  130. ycpp-0.1.0.data/headers/xsimd/types/xsimd_utils.hpp +462 -0
  131. ycpp-0.1.0.data/headers/xsimd/types/xsimd_vsx_register.hpp +77 -0
  132. ycpp-0.1.0.data/headers/xsimd/types/xsimd_vxe_register.hpp +86 -0
  133. ycpp-0.1.0.data/headers/xsimd/types/xsimd_wasm_register.hpp +59 -0
  134. ycpp-0.1.0.data/headers/xsimd/utils/bits.hpp +122 -0
  135. ycpp-0.1.0.data/headers/xsimd/utils/xsimd_type_traits.hpp +145 -0
  136. ycpp-0.1.0.data/headers/xsimd/xsimd.hpp +43 -0
  137. ycpp-0.1.0.dist-info/DELVEWHEEL +2 -0
  138. ycpp-0.1.0.dist-info/METADATA +15 -0
  139. ycpp-0.1.0.dist-info/RECORD +142 -0
  140. ycpp-0.1.0.dist-info/WHEEL +4 -0
  141. ycpp.libs/msvcp140-a4c2229bdc2a2a630acdc095b4d86008.dll +0 -0
  142. ycpp.libs/msvcp140_1-226498d4bfcb4f07974757b596ebec45.dll +0 -0
ycpp/_sync.py ADDED
@@ -0,0 +1,192 @@
1
+ """The y-protocols sync layer, as pycrdt 0.14.13's _sync.py. Every access to a document goes through
2
+ Doc's public methods, so this layer is transport only."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from enum import IntEnum
7
+ from typing import Iterator, TYPE_CHECKING
8
+
9
+ if TYPE_CHECKING:
10
+ from ._doc import Doc
11
+
12
+
13
+ # lib0 caps a variable-length integer at eight bytes and at the largest JS safe integer.
14
+ _MAX_VAR_UINT_BYTES = 8
15
+ _MAX_VAR_UINT = 2**53 - 1
16
+
17
+
18
+ class YMessageType(IntEnum):
19
+ """A generic Y message type."""
20
+
21
+ SYNC = 0
22
+ AWARENESS = 1
23
+
24
+
25
+ class YSyncMessageType(IntEnum):
26
+ """A message type used for synchronizing documents."""
27
+
28
+ SYNC_STEP1 = 0
29
+ SYNC_STEP2 = 1
30
+ SYNC_UPDATE = 2
31
+
32
+
33
+ def write_var_uint(num: int) -> bytes:
34
+ """Encodes a payload length."""
35
+ res = []
36
+ while num > 127:
37
+ res.append(128 | (127 & num))
38
+ num >>= 7
39
+ res.append(num)
40
+ return bytes(res)
41
+
42
+
43
+ def create_awareness_message(data: bytes) -> bytes:
44
+ """Creates an AWARENESS message."""
45
+ return bytes([YMessageType.AWARENESS]) + write_message(data)
46
+
47
+
48
+ def create_message(data: bytes, msg_type: int) -> bytes:
49
+ """Creates a SYNC message."""
50
+ return bytes([YMessageType.SYNC, msg_type]) + write_message(data)
51
+
52
+
53
+ def create_sync_step1_message(data: bytes) -> bytes:
54
+ """Creates a SYNC_STEP1 message consisting of a document state."""
55
+ return create_message(data, YSyncMessageType.SYNC_STEP1)
56
+
57
+
58
+ def create_sync_step2_message(data: bytes) -> bytes:
59
+ """Creates a SYNC_STEP2 message in reply to a SYNC_STEP1 message."""
60
+ return create_message(data, YSyncMessageType.SYNC_STEP2)
61
+
62
+
63
+ def create_update_message(data: bytes) -> bytes:
64
+ """Creates a SYNC_UPDATE message that contains a document update."""
65
+ return create_message(data, YSyncMessageType.SYNC_UPDATE)
66
+
67
+
68
+ class Encoder:
69
+ """An encoder capable of writing messages to a binary stream."""
70
+
71
+ stream: list[bytes]
72
+
73
+ def __init__(self) -> None:
74
+ self.stream = []
75
+
76
+ def write_var_uint(self, num: int) -> None:
77
+ """Encodes a number."""
78
+ self.stream.append(write_var_uint(num))
79
+
80
+ def write_var_string(self, text: str) -> None:
81
+ """Encodes a string, prefixed with its UTF-8 byte count."""
82
+ encoded = text.encode()
83
+ self.stream.append(write_var_uint(len(encoded)))
84
+ self.stream.append(encoded)
85
+
86
+ def to_bytes(self) -> bytes:
87
+ """Returns the binary stream."""
88
+ return b"".join(self.stream)
89
+
90
+
91
+ class Decoder:
92
+ """A decoder capable of reading messages from a byte stream."""
93
+
94
+ def __init__(self, stream: bytes):
95
+ self.stream = stream
96
+ self.length = len(stream)
97
+ self.i0 = 0
98
+
99
+ def read_var_uint(self) -> int:
100
+ """Decodes the current message length."""
101
+ if self.length <= 0:
102
+ raise RuntimeError("Y protocol error")
103
+ uint = 0
104
+ shift = 0
105
+ for _ in range(_MAX_VAR_UINT_BYTES):
106
+ if self.length <= 0:
107
+ raise RuntimeError("Y protocol error")
108
+ byte = self.stream[self.i0]
109
+ uint += (byte & 127) << shift
110
+ shift += 7
111
+ self.i0 += 1
112
+ self.length -= 1
113
+ if byte < 128:
114
+ if uint > _MAX_VAR_UINT:
115
+ raise RuntimeError("Y protocol error")
116
+ return uint
117
+ raise RuntimeError("Y protocol error")
118
+
119
+ def read_message(self) -> bytes | None:
120
+ """Reads a message from the byte stream, ready to read the next message if any."""
121
+ if self.length == 0:
122
+ return None
123
+ length = self.read_var_uint()
124
+ if length > self.length:
125
+ raise RuntimeError("Y protocol error")
126
+ if length == 0:
127
+ return b""
128
+ i1 = self.i0 + length
129
+ message = self.stream[self.i0 : i1]
130
+ self.i0 = i1
131
+ self.length -= length
132
+ return message
133
+
134
+ def read_messages(self) -> Iterator[bytes]:
135
+ """A generator that reads messages from the byte stream."""
136
+ while True:
137
+ message = self.read_message()
138
+ if message is None:
139
+ return
140
+ yield message
141
+
142
+ def read_var_string(self) -> str:
143
+ """Reads a message as an UTF-8 string from the byte stream."""
144
+ message = self.read_message()
145
+ if message is None:
146
+ return ""
147
+ return message.decode("utf-8")
148
+
149
+
150
+ def read_message(stream: bytes) -> bytes:
151
+ """Reads a message from a byte stream."""
152
+ message = Decoder(stream).read_message()
153
+ assert message is not None
154
+ return message
155
+
156
+
157
+ def write_message(stream: bytes) -> bytes:
158
+ """Writes a stream in a message."""
159
+ return write_var_uint(len(stream)) + stream
160
+
161
+
162
+ def handle_sync_message(message: bytes, ydoc: Doc) -> bytes | None:
163
+ """Processes a synchronization message on a document."""
164
+ if not message:
165
+ raise RuntimeError("Y protocol error")
166
+ message_type = message[0]
167
+ msg = message[1:]
168
+
169
+ if message_type == YSyncMessageType.SYNC_STEP1:
170
+ state = read_message(msg)
171
+ if state is None:
172
+ raise RuntimeError("Y protocol error")
173
+ update = ydoc.get_update(state)
174
+ return create_sync_step2_message(update)
175
+
176
+ if message_type in (
177
+ YSyncMessageType.SYNC_STEP2,
178
+ YSyncMessageType.SYNC_UPDATE,
179
+ ):
180
+ update = read_message(msg)
181
+ if update is None:
182
+ raise RuntimeError("Y protocol error")
183
+ # An empty update is the two-byte header of a document state with no clients.
184
+ if update != b"\x00\x00":
185
+ ydoc.apply_update(update)
186
+
187
+ return None
188
+
189
+
190
+ def create_sync_message(ydoc: Doc) -> bytes:
191
+ """Creates a SYNC_STEP1 message that contains the state of a Doc."""
192
+ return create_sync_step1_message(ydoc.get_state())
ycpp/_text.py ADDED
@@ -0,0 +1,239 @@
1
+ from __future__ import annotations
2
+
3
+ from . import _ycpp
4
+ from ._event import Subscription, arg_count
5
+ from ._sticky_index import Assoc, StickyIndex
6
+ from ._stream import EventStreams
7
+
8
+
9
+ def _merged(delta: list) -> list[tuple]:
10
+ """Yjs's toDelta reports one chunk per text run.
11
+
12
+ ycpp splits a run into one operation per item, and formatting splits items
13
+ into single characters, so neighbouring one-character operations are glued
14
+ back together. A longer insert is an embed and is never glued: the engine
15
+ has no other reason to leave two adjacent text operations unmerged.
16
+ """
17
+ chunks: list[tuple] = []
18
+ splittable: list[bool] = []
19
+ for operation in delta:
20
+ if "insert" not in operation:
21
+ continue
22
+ value, attributes = operation["insert"], operation.get("attributes")
23
+ piece = isinstance(value, str) and len(value) == 1
24
+ if chunks and splittable[-1] and piece and chunks[-1][1] == attributes:
25
+ chunks[-1] = (chunks[-1][0] + value, attributes)
26
+ continue
27
+ chunks.append((value, attributes))
28
+ splittable.append(piece)
29
+ return chunks
30
+
31
+
32
+ class Text(EventStreams):
33
+ """A shared text, shaped like pycrdt's Text. Positions count UTF-16 code units."""
34
+
35
+ def __init__(self, init: str | None = None, *, _doc=None, _integrated=None) -> None:
36
+ self._doc = _doc
37
+ self._prelim = None if _integrated is not None else init
38
+ self._ref = _integrated
39
+ self._subscriptions = []
40
+ self._init_event_streams()
41
+
42
+ def _integrate(self, doc, ref) -> None:
43
+ prelim, self._prelim = self._prelim, None
44
+ self._doc = doc
45
+ self._ref = ref
46
+ if prelim:
47
+ self._apply(lambda: ref.insert(0, prelim))
48
+
49
+ @property
50
+ def doc(self):
51
+ if self._doc is None:
52
+ raise RuntimeError("Not integrated in a document yet")
53
+ return self._doc
54
+
55
+ @property
56
+ def integrated(self):
57
+ if self._ref is None:
58
+ raise RuntimeError("Not integrated in a document yet")
59
+ return self._ref
60
+
61
+ @property
62
+ def is_prelim(self) -> bool:
63
+ return self._prelim is not None
64
+
65
+ @property
66
+ def is_integrated(self) -> bool:
67
+ return self._ref is not None
68
+
69
+ @property
70
+ def prelim(self):
71
+ return self._prelim
72
+
73
+ @property
74
+ def type_name(self) -> str:
75
+ return "text"
76
+
77
+ def _apply(self, operation):
78
+ doc = self.doc
79
+ try:
80
+ return operation()
81
+ finally:
82
+ doc._raise_observer_errors()
83
+
84
+ def insert(self, index: int, value: str, attrs: dict | None = None) -> None:
85
+ self._apply(lambda: self.integrated.insert(index, value, attrs))
86
+
87
+ def insert_embed(self, index: int, value, attrs: dict | None = None) -> None:
88
+ from ._array import Array
89
+ from ._map import Map
90
+
91
+ ref = self.integrated
92
+ integrate = None
93
+ if isinstance(value, Map):
94
+ integrate = ref.insert_map
95
+ elif isinstance(value, Text):
96
+ integrate = ref.insert_text
97
+ elif isinstance(value, Array):
98
+ integrate = ref.insert_array
99
+ if integrate is None:
100
+ self._apply(lambda: ref.insert_embed(index, value, attrs))
101
+ return
102
+ self._apply(lambda: value._integrate(self.doc, integrate(index)))
103
+ if attrs:
104
+ # The shared type itself is written unformatted; the attributes still tag that position.
105
+ self.format(index, index + 1, attrs)
106
+
107
+ def format(self, start: int, stop: int, attrs: dict) -> None:
108
+ start, stop = self._range(slice(start, stop))
109
+ length = stop - start
110
+ if length > 0:
111
+ self._apply(lambda: self.integrated.format(start, length, attrs))
112
+
113
+ def apply_delta(self, delta, sanitize: bool = True) -> None:
114
+ self._apply(lambda: self.integrated.apply_delta(delta, sanitize))
115
+
116
+ def cleanup_formatting(self) -> int:
117
+ return self._apply(self.integrated.cleanup_formatting)
118
+
119
+ def clear(self) -> None:
120
+ del self[:]
121
+
122
+ def diff(self) -> list[tuple]:
123
+ from ._array import Array
124
+ from ._map import Map
125
+
126
+ handled = {_ycpp.MapRef: Map, _ycpp.TextRef: Text, _ycpp.ArrayRef: Array}
127
+ delta = self.integrated.delta()
128
+ for operation in delta:
129
+ value = operation.get("insert")
130
+ wrapper = handled.get(type(value))
131
+ if wrapper is not None:
132
+ operation["insert"] = wrapper(_doc=self._doc, _integrated=value)
133
+ return _merged(delta)
134
+
135
+ def to_py(self):
136
+ if self._ref is None:
137
+ return self._prelim
138
+ return str(self)
139
+
140
+ def __str__(self) -> str:
141
+ return self.integrated.to_string()
142
+
143
+ def __len__(self) -> int:
144
+ return len(self.integrated)
145
+
146
+ def __getitem__(self, key):
147
+ return str(self)[key]
148
+
149
+ def __contains__(self, value) -> bool:
150
+ return value in str(self)
151
+
152
+ def __setitem__(self, key, value) -> None:
153
+ if isinstance(key, int):
154
+ if len(value) != 1:
155
+ raise RuntimeError(
156
+ f"Single item assigned value must have a length of 1, not {len(value)}"
157
+ )
158
+ self._apply(
159
+ lambda: (
160
+ self.integrated.erase(key, 1),
161
+ self.integrated.insert(key, value),
162
+ )
163
+ )
164
+ return
165
+ if isinstance(key, slice):
166
+ start, stop = self._range(key)
167
+ self._apply(
168
+ lambda: (
169
+ self.integrated.erase(start, stop - start),
170
+ self.integrated.insert(start, value),
171
+ )
172
+ )
173
+ return
174
+ raise RuntimeError(f"Index not supported: {key}")
175
+
176
+ def __delitem__(self, key) -> None:
177
+ if isinstance(key, int):
178
+ self._apply(lambda: self.integrated.erase(key, 1))
179
+ return
180
+ if isinstance(key, slice):
181
+ start, stop = self._range(key)
182
+ if stop > start:
183
+ self._apply(lambda: self.integrated.erase(start, stop - start))
184
+ return
185
+ raise RuntimeError(f"Index not supported: {key}")
186
+
187
+ def __iadd__(self, value: str):
188
+ self._apply(lambda: self.integrated.insert(len(self), value))
189
+ return self
190
+
191
+ def __eq__(self, other) -> bool:
192
+ if isinstance(other, Text):
193
+ return str(self) == str(other)
194
+ if isinstance(other, str):
195
+ return str(self) == other
196
+ return NotImplemented
197
+
198
+ def __repr__(self) -> str:
199
+ return f"Text({str(self)!r})"
200
+
201
+ def _range(self, key: slice) -> tuple[int, int]:
202
+ if key.step is not None:
203
+ raise RuntimeError("Step not supported")
204
+ if key.start is None:
205
+ start = 0
206
+ elif key.start < 0:
207
+ raise RuntimeError("Negative start not supported")
208
+ else:
209
+ start = key.start
210
+ if key.stop is None:
211
+ stop = len(self)
212
+ elif key.stop < 0:
213
+ raise RuntimeError("Negative stop not supported")
214
+ else:
215
+ stop = key.stop
216
+ return start, stop
217
+
218
+ def observe(self, callback) -> Subscription:
219
+ doc = self.doc
220
+ observer = doc._observe_text(self.integrated)
221
+ doc._text_observers[observer] = (self, callback, arg_count(callback))
222
+ subscription = Subscription(doc, observer)
223
+ self._subscriptions.append(subscription)
224
+ return subscription
225
+
226
+ def observe_deep(self, callback) -> Subscription:
227
+ doc = self.doc
228
+ observer = doc._observe_deep_text(self.integrated)
229
+ doc._deep_observers[observer] = (callback, arg_count(callback))
230
+ subscription = Subscription(doc, observer)
231
+ self._subscriptions.append(subscription)
232
+ return subscription
233
+
234
+ def sticky_index(self, index: int, assoc: Assoc = Assoc.AFTER) -> StickyIndex:
235
+ return StickyIndex.new(self, index, assoc)
236
+
237
+ def unobserve(self, subscription) -> None:
238
+ self._subscriptions.remove(subscription)
239
+ subscription.drop()