Skip to content

Deprecate ByteSourceRange in favor of Range<AbsolutePosition> #2588

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 2 commits into from
Apr 19, 2024

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Apr 3, 2024

While being a little more verbose, this has a few advantages:

  • We only have a single type to represent ranges in SwiftSyntax instead of Range<AbsolutePosition> and ByteSourceRange, both of which we needed to use in sourcekit-lsp
  • Unifying the convenience functions on the two results in a single type that has all the convenience functions, instead of spreading them to two distinct sets
  • The use of AbsolutePosition and SourceLength makes type-system guarantees that these are UTF-8 byte positions / length, making it harder to accidentally add eg. UTF-16 lengths with UTF-8 lengths.

rdar://125624626

@ahoppen ahoppen requested a review from rintaro April 3, 2024 17:49
@ahoppen ahoppen requested a review from bnbarham as a code owner April 3, 2024 17:49
@ahoppen
Copy link
Member Author

ahoppen commented Apr 3, 2024

@swift-ci Please test

ahoppen added a commit to ahoppen/sourcekit-lsp that referenced this pull request Apr 4, 2024
…ent:)`

This initializer was only introduced very recently since `IncrementalEdit` stores the replacement bytes instead of just the replacement length. We are now changing `IncrementalEdit` store the range it’s replacing as `Range<AbsolutePosition>` instead of `ByteSourceRange`.

Companion of swiftlang/swift-syntax#2588
@ahoppen
Copy link
Member Author

ahoppen commented Apr 4, 2024

swiftlang/sourcekit-lsp#1158

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Apr 4, 2024

swiftlang/sourcekit-lsp#1158

@swift-ci Please test macOS

/// at the given offset is unclassified.
@available(*, deprecated, message: "Use classification(at: AbsolutePosition) instead")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deprecation feels a little odd since it actually has different behavior. Should we make that clear in the deprecation message?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I changed it to maintain the same behavior it had before the PR and kept the deprecation message the same.

@ahoppen ahoppen force-pushed the ahoppen/deprecate-bytesourcerange branch from 0dfe3a4 to e51cc5a Compare April 12, 2024 21:31
@ahoppen
Copy link
Member Author

ahoppen commented Apr 12, 2024

@swift-ci Please test

/// Returns the range for the overlapping region between two ranges.
///
/// If the intersection is empty, this returns `nil`.
public func intersecting(_ other: Range<AbsolutePosition>) -> Range<AbsolutePosition>? {
Copy link
Member

@rintaro rintaro Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should use Range methods like clamped(to:) / overlaps(_:) . Is there a reason we can't?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good suggestion. Added a commit that deprecates intersection-style methods.

While being a little more verbose, this has a few advantages:
- We only have a single type to represent ranges in SwiftSyntax instead of `Range<AbsolutePosition>` and `ByteSourceRange`, both of which we needed to use in sourcekit-lsp
- Unifying the convenience functions on the two results in a single type that has all the convenience functions, instead of spreading them to two distinct sets
- The use of `AbsolutePosition` and `SourceLength` makes type-system guarantees that these are UTF-8 byte positions / length, making it harder to accidentally add eg. UTF-16 lengths with UTF-8 lengths.

rdar://125624626
@ahoppen ahoppen force-pushed the ahoppen/deprecate-bytesourcerange branch from e51cc5a to dca214e Compare April 17, 2024 19:32
@ahoppen
Copy link
Member Author

ahoppen commented Apr 17, 2024

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Apr 17, 2024

swiftlang/sourcekit-lsp#1158

@swift-ci Please test

- Description: `SyntaxIdentifier.indexInTree` allows the retrieval of a `SyntaxIdentifier` that identifies the syntax node independent of the syntax tree. `SyntaxIdentifier.fromIndexInTree` allows the creation for a `SyntaxIdentifier` from a tree-agnostic `SyntaxIdentifier.IndexInTree` and the tree's root node.
- Pull request: https://github.com/apple/swift-syntax/pull/2594

- `Range<AbsolutePosition>`
- Description: `Range<AbsolutePosition>` gained a few convenience functions inspired from `ByteSourceRange`: `init(position:length:)`, `length`, `intersectsOrTouches`, `intersects`, `intersecting`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update this for overlaps()/clamped(to:) style APIs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing. I forgot about the release notes

…utePosition>`

We should use the native Range methods `clamped(to:)` and `overlaps` here. To match that naming scheme `intersectsOrTouches` has been renamed to `overlapsOrTouches`.
@ahoppen ahoppen force-pushed the ahoppen/deprecate-bytesourcerange branch from dca214e to b66915f Compare April 18, 2024 04:01
@ahoppen
Copy link
Member Author

ahoppen commented Apr 18, 2024

swiftlang/sourcekit-lsp#1158

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Apr 18, 2024

@swift-ci Please test Windows

@ahoppen
Copy link
Member Author

ahoppen commented Apr 18, 2024

swiftlang/sourcekit-lsp#1158

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 39b4c41 into swiftlang:main Apr 19, 2024
3 checks passed
@ahoppen ahoppen deleted the ahoppen/deprecate-bytesourcerange branch April 19, 2024 03:43
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.

5 participants