Skip to content

Commit dbb4419

Browse files
committed
inatial Support for comments modifiers
1 parent e3d3a83 commit dbb4419

File tree

3 files changed

+4448
-201
lines changed

3 files changed

+4448
-201
lines changed

Sources/SwiftSyntaxBuilder/BuildableNodes.swift.gyb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public struct ${type.buildable()}: ${base_type.buildable()}, ${type.expressible_
3131
% for child in children:
3232
let ${child.name()}: ${child.type().buildable()}
3333
% end
34+
public var itemLeadingTrivia: Trivia?
3435

3536
/// Creates a `${type.buildable()}` using the provided parameters.
3637
/// - Parameters:
@@ -107,7 +108,7 @@ public struct ${type.buildable()}: ${base_type.buildable()}, ${type.expressible_
107108
/// Conformance to `${base_type.buildable()}`.
108109
public func build${base_type.base_name()}(format: Format, leadingTrivia: Trivia? = nil) -> ${base_type.syntax()} {
109110
let result = build${type.base_name()}(format: format, leadingTrivia: leadingTrivia)
110-
return ${base_type.syntax()}(result)
111+
return ${base_type.syntax()}(result).withLeadingTrivia(itemLeadingTrivia ?? .zero)
111112
}
112113

113114
/// Conformance to `${type.expressible_as()}`.
@@ -130,5 +131,24 @@ public struct ${type.buildable()}: ${base_type.buildable()}, ${type.expressible_
130131
return self
131132
}
132133
% end
134+
135+
public func withDocBlokComment(_ text: String) -> Self {
136+
let piece = TriviaPiece.docBlockComment("/** \(text) */")
137+
return addCommentPiece(commentPiece: piece)
138+
}
139+
140+
public func withLineComment(_ text: String) -> Self {
141+
let piece = TriviaPiece.lineComment("// \(text)")
142+
return addCommentPiece(commentPiece: piece)
143+
}
144+
145+
private func addCommentPiece(commentPiece: TriviaPiece) -> Self {
146+
var newSelf = self
147+
newSelf.itemLeadingTrivia = itemLeadingTrivia?
148+
.appending(commentPiece)
149+
.appending(.newlines(1)) ?? [commentPiece, .newlines(1)]
150+
return newSelf
151+
}
152+
133153
}
134154
% end

0 commit comments

Comments
 (0)