forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeDiff.swift
More file actions
379 lines (324 loc) · 12.5 KB
/
CodeDiff.swift
File metadata and controls
379 lines (324 loc) · 12.5 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import Foundation
import SuggestionBasic
public struct CodeDiff {
public typealias LineDiff = CollectionDifference<String>
public struct SnippetDiff {
public struct Change {
public var offset: Int
public var element: String
}
public struct Line {
public var text: String
public var diff: [Change]?
}
public struct Section {
public var oldSnippet: [Line]
public var newSnippet: [Line]
}
public var sections: [Section]
public func line(at index: Int, in keyPath: KeyPath<Section, [Line]>) -> Line? {
var previousSectionEnd = 0
for section in sections {
let lines = section[keyPath: keyPath]
let index = index - previousSectionEnd
if index < lines.endIndex {
return lines[index]
}
previousSectionEnd += lines.endIndex
}
return nil
}
}
public func diff(text: String, from oldText: String) -> LineDiff {
typealias Change = LineDiff.Change
let diffByCharacter = text.difference(from: oldText)
var result = [Change]()
var current: Change?
for item in diffByCharacter {
if let this = current {
switch (this, item) {
case let (.insert(offset, element, associatedWith), .insert(offsetB, elementB, _))
where offset + element.count == offsetB:
current = .insert(
offset: offset,
element: element + String(elementB),
associatedWith: associatedWith
)
continue
case let (.remove(offset, element, associatedWith), .remove(offsetB, elementB, _))
where offset - 1 == offsetB:
current = .remove(
offset: offsetB,
element: String(elementB) + element,
associatedWith: associatedWith
)
continue
default:
result.append(this)
}
}
current = switch item {
case let .insert(offset, element, associatedWith):
.insert(offset: offset, element: String(element), associatedWith: associatedWith)
case let .remove(offset, element, associatedWith):
.remove(offset: offset, element: String(element), associatedWith: associatedWith)
}
}
if let current {
result.append(current)
}
return .init(result) ?? [].difference(from: [])
}
public func diff(snippet: String, from oldSnippet: String) -> SnippetDiff {
let newLines = snippet.breakLines()
let oldLines = oldSnippet.breakLines()
let diffByLine = newLines.difference(from: oldLines)
struct DiffSection: Equatable {
var offset: Int
var end: Int
var lines: [String]
}
func collect(
into all: inout [DiffSection],
changes: [CollectionDifference<String>.Change],
extract: (CollectionDifference<String>.Change) -> (offset: Int, line: String)?
) {
var current: DiffSection?
for change in changes {
guard let (offset, element) = extract(change) else { continue }
if var section = current {
if offset == section.end + 1 {
section.lines.append(element)
section.end = offset
current = section
continue
} else {
all.append(section)
}
}
current = DiffSection(offset: offset, end: offset, lines: [element])
}
if let current {
all.append(current)
}
}
var insertions = [DiffSection]()
var removals = [DiffSection]()
collect(into: &removals, changes: diffByLine.removals) { change in
guard case let .remove(offset, element, _) = change else { return nil }
return (offset, element)
}
collect(into: &insertions, changes: diffByLine.insertions) { change in
guard case let .insert(offset, element, _) = change else { return nil }
return (offset, element)
}
var oldLineIndex = 0
var newLineIndex = 0
var sectionIndex = 0
var result = SnippetDiff(sections: [])
while oldLineIndex < oldLines.endIndex || newLineIndex < newLines.endIndex {
let removalSection = removals[safe: sectionIndex]
let insertionSection = insertions[safe: sectionIndex]
// handle lines before sections
var beforeSection = SnippetDiff.Section(oldSnippet: [], newSnippet: [])
while oldLineIndex < (removalSection?.offset ?? .max) {
beforeSection.oldSnippet.append(.init(text: oldLines[oldLineIndex], diff: nil))
oldLineIndex += 1
}
while newLineIndex < (insertionSection?.offset ?? .max) {
beforeSection.newSnippet.append(.init(text: newLines[newLineIndex], diff: nil))
newLineIndex += 1
}
result.sections.append(beforeSection)
// handle lines inside sections
var insideSection = SnippetDiff.Section(oldSnippet: [], newSnippet: [])
for i in 0..<max(removalSection?.lines.count ?? 0, insertionSection?.lines.count ?? 0) {
let oldLine = removalSection?.lines[safe: i]
let newLine = insertionSection?.lines[safe: i]
let diff = diff(text: newLine ?? "", from: oldLine ?? "")
if let oldLine {
insideSection.oldSnippet.append(.init(
text: oldLine,
diff: diff.removals.compactMap { change in
guard case let .remove(offset, element, _) = change else { return nil }
return .init(offset: offset, element: element)
}
))
}
if let newLine {
insideSection.newSnippet.append(.init(
text: newLine,
diff: diff.insertions.compactMap { change in
guard case let .insert(offset, element, _) = change else { return nil }
return .init(offset: offset, element: element)
}
))
}
}
result.sections.append(insideSection)
oldLineIndex += removalSection?.lines.count ?? 0
newLineIndex += insertionSection?.lines.count ?? 0
sectionIndex += 1
}
return result
}
}
extension Array {
subscript(safe index: Int) -> Element? {
guard index >= 0, index < count else { return nil }
return self[index]
}
subscript(safe index: Int, fallback fallback: Element) -> Element {
guard index >= 0, index < count else { return fallback }
return self[index]
}
}
#if DEBUG
import SwiftUI
struct SnippetDiffPreview: View {
let originalCode: String
let newCode: String
var body: some View {
HStack(alignment: .top) {
let (original, new) = generateTexts()
block(original)
Divider()
block(new)
}
.padding()
.font(.body.monospaced())
}
@ViewBuilder
func block(_ code: [AttributedString]) -> some View {
VStack(alignment: .leading) {
if !code.isEmpty {
ForEach(0..<code.count, id: \.self) { index in
HStack {
Text("\(index)")
.foregroundStyle(.secondary)
.frame(width: 30)
Text(code[index])
.multilineTextAlignment(.leading)
.frame(minWidth: 260, alignment: .leading)
}
}
}
}
}
func generateTexts() -> (original: [AttributedString], new: [AttributedString]) {
let diff = CodeDiff().diff(snippet: newCode, from: originalCode)
let new = diff.sections.flatMap {
$0.newSnippet.map {
let text = $0.text.trimmingCharacters(in: .newlines)
let string = NSMutableAttributedString(string: text)
if let diff = $0.diff {
string.addAttribute(
.backgroundColor,
value: NSColor.green.withAlphaComponent(0.1),
range: NSRange(location: 0, length: text.count)
)
for diffItem in diff {
string.addAttribute(
.backgroundColor,
value: NSColor.green.withAlphaComponent(0.5),
range: NSRange(
location: diffItem.offset,
length: min(text.count - diffItem.offset, diffItem.element.count)
)
)
}
}
return string
}
}
let original = diff.sections.flatMap {
$0.oldSnippet.map {
let text = $0.text.trimmingCharacters(in: .newlines)
let string = NSMutableAttributedString(string: text)
if let diff = $0.diff {
string.addAttribute(
.backgroundColor,
value: NSColor.red.withAlphaComponent(0.1),
range: NSRange(location: 0, length: text.count)
)
for diffItem in diff {
string.addAttribute(
.backgroundColor,
value: NSColor.red.withAlphaComponent(0.5),
range: NSRange(
location: diffItem.offset,
length: min(text.count - diffItem.offset, diffItem.element.count)
)
)
}
}
return string
}
}
return (original.map(AttributedString.init), new.map(AttributedString.init))
}
}
struct LineDiffPreview: View {
let originalCode: String
let newCode: String
var body: some View {
VStack(alignment: .leading) {
let (original, new) = generateTexts()
Text(original)
Divider()
Text(new)
}
.padding()
.font(.body.monospaced())
}
func generateTexts() -> (original: AttributedString, new: AttributedString) {
let diff = CodeDiff().diff(text: newCode, from: originalCode)
let original = NSMutableAttributedString(string: originalCode)
let new = NSMutableAttributedString(string: newCode)
for item in diff {
switch item {
case let .insert(offset, element, _):
new.addAttribute(
.backgroundColor,
value: NSColor.green.withAlphaComponent(0.5),
range: NSRange(location: offset, length: element.count)
)
case let .remove(offset, element, _):
original.addAttribute(
.backgroundColor,
value: NSColor.red.withAlphaComponent(0.5),
range: NSRange(location: offset, length: element.count)
)
}
}
return (.init(original), .init(new))
}
}
#Preview("Line Diff") {
let originalCode = """
let foo = Foo() // yes
"""
let newCode = """
var foo = Bar()
"""
return LineDiffPreview(originalCode: originalCode, newCode: newCode)
}
#Preview("Snippet Diff") {
let originalCode = """
let foo = Foo()
print(foo)
// do something
foo.foo()
func zoo() {}
"""
let newCode = """
var foo = Bar()
// do something
foo.bar()
func zoo() {
print("zoo")
}
"""
return SnippetDiffPreview(originalCode: originalCode, newCode: newCode)
}
#endif