import Foundation public enum DiffViews { /// The Actual view: the updated file with added lines tinted. /// /// A run of removed lines with no added line beside it puts its rule on the edge of /// the next surviving line (`gap: .above`), or under the last line when the run ends /// the file (`gap: .below`). A run that also added lines gets no gap rule. public static func actual(_ diff: Diff) -> [ViewLine] { var above = Set() var endsWithRemoval = false var removed = 0, added = 0 func flush(nextNew: Int?) { if removed > 0 && added == 0 { if let nextNew { above.insert(nextNew) } else { endsWithRemoval = true } } removed = 0 added = 0 } for row in diff.rows { switch row.sign { case .removed: removed += 1 case .added: added += 1 case .same: flush(nextNew: row.newNo) } } flush(nextNew: nil) let last = diff.right.last?.no return diff.right.map { line in let gap: GapEdge? = above.contains(line.no) ? .above : (endsWithRemoval && line.no == last ? .below : nil) return ViewLine(no: line.no, text: line.text, style: line.mark == .added ? .added : nil, gap: gap) } } /// A plain file with no diff, numbered from 1. public static func plain(_ text: String) -> [ViewLine] { LineDiff.droppingTrailingNewline(text).components(separatedBy: "\n").enumerated().map { ViewLine(no: $0.offset + 1, text: $0.element) } } }