Skip to content

Include property names in debug description #1539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ struct GenerateSwiftSyntax: ParsableCommand {
GeneratedFileSpec(swiftParserDiagnosticsGeneratedDir + ["TokenNameForDiagnostics.swift"], tokenNameForDiagnosticFile),

// SwiftSyntax
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["ChildNameForKeyPath.swift"], childNameForKeyPathFile),
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["Keyword.swift"], keywordFile),
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["raw", "RawSyntaxNodes.swift"], rawSyntaxNodesFile),
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["raw", "RawSyntaxValidation.swift"], rawSyntaxValidationFile),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxBuilder
import SyntaxSupport
import Utils

let childNameForKeyPathFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
try! FunctionDeclSyntax(
"""
/// If the keyPath is one from a layout structure, return the property name
/// of it.
internal func childName(_ keyPath: AnyKeyPath) -> String?
"""
) {
try! SwitchExprSyntax("switch keyPath") {
for node in NON_BASE_SYNTAX_NODES where !node.isSyntaxCollection {
for child in node.children {
SwitchCaseSyntax(
"""
case \\\(raw: node.type.syntaxBaseName).\(raw: child.swiftName):
return \(literal: child.swiftName)
"""
)
}
}
SwitchCaseSyntax(
"""
default:
return nil
"""
)
}
}
}
1 change: 1 addition & 0 deletions Sources/SwiftSyntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_swift_host_library(SwiftSyntax
generated/raw/RawSyntaxNodes.swift
generated/raw/RawSyntaxValidation.swift

generated/ChildNameForKeyPath.swift
generated/Keyword.swift
generated/SyntaxAnyVisitor.swift
generated/SyntaxBaseNodes.swift
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,11 @@ public extension SyntaxProtocol {
target.write("\n")
target.write(indentString)
target.write(isLastChild ? "╰─" : "├─")
if let keyPath = child.keyPathInParent, let name = childName(keyPath) {
target.write("\(name): ")
} else if self.kind.isSyntaxCollection {
target.write("[\(num)]: ")
}
let childIndentString = indentString + (isLastChild ? " " : "│ ")
child.debugWrite(
to: &target,
Expand Down
Loading