@@ -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