-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathGetErrorsTool.swift
More file actions
74 lines (68 loc) · 2.88 KB
/
GetErrorsTool.swift
File metadata and controls
74 lines (68 loc) · 2.88 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
import JSONRPC
import Foundation
import ConversationServiceProvider
import XcodeInspector
import AppKit
public class GetErrorsTool: ICopilotTool {
public func invokeTool(
_ request: InvokeClientToolRequest,
completion: @escaping (AnyJSONRPCResponse) -> Void,
chatHistoryUpdater: ChatHistoryUpdater?,
contextProvider: ToolContextProvider?
) -> Bool {
guard let params = request.params,
let input = params.input,
let filePaths = input["filePaths"]?.value as? [String]
else {
completeResponse(request, completion: completion)
return true
}
guard let xcodeInstance = XcodeInspector.shared.xcodes.first(
where: {
$0.workspaceURL?.path == contextProvider?.chatTabInfo.workspacePath
}),
let documentURL = xcodeInstance.realtimeDocumentURL,
filePaths.contains(where: { URL(fileURLWithPath: $0) == documentURL })
else {
completeResponse(request, completion: completion)
return true
}
/// Not leveraging the `getFocusedEditorContent` in `XcodeInspector`.
/// As the resolving should be sync. Especially when completion the JSONRPCResponse
let focusedElement: AXUIElement? = try? xcodeInstance.appElement.copyValue(key: kAXFocusedUIElementAttribute)
let focusedEditor: SourceEditor?
if let editorElement = focusedElement, editorElement.isSourceEditor {
focusedEditor = .init(runningApplication: xcodeInstance.runningApplication, element: editorElement)
} else if let element = focusedElement, let editorElement = element.firstParent(where: \.isSourceEditor) {
focusedEditor = .init(runningApplication: xcodeInstance.runningApplication, element: editorElement)
} else {
focusedEditor = nil
}
var errors: String = ""
if let focusedEditor
{
let editorContent = focusedEditor.getContent()
let errorArray: [String] = editorContent.lineAnnotations.map {
"""
<uri>\(documentURL.absoluteString)</uri>
<error>
<message>\($0.message)</message>
<range>
<start>
<line>\($0.line)</line>
<character>0</character>
</start>
<end>
<line>\($0.line)</line>
<character>0</character>
</end>
</range>
</error>
"""
}
errors = errorArray.joined(separator: "\n")
}
completeResponse(request, response: errors, completion: completion)
return true
}
}