Fix replacement edit range computation#12171
Merged
dhruvmanila merged 1 commit intomainfrom Jul 4, 2024
Merged
Conversation
dhruvmanila
commented
Jul 3, 2024
Comment on lines
+24
to
+25
| .zip(modified_line_starts.iter().copied()) | ||
| .skip(1) |
Member
Author
There was a problem hiding this comment.
Skipping the first line start i.e., change (2)
Member
There was a problem hiding this comment.
Is it guaranteed that source_line_starts[0] == TextSize::new(0)?
Member
Author
There was a problem hiding this comment.
Yes:
ruff/crates/ruff_source_file/src/line_index.rs
Lines 28 to 32 in 8e6f722
Comment on lines
-38
to
+43
| for (old_line_start, new_line_start) in line_iter.by_ref() { | ||
| if old_line_start <= source_start | ||
| || new_line_start <= replaced_start | ||
| || source[TextRange::new(old_line_start, source_end)] | ||
| != modified[TextRange::new(new_line_start, replaced_end)] | ||
| for (source_line_start, modified_line_start) in source_line_starts | ||
| .iter() | ||
| .rev() | ||
| .copied() | ||
| .zip(modified_line_starts.iter().rev().copied()) |
Member
Author
There was a problem hiding this comment.
The zip and rev bug u.e., change (1)
Comment on lines
-38
to
+46
| for (old_line_start, new_line_start) in line_iter.by_ref() { | ||
| if old_line_start <= source_start | ||
| || new_line_start <= replaced_start | ||
| || source[TextRange::new(old_line_start, source_end)] | ||
| != modified[TextRange::new(new_line_start, replaced_end)] | ||
| for (source_line_start, modified_line_start) in source_line_starts | ||
| .iter() | ||
| .rev() | ||
| .copied() | ||
| .zip(modified_line_starts.iter().rev().copied()) | ||
| { | ||
| if source_line_start < source_start | ||
| || modified_line_start < modified_start |
Member
Author
There was a problem hiding this comment.
Switching from <= to < i.e., change (3)
MichaReiser
approved these changes
Jul 3, 2024
Comment on lines
+24
to
+25
| .zip(modified_line_starts.iter().copied()) | ||
| .skip(1) |
Member
There was a problem hiding this comment.
Is it guaranteed that source_line_starts[0] == TextSize::new(0)?
Contributor
|
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
This PR fixes various bugs for computing the replacement range between the original and modified source for the language server.
zipon the reversed iterator. The bug was that it was reversing the already zipped iterator. The problem here is that the length of both slices aren't going to be the same unless the source wasn't modified at all. Refer to the Rust playground where you can see this in action.<instead of<=.fixes: #12128
Test Plan
Add test cases where the text is being inserted, deleted, and replaced between the original and new source code, validate the replacement ranges.