@@ -25,40 +25,51 @@ public struct Trivia {
25
25
self . pieces = Array ( pieces)
26
26
}
27
27
28
- /// Creates Trivia with no pieces.
29
- public static var zero : Trivia {
30
- return Trivia ( pieces: [ ] )
31
- }
32
-
33
28
/// Whether the Trivia contains no pieces.
34
29
public var isEmpty : Bool {
35
30
pieces. isEmpty
36
31
}
37
32
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
+
38
42
/// Creates a new `Trivia` by appending the provided `TriviaPiece` to the end.
39
43
public func appending( _ piece: TriviaPiece ) -> Trivia {
40
44
var copy = pieces
41
45
copy. append ( piece)
42
46
return Trivia ( pieces: copy)
43
47
}
44
48
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)
47
54
}
48
55
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)
52
63
}
53
64
54
65
/// Concatenates two collections of `Trivia` into one collection.
55
- public static func + ( lhs: Trivia , rhs: Trivia ) -> Trivia {
56
- return Trivia ( pieces : lhs. pieces + rhs. pieces )
66
+ public static func + ( lhs: Trivia , rhs: Trivia ) -> Trivia {
67
+ return lhs. appending ( rhs)
57
68
}
58
69
59
70
/// Concatenates two collections of `Trivia` into the left-hand side.
60
- public static func += ( lhs: inout Trivia , rhs: Trivia ) {
61
- lhs = lhs + rhs
71
+ public static func += ( lhs: inout Trivia , rhs: Trivia ) {
72
+ lhs = lhs. appending ( rhs)
62
73
}
63
74
}
64
75
@@ -123,8 +134,8 @@ extension TriviaPiece {
123
134
public var isNewline : Bool {
124
135
switch self {
125
136
case . newlines,
126
- . carriageReturns,
127
- . carriageReturnLineFeeds:
137
+ . carriageReturns,
138
+ . carriageReturnLineFeeds:
128
139
return true
129
140
default :
130
141
return false
@@ -137,8 +148,8 @@ extension RawTriviaPiece {
137
148
public var isNewline : Bool {
138
149
switch self {
139
150
case . newlines,
140
- . carriageReturns,
141
- . carriageReturnLineFeeds:
151
+ . carriageReturns,
152
+ . carriageReturnLineFeeds:
142
153
return true
143
154
default :
144
155
return false
0 commit comments