forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.swift
More file actions
98 lines (92 loc) · 3.7 KB
/
Helpers.swift
File metadata and controls
98 lines (92 loc) · 3.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
import SuggestionBasic
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 newSelection = updatedContent.newSelection {
mutateCompleteBuffer(
modifications: updatedContent.modifications,
restoringSelections: false
)
buffer.selections.removeAllObjects()
buffer.selections.add(XCSourceTextRange(
start: .init(line: newSelection.start.line, column: newSelection.start.character),
end: .init(line: newSelection.end.line, column: newSelection.end.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),
cursorOffset: -1,
selections: buffer.selections.map {
let sl = ($0 as? XCSourceTextRange)?.start.line ?? 0
let sc = ($0 as? XCSourceTextRange)?.start.column ?? 0
let el = ($0 as? XCSourceTextRange)?.end.line ?? 0
let ec = ($0 as? XCSourceTextRange)?.end.column ?? 0
return Selection(
start: CursorPosition( line: sl, character: sc ),
end: CursorPosition( line: el, character: ec )
)
},
tabSize: buffer.tabWidth,
indentSize: buffer.indentationWidth,
usesTabsForIndentation: buffer.usesTabsForIndentation
)
}
}
/// https://gist.github.com/swhitty/9be89dfe97dbb55c6ef0f916273bbb97
extension Task where Failure == Error {
// Start a new Task with a timeout. If the timeout expires before the operation is
// completed then the task is cancelled and an error is thrown.
init(
priority: TaskPriority? = nil,
timeout: TimeInterval,
operation: @escaping @Sendable () async throws -> Success
) {
self = Task(priority: priority) {
try await withThrowingTaskGroup(of: Success.self) { group -> Success in
group.addTask(operation: operation)
group.addTask {
try await _Concurrency.Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000))
throw TimeoutError()
}
guard let success = try await group.next() else {
throw _Concurrency.CancellationError()
}
group.cancelAll()
return success
}
}
}
}
private struct TimeoutError: LocalizedError {
var errorDescription: String? = "Task timed out before completion"
}