Skip to content

Improve debug description of syntax nodes #1503

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
merged 1 commit into from
Apr 13, 2023
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 @@ -233,18 +233,6 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
StmtSyntax("return .choices(\(choices))")
}
}

DeclSyntax(
"""
extension \(raw: node.name): CustomReflectable {
/// Reconstructs the real syntax type for this type from the node's kind and
/// provides a mirror that reflects this type.
public var customMirror: Mirror {
return Mirror(reflecting: Syntax(self).asProtocol(SyntaxProtocol.self))
}
}
"""
)
}

try! ExtensionDeclSyntax("extension Syntax") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,16 +418,4 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
)
}
}

for node in SYNTAX_NODES where node.isSyntaxCollection {
DeclSyntax(
"""
extension \(raw: node.name): CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, unlabeledChildren: self.map{ $0 })
}
}
"""
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,27 +259,6 @@ func syntaxNode(emitKind: String) -> SourceFileSyntax {
StmtSyntax("return .layout(\(layout))")
}
}

try! ExtensionDeclSyntax("extension \(raw: node.name): CustomReflectable") {
let children = DictionaryExprSyntax {
for child in node.children {
DictionaryElementSyntax(
leadingTrivia: .newline,
keyExpression: ExprSyntax(#""\#(raw: child.swiftName)""#),
valueExpression: child.isOptional
? ExprSyntax("\(raw: child.swiftName).map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any")
: ExprSyntax("Syntax(\(raw: child.swiftName)).asProtocol(SyntaxProtocol.self)")
)
}
}
DeclSyntax(
"""
public var customMirror: Mirror {
return Mirror(self, children: \(children))
}
"""
)
}
}
}
}
Expand Down
69 changes: 21 additions & 48 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,6 @@ public struct Syntax: SyntaxProtocol, SyntaxHashable {
}
}

extension Syntax: CustomReflectable {
/// Reconstructs the real syntax type for this type from the node's kind and
/// provides a mirror that reflects this type.
public var customMirror: Mirror {
return Mirror(reflecting: self.asProtocol(SyntaxProtocol.self))
}
}

extension Syntax: Identifiable {
public typealias ID = SyntaxIdentifier
}
Expand Down Expand Up @@ -157,7 +149,7 @@ public extension SyntaxHashable {
/// protocol to provide common functionality for all syntax nodes.
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
public protocol SyntaxProtocol: CustomStringConvertible,
CustomDebugStringConvertible, TextOutputStreamable
CustomDebugStringConvertible, TextOutputStreamable, CustomReflectable
{

/// Retrieve the generic syntax node that is represented by this node.
Expand Down Expand Up @@ -565,14 +557,14 @@ public extension SyntaxProtocol {
debugDescription()
}

/// Same as `debugDescription` but includes all children.
var recursiveDescription: String {
debugDescription(includeChildren: true)
var customMirror: Mirror {
// Suppress printing of children when doing `po node` in the debugger.
// `debugDescription` already prints them in a nicer way.
return Mirror(self, children: [:])
}

/// Returns a summarized dump of this node.
/// - Parameters:
/// - includeChildren: Whether to also dump children, false by default.
/// - includeTrivia: Add trivia to each dumped node, which the default
/// dump skips.
/// - converter: The location converter for the root of the tree. Adds
Expand All @@ -582,31 +574,28 @@ public extension SyntaxProtocol {
/// - indentLevel: The starting indent level, 0 by default. Each level is 2
/// spaces.
func debugDescription(
includeChildren: Bool = false,
includeTrivia: Bool = false,
converter: SourceLocationConverter? = nil,
mark: SyntaxProtocol? = nil,
indentLevel: Int = 0
indentString: String = ""
) -> String {
var str = ""
debugWrite(
to: &str,
includeChildren: includeChildren,
includeTrivia: includeTrivia,
converter: converter,
mark: mark,
indentLevel: indentLevel
indentString: indentString
)
return str
}

private func debugWrite<Target: TextOutputStream>(
to target: inout Target,
includeChildren: Bool,
includeTrivia: Bool,
converter: SourceLocationConverter? = nil,
mark: SyntaxProtocol? = nil,
indentLevel: Int
indentString: String
) {
if let mark = mark, self.id == mark.id {
target.write("*** ")
Expand All @@ -627,11 +616,6 @@ public extension SyntaxProtocol {
}

let allChildren = children(viewMode: .all)
if includeChildren {
if !allChildren.isEmpty {
target.write(" children=\(allChildren.count)")
}
}

if let converter = converter {
let range = sourceRange(converter: converter)
Expand All @@ -646,21 +630,19 @@ public extension SyntaxProtocol {
target.write(" ***")
}

if includeChildren {
let childIndentLevel = indentLevel + 1
for (num, child) in allChildren.enumerated() {
target.write("\n")
target.write(String(repeating: " ", count: childIndentLevel * 2))
target.write("\(num): ")
child.debugWrite(
to: &target,
includeChildren: includeChildren,
includeTrivia: includeTrivia,
converter: converter,
mark: mark,
indentLevel: childIndentLevel
)
}
for (num, child) in allChildren.enumerated() {
let isLastChild = num == allChildren.count - 1
target.write("\n")
target.write(indentString)
target.write(isLastChild ? "╰─" : "├─")
let childIndentString = indentString + (isLastChild ? " " : "│ ")
child.debugWrite(
to: &target,
includeTrivia: includeTrivia,
converter: converter,
mark: mark,
indentString: childIndentString
)
}
}
}
Expand Down Expand Up @@ -763,14 +745,5 @@ extension ReversedTokenSequence: CustomReflectable {
}
}

/// Expose `recursiveDescription` on raw nodes for debugging purposes.
extension RawSyntaxNodeProtocol {
/// Print this raw syntax node including all of its children.
/// Intended for debugging purposes only.
var recursiveDescription: String {
return Syntax(raw: raw).recursiveDescription
}
}

@available(*, unavailable, message: "use 'Syntax' instead")
public struct SyntaxNode {}
40 changes: 0 additions & 40 deletions Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,6 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
}
}

extension DeclSyntax: CustomReflectable {
/// Reconstructs the real syntax type for this type from the node's kind and
/// provides a mirror that reflects this type.
public var customMirror: Mirror {
return Mirror(reflecting: Syntax(self).asProtocol(SyntaxProtocol.self))
}
}

// MARK: - ExprSyntax

/// Protocol to which all `ExprSyntax` nodes conform. Extension point to add
Expand Down Expand Up @@ -318,14 +310,6 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
}
}

extension ExprSyntax: CustomReflectable {
/// Reconstructs the real syntax type for this type from the node's kind and
/// provides a mirror that reflects this type.
public var customMirror: Mirror {
return Mirror(reflecting: Syntax(self).asProtocol(SyntaxProtocol.self))
}
}

// MARK: - PatternSyntax

/// Protocol to which all `PatternSyntax` nodes conform. Extension point to add
Expand Down Expand Up @@ -446,14 +430,6 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
}
}

extension PatternSyntax: CustomReflectable {
/// Reconstructs the real syntax type for this type from the node's kind and
/// provides a mirror that reflects this type.
public var customMirror: Mirror {
return Mirror(reflecting: Syntax(self).asProtocol(SyntaxProtocol.self))
}
}

// MARK: - StmtSyntax

/// Protocol to which all `StmtSyntax` nodes conform. Extension point to add
Expand Down Expand Up @@ -583,14 +559,6 @@ public struct StmtSyntax: StmtSyntaxProtocol, SyntaxHashable {
}
}

extension StmtSyntax: CustomReflectable {
/// Reconstructs the real syntax type for this type from the node's kind and
/// provides a mirror that reflects this type.
public var customMirror: Mirror {
return Mirror(reflecting: Syntax(self).asProtocol(SyntaxProtocol.self))
}
}

// MARK: - TypeSyntax

/// Protocol to which all `TypeSyntax` nodes conform. Extension point to add
Expand Down Expand Up @@ -721,14 +689,6 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
}
}

extension TypeSyntax: CustomReflectable {
/// Reconstructs the real syntax type for this type from the node's kind and
/// provides a mirror that reflects this type.
public var customMirror: Mirror {
return Mirror(reflecting: Syntax(self).asProtocol(SyntaxProtocol.self))
}
}

extension Syntax {
public static var structure: SyntaxNodeStructure {
return .choices([
Expand Down
Loading