|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 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 | +#if swift(>=6) |
| 14 | +public import SwiftSyntax |
| 15 | +#else |
| 16 | +import SwiftSyntax |
| 17 | +#endif |
| 18 | + |
| 19 | +extension SyntaxProtocol { |
| 20 | + /// Indent this node’s lines by the provided amount. |
| 21 | + /// |
| 22 | + /// - Parameter indentFirstLine: Whether the first token of this node should be indented. |
| 23 | + /// Pass `true` if you know that this node will be placed at the beginning of a line, even if its |
| 24 | + /// current leading trivia does not start with a newline (such as at the very start of a file). |
| 25 | + public func indented(by indentation: Trivia, indentFirstLine: Bool = false) -> Self { |
| 26 | + Indenter(indentation: indentation, indentFirstLine: indentFirstLine) |
| 27 | + .rewrite(self) |
| 28 | + .cast(Self.self) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +private class Indenter: SyntaxRewriter { |
| 33 | + private let indentation: Trivia |
| 34 | + private var shouldIndent: Bool |
| 35 | + |
| 36 | + init(indentation: Trivia, indentFirstLine: Bool) { |
| 37 | + self.indentation = indentation |
| 38 | + self.shouldIndent = indentFirstLine |
| 39 | + } |
| 40 | + |
| 41 | + private func indentationIfNeeded() -> [TriviaPiece] { |
| 42 | + if shouldIndent { |
| 43 | + shouldIndent = false |
| 44 | + return indentation.pieces |
| 45 | + } else { |
| 46 | + return [] |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private func indentAfterNewlines(_ content: String) -> String { |
| 51 | + content.split(separator: "\n").joined(separator: "\n" + indentation.description) |
| 52 | + } |
| 53 | + |
| 54 | + private func indent(_ trivia: Trivia, skipEmpty: Bool) -> Trivia { |
| 55 | + if skipEmpty, trivia.isEmpty { return trivia } |
| 56 | + |
| 57 | + var result: [TriviaPiece] = [] |
| 58 | + // most times, we won’t have anything to insert so this will |
| 59 | + // reserve enough space |
| 60 | + result.reserveCapacity(trivia.count) |
| 61 | + |
| 62 | + for piece in trivia.pieces { |
| 63 | + result.append(contentsOf: indentationIfNeeded()) |
| 64 | + switch piece { |
| 65 | + case .newlines, .carriageReturns, .carriageReturnLineFeeds: |
| 66 | + shouldIndent = true |
| 67 | + // style decision: don’t indent totally blank lines |
| 68 | + result.append(piece) |
| 69 | + case .blockComment(let content): |
| 70 | + result.append(.blockComment(indentAfterNewlines(content))) |
| 71 | + case .docBlockComment(let content): |
| 72 | + result.append(.docBlockComment(indentAfterNewlines(content))) |
| 73 | + case .unexpectedText(let content): |
| 74 | + result.append(.unexpectedText(indentAfterNewlines(content))) |
| 75 | + default: |
| 76 | + result.append(piece) |
| 77 | + } |
| 78 | + } |
| 79 | + result.append(contentsOf: indentationIfNeeded()) |
| 80 | + return Trivia(pieces: result) |
| 81 | + } |
| 82 | + |
| 83 | + override func visit(_ token: TokenSyntax) -> TokenSyntax { |
| 84 | + let indentedLeadingTrivia = indent(token.leadingTrivia, skipEmpty: false) |
| 85 | + |
| 86 | + // compute this before indenting the trailing trivia since the |
| 87 | + // newline here is before the start of the trailing trivia (since |
| 88 | + // it is part of the string’s value) |
| 89 | + if case .stringSegment(let content) = token.tokenKind, |
| 90 | + let last = content.last, |
| 91 | + last.isNewline |
| 92 | + { |
| 93 | + shouldIndent = true |
| 94 | + } |
| 95 | + |
| 96 | + return |
| 97 | + token |
| 98 | + .with(\.leadingTrivia, indentedLeadingTrivia) |
| 99 | + // source files as parsed can’t have anything requiring indentation |
| 100 | + // here, but it’s easy to do `.with(\.trailingTrivia, .newline)` so |
| 101 | + // we should still check if there’s something to indent. |
| 102 | + .with(\.trailingTrivia, indent(token.trailingTrivia, skipEmpty: true)) |
| 103 | + } |
| 104 | +} |
0 commit comments