|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | +import SwiftSyntax |
| 15 | +import SwiftSyntaxBuilder |
| 16 | + |
| 17 | +let formatFile = SourceFile { |
| 18 | + ImportDecl( |
| 19 | + leadingTrivia: .docLineComment(copyrightHeader), |
| 20 | + path: "SwiftSyntax" |
| 21 | + ) |
| 22 | + |
| 23 | + StructDecl(modifiers: [TokenSyntax.public], identifier: "Format") { |
| 24 | + VariableDecl( |
| 25 | + modifiers: [TokenSyntax.public], |
| 26 | + .let, |
| 27 | + name: "indentWidth", |
| 28 | + type: "Int" |
| 29 | + ) |
| 30 | + |
| 31 | + VariableDecl( |
| 32 | + modifiers: [TokenSyntax.private], |
| 33 | + .var, |
| 34 | + name: "indents", |
| 35 | + type: "Int", |
| 36 | + initializer: IntegerLiteralExpr(0) |
| 37 | + ) |
| 38 | + |
| 39 | + InitializerDecl( |
| 40 | + modifiers: [TokenSyntax.public], |
| 41 | + signature: FunctionSignature( |
| 42 | + input: ParameterClause { |
| 43 | + FunctionParameter( |
| 44 | + firstName: .identifier("indentWidth"), |
| 45 | + colon: .colon, |
| 46 | + type: "Int", |
| 47 | + defaultArgument: IntegerLiteralExpr(4) |
| 48 | + ) |
| 49 | + } |
| 50 | + ) |
| 51 | + ) { |
| 52 | + SequenceExpr { |
| 53 | + MemberAccessExpr(base: "self", name: "indentWidth") |
| 54 | + AssignmentExpr() |
| 55 | + "indentWidth" |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + ExtensionDecl(extendedType: "Format") { |
| 61 | + FunctionDecl( |
| 62 | + modifiers: [TokenSyntax.public], |
| 63 | + identifier: .identifier("_indented"), |
| 64 | + signature: FunctionSignature( |
| 65 | + input: ParameterClause(), |
| 66 | + output: "Format" |
| 67 | + ) |
| 68 | + ) { |
| 69 | + VariableDecl(.var, name: "copy", initializer: "self") |
| 70 | + SequenceExpr { |
| 71 | + MemberAccessExpr(base: "copy", name: "indents") |
| 72 | + BinaryOperatorExpr("+=") |
| 73 | + IntegerLiteralExpr(1) |
| 74 | + } |
| 75 | + ReturnStmt(expression: "copy") |
| 76 | + } |
| 77 | + |
| 78 | + FunctionDecl( |
| 79 | + modifiers: [TokenSyntax.public], |
| 80 | + identifier: .identifier("_makeIndent"), |
| 81 | + signature: FunctionSignature( |
| 82 | + input: ParameterClause(), |
| 83 | + output: "Trivia" |
| 84 | + ) |
| 85 | + ) { |
| 86 | + // TODO: Use sugared TernaryExpr once https://github.com/apple/swift-syntax/pull/610 is merged |
| 87 | + ReturnStmt(expression: TernaryExpr( |
| 88 | + conditionExpression: SequenceExpr { |
| 89 | + "indents" |
| 90 | + BinaryOperatorExpr("==") |
| 91 | + IntegerLiteralExpr(0) |
| 92 | + }, |
| 93 | + questionMark: .infixQuestionMark.withLeadingTrivia(.space).withTrailingTrivia(.space), |
| 94 | + firstChoice: MemberAccessExpr(name: "zero"), |
| 95 | + colonMark: .colon.withLeadingTrivia(.space).withTrailingTrivia(.space), |
| 96 | + secondChoice: FunctionCallExpr(MemberAccessExpr(base: "Trivia", name: "spaces")) { |
| 97 | + TupleExprElement(expression: SequenceExpr { |
| 98 | + "indents" |
| 99 | + BinaryOperatorExpr("*") |
| 100 | + "indentWidth" |
| 101 | + }) |
| 102 | + } |
| 103 | + )) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments