Sources/OrgApp/DiffView.swift
123 lines · 4697 bytes
1import OrgDocument
2import SwiftUI
3
4/// Lines removed and added between two versions, with a few lines of context around each change.
5public struct DiffView: View {
6 let lines: [DiffLine]
7 var context = 3
8 let removedColor: Color
9 let addedColor: Color
10 let foreground: Color?
11 let background: Color?
12 @Environment(\.colorSchemeContrast) private var contrast
13 @Environment(\.accessibilityDifferentiateWithoutColor) private var withoutColor
14 @ScaledMetric(relativeTo: .caption) private var wordWidth: CGFloat = 64
15
16 /// `removed` and `added` tint the changed lines; `foreground` and `background` default to
17 /// the system's text colours.
18 public init(old: String, new: String, removed: Color = .red, added: Color = .green, foreground: Color? = nil, background: Color? = nil) {
19 lines = lineDiff(old: old, new: new)
20 removedColor = removed
21 addedColor = added
22 self.foreground = foreground
23 self.background = background
24 }
25
26 /// Lines to show, nil standing for a run of unchanged lines left out.
27 private var shown: [DiffLine?] {
28 let changed = lines.indices.filter { lines[$0].kind != .same }
29 guard !changed.isEmpty else { return [] }
30 var keep = Set<Int>()
31 for i in changed { keep.formUnion(max(0, i - context)...min(lines.count - 1, i + context)) }
32 var out: [DiffLine?] = []
33 var skipped = false
34 for i in lines.indices {
35 if keep.contains(i) {
36 out.append(lines[i])
37 skipped = false
38 } else if !skipped {
39 out.append(nil)
40 skipped = true
41 }
42 }
43 return out
44 }
45
46 public var body: some View {
47 let shown = shown
48 ScrollView([.vertical, .horizontal]) {
49 VStack(alignment: .leading, spacing: 0) {
50 if shown.isEmpty { Text("No differences").foregroundStyle(.secondary).padding(8) }
51 ForEach(shown.indices, id: \.self) { i in
52 if let line = shown[i] {
53 row(line)
54 } else {
55 Text("⋯").foregroundStyle(.secondary)
56 }
57 }
58 }
59 #if os(macOS)
60 .font(.system(.callout, design: .monospaced))
61 #else
62 .font(.system(.footnote, design: .monospaced))
63 #endif
64 .foregroundStyle(foreground ?? .primary)
65 .textSelection(.enabled)
66 .padding(8)
67 }
68 .defaultScrollAnchor(.topLeading)
69 #if os(macOS)
70 .background(background ?? Color(nsColor: .textBackgroundColor))
71 #else
72 .background(background ?? Color(uiColor: .secondarySystemBackground))
73 #endif
74 }
75
76 private func row(_ line: DiffLine) -> some View {
77 let text = line.text.trimmingCharacters(in: .newlines)
78 return HStack(spacing: 0) {
79 // Differentiate Without Colour: the change in words before the line.
80 if withoutColor {
81 Text(word(line.kind) ?? "")
82 .font(.caption.weight(.semibold))
83 .frame(width: wordWidth, alignment: .leading)
84 }
85 Text(prefix(line.kind) + text)
86 }
87 .frame(maxWidth: .infinity, alignment: .leading)
88 .background(tint(line.kind))
89 .overlay(alignment: .leading) {
90 // Increase Contrast: a solid bar at the edge of each changed line.
91 if contrast == .increased, line.kind != .same {
92 Rectangle().fill(line.kind == .removed ? removedColor : addedColor).frame(width: 3)
93 }
94 }
95 .accessibilityElement(children: .ignore)
96 .accessibilityLabel(word(line.kind).map { "\($0): \(text)" } ?? text)
97 }
98
99 private func prefix(_ kind: DiffLine.Kind) -> String {
100 switch kind {
101 case .same: " "
102 case .removed: "- "
103 case .added: "+ "
104 }
105 }
106
107 private func word(_ kind: DiffLine.Kind) -> String? {
108 switch kind {
109 case .same: nil
110 case .removed: String(localized: "diff.removed", defaultValue: "Removed", comment: "A line removed, in a comparison of two versions")
111 case .added: String(localized: "diff.added", defaultValue: "Added", comment: "A line added, in a comparison of two versions")
112 }
113 }
114
115 private func tint(_ kind: DiffLine.Kind) -> Color {
116 let strength = contrast == .increased ? 0.35 : 0.18
117 return switch kind {
118 case .same: .clear
119 case .removed: removedColor.opacity(strength)
120 case .added: addedColor.opacity(strength)
121 }
122 }
123}