Skip to content

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

Merged
merged 4 commits into from
Mar 1, 2025

Conversation

jmschonfeld
Copy link
Contributor

This implements tracking of AttributedString indices across mutations

@jmschonfeld jmschonfeld force-pushed the attrstr/tracking-indices branch from f068f07 to 995ca2c Compare January 27, 2025 22:34
@jmschonfeld jmschonfeld force-pushed the attrstr/tracking-indices branch from 995ca2c to 583bc88 Compare February 26, 2025 18:15
@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld marked this pull request as ready for review February 27, 2025 19:22
@jmschonfeld jmschonfeld force-pushed the attrstr/tracking-indices branch from 583bc88 to bfd0106 Compare February 28, 2025 21:41
@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld
Copy link
Contributor Author

@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)
Copy link
Contributor

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?

Copy link
Contributor Author

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 {
Copy link
Contributor

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

Copy link
Contributor Author

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

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld merged commit bfcba67 into swiftlang:main Mar 1, 2025
3 checks passed
@jmschonfeld jmschonfeld deleted the attrstr/tracking-indices branch March 1, 2025 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants