Skip to content

Commit 6a32bc3

Browse files
committed
Make string utils in CodeGeneration extensions instead of global functions
This just reads nicer
1 parent 60e918c commit 6a32bc3

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

CodeGeneration/Sources/Utils/Utils.swift

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,21 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// Trims leading and trailing whitespace from each line.
14-
public func dedented<Lines: Sequence>(lines: Lines) -> [String] where Lines.Element: StringProtocol {
15-
lines.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
16-
}
17-
18-
/// Trims leading and trailing whitespace from each line.
19-
public func dedented(string: String) -> String {
20-
dedented(lines: string.split(separator: "\n"))
21-
.joined(separator: "\n")
22-
}
23-
24-
/// Creates a single-line documentation string from indented
25-
/// documentation as written in `CodeGeneration`.
26-
public func flattened(indentedDocumentation: String) -> String {
27-
dedented(string: indentedDocumentation)
28-
.replacingOccurrences(of: "\n", with: "")
29-
.trimmingCharacters(in: .whitespacesAndNewlines)
30-
}
13+
extension String {
14+
/// Creates a single-line documentation string from indented
15+
/// documentation as written in `CodeGeneration`.
16+
public var flattened: String {
17+
self
18+
.replacingOccurrences(of: "\n", with: "")
19+
.trimmingCharacters(in: .whitespacesAndNewlines)
20+
}
3121

32-
/// Removes all empty lines from a multi-line string.
33-
public func removedEmptyLines(string: String) -> String {
34-
string.split(whereSeparator: \.isNewline)
35-
.filter { !$0.allSatisfy(\.isWhitespace) }
36-
.joined(separator: "\n")
22+
/// Removes all empty lines from a multi-line string.
23+
public var removingEmptyLines: String {
24+
return
25+
self
26+
.split(whereSeparator: \.isNewline)
27+
.filter { !$0.allSatisfy(\.isWhitespace) }
28+
.joined(separator: "\n")
29+
}
3730
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,14 @@ extension LayoutNode {
8686
return " - \(child.varName): \(firstLine)"
8787
}
8888

89-
let formattedParams = removedEmptyLines(
90-
string: """
91-
- Parameters:
92-
- leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. \
93-
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
94-
\(children.compactMap(generateParamDocComment).joined(separator: "\n"))
95-
- trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. \
96-
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
97-
"""
98-
)
89+
let formattedParams = """
90+
- Parameters:
91+
- leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. \
92+
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
93+
\(children.compactMap(generateParamDocComment).joined(separator: "\n"))
94+
- trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. \
95+
If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
96+
""".removingEmptyLines
9997

10098
return docCommentTrivia(from: formattedParams)
10199
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxNodesFile.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ extension Child {
2020
guard let description = documentation else {
2121
return []
2222
}
23-
let dedented = dedented(string: description)
24-
let lines = dedented.split(separator: "\n", omittingEmptySubsequences: false)
23+
let lines = description.split(separator: "\n", omittingEmptySubsequences: false)
2524
let pieces = lines.map { SwiftSyntax.TriviaPiece.docLineComment("/// \($0)") }
2625
return Trivia(pieces: pieces)
2726
}

0 commit comments

Comments
 (0)