|
| 1 | +import SwiftBasicFormat |
| 2 | +@_spi(RawSyntax) import SwiftSyntax |
| 3 | +import SwiftSyntaxBuilder |
| 4 | + |
| 5 | +private class InitializerExprFormat: BasicFormat { |
| 6 | + override var indentation: TriviaPiece { return .spaces(indentationLevel * 2) } |
| 7 | + |
| 8 | + private func formatChildrenSeparatedByNewline<SyntaxType: SyntaxProtocol>(children: SyntaxChildren, elementType: SyntaxType.Type) -> [SyntaxType] { |
| 9 | + indentationLevel += 1 |
| 10 | + var formattedChildren = children.map { |
| 11 | + self.visit($0).as(SyntaxType.self)! |
| 12 | + } |
| 13 | + formattedChildren = formattedChildren.map { |
| 14 | + if $0.leadingTrivia?.first?.isNewline == true { |
| 15 | + return $0 |
| 16 | + } else { |
| 17 | + return $0.withLeadingTrivia(indentedNewline + ($0.leadingTrivia ?? [])) |
| 18 | + } |
| 19 | + } |
| 20 | + indentationLevel -= 1 |
| 21 | + if !formattedChildren.isEmpty { |
| 22 | + formattedChildren[formattedChildren.count - 1] = formattedChildren[formattedChildren.count - 1].withTrailingTrivia(indentedNewline) |
| 23 | + } |
| 24 | + return formattedChildren |
| 25 | + } |
| 26 | + |
| 27 | + override func visit(_ node: TupleExprElementListSyntax) -> Syntax { |
| 28 | + let children = node.children(viewMode: .all) |
| 29 | + // If the function only takes a single argument, display it on the same line |
| 30 | + if children.count > 1 { |
| 31 | + return Syntax(TupleExprElementListSyntax(formatChildrenSeparatedByNewline(children: children, elementType: TupleExprElementSyntax.self))) |
| 32 | + } else { |
| 33 | + return super.visit(node) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + override func visit(_ node: ArrayElementListSyntax) -> Syntax { |
| 38 | + let children = node.children(viewMode: .all) |
| 39 | + // Short array literals are presented on one line, list each element on a different line. |
| 40 | + if node.description.count > 30 { |
| 41 | + return Syntax(ArrayElementListSyntax(formatChildrenSeparatedByNewline(children: children, elementType: ArrayElementSyntax.self))) |
| 42 | + } else { |
| 43 | + return super.visit(node) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +private extension TriviaPiece { |
| 49 | + var initializerExpr: ExprBuildable { |
| 50 | + let (label, value) = Mirror(reflecting: self).children.first! |
| 51 | + switch value { |
| 52 | + case let value as String: |
| 53 | + return FunctionCallExpr(calledExpression: MemberAccessExpr(name: label!)) { |
| 54 | + TupleExprElement(expression: StringLiteralExpr(value)) |
| 55 | + } |
| 56 | + case let value as Int: |
| 57 | + return FunctionCallExpr(calledExpression: MemberAccessExpr(name: label!)) { |
| 58 | + TupleExprElement(expression: IntegerLiteralExpr(value)) |
| 59 | + } |
| 60 | + default: |
| 61 | + fatalError("Unknown associated value type") |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +private extension Trivia { |
| 67 | + var initializerExpr: ExprBuildable { |
| 68 | + if pieces.count == 1 { |
| 69 | + switch pieces.first { |
| 70 | + case .spaces(1): |
| 71 | + return MemberAccessExpr(name: "space") |
| 72 | + case .newlines(1): |
| 73 | + return MemberAccessExpr(name: "newline") |
| 74 | + default: |
| 75 | + break |
| 76 | + } |
| 77 | + } |
| 78 | + return ArrayExpr() { |
| 79 | + for piece in pieces { |
| 80 | + ArrayElement(expression: piece.initializerExpr) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +extension SyntaxProtocol { |
| 87 | + /// Returns a Swift expression that, when parsed, constructs this syntax node |
| 88 | + /// (or at least an expression that's very close to constructing this node, the addition of a few manual upcast by hand is still needed). |
| 89 | + /// The intended use case for this is to print a syntax tree and create a substructure assertion from the generated expression. |
| 90 | + public var debugInitCall: String { |
| 91 | + return self.debugInitCallExpr.build(format: InitializerExprFormat()).description |
| 92 | + } |
| 93 | + |
| 94 | + private var debugInitCallExpr: ExprBuildable { |
| 95 | + let mirror = Mirror(reflecting: self) |
| 96 | + if self.isCollection { |
| 97 | + return FunctionCallExpr(calledExpression: "\(type(of: self))") { |
| 98 | + TupleExprElement( |
| 99 | + expression: ArrayExpr() { |
| 100 | + for child in mirror.children { |
| 101 | + let value = child.value as! SyntaxProtocol? |
| 102 | + ArrayElement(expression: value?.debugInitCallExpr ?? NilLiteralExpr()) |
| 103 | + } |
| 104 | + } |
| 105 | + ) |
| 106 | + } |
| 107 | + } else if let token = Syntax(self).as(TokenSyntax.self) { |
| 108 | + let tokenKind = token.tokenKind |
| 109 | + let tokenInitializerName: String |
| 110 | + let requiresExplicitText: Bool |
| 111 | + if tokenKind.isKeyword || tokenKind == .eof { |
| 112 | + tokenInitializerName = String(describing: tokenKind) |
| 113 | + requiresExplicitText = false |
| 114 | + } else if tokenKind.decomposeToRaw().rawKind.defaultText != nil { |
| 115 | + tokenInitializerName = "\(String(describing: tokenKind))Token" |
| 116 | + requiresExplicitText = false |
| 117 | + } else { |
| 118 | + let tokenKindStr = String(describing: tokenKind) |
| 119 | + tokenInitializerName = String(tokenKindStr[..<tokenKindStr.firstIndex(of: "(")!]) |
| 120 | + requiresExplicitText = true |
| 121 | + } |
| 122 | + return FunctionCallExpr(calledExpression: MemberAccessExpr(name: tokenInitializerName)) { |
| 123 | + if requiresExplicitText { |
| 124 | + TupleExprElement( |
| 125 | + expression: StringLiteralExpr(token.text) |
| 126 | + ) |
| 127 | + } |
| 128 | + if !token.leadingTrivia.isEmpty { |
| 129 | + TupleExprElement( |
| 130 | + label: .identifier("leadingTrivia"), |
| 131 | + colon: .colon, |
| 132 | + expression: token.leadingTrivia.initializerExpr |
| 133 | + ) |
| 134 | + } |
| 135 | + if !token.trailingTrivia.isEmpty { |
| 136 | + TupleExprElement( |
| 137 | + label: .identifier("trailingTrivia"), |
| 138 | + colon: .colon, |
| 139 | + expression: token.trailingTrivia.initializerExpr |
| 140 | + ) |
| 141 | + } |
| 142 | + if token.presence != .present { |
| 143 | + TupleExprElement( |
| 144 | + label: .identifier("presence"), |
| 145 | + colon: .colon, |
| 146 | + expression: MemberAccessExpr(name: "missing") |
| 147 | + ) |
| 148 | + } |
| 149 | + } |
| 150 | + } else { |
| 151 | + return FunctionCallExpr(calledExpression: "\(type(of: self))") { |
| 152 | + for child in mirror.children { |
| 153 | + let label = child.label! |
| 154 | + let value = child.value as! SyntaxProtocol? |
| 155 | + let isUnexpected = label.hasPrefix("unexpected") |
| 156 | + if !isUnexpected || value != nil { |
| 157 | + TupleExprElement( |
| 158 | + label: isUnexpected ? nil : .identifier(label), |
| 159 | + colon: isUnexpected ? nil : .colon, |
| 160 | + expression: value?.debugInitCallExpr ?? NilLiteralExpr() |
| 161 | + ) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments