@@ -69,6 +69,7 @@ public struct Trivia: Sendable {
6969
7070 /// Creates a new ``Trivia`` by merging in the given trivia. Only includes one
7171 /// copy of a common prefix of `self` and `trivia`.
72+ @available ( * , deprecated, message: " Use mergingCommonPrefix(trivia) or mergingCommonSuffix(trivia) instead " )
7273 public func merging( _ trivia: Trivia ? ) -> Trivia {
7374 guard let trivia else {
7475 return self
@@ -84,16 +85,69 @@ public struct Trivia: Sendable {
8485 return lhs. appending ( rhs)
8586 }
8687
88+ /// Creates a new ``Trivia`` by merging in the given trivia. Only includes one
89+ /// copy of the common prefix of `self` and `trivia`.
90+ public func mergingCommonPrefix( _ trivia: Trivia ? ) -> Trivia {
91+ guard let trivia else {
92+ return self
93+ }
94+
95+ let lhs = self . decomposed
96+ let rhs = trivia. decomposed
97+ let commonPrefix = zip ( lhs, rhs) . prefix ( while: == )
98+ if commonPrefix. isEmpty {
99+ return lhs + rhs
100+ } else {
101+ return lhs + Trivia( pieces: rhs. dropFirst ( commonPrefix. count) )
102+ }
103+ }
104+
105+ /// Creates a new ``Trivia`` by merging in the given trivia. Only includes one
106+ /// copy of the common suffix of `self` and `trivia`.
107+ public func mergingCommonSuffix( _ trivia: Trivia ? ) -> Trivia {
108+ guard let trivia else {
109+ return self
110+ }
111+
112+ let lhs = self . decomposed
113+ let rhs = trivia. decomposed
114+ let commonSuffix = zip ( lhs. reversed ( ) , rhs. reversed ( ) ) . prefix ( while: == )
115+ if commonSuffix. isEmpty {
116+ return lhs + rhs
117+ } else {
118+ return Trivia ( pieces: lhs. dropLast ( commonSuffix. count) ) + rhs
119+ }
120+ }
121+
87122 /// Creates a new ``Trivia`` by merging the leading and trailing ``Trivia``
88123 /// of `triviaOf` into the end of `self`. Only includes one copy of any
89124 /// common prefixes.
125+ @available ( * , deprecated, message: " Use mergingCommonPrefix(triviaOf:) or mergingCommonSuffix(triviaOf:) instead " )
90126 public func merging( triviaOf node: ( some SyntaxProtocol ) ? ) -> Trivia {
91127 guard let node else {
92128 return self
93129 }
94130 return merging ( node. leadingTrivia) . merging ( node. trailingTrivia)
95131 }
96132
133+ /// Creates a new ``Trivia`` by merging ``SyntaxProtocol/leadingTrivia`` and ``SyntaxProtocol/trailingTrivia`` of
134+ /// `node` into the end of `self`. Only includes one copy of any common prefixes.
135+ public func mergingCommonPrefix( triviaOf node: ( some SyntaxProtocol ) ? ) -> Trivia {
136+ guard let node else {
137+ return self
138+ }
139+ return self . mergingCommonPrefix ( node. leadingTrivia) . mergingCommonPrefix ( node. trailingTrivia)
140+ }
141+
142+ /// Creates a new ``Trivia`` by merging ``SyntaxProtocol/leadingTrivia`` and ``SyntaxProtocol/trailingTrivia`` of
143+ /// `node` into the end of `self`. Only includes one copy of any common suffixes.
144+ public func mergingCommonSuffix( triviaOf node: ( some SyntaxProtocol ) ? ) -> Trivia {
145+ guard let node else {
146+ return self
147+ }
148+ return self . mergingCommonSuffix ( node. leadingTrivia) . mergingCommonSuffix ( node. trailingTrivia)
149+ }
150+
97151 /// Concatenates two collections of ``Trivia`` into one collection.
98152 public static func + ( lhs: Trivia , rhs: Trivia ) -> Trivia {
99153 return lhs. appending ( rhs)
@@ -215,3 +269,17 @@ extension RawTriviaPiece: CustomDebugStringConvertible {
215269 TriviaPiece ( raw: self ) . debugDescription
216270 }
217271}
272+
273+ public extension SyntaxProtocol {
274+ /// Create a new ``Trivia`` by merging ``SyntaxProtocol/leadingTrivia`` and ``SyntaxProtocol/trailingTrivia`` of
275+ /// this node, including only one copy of their common prefix.
276+ var triviaByMergingCommonPrefix : Trivia {
277+ self . leadingTrivia. mergingCommonPrefix ( self . trailingTrivia)
278+ }
279+
280+ /// Create a new ``Trivia`` by merging ``SyntaxProtocol/leadingTrivia`` and ``SyntaxProtocol/trailingTrivia`` of
281+ /// this node, including only one copy of their common suffix.
282+ var triviaByMergingCommonSuffix : Trivia {
283+ self . leadingTrivia. mergingCommonSuffix ( self . trailingTrivia)
284+ }
285+ }
0 commit comments