1- import SuggestionModel
21import Foundation
2+ import SuggestionModel
33
44let suggestionStart = " /*========== Copilot Suggestion"
55let suggestionEnd = "*///======== End of Copilot Suggestion "
@@ -141,8 +141,6 @@ public struct SuggestionInjector {
141141 let end = completion. range. end
142142 let suggestionContent = completion. text
143143
144- let _ = start. line < content. endIndex ? content [ start. line] : nil
145-
146144 let firstRemovedLine = content [ safe: start. line]
147145 let lastRemovedLine = content [ safe: end. line]
148146 let startLine = max ( 0 , start. line)
@@ -177,12 +175,24 @@ public struct SuggestionInjector {
177175 }
178176
179177 // appending suffix text not in range if needed.
180- let cursorCol = toBeInserted [ toBeInserted. endIndex - 1 ] . count - 1
181178 let skipAppendingDueToContinueTyping = {
182179 guard let first = toBeInserted. first? . dropLast ( 1 ) , !first. isEmpty else { return false }
183180 let droppedLast = lastRemovedLine? . dropLast ( 1 )
184181 guard let droppedLast, !droppedLast. isEmpty else { return false }
185- return first. hasPrefix ( droppedLast)
182+ // case 1: user keeps typing as the suggestion suggests.
183+ if first. hasPrefix ( droppedLast) {
184+ return true
185+ }
186+ // case 2: user also typed the suffix of the suggestion (or auto-completed by Xcode)
187+ if end. character < droppedLast. count - 1 {
188+ let splitIndex = droppedLast. index ( droppedLast. startIndex, offsetBy: end. character)
189+ let prefix = droppedLast [ ..< splitIndex]
190+ let suffix = droppedLast [ splitIndex... ]
191+ if first. hasPrefix ( prefix) , first. hasSuffix ( suffix) {
192+ return true
193+ }
194+ }
195+ return false
186196 } ( )
187197 if let lastRemovedLine,
188198 !skipAppendingDueToContinueTyping,
@@ -200,11 +210,12 @@ public struct SuggestionInjector {
200210 toBeInserted [ toBeInserted. endIndex - 1 ] . removeLast ( 1 )
201211 }
202212 let leftover = lastRemovedLine [ leftoverRange]
203-
213+
204214 toBeInserted [ toBeInserted. endIndex - 1 ]
205215 . append ( contentsOf: leftover)
206216 }
207217
218+ let cursorCol = toBeInserted [ toBeInserted. endIndex - 1 ] . count - 1
208219 let insertingIndex = min ( start. line, content. endIndex)
209220 content. insert ( contentsOf: toBeInserted, at: insertingIndex)
210221 extraInfo. modifications. append ( . inserted( insertingIndex, toBeInserted) )
@@ -270,3 +281,4 @@ extension Array {
270281 indices. contains ( index) ? self [ index] : nil
271282 }
272283}
284+
0 commit comments