-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDiffFileView.swift
More file actions
261 lines (229 loc) · 8.58 KB
/
DiffFileView.swift
File metadata and controls
261 lines (229 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import SwiftUI
struct DiffFileView: View {
static let collapseForLargeFileThreshold = 2_000
static let initialRenderedLinesForLargeFile = 1_000
let file: UnifiedDiffFile
@Binding var isExpanded: Bool
@State private var showRemainingLines = false
private var totalRenderedLines: Int {
file.renderedLineCount
}
private var shouldPaginate: Bool {
totalRenderedLines > Self.collapseForLargeFileThreshold
}
private var visibleLineCount: Int {
if showRemainingLines || !shouldPaginate {
return totalRenderedLines
}
return Self.initialRenderedLinesForLargeFile
}
private var paginationRemainder: Int {
max(0, totalRenderedLines - visibleLineCount)
}
private var displayedHunks: [UnifiedDiffHunk] {
guard !file.isBinary else { return [] }
if showRemainingLines || !shouldPaginate {
return file.hunks
}
var remaining = visibleLineCount
var trimmed: [UnifiedDiffHunk] = []
for hunk in file.hunks {
guard remaining > 0 else { break }
if hunk.lines.count <= remaining {
trimmed.append(hunk)
remaining -= hunk.lines.count
continue
}
let partialLines = Array(hunk.lines.prefix(remaining))
trimmed.append(UnifiedDiffHunk(
oldStart: hunk.oldStart,
oldCount: hunk.oldCount,
newStart: hunk.newStart,
newCount: hunk.newCount,
header: hunk.header,
lines: partialLines
))
remaining = 0
}
return trimmed
}
var body: some View {
VStack(spacing: 0) {
header
if isExpanded {
bodyContent
}
}
.background(Theme.surface1)
.clipShape(RoundedRectangle(cornerRadius: 8))
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Theme.border, lineWidth: 1)
)
.accessibilityIdentifier("diffFile.section.\(safeAccessibilityID(file.id))")
.onChange(of: file.id) { _ in
showRemainingLines = false
}
}
private var header: some View {
HStack(spacing: 8) {
Button {
isExpanded.toggle()
} label: {
HStack(spacing: 8) {
Image(systemName: isExpanded ? "chevron.down" : "chevron.right")
.font(.system(size: 10, weight: .bold))
.foregroundColor(Theme.textTertiary)
statusBadge
Text(file.path)
.font(.system(size: 12, weight: .semibold, design: .monospaced))
.foregroundColor(Theme.diffFileFg)
.lineLimit(1)
.truncationMode(.middle)
if file.status == .renamed, let oldPath = file.oldPath, !oldPath.isEmpty {
Text("(from \(oldPath))")
.font(.system(size: 10, design: .monospaced))
.foregroundColor(Theme.textTertiary)
.lineLimit(1)
.truncationMode(.middle)
}
}
}
.buttonStyle(.plain)
.accessibilityIdentifier("diffFile.toggle.\(safeAccessibilityID(file.id))")
Spacer()
if file.isBinary {
Text("Binary")
.font(.system(size: 10, weight: .bold))
.foregroundColor(Theme.warning)
.accessibilityIdentifier("diffFile.binaryBadge.\(safeAccessibilityID(file.id))")
}
if file.additions > 0 {
Text("+\(file.additions)")
.font(.system(size: 10, weight: .bold, design: .monospaced))
.foregroundColor(Theme.diffAddFg)
}
if file.deletions > 0 {
Text("-\(file.deletions)")
.font(.system(size: 10, weight: .bold, design: .monospaced))
.foregroundColor(Theme.diffDelFg)
}
}
.padding(.horizontal, 10)
.padding(.vertical, 8)
.background(Theme.diffFileBg)
}
@ViewBuilder
private var bodyContent: some View {
VStack(alignment: .leading, spacing: 8) {
if file.status == .deleted {
Text("File deleted")
.font(.system(size: 11, weight: .semibold))
.foregroundColor(Theme.diffDelFg)
.padding(.horizontal, 10)
.padding(.top, 6)
} else if file.status == .added {
Text("New file")
.font(.system(size: 11, weight: .semibold))
.foregroundColor(Theme.diffAddFg)
.padding(.horizontal, 10)
.padding(.top, 6)
}
if file.partialParse {
Text("Partial parse: some hunks could not be rendered.")
.font(.system(size: 11))
.foregroundColor(Theme.warning)
.padding(.horizontal, 10)
}
if !file.modeChanges.isEmpty {
VStack(alignment: .leading, spacing: 2) {
ForEach(file.modeChanges, id: \.self) { modeLine in
Text(modeLine)
.font(.system(size: 10, design: .monospaced))
.foregroundColor(Theme.textTertiary)
}
}
.padding(.horizontal, 10)
}
if file.isBinary {
binaryBody
} else {
ForEach(displayedHunks) { hunk in
DiffHunkView(hunk: hunk)
}
if shouldPaginate && paginationRemainder > 0 {
Button("Expand remaining \(paginationRemainder) lines") {
showRemainingLines = true
}
.buttonStyle(.plain)
.font(.system(size: 11, weight: .semibold))
.foregroundColor(Theme.accent)
.padding(.horizontal, 10)
.padding(.bottom, 8)
.accessibilityIdentifier("diffFile.expandRemaining.\(safeAccessibilityID(file.id))")
}
}
}
.padding(.bottom, 8)
}
private var statusBadge: some View {
let style: (label: String, color: Color) = {
switch file.status {
case .added:
return ("A", Theme.diffAddFg)
case .modified:
return ("M", Theme.diffHunkFg)
case .deleted:
return ("D", Theme.diffDelFg)
case .renamed:
return ("R", Theme.warning)
case .unknown:
return ("?", Theme.textTertiary)
}
}()
return Text(style.label)
.font(.system(size: 9, weight: .bold, design: .monospaced))
.foregroundColor(style.color)
.frame(width: 18, height: 18)
.background(style.color.opacity(0.16))
.cornerRadius(4)
.accessibilityIdentifier("diffFile.status.\(safeAccessibilityID(file.id))")
}
private var binaryBody: some View {
HStack(spacing: 8) {
Image(systemName: "doc.fill")
.font(.system(size: 11))
.foregroundColor(Theme.warning)
if let binarySize = file.binarySizeBytes {
Text("Binary file (\(byteCountString(binarySize)))")
.font(.system(size: 11))
.foregroundColor(Theme.textSecondary)
} else {
Text("Binary file")
.font(.system(size: 11))
.foregroundColor(Theme.textSecondary)
}
}
.padding(.horizontal, 10)
}
private func byteCountString(_ bytes: Int) -> String {
if bytes < 1_024 {
return "\(bytes) B"
}
if bytes < 1_024 * 1_024 {
return "\(bytes / 1_024) KB"
}
return "\(bytes / (1_024 * 1_024)) MB"
}
private func safeAccessibilityID(_ raw: String) -> String {
raw.map { character in
if character.isLetter || character.isNumber {
return character
}
return "_"
}
.reduce(into: "") { partial, character in
partial.append(character)
}
}
}