Skip to content

Commit dbc3fa8

Browse files
committed
Add AsyncDiffCodeBlock
1 parent 57f00a5 commit dbc3fa8

File tree

5 files changed

+625
-47
lines changed

5 files changed

+625
-47
lines changed

Tool/Sources/CodeDiff/CodeDiff.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SuggestionBasic
33

44
public struct CodeDiff {
55
public init() {}
6-
6+
77
public typealias LineDiff = CollectionDifference<String>
88

99
public struct SnippetDiff: Equatable {
@@ -23,6 +23,8 @@ public struct CodeDiff {
2323
}
2424

2525
public struct Section: Equatable {
26+
public var oldOffset: Int
27+
public var newOffset: Int
2628
public var oldSnippet: [Line]
2729
public var newSnippet: [Line]
2830

@@ -113,7 +115,12 @@ public struct CodeDiff {
113115
let insertionSection = insertions[safe: sectionIndex]
114116

115117
// handle lines before sections
116-
var beforeSection = SnippetDiff.Section(oldSnippet: [], newSnippet: [])
118+
var beforeSection = SnippetDiff.Section(
119+
oldOffset: 0,
120+
newOffset: 0,
121+
oldSnippet: [],
122+
newSnippet: []
123+
)
117124

118125
while oldLineIndex < (removalSection?.offset ?? oldLines.endIndex) {
119126
if oldLineIndex < oldLines.endIndex {
@@ -140,7 +147,12 @@ public struct CodeDiff {
140147

141148
// handle lines inside sections
142149

143-
var insideSection = SnippetDiff.Section(oldSnippet: [], newSnippet: [])
150+
var insideSection = SnippetDiff.Section(
151+
oldOffset: removalSection?.offset ?? 0,
152+
newOffset: insertionSection?.offset ?? 0,
153+
oldSnippet: [],
154+
newSnippet: []
155+
)
144156

145157
for i in 0..<max(removalSection?.lines.count ?? 0, insertionSection?.lines.count ?? 0) {
146158
let oldLine = removalSection?.lines[safe: i]
@@ -247,7 +259,7 @@ extension CodeDiff {
247259
removalUnchangedGap = 0
248260
insertionUnchangedGap = 0
249261
}
250-
} else if removalUnchangedGap > insertionUnchangedGap {
262+
} else if removalUnchangedGap > insertionUnchangedGap {
251263
// insert empty sections to removals
252264
if insertionUnchangedGap > 0 {
253265
let count = removalUnchangedGap - insertionUnchangedGap

Tool/Sources/SharedUIComponents/AsyncCodeBlock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
import Perception
55
import SwiftUI
66

7-
public struct AsyncCodeBlock: View { // chat: hid
7+
public struct AsyncCodeBlock: View {
88
@State var storage = Storage()
99
@Environment(\.colorScheme) var colorScheme
1010

0 commit comments

Comments
 (0)