Skip to content

Commit 5774302

Browse files
committed
Fix modification diff
1 parent 284ed9e commit 5774302

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanelView.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,8 @@ extension PromptToCodePanelView {
619619
scenario: "promptToCode",
620620
font: codeFont.value.nsFont,
621621
droppingLeadingSpaces: hideCommonPrecedingSpaces,
622-
proposedForegroundColor: codeForegroundColor
622+
proposedForegroundColor: codeForegroundColor,
623+
ignoreWholeLineChange: false
623624
)
624625
.frame(maxWidth: .infinity)
625626

@@ -741,7 +742,7 @@ extension PromptToCodePanelView {
741742
.init(
742743
startLineIndex: 8,
743744
originalCode: "print(foo)",
744-
modifiedCode: "print(bar)",
745+
modifiedCode: "print(bar)\nprint(baz)",
745746
description: "",
746747
error: "Error",
747748
attachedRange: CursorRange(

Tool/Sources/SharedUIComponents/AsyncCodeBlock.swift

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public struct AsyncCodeBlock: View { // chat: hid
2626
let dimmedCharacterCount: DimmedCharacterCount
2727
/// Whether to drop common leading spaces of each line.
2828
let droppingLeadingSpaces: Bool
29+
/// Whether to ignore whole line change in diff.
30+
let ignoreWholeLineChangeInDiff: Bool
2931

3032
public init(
3133
code: String,
@@ -36,7 +38,8 @@ public struct AsyncCodeBlock: View { // chat: hid
3638
font: NSFont,
3739
droppingLeadingSpaces: Bool,
3840
proposedForegroundColor: Color?,
39-
dimmedCharacterCount: DimmedCharacterCount = .init(prefix: 0, suffix: 0)
41+
dimmedCharacterCount: DimmedCharacterCount = .init(prefix: 0, suffix: 0),
42+
ignoreWholeLineChangeInDiff: Bool = true
4043
) {
4144
self.code = code
4245
self.originalCode = originalCode
@@ -47,6 +50,7 @@ public struct AsyncCodeBlock: View { // chat: hid
4750
self.proposedForegroundColor = proposedForegroundColor
4851
self.dimmedCharacterCount = dimmedCharacterCount
4952
self.droppingLeadingSpaces = droppingLeadingSpaces
53+
self.ignoreWholeLineChangeInDiff = ignoreWholeLineChangeInDiff
5054
}
5155

5256
var foregroundColor: Color {
@@ -89,6 +93,7 @@ public struct AsyncCodeBlock: View { // chat: hid
8993
.padding(.bottom, 4)
9094
.onAppear {
9195
storage.dimmedCharacterCount = dimmedCharacterCount
96+
storage.ignoreWholeLineChangeInDiff = ignoreWholeLineChangeInDiff
9297
storage.highlightStorage.highlight(debounce: false, for: self)
9398
storage.diffStorage.diff(for: self)
9499
}
@@ -119,6 +124,9 @@ public struct AsyncCodeBlock: View { // chat: hid
119124
.onChange(of: dimmedCharacterCount) { value in
120125
storage.dimmedCharacterCount = value
121126
}
127+
.onChange(of: ignoreWholeLineChangeInDiff) { value in
128+
storage.ignoreWholeLineChangeInDiff = value
129+
}
122130
}
123131
}
124132
}
@@ -146,6 +154,7 @@ extension AsyncCodeBlock {
146154
var dimmedCharacterCount: DimmedCharacterCount = .init(prefix: 0, suffix: 0)
147155
let diffStorage = DiffStorage()
148156
let highlightStorage = HighlightStorage()
157+
var ignoreWholeLineChangeInDiff: Bool = true
149158

150159
var code: String? {
151160
get { highlightStorage.code }
@@ -175,6 +184,7 @@ extension AsyncCodeBlock {
175184
Self.presentDiff(
176185
highlightedCode,
177186
commonPrecedingSpaceCount: commonPrecedingSpaceCount,
187+
ignoreWholeLineChange: ignoreWholeLineChangeInDiff,
178188
diffResult: diffResult
179189
)
180190
}
@@ -251,6 +261,7 @@ extension AsyncCodeBlock {
251261
static func presentDiff(
252262
_ highlightedCode: [NSMutableAttributedString],
253263
commonPrecedingSpaceCount: Int,
264+
ignoreWholeLineChange: Bool,
254265
diffResult: CodeDiff.SnippetDiff
255266
) {
256267
let originalCodeIsSingleLine = diffResult.sections.count == 1
@@ -266,8 +277,9 @@ extension AsyncCodeBlock {
266277
change.element.count - commonPrecedingSpaceCount
267278
== mutableString.string.count
268279
{
269-
// ignore the whole line change
270-
continue
280+
if ignoreWholeLineChange {
281+
continue
282+
}
271283
}
272284

273285
let offset = change.offset - commonPrecedingSpaceCount
@@ -488,6 +500,22 @@ extension AsyncCodeBlock {
488500
.frame(width: 400, height: 100)
489501
}
490502

503+
#Preview("Multiple Line Suggestion Including Whole Line Change in Diff") {
504+
AsyncCodeBlock(
505+
code: "// comment\n let foo = Bar()\n print(bar)\n print(foo)",
506+
originalCode: " let foo = Bar()\n",
507+
language: "swift",
508+
startLineIndex: 10,
509+
scenario: "",
510+
font: .monospacedSystemFont(ofSize: 12, weight: .regular),
511+
droppingLeadingSpaces: true,
512+
proposedForegroundColor: .primary,
513+
dimmedCharacterCount: .init(prefix: 11, suffix: 0),
514+
ignoreWholeLineChangeInDiff: false
515+
)
516+
.frame(width: 400, height: 100)
517+
}
518+
491519
#Preview("Updating Content") {
492520
struct UpdateContent: View {
493521
@State var index = 0
@@ -524,3 +552,4 @@ extension AsyncCodeBlock {
524552
return UpdateContent()
525553
.frame(width: 400, height: 200)
526554
}
555+

0 commit comments

Comments
 (0)