@@ -85,7 +85,7 @@ public struct CodeDiff {
8585 }
8686
8787 public var description : String {
88- " Diff: \n " + sections. map ( \. description) . joined ( separator: " \n " ) + " \n "
88+ " Diff: \n " + sections. map ( \. description) . joined ( separator: " \n --- \n " ) + " \n "
8989 }
9090 }
9191
@@ -154,6 +154,31 @@ public struct CodeDiff {
154154 let removalSection = removals [ safe: sectionIndex]
155155 let insertionSection = insertions [ safe: sectionIndex]
156156
157+ if removalSection == nil , insertionSection == nil {
158+ let finishingSection = SnippetDiff . Section (
159+ oldOffset: oldLineIndex,
160+ newOffset: newLineIndex,
161+ oldSnippet: {
162+ guard oldLineIndex < oldLines. endIndex else { return [ ] }
163+ return oldLines [ oldLineIndex..< oldLines. endIndex] . map {
164+ . init( text: String ( $0) , diff: . unchanged)
165+ }
166+ } ( ) ,
167+ newSnippet: {
168+ guard newLineIndex < newLines. endIndex else { return [ ] }
169+ return newLines [ newLineIndex..< newLines. endIndex] . map {
170+ . init( text: String ( $0) , diff: . unchanged)
171+ }
172+ } ( )
173+ )
174+
175+ if !finishingSection. isEmpty {
176+ result. sections. append ( finishingSection)
177+ }
178+
179+ break
180+ }
181+
157182 // handle lines before sections
158183 var beforeSection = SnippetDiff . Section (
159184 oldOffset: oldLineIndex,
@@ -162,7 +187,7 @@ public struct CodeDiff {
162187 newSnippet: [ ]
163188 )
164189
165- while oldLineIndex < ( removalSection? . offset ?? oldLines. endIndex) {
190+ while oldLineIndex < ( removalSection? . offset ?? ( sectionIndex == 0 ? 0 : oldLines. endIndex) ) {
166191 if oldLineIndex < oldLines. endIndex {
167192 beforeSection. oldSnippet. append ( . init(
168193 text: String ( oldLines [ oldLineIndex] ) ,
@@ -171,7 +196,7 @@ public struct CodeDiff {
171196 }
172197 oldLineIndex += 1
173198 }
174- while newLineIndex < ( insertionSection? . offset ?? newLines. endIndex) {
199+ while newLineIndex < ( insertionSection? . offset ?? ( sectionIndex == 0 ? 0 : newLines. endIndex) ) {
175200 if newLineIndex < newLines. endIndex {
176201 beforeSection. newSnippet. append ( . init(
177202 text: String ( newLines [ newLineIndex] ) ,
@@ -182,6 +207,7 @@ public struct CodeDiff {
182207 }
183208
184209 if !beforeSection. isEmpty {
210+ // print("before section\n\n",beforeSection)
185211 result. sections. append ( beforeSection)
186212 }
187213
@@ -218,13 +244,14 @@ public struct CodeDiff {
218244 }
219245 }
220246
221- if !insideSection. isEmpty {
222- result. sections. append ( insideSection)
223- }
224-
225247 oldLineIndex += removalSection? . lines. count ?? 0
226248 newLineIndex += insertionSection? . lines. count ?? 0
227249 sectionIndex += 1
250+
251+ if !insideSection. isEmpty {
252+ // print("inside section\n\n", insideSection)
253+ result. sections. append ( insideSection)
254+ }
228255 }
229256
230257 return result
0 commit comments