feat: support insert_embed shared types in Text - #408
Conversation
Allow embedding Array, Map and Text shared types in a Text via insert_embed(), returning a live, integrated handle that can be mutated afterwards. This brings Text to parity with XmlText, which already supported embedding shared types. The underlying yrs library already returns the integrated reference from Text::insert_embed for preliminary types, so this simply exposes the existing capability through the pyo3 bindings, reusing the same _do_and_integrate path used by Map/Array/XmlText. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: David Brochart <david.brochart@gmail.com>
Co-authored-by: David Brochart <david.brochart@gmail.com>
|
Sorry @samuelstecher, my suggestion in the test was wrong. The types are not the Python types but the integrated types indeed (e.g. not |
|
@davidbrochart its a good point. I originally submitted it like this because these assertions match the test suite of I agree that it would be more convenient if This would be a breaking change though right? |
Wrap embedded shared types returned by Text.diff() and XmlText.diff() in their pycrdt type (Array/Map/Text/XmlText) via _maybe_as_type_or_doc, instead of leaking the raw _pycrdt objects. This matches how Map and Array already return values from __getitem__. Primitive embeds (bytes, str) are unchanged. Co-authored-by: Cursor <cursoragent@cursor.com>
Indeed, I didn't remember I wrote that.
Unfortunately yes, but I think it's better like that since as you said "this matches how |
|
@davidbrochart I pushed the changes that align both |
There was a problem hiding this comment.
Thanks @samuelstecher, that looks good to me.
I don't think this can be considered a breaking change, I rather see it as a fix since previously the returned integrated types were pretty useless, and I doubt anyone ever used them.
Summary
Support embedding shared types (
Array,Map,Text) in aTextviainsert_embed(), returning a live, mutable handle to the integrated child.This brings
Textto parity withXmlText, whoseinsert_embedalready integrates shared types (added in #390, released in v0.13.1).Text.insert_embedstill hardcoded the immutable-JSON path (py_to_any) and discarded the integrated reference returned byyrs.The underlying
yrslibrary already returns the integratedArrayRef/MapRef/TextReffromText::insert_embedfor preliminary types, so this simply exposes the existing capability through the pyo3 bindings, reusing the same_do_and_integratepath already used byMap/Array/XmlText.Motivation
Enables inline, mutable nested structures inside a single collaborative
Textavoiding expensive re-inserts.Changes
src/text.rs: Addinsert_array_prelim,insert_map_prelim,insert_text_prelimtoText(mirror the existingXmlTextequivalents).python/pycrdt/_text.py: DispatchBaseTypeembeds through_do_and_integrate(same pattern asXmlText.insert_embed); primitives keep the existing path.tests/test_text.py: Addtest_insert_embed_shared_types(integration + mutability + attrs +diff()types).CHANGELOG.md: Add entry.Tests
The full suite passes (
238 passed) with 100% coverage maintained;mypy pythonandruff/ruff-formatare clean.Made with Cursor