Skip to content

Commit 7f2f43c

Browse files
committed
Make string utils in CodeGeneration extensions instead of global functions
This just reads nicer
1 parent b096c36 commit 7f2f43c

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

CodeGeneration/Sources/Utils/Utils.swift

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,36 @@
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) }
13+
extension Sequence where Element: StringProtocol {
14+
/// Trims leading and trailing whitespace from each line.
15+
var dedented: [String] {
16+
return self.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
17+
}
1618
}
1719

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-
}
20+
extension String {
21+
/// Trims leading and trailing whitespace from each line.
22+
public var dedented: String {
23+
return self.split(separator: "\n")
24+
.dedented
25+
.joined(separator: "\n")
26+
}
2327

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-
}
28+
/// Creates a single-line documentation string from indented
29+
/// documentation as written in `CodeGeneration`.
30+
public var flattened: String {
31+
self
32+
.dedented
33+
.replacingOccurrences(of: "\n", with: "")
34+
.trimmingCharacters(in: .whitespacesAndNewlines)
35+
}
3136

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")
37+
/// Removes all empty lines from a multi-line string.
38+
public var removingEmptyLines: String {
39+
return
40+
self
41+
.split(whereSeparator: \.isNewline)
42+
.filter { !$0.allSatisfy(\.isWhitespace) }
43+
.joined(separator: "\n")
44+
}
3745
}

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.dedented.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)