-
Notifications
You must be signed in to change notification settings - Fork 194
AttributedString Index Tracking #1109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AttributedString Index Tracking #1109
Conversation
f068f07
to
995ca2c
Compare
995ca2c
to
583bc88
Compare
@swift-ci please test |
583bc88
to
bfd0106
Compare
@swift-ci please test |
@swift-ci please test |
lowerBound = string.utf8.index(string.startIndex, offsetBy: lowerBound.utf8Offset + utf8LengthDelta) | ||
} else { | ||
// Form new indices even if the offsets don't change to ensure the indices are valid in the newly-mutated rope | ||
string.utf8.formIndex(&lowerBound, offsetBy: 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this effectively the same as string.utf8.startIndex
? and if so is there a reason for choosing one over the other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite - lowerBound
may not be the start index of the AttributedString
, we just know that the raw UTF-8 offset doesn't need to change based on the mutation. This is just a way to "regenerate" a new BigString.Index
with a valid rope path based on the existing offset. That existing offset may be at the start of the string or somewhere in the middle, but it doesn't change - we just want to make sure that any additional information in the index about the tree structure gets updated too based on the new tree layout.
// Shift the lower bound if either: | ||
// A) The lower bound is greater than the start of the mutation (meaning it must be after the mutation due to the prepare step) | ||
// B) The lower bound is equal to the start of the mutation, but the mutation is an insertion (meaning the text is inserted before the start offset) | ||
if lowerBound.utf8Offset > mutationStartOffset || (lowerBound.utf8Offset == mutationStartOffset && isInsertion), utf8LengthDelta != 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment suggests that you want to shift the lower bound if either of these condition satisfies, but it doesn't mention how utf8LengthDelta
affects this. Just checking that we're not missing this just because utf8LengthDelta == 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right the comment doesn't mention that - that check is just so that if the mutation doesn't actually change the length of the string (i.e. "ABC" was replaced with "DEF") then there's no need to re-calculate offsets, we just do the same thing we would have done if the index was before the mutation. I can add a note to the comment here to make that a bit clearer
…ngth of the string
@swift-ci please test |
This implements tracking of
AttributedString
indices across mutations