Skip to content

Commit f037921

Browse files
committed
[SyntaxLayout] Eliminate childName(_:AnyKeyPath)
Replace with SyntaxLayoutProperty.name
1 parent 94ca9ba commit f037921

File tree

4 files changed

+3514
-3518
lines changed

4 files changed

+3514
-3518
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/ChildNameForKeyPathFile.swift

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,28 @@ import SwiftSyntaxBuilder
1515
import SyntaxSupport
1616
import Utils
1717

18-
let childNameForKeyPathFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19-
try! FunctionDeclSyntax(
20-
"""
21-
/// If the keyPath is one from a layout structure, return the property name
22-
/// of it.
23-
@_spi(RawSyntax)
24-
public func childName(_ keyPath: AnyKeyPath) -> String?
25-
"""
26-
) {
27-
try! SwitchExprSyntax("switch keyPath") {
28-
for node in NON_BASE_SYNTAX_NODES.compactMap(\.layoutNode) {
29-
for child in node.children {
30-
SwitchCaseSyntax(
31-
"""
32-
case \\\(node.type.syntaxBaseName).\(child.memberCallName):
33-
return \(literal: child.identifier.description)
34-
"""
35-
)
18+
let childNameForKeyPathFile = try! SourceFileSyntax(leadingTrivia: copyrightHeader) {
19+
try ExtensionDeclSyntax("extension SyntaxLayoutProperty") {
20+
try VariableDeclSyntax(
21+
"""
22+
/// Property name if this is a valid property.
23+
/// 'nil' if the `baseKind` is not a layout syntax, or the index is out of range.
24+
@_spi(RawSyntax)
25+
public var name: String?
26+
"""
27+
) {
28+
try SwitchExprSyntax("switch (self.baseKind, self.index.value)") {
29+
for node in NON_BASE_SYNTAX_NODES {
30+
if let layoutNode = node.layoutNode {
31+
for (index, child) in layoutNode.children.enumerated() {
32+
SwitchCaseSyntax("case (.\(node.enumCaseCallName), \(literal: index)):") {
33+
StmtSyntax("return \(literal: child.identifier.description)")
34+
}
35+
}
36+
}
3637
}
38+
SwitchCaseSyntax("default: return nil")
3739
}
38-
SwitchCaseSyntax(
39-
"""
40-
default:
41-
return nil
42-
"""
43-
)
4440
}
4541
}
4642
}

Sources/SwiftSyntax/SyntaxProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ extension SyntaxProtocol {
731731
target.write("\n")
732732
target.write(indentString)
733733
target.write(isLastChild ? "╰─" : "├─")
734-
if let keyPath = child.keyPathInParent, let name = childName(keyPath) {
734+
if let property = child.propertyInParent, let name = property.name {
735735
target.write("\(name): ")
736736
} else if self.kind.isSyntaxCollection {
737737
target.write("[\(num)]: ")

0 commit comments

Comments
 (0)