Merged
Conversation
DavisVaughan
commented
Jun 13, 2025
Comment on lines
-187
to
-190
| index_valid = range.start.line; | ||
| if let Ok(range) = from_proto::text_range(range, &line_index, encoding) { | ||
| text.replace_range(Range::<usize>::from(range), &change.text); | ||
| } |
Collaborator
Author
There was a problem hiding this comment.
We used to just...skip (??) the file update if we were unable to convert from Position to TextRange.
This is a critical failure IMO and should be treated as a panic, which we now do
DavisVaughan
commented
Jun 13, 2025
| } | ||
| text | ||
| // Rebuild the `line_index` after applying the final edit, and sync other fields | ||
| self.line_index = LineIndex::new(&self.contents); |
Collaborator
Author
There was a problem hiding this comment.
For the cases of:
- A single full document change
- 1 or more incremental changes that don't invalidate each other
this is now the only place we recreate the line index
lionel-
approved these changes
Jun 16, 2025
ddb5fdb to
8f9da47
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.
Branched from #363
It turns out that we are rebuilding our
LineIndexat least twice on every singleon_did_change()call.LineIndexbuilt for thisDocumentair/crates/lsp/src/rust_analyzer/utils.rs
Line 35 in be96e71
air/crates/lsp/src/documents.rs
Lines 135 to 136 in be96e71
That's not super efficient! In the ~90% case where the user is just typing one character at a time, we should only be rebuilding the line index exactly 1 time.
I've folded
apply_document_changes()intoon_did_change()to keep all of the mutable-state-updating code close together.The actual implementation is basically still the same, I've just had to rework things a little to do this optimization.
I had added in some logging and can confirm that we now only rebuild the
LineIndex1 time on every update as the user is typing. We typically also rebuild it exactly 1 time during a find-and-replace as well, due to how we take advantage of VS Code sending us the updates from bottom to top (so we can apply them sequentially without invalidating our line index).I might try a PR upstream to rust-analyzer, because I'm fairly certain they have this too from looking at their code.