in Sources/SwiftFormatPrettyPrint/Verbatim.swift [39:77]
init(text: String, indentingBehavior: IndentingBehavior) {
self.indentingBehavior = indentingBehavior
var originalLines = text.split(separator: "\n", omittingEmptySubsequences: false)
// Prevents an extra leading new line from being created.
if originalLines[0].isEmpty {
originalLines.remove(at: 0)
}
// If we have no lines left (or none with any content), just initialize everything empty and
// exit.
guard
!originalLines.isEmpty,
let index = originalLines.firstIndex(where: { !$0.isEmpty })
else {
self.lines = []
self.leadingWhitespaceCounts = []
return
}
// If our indenting behavior is `none`, then keep the original lines _exactly_ as is---don't
// attempt to calculate or trim their leading indentation.
guard indentingBehavior != .none else {
self.lines = originalLines.map(String.init)
self.leadingWhitespaceCounts = [Int](repeating: 0, count: originalLines.count)
return
}
// Otherwise, we're in one of the indentation compensating modes. Get the number of leading
// whitespaces of the first line, and subtract this from the number of leading whitespaces for
// subsequent lines (if possible). Record the new leading whitespaces counts, and trim off
// whitespace from the ends of the strings.
let firstLineLeadingSpaceCount = numberOfLeadingSpaces(in: originalLines[index])
self.leadingWhitespaceCounts = originalLines.map {
max(numberOfLeadingSpaces(in: $0) - firstLineLeadingSpaceCount, 0)
}
self.lines = originalLines.map { $0.trimmingCharacters(in: CharacterSet(charactersIn: " ")) }
}