@@ -169,13 +169,10 @@ struct CodeBlockSuggestionPanel: View {
169169 VStack ( spacing: 0 ) {
170170 CustomScrollView {
171171 WithPerceptionTracking {
172- let diffResult = Self . diff (
173- suggestion: suggestion,
174- textCursorTracker: textCursorTracker
175- )
176-
172+ let ( code, originalCode, dimmedCharacterCount) = extractCode ( )
177173 AsyncCodeBlock (
178- code: suggestion. code,
174+ code: code,
175+ originalCode: originalCode,
179176 language: suggestion. language,
180177 startLineIndex: suggestion. startLineIndex,
181178 scenario: " suggestion " ,
@@ -195,7 +192,7 @@ struct CodeBlockSuggestionPanel: View {
195192 }
196193 return nil
197194 } ( ) ,
198- dimmedCharacterCount: 0
195+ dimmedCharacterCount: dimmedCharacterCount
199196 )
200197 . frame ( maxWidth: . infinity)
201198 . background ( { ( ) -> Color in
@@ -228,21 +225,55 @@ struct CodeBlockSuggestionPanel: View {
228225 }
229226 }
230227
231- struct DiffResult {
232- var dimmedRanges : [ Range < String . Index > ]
233- var mutatedRanges : [ Range < String . Index > ]
234- var deletedRanges : [ Range < String . Index > ]
235- }
236-
237228 @MainActor
238- static func diff(
239- suggestion: PresentingCodeSuggestion ,
240- textCursorTracker: TextCursorTracker
241- ) -> DiffResult {
242- let typedContentCount = suggestion. startLineIndex == textCursorTracker. cursorPosition. line
229+ func extractCode( ) -> (
230+ code: String ,
231+ originalCode: String ,
232+ dimmedCharacterCount: AsyncCodeBlock . DimmedCharacterCount
233+ ) {
234+ let range = suggestion. replacingRange
235+ let codeInRange = EditorInformation . code ( in: textCursorTracker. content. lines, inside: range)
236+ let leftover = {
237+ if range. end. line >= 0 , range. end. line < textCursorTracker. content. lines. endIndex {
238+ let lastLine = textCursorTracker. content. lines [ range. end. line]
239+ if range. end. character < lastLine. utf16. count {
240+ let startIndex = lastLine. utf16. index (
241+ lastLine. utf16. startIndex,
242+ offsetBy: range. end. character
243+ )
244+ let leftover = String ( lastLine. utf16. suffix ( from: startIndex) )
245+ return leftover ?? " "
246+ }
247+ }
248+ return " "
249+ } ( )
250+
251+ let prefix = {
252+ if range. start. line >= 0 , range. start. line < textCursorTracker. content. lines. endIndex {
253+ let firstLine = textCursorTracker. content. lines [ range. start. line]
254+ if range. start. character < firstLine. utf16. count {
255+ let endIndex = firstLine. utf16. index (
256+ firstLine. utf16. startIndex,
257+ offsetBy: range. start. character
258+ )
259+ let prefix = String ( firstLine. utf16. prefix ( upTo: endIndex) )
260+ return prefix ?? " "
261+ }
262+ }
263+ return " "
264+ } ( )
265+
266+ let code = prefix + suggestion. code + leftover
267+
268+ let typedCount = suggestion. startLineIndex == textCursorTracker. cursorPosition. line
243269 ? textCursorTracker. cursorPosition. character
244270 : 0
245- return . init( dimmedRanges: [ ] , mutatedRanges: [ ] , deletedRanges: [ ] )
271+
272+ return (
273+ code,
274+ codeInRange. code,
275+ . init( prefix: typedCount, suffix: leftover. utf16. count)
276+ )
246277 }
247278}
248279
@@ -261,7 +292,8 @@ struct CodeBlockSuggestionPanel: View {
261292 language: " swift " ,
262293 startLineIndex: 8 ,
263294 suggestionCount: 2 ,
264- currentSuggestionIndex: 0
295+ currentSuggestionIndex: 0 ,
296+ replacingRange: . outOfScope
265297 ) , suggestionDisplayCompactMode: . init(
266298 wrappedValue: false ,
267299 " suggestionDisplayCompactMode " ,
@@ -289,7 +321,8 @@ struct CodeBlockSuggestionPanel: View {
289321 language: " swift " ,
290322 startLineIndex: 8 ,
291323 suggestionCount: 2 ,
292- currentSuggestionIndex: 0
324+ currentSuggestionIndex: 0 ,
325+ replacingRange: . outOfScope
293326 ) , suggestionDisplayCompactMode: . init(
294327 wrappedValue: true ,
295328 " suggestionDisplayCompactMode " ,
@@ -315,7 +348,8 @@ struct CodeBlockSuggestionPanel: View {
315348 language: " objective-c " ,
316349 startLineIndex: 8 ,
317350 suggestionCount: 2 ,
318- currentSuggestionIndex: 0
351+ currentSuggestionIndex: 0 ,
352+ replacingRange: . outOfScope
319353 ) )
320354 . preferredColorScheme ( . light)
321355 . frame ( width: 450 , height: 400 )
0 commit comments