Skip to content

Commit 7be7157

Browse files
author
Harlan Haskins
authored
Merge pull request #94 from DeNA/pretty-dump
Pretty results of dump() for Syntax
2 parents ce8fa85 + 64c43d2 commit 7be7157

File tree

6 files changed

+332
-1
lines changed

6 files changed

+332
-1
lines changed

Sources/SwiftSyntax/Syntax.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/// Each node has accessors for its known children, and allows efficient
1515
/// iteration over the children through its `children` property.
1616
public protocol Syntax:
17-
CustomStringConvertible, TextOutputStreamable {}
17+
CustomStringConvertible, CustomDebugStringConvertible, TextOutputStreamable {}
1818

1919
internal protocol _SyntaxBase: Syntax {
2020
/// The data backing this node.
@@ -275,6 +275,11 @@ extension _SyntaxBase {
275275
return data.raw.description
276276
}
277277

278+
/// Returns a description used by dump.
279+
public var debugDescription: String {
280+
return String(reflecting: type(of: self))
281+
}
282+
278283
/// Prints the raw value of this node to the provided stream.
279284
/// - Parameter stream: The stream to which to print the raw tree.
280285
public func write<Target>(to target: inout Target)
@@ -654,6 +659,17 @@ public struct TokenSyntax: _SyntaxBase, Hashable {
654659
}
655660
}
656661

662+
extension TokenSyntax: CustomReflectable {
663+
public var customMirror: Mirror {
664+
return Mirror(self, children: [
665+
"text": text,
666+
"leadingTrivia": leadingTrivia,
667+
"trailingTrivia": trailingTrivia,
668+
"tokenKind": tokenKind,
669+
])
670+
}
671+
}
672+
657673
/// Sequence of tokens that are part of the provided Syntax node.
658674
public struct TokenSequence: Sequence {
659675
public struct Iterator: IteratorProtocol {
@@ -695,6 +711,12 @@ public struct TokenSequence: Sequence {
695711
}
696712
}
697713

714+
extension TokenSequence: CustomReflectable {
715+
public var customMirror: Mirror {
716+
return Mirror(self, unlabeledChildren: self.map{ $0 })
717+
}
718+
}
719+
698720
/// Reverse sequence of tokens that are part of the provided Syntax node.
699721
public struct ReversedTokenSequence: Sequence {
700722
public struct Iterator: IteratorProtocol {
@@ -736,6 +758,12 @@ public struct ReversedTokenSequence: Sequence {
736758
}
737759
}
738760

761+
extension ReversedTokenSequence: CustomReflectable {
762+
public var customMirror: Mirror {
763+
return Mirror(self, unlabeledChildren: self.map{ $0 })
764+
}
765+
}
766+
739767
/// Represents a node from the syntax tree.
740768
///
741769
/// This is a more efficient representation than `Syntax` because it avoids casts

Sources/SwiftSyntax/SyntaxCollections.swift.gyb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ extension ${node.name}: Sequence {
223223
}
224224
}
225225
}
226+
% end
227+
% end
226228

229+
% for node in SYNTAX_NODES:
230+
% if node.is_syntax_collection():
231+
extension ${node.name}: CustomReflectable {
232+
public var customMirror: Mirror {
233+
return Mirror(self, unlabeledChildren: self.map{ $0 })
234+
}
235+
}
227236
% end
228237
% end

Sources/SwiftSyntax/SyntaxNodes.swift.gyb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ public struct UnknownSyntax: _SyntaxBase, Hashable {
5858
}
5959
}
6060

61+
extension UnknownSyntax: CustomReflectable {
62+
public var customMirror: Mirror {
63+
return Mirror(self, children: [:])
64+
}
65+
}
66+
6167
% for node in SYNTAX_NODES:
6268
% base_type = node.base_type
6369
% if node.is_base():
@@ -169,7 +175,20 @@ public struct ${node.name}: ${base_type}, _SyntaxBase, Hashable {
169175
return data.nodeId.hash(into: &hasher)
170176
}
171177
}
178+
% end
179+
% end
172180

181+
% for node in SYNTAX_NODES:
182+
% if not node.is_base() and not node.is_syntax_collection():
183+
extension ${node.name}: CustomReflectable {
184+
public var customMirror: Mirror {
185+
return Mirror(self, children: [
186+
% for child in node.children:
187+
"${child.swift_name}": ${child.swift_name} as Any,
188+
% end
189+
])
190+
}
191+
}
173192
% end
174193
% end
175194

Sources/SwiftSyntax/Trivia.swift.gyb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ extension TriviaPiece: TextOutputStreamable {
6262
}
6363
}
6464

65+
extension TriviaPiece: CustomDebugStringConvertible {
66+
/// Returns a description used by dump.
67+
public var debugDescription: String {
68+
return "TriviaPiece"
69+
}
70+
}
71+
6572
/// A collection of leading or trailing trivia. This is the main data structure
6673
/// for thinking about trivia.
6774
public struct Trivia {

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ XCTMain({ () -> [XCTestCaseEntry] in
1717
testCase(TokenSyntaxTestCase.allTests),
1818
testCase(SyntaxTreeModifierTests.allTests),
1919
testCase(TriviaTests.allTests),
20+
testCase(CustomReflectableTests.allTests),
2021
]
2122
return testCases
2223
}())

0 commit comments

Comments
 (0)