@@ -10,7 +10,7 @@ public struct SuggestionInjector {
1010 public struct ExtraInfo {
1111 public var didChangeContent = false
1212 public var didChangeCursorPosition = false
13- public var suggestionRange : ClosedRange < Int > ?
13+ public var modificationRanges : [ String : CursorRange ] = [ : ]
1414 public var modifications : [ Modification ] = [ ]
1515 public init ( ) { }
1616 }
@@ -23,7 +23,6 @@ public struct SuggestionInjector {
2323 ) {
2424 extraInfo. didChangeContent = true
2525 extraInfo. didChangeCursorPosition = true
26- extraInfo. suggestionRange = nil
2726 let start = completion. range. start
2827 let end = completion. range. end
2928 let suggestionContent = completion. text
@@ -85,6 +84,52 @@ public struct SuggestionInjector {
8584 line: startLine + toBeInserted. count - 1 ,
8685 character: max ( 0 , cursorCol)
8786 )
87+ extraInfo. modificationRanges [ completion. id] = . init( start: start, end: cursorPosition)
88+ }
89+
90+ public func acceptSuggestions(
91+ intoContentWithoutSuggestion content: inout [ String ] ,
92+ cursorPosition: inout CursorPosition ,
93+ completions: [ CodeSuggestion ] ,
94+ extraInfo: inout ExtraInfo
95+ ) {
96+ var previousCompletion : CodeSuggestion ?
97+
98+ let sortedCompletions = completions. sorted { $0. range. start. line < $1. range. start. line }
99+
100+ for var completion in sortedCompletions {
101+ defer { previousCompletion = completion }
102+
103+ // Adjust the position of the completion by the accumulated line count change
104+ if let previousCompletionId = previousCompletion? . id,
105+ let previousRange = extraInfo. modificationRanges [ previousCompletionId]
106+ {
107+ let lineCountChange = previousRange
108+ . lineCount - ( completion. range. start. line - previousRange. start. line)
109+ completion. position = CursorPosition (
110+ line: completion. position. line + lineCountChange,
111+ character: completion. position. character
112+ )
113+ completion. range = CursorRange (
114+ start: CursorPosition (
115+ line: completion. range. start. line + lineCountChange,
116+ character: completion. range. start. character
117+ ) ,
118+ end: CursorPosition (
119+ line: completion. range. end. line + lineCountChange,
120+ character: completion. range. end. character
121+ )
122+ )
123+ }
124+
125+ // Accept the suggestion
126+ acceptSuggestion (
127+ intoContentWithoutSuggestion: & content,
128+ cursorPosition: & cursorPosition,
129+ completion: completion,
130+ extraInfo: & extraInfo
131+ )
132+ }
88133 }
89134
90135 func recoverSuffixIfNeeded(
0 commit comments