forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveToFocusedCodeFunction.swift
More file actions
55 lines (44 loc) · 1.6 KB
/
MoveToFocusedCodeFunction.swift
File metadata and controls
55 lines (44 loc) · 1.6 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
import ASTParser
import Foundation
import OpenAIService
import SuggestionModel
struct MoveToFocusedCodeFunction: ChatGPTFunction {
typealias Arguments = NoArguments
struct Result: ChatGPTFunctionResult {
var range: CursorRange
var botReadableContent: String {
"Editing Document Context is updated to display code at \(range)."
}
}
struct E: Error, LocalizedError {
var errorDescription: String?
}
var name: String {
"moveToFocusedCode"
}
var description: String {
"Move editing document context to the selected or focused code"
}
weak var contextCollector: ActiveDocumentChatContextCollector?
init(contextCollector: ActiveDocumentChatContextCollector) {
self.contextCollector = contextCollector
}
func prepare(reportProgress: @escaping (String) async -> Void) async {
await reportProgress("Finding the focused code..")
}
func call(
arguments: Arguments,
reportProgress: @escaping (String) async -> Void
) async throws -> Result {
await reportProgress("Finding the focused code..")
contextCollector?.activeDocumentContext?.moveToFocusedCode()
guard let newContext = contextCollector?.activeDocumentContext?.focusedContext else {
let progress = "Failed to move to focused code."
await reportProgress(progress)
throw E(errorDescription: progress)
}
let progress = "Looking at \(newContext.codeRange)."
await reportProgress(progress)
return .init(range: newContext.codeRange)
}
}