Skip to content

Commit 3388747

Browse files
committed
Support full code diff in modification
1 parent 7290b2a commit 3388747

File tree

4 files changed

+236
-40
lines changed

4 files changed

+236
-40
lines changed

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanelView.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,20 +611,18 @@ extension PromptToCodePanelView {
611611
var body: some View {
612612
WithPerceptionTracking {
613613
let startLineIndex = store.snippet.attachedRange.start.line
614-
AsyncCodeBlock(
614+
AsyncDiffCodeBlock(
615615
code: store.snippet.modifiedCode,
616616
originalCode: store.snippet.originalCode,
617617
language: language.rawValue,
618618
startLineIndex: startLineIndex,
619619
scenario: "promptToCode",
620620
font: codeFont.value.nsFont,
621621
droppingLeadingSpaces: hideCommonPrecedingSpaces,
622-
proposedForegroundColor: codeForegroundColor,
623-
ignoreWholeLineChangeInDiff: false
622+
proposedForegroundColor: codeForegroundColor
624623
)
625-
.frame(maxWidth: .infinity)
626-
627-
.scaleEffect(x: 1, y: -1, anchor: .center)
624+
.frame(maxWidth: CGFloat.infinity)
625+
.scaleEffect(x: 1, y: -1, anchor: UnitPoint.center)
628626
}
629627
}
630628
}

Tool/Sources/CodeDiff/CodeDiff.swift

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public struct CodeDiff {
8585
}
8686

8787
public var description: String {
88-
"Diff:\n" + sections.map(\.description).joined(separator: "\n") + "\n"
88+
"Diff:\n" + sections.map(\.description).joined(separator: "\n---\n") + "\n"
8989
}
9090
}
9191

@@ -154,6 +154,31 @@ public struct CodeDiff {
154154
let removalSection = removals[safe: sectionIndex]
155155
let insertionSection = insertions[safe: sectionIndex]
156156

157+
if removalSection == nil, insertionSection == nil {
158+
let finishingSection = SnippetDiff.Section(
159+
oldOffset: oldLineIndex,
160+
newOffset: newLineIndex,
161+
oldSnippet: {
162+
guard oldLineIndex < oldLines.endIndex else { return [] }
163+
return oldLines[oldLineIndex..<oldLines.endIndex].map {
164+
.init(text: String($0), diff: .unchanged)
165+
}
166+
}(),
167+
newSnippet: {
168+
guard newLineIndex < newLines.endIndex else { return [] }
169+
return newLines[newLineIndex..<newLines.endIndex].map {
170+
.init(text: String($0), diff: .unchanged)
171+
}
172+
}()
173+
)
174+
175+
if !finishingSection.isEmpty {
176+
result.sections.append(finishingSection)
177+
}
178+
179+
break
180+
}
181+
157182
// handle lines before sections
158183
var beforeSection = SnippetDiff.Section(
159184
oldOffset: oldLineIndex,
@@ -162,7 +187,7 @@ public struct CodeDiff {
162187
newSnippet: []
163188
)
164189

165-
while oldLineIndex < (removalSection?.offset ?? oldLines.endIndex) {
190+
while oldLineIndex < (removalSection?.offset ?? (sectionIndex == 0 ? 0 : oldLines.endIndex)) {
166191
if oldLineIndex < oldLines.endIndex {
167192
beforeSection.oldSnippet.append(.init(
168193
text: String(oldLines[oldLineIndex]),
@@ -171,7 +196,7 @@ public struct CodeDiff {
171196
}
172197
oldLineIndex += 1
173198
}
174-
while newLineIndex < (insertionSection?.offset ?? newLines.endIndex) {
199+
while newLineIndex < (insertionSection?.offset ?? (sectionIndex == 0 ? 0 : newLines.endIndex)) {
175200
if newLineIndex < newLines.endIndex {
176201
beforeSection.newSnippet.append(.init(
177202
text: String(newLines[newLineIndex]),
@@ -182,6 +207,7 @@ public struct CodeDiff {
182207
}
183208

184209
if !beforeSection.isEmpty {
210+
// print("before section\n\n",beforeSection)
185211
result.sections.append(beforeSection)
186212
}
187213

@@ -218,13 +244,14 @@ public struct CodeDiff {
218244
}
219245
}
220246

221-
if !insideSection.isEmpty {
222-
result.sections.append(insideSection)
223-
}
224-
225247
oldLineIndex += removalSection?.lines.count ?? 0
226248
newLineIndex += insertionSection?.lines.count ?? 0
227249
sectionIndex += 1
250+
251+
if !insideSection.isEmpty {
252+
// print("inside section\n\n", insideSection)
253+
result.sections.append(insideSection)
254+
}
228255
}
229256

230257
return result

Tool/Sources/SharedUIComponents/AsyncDiffCodeBlock.swift

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -230,21 +230,27 @@ extension AsyncDiffCodeBlock {
230230

231231
for section in diffResult.sections {
232232
guard !section.isEmpty else { continue }
233-
let startIndex = section.oldOffset
234-
let endIndex = section.oldOffset + section.oldSnippet.count
235-
236-
let oldLines = originalHighlightedCode[startIndex..<endIndex]
237-
lines.append(contentsOf: oldLines.enumerated().map {
238-
Line(index: $0 + startIndex, kind: .deleted, string: $1)
239-
})
240-
241-
let newStartIndex = section.newOffset
242-
let newEndIndex = section.newOffset + section.newSnippet.count
233+
234+
for (index, line) in section.oldSnippet.enumerated() {
235+
if line.diff == .unchanged { continue }
236+
let lineIndex = section.oldOffset + index
237+
if lineIndex >= 0, lineIndex < originalHighlightedCode.count {
238+
let oldLine = originalHighlightedCode[lineIndex]
239+
lines.append(Line(index: lineIndex, kind: .deleted, string: oldLine))
240+
}
241+
}
243242

244-
let newLines = highlightedCode[newStartIndex..<newEndIndex]
245-
lines.append(contentsOf: newLines.enumerated().map {
246-
Line(index: $0 + newStartIndex, kind: .added, string: $1)
247-
})
243+
for (index, line) in section.newSnippet.enumerated() {
244+
let lineIndex = section.newOffset + index
245+
guard lineIndex >= 0, lineIndex < highlightedCode.count else { continue }
246+
if line.diff == .unchanged {
247+
let newLine = highlightedCode[lineIndex]
248+
lines.append(Line(index: lineIndex, kind: .unchanged, string: newLine))
249+
} else {
250+
let newLine = highlightedCode[lineIndex]
251+
lines.append(Line(index: lineIndex, kind: .added, string: newLine))
252+
}
253+
}
248254
}
249255

250256
return lines
@@ -384,8 +390,8 @@ extension AsyncDiffCodeBlock {
384390

385391
#Preview("Multiple Line Suggestion") {
386392
AsyncDiffCodeBlock(
387-
code: " let foo = Bar()\n print(foo)",
388-
originalCode: " var foo // comment\n print(bar)",
393+
code: " let foo = Bar()\n print(foo)\n print(a)",
394+
originalCode: " var foo // comment\n print(bar)\n print(a)",
389395
language: "swift",
390396
startLineIndex: 10,
391397
scenario: "",
@@ -397,8 +403,8 @@ extension AsyncDiffCodeBlock {
397403
}
398404

399405
#Preview("Multiple Line Suggestion Including Whole Line Change in Diff") {
400-
AsyncCodeBlock(
401-
code: "// comment\n let foo = Bar()\n print(bar)\n print(foo)",
406+
AsyncDiffCodeBlock(
407+
code: "// comment\n let foo = Bar()\n print(bar)\n print(foo)\n",
402408
originalCode: " let foo = Bar()\n",
403409
language: "swift",
404410
startLineIndex: 10,

0 commit comments

Comments
 (0)