-
Notifications
You must be signed in to change notification settings - Fork 433
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
[Diagnostics] fix trivia merging and transfer #2848
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ public struct Trivia: Sendable { | |
|
||
/// Creates a new ``Trivia`` by merging in the given trivia. Only includes one | ||
/// copy of a common prefix of `self` and `trivia`. | ||
@available(*, deprecated, message: "Use mergingCommonPrefix(trivia) or mergingCommonSuffix(trivia) instead") | ||
public func merging(_ trivia: Trivia?) -> Trivia { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of continuing to have a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue here is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would consider that a bug fix and thus an acceptable behavior change. |
||
guard let trivia else { | ||
return self | ||
|
@@ -84,16 +85,69 @@ public struct Trivia: Sendable { | |
return lhs.appending(rhs) | ||
} | ||
|
||
/// Creates a new ``Trivia`` by merging in the given trivia. Only includes one | ||
/// copy of the common prefix of `self` and `trivia`. | ||
public func mergingCommonPrefix(_ trivia: Trivia?) -> Trivia { | ||
guard let trivia else { | ||
return self | ||
} | ||
|
||
let lhs = self.decomposed | ||
let rhs = trivia.decomposed | ||
let commonPrefix = zip(lhs, rhs).prefix(while: ==) | ||
if commonPrefix.isEmpty { | ||
return lhs + rhs | ||
} else { | ||
return lhs + Trivia(pieces: rhs.dropFirst(commonPrefix.count)) | ||
} | ||
} | ||
|
||
/// Creates a new ``Trivia`` by merging in the given trivia. Only includes one | ||
/// copy of the common suffix of `self` and `trivia`. | ||
public func mergingCommonSuffix(_ trivia: Trivia?) -> Trivia { | ||
guard let trivia else { | ||
return self | ||
} | ||
|
||
let lhs = self.decomposed | ||
let rhs = trivia.decomposed | ||
let commonSuffix = zip(lhs.reversed(), rhs.reversed()).prefix(while: ==) | ||
if commonSuffix.isEmpty { | ||
return lhs + rhs | ||
} else { | ||
return Trivia(pieces: lhs.dropLast(commonSuffix.count)) + rhs | ||
} | ||
} | ||
|
||
/// Creates a new ``Trivia`` by merging the leading and trailing ``Trivia`` | ||
/// of `triviaOf` into the end of `self`. Only includes one copy of any | ||
/// common prefixes. | ||
@available(*, deprecated, message: "Use mergingCommonPrefix(triviaOf:) or mergingCommonSuffix(triviaOf:) instead") | ||
public func merging(triviaOf node: (some SyntaxProtocol)?) -> Trivia { | ||
guard let node else { | ||
return self | ||
} | ||
return merging(node.leadingTrivia).merging(node.trailingTrivia) | ||
} | ||
|
||
/// Creates a new ``Trivia`` by merging ``SyntaxProtocol/leadingTrivia`` and ``SyntaxProtocol/trailingTrivia`` of | ||
/// `node` into the end of `self`. Only includes one copy of any common prefixes. | ||
public func mergingCommonPrefix(triviaOf node: (some SyntaxProtocol)?) -> Trivia { | ||
guard let node else { | ||
return self | ||
} | ||
return self.mergingCommonPrefix(node.leadingTrivia).mergingCommonPrefix(node.trailingTrivia) | ||
} | ||
|
||
/// Creates a new ``Trivia`` by merging ``SyntaxProtocol/leadingTrivia`` and ``SyntaxProtocol/trailingTrivia`` of | ||
/// `node` into the end of `self`. Only includes one copy of any common suffixes. | ||
public func mergingCommonSuffix(triviaOf node: (some SyntaxProtocol)?) -> Trivia { | ||
guard let node else { | ||
return self | ||
} | ||
return self.mergingCommonSuffix(node.leadingTrivia).mergingCommonSuffix(node.trailingTrivia) | ||
} | ||
|
||
/// Concatenates two collections of ``Trivia`` into one collection. | ||
public static func + (lhs: Trivia, rhs: Trivia) -> Trivia { | ||
return lhs.appending(rhs) | ||
|
@@ -215,3 +269,17 @@ extension RawTriviaPiece: CustomDebugStringConvertible { | |
TriviaPiece(raw: self).debugDescription | ||
} | ||
} | ||
|
||
public extension SyntaxProtocol { | ||
/// Create a new ``Trivia`` by merging ``SyntaxProtocol/leadingTrivia`` and ``SyntaxProtocol/trailingTrivia`` of | ||
/// this node, including only one copy of their common prefix. | ||
var triviaByMergingCommonPrefix: Trivia { | ||
self.leadingTrivia.mergingCommonPrefix(self.trailingTrivia) | ||
} | ||
|
||
/// Create a new ``Trivia`` by merging ``SyntaxProtocol/leadingTrivia`` and ``SyntaxProtocol/trailingTrivia`` of | ||
/// this node, including only one copy of their common suffix. | ||
var triviaByMergingCommonSuffix: Trivia { | ||
self.leadingTrivia.mergingCommonSuffix(self.trailingTrivia) | ||
} | ||
} |
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.
Could you add an entry in
Release Notes/601.md
for the public API changes?