feat: rope-backed Documents store with self.documents() (#12)#15
Merged
Conversation
Build the root of document-sync (CONTEXT.md, ADR 0003/0005): - Rope-backed `Document` (ropey::Rope) keyed by URI, carrying language id, version, and contents; `ropey` never leaks into the public API. - Concurrency-safe `Documents` handle (`Arc<RwLock<..>>`), cheap to clone; exposed on the trait via `documents()` and on the handler receiver via `Context::documents()`. - Position encoding field defaulting to UTF-16 (the LSP-mandatory default; #10 layers the UTF-8 handshake on top). Both position<->offset paths read it: UTF-8 native byte index, UTF-16 via char/utf16 helpers (ADR 0016). - Mutation primitives (open / apply_incremental_change / close / save) as plain methods. apply_incremental_change advances the document version and rejects a reversed range (which would otherwise panic Rope::remove under the write lock and poison the store). save() returns None for an unopened URI. UTF-8 position_to_offset rejects mid-codepoint / past-EOL offsets. Wiring the mutations into the dispatcher is out of scope (#9); the UTF-8 negotiation handshake is out of scope (#10). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
6055a72 to
6e3ebda
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #12.
The root of the document-sync work (CONTEXT.md, ADR 0003 / 0005): a rope-backed document store, concurrency-safe and reachable from handlers without writing lock code.
Change
Document— backed byropey::Rope, keyed byUri, carrying language id, version, and contents.ropeynever leaks into the public API.Documents—Arc<RwLock<..>>handle, cheap to clone. Exposed on the trait viaLanguageServer::documents()and on the handler receiver viaContext::documents(); all clones share one store.PositionEncodingfield defaulting toUtf16(the LSP-mandatory default; Position encoding negotiation (UTF-8 preferred, UTF-16 fallback) #10 layers the UTF-8 handshake on top). Bothposition <-> offsetpaths read it: UTF-8 native byte index, UTF-16 viachar/utf16helpers (ADR 0016).Position.characteris never treated as aropeychar index.open/apply_incremental_change/close/saveas plain methods.Robustness (from review)
apply_incremental_changeadvances the document version, and rejects a reversed range (endbeforestart) withInvalidRequestinstead of panickingRope::removeunder the write lock (which would poison the store for every later access).save()returnsNonefor a URI that was never opened.position_to_offsetrejects mid-codepoint and past-end-of-line offsets.Out of scope
didChange/didClose/didSavetrait methods — Apply built-in document-sync mutation inline in the read-loop #9.Test
tests/documents.rs— 16 tests covering read-back, encoding default/set, UTF-8 & UTF-16 conversions (incl. emoji / combining-char round-trips), incremental + full-document replacement with version assertions, reversed-range rejection (store stays usable),saveon a missing doc, UTF-8 boundary rejection, cheap clone, andself.documents()/ctx.documents()sharing the same store.cargo clippy --all-targetsclean.🤖 Generated with Claude Code