Skip to content

Commit 26fbd78

Browse files
committed
Add a function to merge trivia from a node into an existing trivia
Generally when a node is removed, we need to add its trivia to an existing node. Add a small helper function to aid in this case.
1 parent cb0f24e commit 26fbd78

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

Sources/SwiftSyntax/Trivia.swift

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
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)
5768
}
5869

5970
/// 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)
6273
}
6374
}
6475

@@ -123,8 +134,8 @@ extension TriviaPiece {
123134
public var isNewline: Bool {
124135
switch self {
125136
case .newlines,
126-
.carriageReturns,
127-
.carriageReturnLineFeeds:
137+
.carriageReturns,
138+
.carriageReturnLineFeeds:
128139
return true
129140
default:
130141
return false
@@ -137,8 +148,8 @@ extension RawTriviaPiece {
137148
public var isNewline: Bool {
138149
switch self {
139150
case .newlines,
140-
.carriageReturns,
141-
.carriageReturnLineFeeds:
151+
.carriageReturns,
152+
.carriageReturnLineFeeds:
142153
return true
143154
default:
144155
return false

utils/group.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"TokenKind.swift",
1919
"SyntaxTreeViewMode.swift",
2020
"Trivia.swift",
21+
"TriviaPieces.swift",
2122
"Tokens.swift",
2223
],
2324
"Syntax nodes": [

0 commit comments

Comments
 (0)