forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.swift
More file actions
68 lines (65 loc) · 2.48 KB
/
Helpers.swift
File metadata and controls
68 lines (65 loc) · 2.48 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
import CopilotModel
import Foundation
import XcodeKit
import XPCShared
extension XCSourceEditorCommandInvocation {
func mutateCompleteBuffer(modifications: [Modification], restoringSelections restore: Bool) {
if restore {
let selectionsRangesToRestore = buffer.selections
.compactMap { $0 as? XCSourceTextRange }
buffer.selections.removeAllObjects()
buffer.lines.apply(modifications)
for range in selectionsRangesToRestore {
buffer.selections.add(range)
}
} else {
buffer.lines.apply(modifications)
}
}
func accept(_ updatedContent: UpdatedContent) {
if let newCursor = updatedContent.newCursor {
mutateCompleteBuffer(
modifications: updatedContent.modifications,
restoringSelections: false
)
buffer.selections.removeAllObjects()
buffer.selections.add(XCSourceTextRange(
start: .init(line: newCursor.line, column: newCursor.character),
end: .init(line: newCursor.line, column: newCursor.character)
))
} else {
mutateCompleteBuffer(
modifications: updatedContent.modifications,
restoringSelections: true
)
}
}
}
extension EditorContent {
init(_ invocation: XCSourceEditorCommandInvocation) {
let buffer = invocation.buffer
self.init(
content: buffer.completeBuffer,
lines: buffer.lines as! [String],
uti: buffer.contentUTI,
cursorPosition: ((buffer.selections.lastObject as? XCSourceTextRange)?.end).map {
CursorPosition(line: $0.line, character: $0.column)
} ?? CursorPosition(line: 0, character: 0),
selections: buffer.selections.map {
Selection(
start: CursorPosition(
line: ($0 as! XCSourceTextRange).start.line,
character: ($0 as! XCSourceTextRange).start.column
),
end: CursorPosition(
line: ($0 as! XCSourceTextRange).end.line,
character: ($0 as! XCSourceTextRange).end.column
)
)
},
tabSize: buffer.tabWidth,
indentSize: buffer.indentationWidth,
usesTabsForIndentation: buffer.usesTabsForIndentation
)
}
}