@@ -160,23 +160,27 @@ public struct CodeDiff {
160160 var insideSection = SnippetDiff . Section ( oldSnippet: [ ] , newSnippet: [ ] )
161161
162162 for i in 0 ..< max ( removalSection? . lines. count ?? 0 , insertionSection? . lines. count ?? 0 ) {
163- let oldLine = removalSection? . lines [ safe: i, fallback: " " ] ?? " "
164- let newLine = insertionSection? . lines [ safe: i, fallback: " " ] ?? " "
165- let diff = diff ( text: newLine, from: oldLine)
166- insideSection. oldSnippet. append ( . init(
167- text: oldLine,
168- diff: diff. removals. compactMap { change in
169- guard case let . remove( offset, element, _) = change else { return nil }
170- return . init( offset: offset, element: element)
171- }
172- ) )
173- insideSection. newSnippet. append ( . init(
174- text: newLine,
175- diff: diff. insertions. compactMap { change in
176- guard case let . insert( offset, element, _) = change else { return nil }
177- return . init( offset: offset, element: element)
178- }
179- ) )
163+ let oldLine = removalSection? . lines [ safe: i]
164+ let newLine = insertionSection? . lines [ safe: i]
165+ let diff = diff ( text: newLine ?? " " , from: oldLine ?? " " )
166+ if let oldLine {
167+ insideSection. oldSnippet. append ( . init(
168+ text: oldLine,
169+ diff: diff. removals. compactMap { change in
170+ guard case let . remove( offset, element, _) = change else { return nil }
171+ return . init( offset: offset, element: element)
172+ }
173+ ) )
174+ }
175+ if let newLine {
176+ insideSection. newSnippet. append ( . init(
177+ text: newLine,
178+ diff: diff. insertions. compactMap { change in
179+ guard case let . insert( offset, element, _) = change else { return nil }
180+ return . init( offset: offset, element: element)
181+ }
182+ ) )
183+ }
180184 }
181185
182186 result. sections. append ( insideSection)
0 commit comments