@@ -6,7 +6,7 @@ public struct CodeDiff {
66
77 public typealias LineDiff = CollectionDifference < String >
88
9- public struct SnippetDiff : Equatable {
9+ public struct SnippetDiff : Equatable , CustomStringConvertible {
1010 public struct Change : Equatable {
1111 public var offset : Int
1212 public var element : String
@@ -20,9 +20,20 @@ public struct CodeDiff {
2020
2121 public var text : String
2222 public var diff : Diff = . unchanged
23+
24+ var description : String {
25+ switch diff {
26+ case . unchanged:
27+ return text
28+ case let . mutated( changes) :
29+ return text + " [ " + changes. map { change in
30+ return " \( change. offset) : \( change. element) "
31+ } . joined ( separator: " | " ) + " ] "
32+ }
33+ }
2334 }
2435
25- public struct Section : Equatable {
36+ public struct Section : Equatable , CustomStringConvertible {
2637 public var oldOffset : Int
2738 public var newOffset : Int
2839 public var oldSnippet : [ Line ]
@@ -31,6 +42,31 @@ public struct CodeDiff {
3142 public var isEmpty : Bool {
3243 oldSnippet. isEmpty && newSnippet. isEmpty
3344 }
45+
46+ public var description : String {
47+ """
48+ \( oldSnippet. enumerated ( ) . compactMap { item in
49+ let ( index, line) = item
50+ let lineIndex = String ( format: " %3d " , oldOffset + index) + " "
51+ switch line. diff {
52+ case . unchanged:
53+ return " \( lineIndex) | \( line. description) "
54+ case . mutated:
55+ return " \( lineIndex) | - \( line. description) "
56+ }
57+ } . joined ( separator: " \n " ) )
58+ \( newSnippet. enumerated ( ) . map { item in
59+ let ( index, line) = item
60+ let lineIndex = " " + String( format: " %3d " , newOffset + index)
61+ switch line. diff {
62+ case . unchanged:
63+ return " \( lineIndex) | \( line. description) "
64+ case . mutated:
65+ return " \( lineIndex) | + \( line. description) "
66+ }
67+ } . joined ( separator: " \n " ) )
68+ """
69+ }
3470 }
3571
3672 public var sections : [ Section ]
@@ -47,6 +83,10 @@ public struct CodeDiff {
4783 }
4884 return nil
4985 }
86+
87+ public var description : String {
88+ " Diff: \n " + sections. map ( \. description) . joined ( separator: " \n " ) + " \n "
89+ }
5090 }
5191
5292 public func diff( text: String , from oldText: String ) -> LineDiff {
0 commit comments