@@ -174,12 +174,16 @@ public struct SuggestionInjector {
174174 )
175175 }
176176
177- // appending suffix text not in range if needed.
177+ #warning(
178+ " TODO: I feel like the implementation is doing a lot of receptive work to recover suffix. "
179+ )
180+
181+ /// The number of character that is in the last modified line but not included.
178182 let leftoverCount : Int = {
179183 let maxCount = lastRemovedLine? . count ?? 0
180184 guard let first = toBeInserted. first?
181185 . dropLast ( ( toBeInserted. first? . hasSuffix ( " \n " ) ?? false ) ? 1 : 0 ) ,
182- !first. isEmpty else { return maxCount }
186+ !first. isEmpty else { return maxCount }
183187 guard let last = toBeInserted. last?
184188 . dropLast ( ( toBeInserted. last? . hasSuffix ( " \n " ) ?? false ) ? 1 : 0 ) ,
185189 !last. isEmpty else { return maxCount }
@@ -193,7 +197,7 @@ public struct SuggestionInjector {
193197 // case 2: user also typed the suffix of the suggestion (or auto-completed by Xcode)
194198 if end. character < droppedLast. count {
195199 // locate the split index, the prefix of which matches the suggestion prefix.
196- var splitIndex : String . Index ? = nil
200+ var splitIndex : String . Index ?
197201 for offset in end. character..< droppedLast. count {
198202 let proposedIndex = droppedLast. index (
199203 droppedLast. startIndex,
@@ -204,7 +208,7 @@ public struct SuggestionInjector {
204208 splitIndex = proposedIndex
205209 }
206210 }
207-
211+
208212 // then check how many characters are not in the suffix of the suggestion.
209213 if let splitIndex {
210214 let suffix = droppedLast [ splitIndex... ]
@@ -214,10 +218,13 @@ public struct SuggestionInjector {
214218 return suffix. count
215219 }
216220 }
217-
221+
218222 return maxCount
219223 } ( )
220-
224+
225+ /// The number of characters appended to the last line.
226+ var appenderCount = 0
227+
221228 // appending suffix text not in range if needed.
222229 if let lastRemovedLine,
223230 leftoverCount > 0 ,
@@ -234,12 +241,23 @@ public struct SuggestionInjector {
234241 if toBeInserted [ toBeInserted. endIndex - 1 ] . hasSuffix ( " \n " ) {
235242 toBeInserted [ toBeInserted. endIndex - 1 ] . removeLast ( 1 )
236243 }
237- let leftover = lastRemovedLine [ leftoverRange] . suffix ( leftoverCount)
244+ let leftover = {
245+ if lastRemovedLine. hasSuffix ( " \n " ) {
246+ return lastRemovedLine [ leftoverRange] . dropLast ( 1 ) . suffix ( leftoverCount)
247+ }
248+ return lastRemovedLine [ leftoverRange] . suffix ( leftoverCount)
249+ } ( )
238250
239251 toBeInserted [ toBeInserted. endIndex - 1 ] . append ( contentsOf: leftover)
252+
253+ appenderCount = leftover. count
254+
255+ if !toBeInserted[ toBeInserted. endIndex - 1 ] . hasSuffix ( " \n " ) {
256+ toBeInserted [ toBeInserted. endIndex - 1 ] . append ( " \n " )
257+ }
240258 }
241259
242- let cursorCol = toBeInserted [ toBeInserted. endIndex - 1 ] . count - 1
260+ let cursorCol = toBeInserted [ toBeInserted. endIndex - 1 ] . count - 1 - appenderCount
243261 let insertingIndex = min ( start. line, content. endIndex)
244262 content. insert ( contentsOf: toBeInserted, at: insertingIndex)
245263 extraInfo. modifications. append ( . inserted( insertingIndex, toBeInserted) )
0 commit comments