Skip to content

Commit bd16ff7

Browse files
committed
Add indent support
1 parent 7f3395e commit bd16ff7

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

Sources/OpenGraphShims/Attribute+Debug.swift

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,82 @@
44

55
extension Attribute: Swift.CustomDebugStringConvertible {
66
public var debugDescription: String {
7-
#"""
8-
Attribute<\#(Value.self)> {
9-
\#(identifier.debugDescription)
10-
}
7+
_debugDescription(indent: 0)
8+
}
9+
10+
func _debugDescription(indent: Int) -> String {
11+
let tabs = String(repeating: "\t", count: indent)
12+
return #"""
13+
\#(tabs)Attribute<\#(Value.self)> {
14+
\#(identifier._debugDescription(indent: indent + 1))
15+
\#(tabs)}
1116
"""#
1217
}
1318
}
1419

1520
extension AnyAttribute: Swift.CustomDebugStringConvertible {
1621
public var debugDescription: String {
22+
_debugDescription(indent: 0)
23+
}
24+
25+
func _debugDescription(indent: Int) -> String {
26+
let tabs = String(repeating: "\t", count: indent)
27+
1728
guard self != .nil else {
18-
return "AnyAttribute.nil"
29+
return "\(tabs)AnyAttribute.nil"
1930
}
31+
2032
var description = #"""
21-
rawValue: \#(rawValue)
22-
graph: \#(graph)
33+
\#(tabs)rawValue: \#(rawValue)
34+
\#(tabs)graph: \#(graph)
2335
"""#
36+
2437
if rawValue % 2 == 0 { // direct
25-
description.append("\n(direct attribute)")
38+
description.append("\n\(tabs)(direct attribute)")
2639

2740
let valueType = valueType
28-
description.append("\nvalueType: \(valueType)")
41+
description.append("\n\(tabs)valueType: \(valueType)")
2942

3043
var value: Any!
3144
func project1<T>(type: T.Type) {
3245
value = unsafeCast(to: type).value
3346
}
3447
_openExistential(valueType, do: project1)
35-
description.append("\nvalue: \(value!)")
48+
description.append("\n\(tabs)value: \(value!)")
3649

3750
let bodyType = _bodyType
38-
description.append("\nbodyType: \(bodyType)")
51+
description.append("\n\(tabs)bodyType: \(bodyType)")
3952

4053
var bodyValue: Any!
4154
func project2<T>(type: T.Type) {
4255
bodyValue = _bodyPointer.assumingMemoryBound(to: type).pointee
4356
}
4457
_openExistential(bodyType, do: project2)
45-
description.append("\nbodyValue: \(bodyValue!)")
58+
59+
let bodyValueDescription = _formatBodyValue(bodyValue!, indent: indent)
60+
description.append("\n\(tabs)bodyValue:\n\(bodyValueDescription)")
4661

47-
} else { // indrect
48-
description.append("\n(indirect attribute)")
49-
description.append("\n\nsource attribute:")
50-
description.append("\n\(source.debugDescription)")
62+
} else { // indirect
63+
description.append("\n\(tabs)(indirect attribute)")
64+
description.append("\n\(tabs)source attribute:")
65+
description.append("\n\(source._debugDescription(indent: indent + 1))")
5166
}
5267
return description
5368
}
54-
}
69+
70+
private func _formatBodyValue(_ bodyValue: Any, indent: Int) -> String {
71+
let bodyValueString = String(describing: bodyValue)
72+
let nextTabs = String(repeating: "\t", count: indent + 1)
73+
74+
// Check if the body value contains attributes
75+
if bodyValueString.contains("Attribute<") {
76+
// Split lines and add proper indentation
77+
let lines = bodyValueString.components(separatedBy: .newlines)
78+
return lines.enumerated().map { index, line in
79+
return "\(nextTabs)\(line)"
80+
}.joined(separator: "\n")
81+
}
82+
83+
return "\(nextTabs)\(bodyValueString)"
84+
}
85+
}

0 commit comments

Comments
 (0)