|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 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 | +import SwiftSyntax |
| 14 | + |
| 15 | +extension BasicFormat { |
| 16 | + /// Uses heuristics to infer the indentation width used in the given syntax tree. |
| 17 | + /// |
| 18 | + /// Returns `nil` if the indentation could not be inferred, eg. because it is inconsistent or there are not enough |
| 19 | + /// indented lines to infer the indentation with sufficient accuracy. |
| 20 | + public static func inferIndentation(of tree: some SyntaxProtocol) -> Trivia? { |
| 21 | + return IndentationInferrer.inferIndentation(of: tree) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +private class IndentationInferrer: SyntaxVisitor { |
| 26 | + /// The trivia of the previous visited token. |
| 27 | + /// |
| 28 | + /// The previous token's trailing trivia will be concatenated with the current token's leading trivia to infer |
| 29 | + /// indentation. |
| 30 | + /// |
| 31 | + /// We start with .newline to indicate that the first token starts on a newline, even if it technically doesn't have |
| 32 | + /// a leading newline character. |
| 33 | + private var previousTokenTrailingTrivia: Trivia = .newline |
| 34 | + |
| 35 | + /// Counts how many lines had how many spaces of indentation. |
| 36 | + /// |
| 37 | + /// For example, spaceIndentedLines[2] = 4 means that for lines had exactly 2 spaces of indentation. |
| 38 | + private var spaceIndentedLines: [Int: Int] = [:] |
| 39 | + |
| 40 | + /// See `spaceIndentedLines` |
| 41 | + private var tabIndentedLines: [Int: Int] = [:] |
| 42 | + |
| 43 | + /// The number of lines that were processed for indentation inference. |
| 44 | + /// |
| 45 | + /// This will be lower than the actual number of lines in the syntax node because |
| 46 | + /// - It does not count lines without indentation |
| 47 | + //// - It does not count newlines in block doc comments (because we don't process the comment's contents) |
| 48 | + private var linesProcessed = 0 |
| 49 | + |
| 50 | + override func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind { |
| 51 | + defer { previousTokenTrailingTrivia = token.trailingTrivia } |
| 52 | + let triviaAtStartOfLine = |
| 53 | + (previousTokenTrailingTrivia + token.leadingTrivia) |
| 54 | + .drop(while: { !$0.isNewline }) // Ignore any trivia that's on the previous line |
| 55 | + .split(omittingEmptySubsequences: false, whereSeparator: \.isNewline) // Split trivia into the lines it occurs on |
| 56 | + .dropFirst() // Drop the first empty array; exists because we dropped non-newline prefix and newline is separator |
| 57 | + |
| 58 | + LINE_TRIVIA_LOOP: for lineTrivia in triviaAtStartOfLine { |
| 59 | + switch lineTrivia.first { |
| 60 | + case .spaces(var spaces): |
| 61 | + linesProcessed += 1 |
| 62 | + for triviaPiece in lineTrivia.dropFirst() { |
| 63 | + switch triviaPiece { |
| 64 | + case .spaces(let followupSpaces): spaces += followupSpaces |
| 65 | + case .tabs: break LINE_TRIVIA_LOOP // Count as processed line but don't add to any indentation count |
| 66 | + default: break |
| 67 | + } |
| 68 | + } |
| 69 | + spaceIndentedLines[spaces, default: 0] += 1 |
| 70 | + case .tabs(var tabs): |
| 71 | + linesProcessed += 1 |
| 72 | + for triviaPiece in lineTrivia.dropFirst() { |
| 73 | + switch triviaPiece { |
| 74 | + case .tabs(let followupTabs): tabs += followupTabs |
| 75 | + case .spaces: break LINE_TRIVIA_LOOP // Count as processed line but don't add to any indentation count |
| 76 | + default: break |
| 77 | + } |
| 78 | + } |
| 79 | + tabIndentedLines[tabs, default: 0] += 1 |
| 80 | + default: |
| 81 | + break |
| 82 | + } |
| 83 | + } |
| 84 | + return .skipChildren |
| 85 | + } |
| 86 | + |
| 87 | + static func inferIndentation(of tree: some SyntaxProtocol) -> Trivia? { |
| 88 | + let visitor = IndentationInferrer(viewMode: .sourceAccurate) |
| 89 | + visitor.walk(tree) |
| 90 | + if visitor.linesProcessed < 3 { |
| 91 | + // We don't have enough lines to infer indentation reliably |
| 92 | + return nil |
| 93 | + } |
| 94 | + |
| 95 | + // Pick biggest indentation that encompasses at least 90% of the source lines. |
| 96 | + let threshold = Int(Double(visitor.linesProcessed) * 0.9) |
| 97 | + |
| 98 | + for spaceIndentation in [8, 4, 2] { |
| 99 | + let linesMatchingIndentation = visitor |
| 100 | + .spaceIndentedLines |
| 101 | + .filter { $0.key.isMultiple(of: spaceIndentation) } |
| 102 | + .map { $0.value } |
| 103 | + .sum |
| 104 | + if linesMatchingIndentation > threshold { |
| 105 | + return .spaces(spaceIndentation) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + for tabIndentation in [2, 1] { |
| 110 | + let linesMatchingIndentation = visitor |
| 111 | + .tabIndentedLines |
| 112 | + .filter { $0.key.isMultiple(of: tabIndentation) } |
| 113 | + .map { $0.value } |
| 114 | + .sum |
| 115 | + if linesMatchingIndentation > threshold { |
| 116 | + return .tabs(tabIndentation) |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +fileprivate extension Array<Int> { |
| 124 | + var sum: Int { |
| 125 | + return self.reduce(0) { return $0 + $1 } |
| 126 | + } |
| 127 | +} |
0 commit comments