Skip to content

Commit 35d32ca

Browse files
committed
Move methods modifying a node's trivia to SyntaxProtocol
1 parent a7e74ca commit 35d32ca

File tree

3 files changed

+1987
-10027
lines changed

3 files changed

+1987
-10027
lines changed

Sources/SwiftSyntax/Syntax.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,24 @@ public extension SyntaxProtocol {
257257
/// the first token syntax contained by this node. Without such token, this
258258
/// property will return nil.
259259
var leadingTrivia: Trivia? {
260-
return raw.formLeadingTrivia()
260+
get {
261+
return raw.formLeadingTrivia()
262+
}
263+
set {
264+
self = withLeadingTrivia(newValue ?? [])
265+
}
261266
}
262267

263268
/// The trailing trivia of this syntax node. Trailing trivia is attached to
264269
/// the last token syntax contained by this node. Without such token, this
265270
/// property will return nil.
266271
var trailingTrivia: Trivia? {
267-
return raw.formTrailingTrivia()
272+
get {
273+
return raw.formTrailingTrivia()
274+
}
275+
set {
276+
self = withTrailingTrivia(newValue ?? [])
277+
}
268278
}
269279

270280
/// The length this node's leading trivia takes up spelled out in source.
@@ -277,6 +287,33 @@ public extension SyntaxProtocol {
277287
return raw.trailingTriviaLength
278288
}
279289

290+
/// Returns a new syntax node with its leading trivia replaced
291+
/// by the provided trivia.
292+
func withLeadingTrivia(_ leadingTrivia: Trivia) -> Self {
293+
return Self(Syntax(data.withLeadingTrivia(leadingTrivia)))!
294+
}
295+
296+
/// Returns a new syntax node with its trailing trivia replaced
297+
/// by the provided trivia.
298+
func withTrailingTrivia(_ trailingTrivia: Trivia) -> Self {
299+
return Self(Syntax(data.withTrailingTrivia(trailingTrivia)))!
300+
}
301+
302+
/// Returns a new `${node.name}` with its leading trivia removed.
303+
func withoutLeadingTrivia() -> Self {
304+
return withLeadingTrivia([])
305+
}
306+
307+
/// Returns a new `${node.name}` with its trailing trivia removed.
308+
func withoutTrailingTrivia() -> Self {
309+
return withTrailingTrivia([])
310+
}
311+
312+
/// Returns a new `${node.name}` with all trivia removed.
313+
func withoutTrivia() -> Self {
314+
return withoutLeadingTrivia().withoutTrailingTrivia()
315+
}
316+
280317
/// The length of this node including all of its trivia.
281318
var totalLength: SourceLength {
282319
return raw.totalLength

Sources/SwiftSyntax/SyntaxNodes.swift.gyb

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Each node will have:
4545
% # Handled in SyntaxCollections.swift.gyb
4646
% pass
4747
% else:
48+
% # We are actually handling this node now
49+
// MARK: - ${node.name}
4850

4951
% for line in dedented_lines(node.description):
5052
/// ${line}
@@ -138,64 +140,8 @@ public struct ${node.name}: ${base_type}Protocol {
138140
return ${node.name}(newData)
139141
}
140142
% end
141-
142-
/// Returns a new `${node.name}` with its leading trivia replaced
143-
/// by the provided trivia.
144-
public func withLeadingTrivia(_ leadingTrivia: Trivia) -> ${node.name} {
145-
return ${node.name}(data.withLeadingTrivia(leadingTrivia))
146-
}
147-
148-
/// Returns a new `${node.name}` with its trailing trivia replaced
149-
/// by the provided trivia.
150-
public func withTrailingTrivia(_ trailingTrivia: Trivia) -> ${node.name} {
151-
return ${node.name}(data.withTrailingTrivia(trailingTrivia))
152-
}
153-
154-
/// Returns a new `${node.name}` with its leading trivia removed.
155-
public func withoutLeadingTrivia() -> ${node.name} {
156-
return withLeadingTrivia([])
157-
}
158-
159-
/// Returns a new `${node.name}` with its trailing trivia removed.
160-
public func withoutTrailingTrivia() -> ${node.name} {
161-
return withTrailingTrivia([])
162-
}
163-
164-
/// Returns a new `${node.name}` with all trivia removed.
165-
public func withoutTrivia() -> ${node.name} {
166-
return withoutLeadingTrivia().withoutTrailingTrivia()
167-
}
168-
169-
/// The leading trivia (spaces, newlines, etc.) associated with this `${node.name}`.
170-
public var leadingTrivia: Trivia? {
171-
get {
172-
return raw.formLeadingTrivia()
173-
}
174-
set {
175-
self = withLeadingTrivia(newValue ?? [])
176-
}
177-
}
178-
179-
/// The trailing trivia (spaces, newlines, etc.) associated with this `${node.name}`.
180-
public var trailingTrivia: Trivia? {
181-
get {
182-
return raw.formTrailingTrivia()
183-
}
184-
set {
185-
self = withTrailingTrivia(newValue ?? [])
186-
}
187-
}
188143
}
189-
% end
190-
% end
191144

192-
% for node in SYNTAX_NODES:
193-
% if node.is_base():
194-
% # Handled in SyntaxNodesBase.swift.gyb
195-
% pass
196-
% elif node.is_syntax_collection():
197-
% pass
198-
% else:
199145
extension ${node.name}: CustomReflectable {
200146
public var customMirror: Mirror {
201147
return Mirror(self, children: [
@@ -209,5 +155,6 @@ extension ${node.name}: CustomReflectable {
209155
])
210156
}
211157
}
158+
212159
% end
213160
% end

0 commit comments

Comments
 (0)