forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuggestionInjector.swift
More file actions
335 lines (283 loc) · 11.7 KB
/
Copy pathSuggestionInjector.swift
File metadata and controls
335 lines (283 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import Foundation
import SuggestionModel
let suggestionStart = "/*========== Copilot Suggestion"
let suggestionEnd = "*///======== End of Copilot Suggestion"
// NOTE: Every lines from Xcode Extension has a line break at its end, even the last line.
// NOTE: Copilot's completion always start at character 0, no matter where the cursor is.
public struct SuggestionInjector {
public init() {}
public struct ExtraInfo {
public var didChangeContent = false
public var didChangeCursorPosition = false
public var suggestionRange: ClosedRange<Int>?
public var modifications: [Modification] = []
public init() {}
}
public func rejectCurrentSuggestions(
from content: inout [String],
cursorPosition: inout CursorPosition,
extraInfo: inout ExtraInfo
) {
var ranges = [ClosedRange<Int>]()
var suggestionStartIndex = -1
// find ranges of suggestion comments
for (index, line) in content.enumerated() {
if line.hasPrefix(suggestionStart) {
suggestionStartIndex = index
}
if suggestionStartIndex >= 0, line.hasPrefix(suggestionEnd) {
ranges.append(.init(uncheckedBounds: (suggestionStartIndex, index)))
suggestionStartIndex = -1
}
}
let reversedRanges = ranges.reversed()
extraInfo.modifications.append(contentsOf: reversedRanges.map(Modification.deleted))
extraInfo.didChangeContent = !ranges.isEmpty
// remove the lines from bottom to top
for range in reversedRanges {
for i in stride(from: range.upperBound, through: range.lowerBound, by: -1) {
if i <= cursorPosition.line, cursorPosition.line >= 0 {
cursorPosition = .init(
line: cursorPosition.line - 1,
character: i == cursorPosition.line ? 0 : cursorPosition.character
)
extraInfo.didChangeCursorPosition = true
}
content.remove(at: i)
}
}
extraInfo.suggestionRange = nil
}
public func proposeSuggestion(
intoContentWithoutSuggestion content: inout [String],
completion: CodeSuggestion,
index: Int,
count: Int,
extraInfo: inout ExtraInfo
) {
// assemble suggestion comment
let start = completion.range.start
let startText = "\(suggestionStart) \(index + 1)/\(count)"
var lines = [startText + "\n"]
lines.append(contentsOf: completion.text.breakLines(appendLineBreakToLastLine: true))
lines.append(suggestionEnd + "\n")
// if suggestion is empty, returns without modifying the code
guard lines.count > 2 else { return }
// replace the common prefix of the first line with space and carrot
let existedLine = start.line < content.endIndex ? content[start.line] : nil
let commonPrefix = longestCommonPrefix(of: lines[1], and: existedLine ?? "")
if !commonPrefix.isEmpty {
let replacingText = {
switch (commonPrefix.hasSuffix("\n"), commonPrefix.count) {
case (false, let count):
return String(repeating: " ", count: count - 1) + "^"
case (true, let count) where count > 1:
return String(repeating: " ", count: count - 2) + "^\n"
case (true, _):
return "\n"
}
}()
lines[1].replaceSubrange(
lines[1].startIndex..<(
lines[1].index(
lines[1].startIndex,
offsetBy: commonPrefix.count,
limitedBy: lines[1].endIndex
) ?? lines[1].endIndex
),
with: replacingText
)
}
// if the suggestion is only appending new lines and spaces, return without modification
if completion.text.dropFirst(commonPrefix.count)
.allSatisfy({ $0.isWhitespace || $0.isNewline }) { return }
// determine if it's inserted to the current line or the next line
let lineIndex = start.line + {
guard let existedLine else { return 0 }
if existedLine.isEmptyOrNewLine { return 1 }
if commonPrefix.isEmpty { return 0 }
return 1
}()
if content.endIndex < lineIndex {
extraInfo.didChangeContent = true
extraInfo.suggestionRange = content.endIndex...content.endIndex + lines.count - 1
extraInfo.modifications.append(.inserted(content.endIndex, lines))
content.append(contentsOf: lines)
} else {
extraInfo.didChangeContent = true
extraInfo.suggestionRange = lineIndex...lineIndex + lines.count - 1
extraInfo.modifications.append(.inserted(lineIndex, lines))
content.insert(contentsOf: lines, at: lineIndex)
}
}
public func acceptSuggestion(
intoContentWithoutSuggestion content: inout [String],
cursorPosition: inout CursorPosition,
completion: CodeSuggestion,
extraInfo: inout ExtraInfo
) {
extraInfo.didChangeContent = true
extraInfo.didChangeCursorPosition = true
extraInfo.suggestionRange = nil
let start = completion.range.start
let end = completion.range.end
let suggestionContent = completion.text
let firstRemovedLine = content[safe: start.line]
let lastRemovedLine = content[safe: end.line]
let startLine = max(0, start.line)
let endLine = max(start.line, min(end.line, content.endIndex - 1))
if startLine < content.endIndex {
extraInfo.modifications.append(.deleted(startLine...endLine))
content.removeSubrange(startLine...endLine)
}
var toBeInserted = suggestionContent.breakLines(appendLineBreakToLastLine: true)
// prepending prefix text not in range if needed.
if let firstRemovedLine,
!firstRemovedLine.isEmptyOrNewLine,
start.character > 0,
start.character < firstRemovedLine.count,
!toBeInserted.isEmpty
{
let leftoverRange = firstRemovedLine.startIndex..<(firstRemovedLine.index(
firstRemovedLine.startIndex,
offsetBy: start.character,
limitedBy: firstRemovedLine.endIndex
) ?? firstRemovedLine.endIndex)
var leftover = firstRemovedLine[leftoverRange]
if leftover.hasSuffix("\n") {
leftover.removeLast(1)
}
toBeInserted[0].insert(
contentsOf: leftover,
at: toBeInserted[0].startIndex
)
}
let recoveredSuffixLength = recoverSuffixIfNeeded(
endOfReplacedContent: end,
toBeInserted: &toBeInserted,
lastRemovedLine: lastRemovedLine
)
let cursorCol = toBeInserted[toBeInserted.endIndex - 1].count - 1 - recoveredSuffixLength
let insertingIndex = min(start.line, content.endIndex)
content.insert(contentsOf: toBeInserted, at: insertingIndex)
extraInfo.modifications.append(.inserted(insertingIndex, toBeInserted))
cursorPosition = .init(
line: startLine + toBeInserted.count - 1,
character: max(0, cursorCol)
)
}
func recoverSuffixIfNeeded(
endOfReplacedContent end: CursorPosition,
toBeInserted: inout [String],
lastRemovedLine: String?
) -> Int {
// If there is no line removed, there is no need to recover anything.
guard let lastRemovedLine, !lastRemovedLine.isEmptyOrNewLine else { return 0 }
let lastRemovedLineCleaned = lastRemovedLine.droppedLineBreak()
// If the replaced range covers the whole line, return immediately.
guard end.character >= 0, end.character - 1 < lastRemovedLineCleaned.count else { return 0 }
// if we are not inserting anything, return immediately.
guard !toBeInserted.isEmpty,
let first = toBeInserted.first?.droppedLineBreak(), !first.isEmpty,
let last = toBeInserted.last?.droppedLineBreak(), !last.isEmpty
else { return 0 }
// case 1: user keeps typing as the suggestion suggests.
if first.hasPrefix(lastRemovedLineCleaned) {
return 0
}
// case 2: user also typed the suffix of the suggestion (or auto-completed by Xcode)
// locate the split index, the prefix of which matches the suggestion prefix.
var splitIndex: String.Index?
for offset in end.character..<lastRemovedLineCleaned.count {
let proposedIndex = lastRemovedLineCleaned.index(
lastRemovedLineCleaned.startIndex,
offsetBy: offset
)
let prefix = lastRemovedLineCleaned[..<proposedIndex]
if first.hasPrefix(prefix) {
splitIndex = proposedIndex
}
}
// then check how many characters are not in the suffix of the suggestion.
guard let splitIndex else { return 0 }
var suffix = String(lastRemovedLineCleaned[splitIndex...])
if last.hasSuffix(suffix) { return 0 }
// remove the first adjacent placeholder in suffix which looks like `<#Hello#>`
let regex = try! NSRegularExpression(pattern: "\\s*?<#.*?#>")
if let firstPlaceholderRange = regex.firstMatch(
in: suffix,
options: [],
range: NSRange(suffix.startIndex..., in: suffix)
)?.range,
firstPlaceholderRange.location == 0,
let r = Range(firstPlaceholderRange, in: suffix)
{
suffix.removeSubrange(r)
}
let lastInsertingLine = toBeInserted[toBeInserted.endIndex - 1]
.droppedLineBreak()
.appending(suffix)
.recoveredLineBreak()
toBeInserted[toBeInserted.endIndex - 1] = lastInsertingLine
return suffix.count
}
}
public struct SuggestionAnalyzer {
struct Result {
enum InsertPostion {
case currentLine
case nextLine
}
var insertPosition: InsertPostion
var commonPrefix: String?
}
func analyze() -> Result {
fatalError()
}
}
extension String {
/// Break a string into lines.
func breakLines(appendLineBreakToLastLine: Bool = false) -> [String] {
let lines = split(separator: "\n", omittingEmptySubsequences: false)
var all = [String]()
for (index, line) in lines.enumerated() {
if !appendLineBreakToLastLine, index == lines.endIndex - 1 {
all.append(String(line))
} else {
all.append(String(line) + "\n")
}
}
return all
}
var isEmptyOrNewLine: Bool {
isEmpty || self == "\n"
}
func droppedLineBreak() -> String {
if hasSuffix("\n") {
return String(dropLast(1))
}
return self
}
func recoveredLineBreak() -> String {
if hasSuffix("\n") {
return self
}
return self + "\n"
}
}
func longestCommonPrefix(of a: String, and b: String) -> String {
let length = min(a.count, b.count)
var prefix = ""
for i in 0..<length {
let charIndex = a.index(a.startIndex, offsetBy: i)
let firstStrChar = a[charIndex]
guard b[charIndex] == firstStrChar else { return prefix }
prefix += String(firstStrChar)
}
return prefix
}
extension Array {
subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
}