|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +/// A type that describes the difference between two values as a sequence of |
| 12 | +/// insertions, deletions, or unmodified elements. |
| 13 | +/// |
| 14 | +/// To ensure that ``Difference`` can always conform to `Sendable`, the elements |
| 15 | +/// in an instance of this type are stored as strings rather than as their |
| 16 | +/// original types. They are converted to strings using |
| 17 | +/// ``Swift/String/init(describingForTest:)``. Types can implement |
| 18 | +/// ``CustomTestStringConvertible`` to customize how they appear in the |
| 19 | +/// description of an instance of this type. |
| 20 | +@_spi(ExperimentalEventHandling) |
| 21 | +public struct Difference: Sendable { |
| 22 | + /// An enumeration representing the kinds of change that can occur during |
| 23 | + /// diffing. |
| 24 | + enum ElementKind: Sendable { |
| 25 | + /// The element was inserted. |
| 26 | + case insert |
| 27 | + |
| 28 | + /// The element was removed. |
| 29 | + case remove |
| 30 | + |
| 31 | + /// The element replaced a previous value. |
| 32 | + /// |
| 33 | + /// - Parameters: |
| 34 | + /// - oldValue: The old value at this position. |
| 35 | + case replace(oldValue: String) |
| 36 | + } |
| 37 | + |
| 38 | + /// A type representing an element of a collection that may have been changed |
| 39 | + /// after diffing was applied. |
| 40 | + /// |
| 41 | + /// This type roughly approximates `CollectionDifference.Change`, however it |
| 42 | + /// is used to track _all_ elements in the collection, not just those that |
| 43 | + /// have changed, allowing for insertion of "marker" elements where removals |
| 44 | + /// occurred. |
| 45 | + typealias Element = (value: String, kind: ElementKind?) |
| 46 | + |
| 47 | + /// The changed elements from the comparison. |
| 48 | + var elements = [Element]() |
| 49 | + |
| 50 | + init(elements: some Sequence<Element>) { |
| 51 | + self.elements = Array(elements) |
| 52 | + } |
| 53 | + |
| 54 | + /// Initialize an instance of this type by comparing two collections. |
| 55 | + /// |
| 56 | + /// - Parameters: |
| 57 | + /// - lhs: The "old" state of the collection to compare. |
| 58 | + /// - rhs: The "new" state of the collection to compare. |
| 59 | + /// - describingForTest: Whether or not to convert values in `lhs` and `rhs` |
| 60 | + /// to strings using ``Swift/String/init(describingForTest:)``. |
| 61 | + init<T, U>(from lhs: T, to rhs: U, describingForTest: Bool = true) |
| 62 | + where T: BidirectionalCollection, T.Element: Equatable, U: BidirectionalCollection, T.Element == U.Element { |
| 63 | + // Compute the difference between the two elements. Sort the resulting set |
| 64 | + // of changes by their offsets, and ensure that insertions come before |
| 65 | + // removals located at the same offset. This helps to ensure that the offset |
| 66 | + // values do not drift as we walk the changeset. |
| 67 | + let difference = rhs.difference(from: lhs) |
| 68 | + |
| 69 | + // Walk the initial string and slowly transform it into the final string. |
| 70 | + // Add an additional "scratch" string that is used to store a removal marker |
| 71 | + // if the last character is removed. |
| 72 | + var result: [[(value: T.Element, kind: ElementKind?)]] = lhs.map { [($0, nil)] } + CollectionOfOne([]) |
| 73 | + for change in difference.removals.reversed() { |
| 74 | + // Remove the character at the specified index, then re-insert it into the |
| 75 | + // slot at the previous index (with the marker character applied.) The |
| 76 | + // previous index will then contain whatever character it already |
| 77 | + // contained after the character representing this removal. |
| 78 | + result.remove(at: change.offset) |
| 79 | + result[change.offset].insert((change.element, kind: .remove), at: 0) |
| 80 | + } |
| 81 | + for change in difference.insertions { |
| 82 | + // Insertions can occur verbatim by inserting a new substring at the |
| 83 | + // specified offset. |
| 84 | + result.insert([(change.element, kind: .insert)], at: change.offset) |
| 85 | + } |
| 86 | + |
| 87 | + let describe: (T.Element) -> String = if describingForTest { |
| 88 | + String.init(describingForTest:) |
| 89 | + } else { |
| 90 | + String.init(describing:) |
| 91 | + } |
| 92 | + |
| 93 | + self.init( |
| 94 | + elements: result.lazy |
| 95 | + .flatMap { $0 } |
| 96 | + .map { (describe($0.value), $0.kind) } |
| 97 | + .reduce(into: []) { (result: inout _, element: Element) in |
| 98 | + if element.kind == .remove, let previous = result.last, previous.kind == .insert { |
| 99 | + result[result.index(before: result.endIndex)] = (previous.value, .replace(oldValue: element.value)) |
| 100 | + } else { |
| 101 | + result.append(element) |
| 102 | + } |
| 103 | + } |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + /// Get a string reflecting a value, similar to how it might have been |
| 108 | + /// initialized and suitable for display as part of a difference. |
| 109 | + /// |
| 110 | + /// - Parameters: |
| 111 | + /// - value: The value to reflect. |
| 112 | + /// |
| 113 | + /// - Returns: A string reflecting `value`, or `nil` if its reflection is |
| 114 | + /// trivial. |
| 115 | + /// |
| 116 | + /// This function uses `Mirror`, so if the type of `value` conforms to |
| 117 | + /// `CustomReflectable`, the resulting string will be derived from the value's |
| 118 | + /// custom mirror. |
| 119 | + private static func _reflect<T>(_ value: T) -> String? { |
| 120 | + let mirrorChildren = Mirror(reflecting: value).children |
| 121 | + if mirrorChildren.isEmpty { |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + let typeName = _typeName(T.self, qualified: true) |
| 126 | + let children = mirrorChildren.lazy |
| 127 | + .map { child in |
| 128 | + if let label = child.label { |
| 129 | + " \(label): \(String(describingForTest: child.value))" |
| 130 | + } else { |
| 131 | + " \(String(describingForTest: child.value))" |
| 132 | + } |
| 133 | + }.joined(separator: "\n") |
| 134 | + return """ |
| 135 | + \(typeName)( |
| 136 | + \(children) |
| 137 | + ) |
| 138 | + """ |
| 139 | + } |
| 140 | + |
| 141 | + /// Initialize an instance of this type by comparing the reflections of two |
| 142 | + /// values. |
| 143 | + /// |
| 144 | + /// - Parameters: |
| 145 | + /// - lhs: The "old" value to compare. |
| 146 | + /// - rhs: The "new" value to compare. |
| 147 | + init?<T, U>(comparingValue lhs: T, to rhs: U) { |
| 148 | + guard let lhsDump = Self._reflect(lhs), let rhsDump = Self._reflect(rhs) else { |
| 149 | + return nil |
| 150 | + } |
| 151 | + |
| 152 | + let lhsDumpLines = lhsDump.split(whereSeparator: \.isNewline) |
| 153 | + let rhsDumpLines = rhsDump.split(whereSeparator: \.isNewline) |
| 154 | + if lhsDumpLines.count > 1 || rhsDumpLines.count > 1 { |
| 155 | + self.init(from: lhsDumpLines, to: rhsDumpLines, describingForTest: false) |
| 156 | + } else { |
| 157 | + return nil |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// MARK: - Equatable |
| 163 | + |
| 164 | +extension Difference.ElementKind: Equatable {} |
| 165 | + |
| 166 | +// MARK: - CustomStringConvertible |
| 167 | + |
| 168 | +extension Difference: CustomStringConvertible { |
| 169 | + public var description: String { |
| 170 | + // Show individual lines of the text with leading + or - characters to |
| 171 | + // indicate insertions and removals respectively. |
| 172 | + // FIXME: better descriptive output for one-line strings. |
| 173 | + let diffCount = elements.lazy |
| 174 | + .filter { $0.kind != nil } |
| 175 | + .count |
| 176 | + return "\(diffCount.counting("change")):\n" + elements.lazy |
| 177 | + .flatMap { element in |
| 178 | + switch element.kind { |
| 179 | + case nil: |
| 180 | + [" \(element.value)"] |
| 181 | + case .insert: |
| 182 | + ["+ \(element.value)"] |
| 183 | + case .remove: |
| 184 | + ["- \(element.value)"] |
| 185 | + case let .replace(oldValue): |
| 186 | + [ |
| 187 | + "- \(oldValue)", |
| 188 | + "+ \(element.value)" |
| 189 | + ] |
| 190 | + } |
| 191 | + }.joined(separator: "\n") |
| 192 | + } |
| 193 | +} |
0 commit comments