forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceEditorExtension.swift
More file actions
61 lines (54 loc) · 1.84 KB
/
SourceEditorExtension.swift
File metadata and controls
61 lines (54 loc) · 1.84 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
import Client
import Foundation
import XcodeKit
class SourceEditorExtension: NSObject, XCSourceEditorExtension {
var commandDefinitions: [[XCSourceEditorCommandDefinitionKey: Any]] {
[
GetSuggestionsCommand(),
AcceptSuggestionCommand(),
RejectSuggestionCommand(),
NextSuggestionCommand(),
PreviousSuggestionCommand(),
ToggleRealtimeSuggestionsCommand(),
RealtimeSuggestionsCommand(),
PrefetchSuggestionsCommand(),
ChatWithSelectionCommand(),
ExplainSelectionCommand(),
].map(makeCommandDefinition)
}
func extensionDidFinishLaunching() {
#if DEBUG
// In a debug build, we usually want to use the XPC service run from Xcode.
#else
// When the source extension is initialized
// we can call a random command to wake up the XPC service.
Task.detached {
try await Task.sleep(nanoseconds: 1_000_000_000)
let service = try getService()
_ = try await service.checkStatus()
}
#endif
}
}
private let identifierPrefix: String = Bundle.main.bundleIdentifier ?? ""
protocol CommandType: AnyObject {
var commandClassName: String { get }
var identifier: String { get }
var name: String { get }
}
extension CommandType where Self: NSObject {
var commandClassName: String { Self.className() }
var identifier: String { commandClassName }
}
extension CommandType {
func makeCommandDefinition() -> [XCSourceEditorCommandDefinitionKey: Any] {
[.classNameKey: commandClassName,
.identifierKey: identifierPrefix + identifier,
.nameKey: name]
}
}
func makeCommandDefinition(_ commandType: CommandType)
-> [XCSourceEditorCommandDefinitionKey: Any]
{
commandType.makeCommandDefinition()
}