forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveToCodeAroundLineFunction.swift
More file actions
68 lines (56 loc) · 1.94 KB
/
MoveToCodeAroundLineFunction.swift
File metadata and controls
68 lines (56 loc) · 1.94 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
import ASTParser
import Foundation
import OpenAIService
import SuggestionModel
struct MoveToCodeAroundLineFunction: ChatGPTFunction {
struct Arguments: Codable {
var line: Int
}
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 {
"getCodeAtLine"
}
var description: String {
"Get the code at the given line, so you can answer the question about the code at that line."
}
var argumentSchema: JSONSchemaValue { [
.type: "object",
.properties: [
"line": [
.type: "number",
.description: "The line number in the file",
],
],
.required: ["line"],
] }
weak var contextCollector: ActiveDocumentChatContextCollector?
init(contextCollector: ActiveDocumentChatContextCollector) {
self.contextCollector = contextCollector
}
func prepare(reportProgress: @escaping (String) async -> Void) async {
await reportProgress("Finding code around..")
}
func call(
arguments: Arguments,
reportProgress: @escaping (String) async -> Void
) async throws -> Result {
await reportProgress("Finding code around line \(arguments.line)..")
contextCollector?.activeDocumentContext?.moveToCodeAroundLine(arguments.line)
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)
}
}