Skip to content

Commit d4edeb8

Browse files
committed
Include property names in debug description
1 parent e92c961 commit d4edeb8

File tree

7 files changed

+3419
-15
lines changed

7 files changed

+3419
-15
lines changed

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
9898
GeneratedFileSpec(swiftParserDiagnosticsGeneratedDir + ["TokenNameForDiagnostics.swift"], tokenNameForDiagnosticFile),
9999

100100
// SwiftSyntax
101+
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["ChildNameForKeyPath.swift"], childNameForKeyPathFile),
101102
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["Keyword.swift"], keywordFile),
102103
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["raw", "RawSyntaxNodes.swift"], rawSyntaxNodesFile),
103104
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["raw", "RawSyntaxValidation.swift"], rawSyntaxValidationFile),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
import SyntaxSupport
16+
import Utils
17+
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+
internal func childName(_ keyPath: AnyKeyPath) -> String?
24+
"""
25+
) {
26+
try! SwitchExprSyntax("switch keyPath") {
27+
for node in NON_BASE_SYNTAX_NODES where !node.isSyntaxCollection {
28+
for child in node.children {
29+
SwitchCaseSyntax(
30+
"""
31+
case \\\(raw: node.type.syntaxBaseName).\(raw: child.swiftName):
32+
return \(literal: child.swiftName)
33+
"""
34+
)
35+
}
36+
}
37+
SwitchCaseSyntax(
38+
"""
39+
default:
40+
return nil
41+
"""
42+
)
43+
}
44+
}
45+
}

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_swift_host_library(SwiftSyntax
3434
generated/raw/RawSyntaxNodes.swift
3535
generated/raw/RawSyntaxValidation.swift
3636

37+
generated/ChildNameForKeyPath.swift
3738
generated/Keyword.swift
3839
generated/SyntaxAnyVisitor.swift
3940
generated/SyntaxBaseNodes.swift

Sources/SwiftSyntax/Syntax.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,11 @@ public extension SyntaxProtocol {
635635
target.write("\n")
636636
target.write(indentString)
637637
target.write(isLastChild ? "╰─" : "├─")
638+
if let keyPath = child.keyPathInParent, let name = childName(keyPath) {
639+
target.write("\(name): ")
640+
} else if self.kind.isSyntaxCollection {
641+
target.write("[\(num)]: ")
642+
}
638643
let childIndentString = indentString + (isLastChild ? " " : "")
639644
child.debugWrite(
640645
to: &target,

0 commit comments

Comments
 (0)