Skip to content

Commit 8862f9a

Browse files
committed
Refactor docCommentTrivia() to Trivia extension
1 parent 11582df commit 8862f9a

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public class Child {
106106
\(choices.map { " - \(grammar.grammar(for: $0))" }.joined(separator: "\n"))
107107
"""
108108

109-
let tokenChoicesTrivia = docCommentTrivia(
109+
let tokenChoicesTrivia = SwiftSyntax.Trivia.docCommentTrivia(
110110
from: """
111111
### Tokens
112112
@@ -258,7 +258,7 @@ public class Child {
258258
self.deprecatedName = deprecatedName
259259
self.kind = kind
260260
self.nameForDiagnostics = nameForDiagnostics
261-
self.documentationSummary = docCommentTrivia(from: documentation)
261+
self.documentationSummary = SwiftSyntax.Trivia.docCommentTrivia(from: documentation)
262262
self.documentationAbstract = String(documentation?.split(whereSeparator: \.isNewline).first ?? "")
263263
self.isOptional = isOptional
264264
}

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class Node {
121121
self.base = base
122122
self.isExperimental = isExperimental
123123
self.nameForDiagnostics = nameForDiagnostics
124-
self.documentation = docCommentTrivia(from: documentation)
124+
self.documentation = SwiftSyntax.Trivia.docCommentTrivia(from: documentation)
125125
self.parserFunction = parserFunction
126126

127127
let childrenWithUnexpected: [Child]
@@ -211,7 +211,7 @@ public class Node {
211211
}
212212
.joined(separator: "\n")
213213

214-
return docCommentTrivia(
214+
return .docCommentTrivia(
215215
from: """
216216
### Contained in
217217
@@ -237,7 +237,7 @@ public class Node {
237237
self.base = base
238238
self.isExperimental = isExperimental
239239
self.nameForDiagnostics = nameForDiagnostics
240-
self.documentation = docCommentTrivia(from: documentation)
240+
self.documentation = SwiftSyntax.Trivia.docCommentTrivia(from: documentation)
241241
self.parserFunction = parserFunction
242242

243243
assert(!elementChoices.isEmpty)
@@ -299,7 +299,7 @@ public struct LayoutNode {
299299
return []
300300
}
301301

302-
return docCommentTrivia(
302+
return .docCommentTrivia(
303303
from: """
304304
### Children
305305
@@ -352,7 +352,7 @@ public struct CollectionNode {
352352
grammar = "(\(elementChoices.map { "``\($0.syntaxType)``" }.joined(separator: " | "))) `*`"
353353
}
354354

355-
return docCommentTrivia(
355+
return .docCommentTrivia(
356356
from: """
357357
### Children
358358

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Trait {
2121
init(traitName: String, documentation: String? = nil, children: [Child]) {
2222
self.traitName = traitName
2323
self.protocolName = .identifier("\(traitName)Syntax")
24-
self.documentation = docCommentTrivia(from: documentation)
24+
self.documentation = SwiftSyntax.Trivia.docCommentTrivia(from: documentation)
2525
self.children = children
2626
}
2727
}

CodeGeneration/Sources/SyntaxSupport/Utils.swift

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,39 @@ public func lowercaseFirstWord(name: String) -> String {
3838
return name.prefix(wordIndex).lowercased() + name[name.index(name.startIndex, offsetBy: wordIndex)..<name.endIndex]
3939
}
4040

41-
/// Give a (possibly multi-line) string, prepends `///` to each line and creates
42-
/// a `.docLineComment` trivia piece for each line.
43-
public func docCommentTrivia(from string: String?) -> SwiftSyntax.Trivia {
44-
guard let string else {
45-
return []
46-
}
47-
let lines = string.split(separator: "\n", omittingEmptySubsequences: false)
48-
let pieces = lines.enumerated().map { (index, line) in
49-
var line = line
50-
if index != lines.count - 1 {
51-
line = "\(line)\n"
41+
// Helpers to create trivia pieces
42+
extension SwiftSyntax.Trivia {
43+
/// Make a new trivia from a (possibly multi-line) string, prepending `///`
44+
/// to each line and creating a `.docLineComment` trivia piece for each line.
45+
public static func docCommentTrivia(from string: String?) -> SwiftSyntax.Trivia {
46+
guard let string else {
47+
return []
48+
}
49+
50+
let lines = string.split(separator: "\n", omittingEmptySubsequences: false)
51+
let pieces = lines.enumerated().map { (index, line) in
52+
var line = line
53+
if index != lines.count - 1 {
54+
line = "\(line)\n"
55+
}
56+
return SwiftSyntax.TriviaPiece.docLineComment("/// \(line)")
5257
}
53-
return SwiftSyntax.TriviaPiece.docLineComment("/// \(line)")
58+
return SwiftSyntax.Trivia(pieces: pieces)
59+
}
60+
61+
62+
/// Make a new trivia by joining together ``SwiftSyntax/TriviaPiece``s from `joining`,
63+
/// and gluing them together with pieces from `separator`.
64+
public init(
65+
joining items: [SwiftSyntax.Trivia],
66+
separator: SwiftSyntax.Trivia = SwiftSyntax.Trivia(pieces: [TriviaPiece.newlines(1), TriviaPiece.docLineComment("///"), TriviaPiece.newlines(1)])) {
67+
68+
self.init(
69+
pieces: items
70+
.filter { !$0.isEmpty }
71+
.joined(separator: separator)
72+
)
5473
}
55-
return SwiftSyntax.Trivia(pieces: pieces)
5674
}
5775

5876
public extension Collection {

CodeGeneration/Sources/generate-swift-syntax/LayoutNode+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ extension LayoutNode {
9292
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
9393
""".removingEmptyLines
9494

95-
return docCommentTrivia(from: formattedParams)
95+
return SwiftSyntax.Trivia.docCommentTrivia(from: formattedParams)
9696
}
9797

9898
/// Create a builder-based convenience initializer, if needed.

0 commit comments

Comments
 (0)