127 lines
4.7 KiB
Swift
127 lines
4.7 KiB
Swift
import Foundation
|
|
|
|
/// A group in the Git view's change list, in the order the list shows them.
|
|
public enum GitSectionKind: String, CaseIterable, Sendable {
|
|
case staged
|
|
case changed
|
|
case untracked
|
|
case deleted
|
|
}
|
|
|
|
public struct GitSection: Sendable, Equatable, Identifiable {
|
|
public var kind: GitSectionKind
|
|
public var rows: [Change]
|
|
|
|
public init(kind: GitSectionKind, rows: [Change]) {
|
|
self.kind = kind
|
|
self.rows = rows
|
|
}
|
|
|
|
public var id: String { kind.rawValue }
|
|
}
|
|
|
|
public enum GitSections {
|
|
/// Sorts rows into their sections; only non-empty sections come back and rows keep their order.
|
|
///
|
|
/// Every staged row is staged, whatever its status; an unstaged rename or conflict counts as changed.
|
|
public static func build(_ changes: [Change]) -> [GitSection] {
|
|
var byKind: [GitSectionKind: [Change]] = [:]
|
|
for change in changes {
|
|
byKind[kind(of: change), default: []].append(change)
|
|
}
|
|
return GitSectionKind.allCases.compactMap { kind in
|
|
byKind[kind].map { GitSection(kind: kind, rows: $0) }
|
|
}
|
|
}
|
|
|
|
/// Distinct paths of the rows, in order.
|
|
public static func uniquePaths(_ rows: [Change]) -> [String] {
|
|
var seen = Set<String>()
|
|
return rows.compactMap { seen.insert($0.path).inserted ? $0.path : nil }
|
|
}
|
|
|
|
private static func kind(of change: Change) -> GitSectionKind {
|
|
if change.staged { return .staged }
|
|
switch change.status {
|
|
case .added: return .untracked
|
|
case .deleted: return .deleted
|
|
case .modified, .renamed, .unmerged: return .changed
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One side of an inline diff line.
|
|
public struct InlineDiffCell: Sendable, Equatable {
|
|
public var number: Int
|
|
public var text: String
|
|
public var mark: LineMark?
|
|
/// Character offsets of the part that differs from the other side; nil when the whole line counts.
|
|
public var highlight: Range<Int>?
|
|
|
|
public init(number: Int, text: String, mark: LineMark? = nil, highlight: Range<Int>? = nil) {
|
|
self.number = number
|
|
self.text = text
|
|
self.mark = mark
|
|
self.highlight = highlight
|
|
}
|
|
}
|
|
|
|
public enum InlineDiffRow: Sendable, Equatable {
|
|
case line(left: InlineDiffCell?, right: InlineDiffCell?)
|
|
/// A run of unchanged rows folded away.
|
|
case gap(Int)
|
|
}
|
|
|
|
public enum InlineDiff {
|
|
/// The rows of `diff.split` with `context` unchanged rows kept around each change; other unchanged runs fold into a gap.
|
|
public static func rows(from diff: Diff, context: Int = 3) -> [InlineDiffRow] {
|
|
let split = diff.split
|
|
let changed = split.map { $0.left?.mark != nil || $0.right?.mark != nil }
|
|
guard changed.contains(true) else { return [] }
|
|
var keep = [Bool](repeating: false, count: split.count)
|
|
for (index, isChanged) in changed.enumerated() where isChanged {
|
|
let low = max(0, index - context), high = min(split.count - 1, index + context)
|
|
for kept in low...high { keep[kept] = true }
|
|
}
|
|
var rows: [InlineDiffRow] = []
|
|
var folded = 0
|
|
for (index, row) in split.enumerated() {
|
|
guard keep[index] else {
|
|
folded += 1
|
|
continue
|
|
}
|
|
if folded > 0 { rows.append(.gap(folded)) }
|
|
folded = 0
|
|
rows.append(line(row))
|
|
}
|
|
if folded > 0 { rows.append(.gap(folded)) }
|
|
return rows
|
|
}
|
|
|
|
private static func line(_ row: SplitRow) -> InlineDiffRow {
|
|
var left = row.left.map { InlineDiffCell(number: $0.no, text: $0.text, mark: $0.mark) }
|
|
var right = row.right.map { InlineDiffCell(number: $0.no, text: $0.text, mark: $0.mark) }
|
|
if let old = left, let new = right, old.mark == .removed, new.mark == .added {
|
|
let ranges = highlights(old.text, new.text)
|
|
left?.highlight = ranges.left
|
|
right?.highlight = ranges.right
|
|
}
|
|
return .line(left: left, right: right)
|
|
}
|
|
|
|
/// The middle between the common prefix and the common suffix, per side; nil when nothing is shared or a middle is empty.
|
|
static func highlights(_ old: String, _ new: String) -> (left: Range<Int>?, right: Range<Int>?) {
|
|
let a = Array(old), b = Array(new)
|
|
var prefix = 0
|
|
while prefix < a.count && prefix < b.count && a[prefix] == b[prefix] { prefix += 1 }
|
|
var suffix = 0
|
|
while suffix < a.count - prefix && suffix < b.count - prefix && a[a.count - 1 - suffix] == b[b.count - 1 - suffix] {
|
|
suffix += 1
|
|
}
|
|
guard prefix > 0 || suffix > 0 else { return (nil, nil) }
|
|
let left = prefix..<(a.count - suffix)
|
|
let right = prefix..<(b.count - suffix)
|
|
return (left.isEmpty ? nil : left, right.isEmpty ? nil : right)
|
|
}
|
|
}
|