@@ -25,40 +25,51 @@ public struct Trivia {
2525 self . pieces = Array ( pieces)
2626 }
2727
28- /// Creates Trivia with no pieces.
29- public static var zero : Trivia {
30- return Trivia ( pieces: [ ] )
31- }
32-
3328 /// Whether the Trivia contains no pieces.
3429 public var isEmpty : Bool {
3530 pieces. isEmpty
3631 }
3732
33+ public var sourceLength : SourceLength {
34+ return pieces. map ( { $0. sourceLength } ) . reduce ( . zero, + )
35+ }
36+
37+ /// Get the byteSize of this trivia
38+ public var byteSize : Int {
39+ return sourceLength. utf8Length
40+ }
41+
3842 /// Creates a new `Trivia` by appending the provided `TriviaPiece` to the end.
3943 public func appending( _ piece: TriviaPiece ) -> Trivia {
4044 var copy = pieces
4145 copy. append ( piece)
4246 return Trivia ( pieces: copy)
4347 }
4448
45- public var sourceLength : SourceLength {
46- return pieces. map ( { $0. sourceLength } ) . reduce ( . zero, + )
49+ /// Creates a new `Trivia` by appending the given trivia to the end.
50+ public func appending( _ trivia: Trivia ) -> Trivia {
51+ var copy = pieces
52+ copy. append ( contentsOf: trivia. pieces)
53+ return Trivia ( pieces: copy)
4754 }
4855
49- /// Get the byteSize of this trivia
50- public var byteSize : Int {
51- return sourceLength. utf8Length
56+ /// Creates a new `Trivia` by appending the leading and trailing `Trivia`
57+ /// of `triviaOf` to the end.
58+ public func appending< T: SyntaxProtocol > ( triviaOf node: T ) -> Trivia {
59+ var copy = pieces
60+ copy. append ( contentsOf: node. leadingTrivia. pieces)
61+ copy. append ( contentsOf: node. trailingTrivia. pieces)
62+ return Trivia ( pieces: copy)
5263 }
5364
5465 /// Concatenates two collections of `Trivia` into one collection.
5566 public static func + ( lhs: Trivia , rhs: Trivia ) -> Trivia {
56- return Trivia ( pieces : lhs. pieces + rhs. pieces )
67+ return lhs. appending ( rhs)
5768 }
5869
5970 /// Concatenates two collections of `Trivia` into the left-hand side.
6071 public static func += ( lhs: inout Trivia , rhs: Trivia ) {
61- lhs = lhs + rhs
72+ lhs = lhs. appending ( rhs)
6273 }
6374}
6475
0 commit comments