Skip to content

Pretty results of dump() for Syntax #94

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 6 commits into from
Mar 5, 2019
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
30 changes: 29 additions & 1 deletion Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/// Each node has accessors for its known children, and allows efficient
/// iteration over the children through its `children` property.
public protocol Syntax:
CustomStringConvertible, TextOutputStreamable {}
CustomStringConvertible, CustomDebugStringConvertible, TextOutputStreamable {}

internal protocol _SyntaxBase: Syntax {
/// The data backing this node.
Expand Down Expand Up @@ -275,6 +275,11 @@ extension _SyntaxBase {
return data.raw.description
}

/// Returns a description used by dump.
public var debugDescription: String {
return String(reflecting: type(of: self))
}

/// Prints the raw value of this node to the provided stream.
/// - Parameter stream: The stream to which to print the raw tree.
public func write<Target>(to target: inout Target)
Expand Down Expand Up @@ -654,6 +659,17 @@ public struct TokenSyntax: _SyntaxBase, Hashable {
}
}

extension TokenSyntax: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, children: [
"text": text,
"leadingTrivia": leadingTrivia,
"trailingTrivia": trailingTrivia,
"tokenKind": tokenKind,
])
}
}

/// Sequence of tokens that are part of the provided Syntax node.
public struct TokenSequence: Sequence {
public struct Iterator: IteratorProtocol {
Expand Down Expand Up @@ -695,6 +711,12 @@ public struct TokenSequence: Sequence {
}
}

extension TokenSequence: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, unlabeledChildren: self.map{ $0 })
}
}

/// Reverse sequence of tokens that are part of the provided Syntax node.
public struct ReversedTokenSequence: Sequence {
public struct Iterator: IteratorProtocol {
Expand Down Expand Up @@ -736,6 +758,12 @@ public struct ReversedTokenSequence: Sequence {
}
}

extension ReversedTokenSequence: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, unlabeledChildren: self.map{ $0 })
}
}

/// Represents a node from the syntax tree.
///
/// This is a more efficient representation than `Syntax` because it avoids casts
Expand Down
9 changes: 9 additions & 0 deletions Sources/SwiftSyntax/SyntaxCollections.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ extension ${node.name}: Sequence {
}
}
}
% end
% end

% for node in SYNTAX_NODES:
% if node.is_syntax_collection():
extension ${node.name}: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, unlabeledChildren: self.map{ $0 })
}
}
% end
% end
19 changes: 19 additions & 0 deletions Sources/SwiftSyntax/SyntaxNodes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public struct UnknownSyntax: _SyntaxBase, Hashable {
}
}

extension UnknownSyntax: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, children: [:])
}
}

% for node in SYNTAX_NODES:
% base_type = node.base_type
% if node.is_base():
Expand Down Expand Up @@ -169,7 +175,20 @@ public struct ${node.name}: ${base_type}, _SyntaxBase, Hashable {
return data.nodeId.hash(into: &hasher)
}
}
% end
% end

% for node in SYNTAX_NODES:
% if not node.is_base() and not node.is_syntax_collection():
extension ${node.name}: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, children: [
% for child in node.children:
"${child.swift_name}": ${child.swift_name} as Any,
% end
])
}
}
% end
% end

Expand Down
7 changes: 7 additions & 0 deletions Sources/SwiftSyntax/Trivia.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ extension TriviaPiece: TextOutputStreamable {
}
}

extension TriviaPiece: CustomDebugStringConvertible {
/// Returns a description used by dump.
public var debugDescription: String {
return "TriviaPiece"
}
}

/// A collection of leading or trailing trivia. This is the main data structure
/// for thinking about trivia.
public struct Trivia {
Expand Down
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ XCTMain({ () -> [XCTestCaseEntry] in
testCase(TokenSyntaxTestCase.allTests),
testCase(SyntaxTreeModifierTests.allTests),
testCase(TriviaTests.allTests),
testCase(CustomReflectableTests.allTests),
]
return testCases
}())
Loading