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
89 lines (75 loc) · 2.63 KB
/
SourceEditorExtension.swift
File metadata and controls
89 lines (75 loc) · 2.63 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
import Client
import Foundation
import Preferences
import XcodeKit
class SourceEditorExtension: NSObject, XCSourceEditorExtension {
var builtin: [[XCSourceEditorCommandDefinitionKey: Any]] {
[
GetSuggestionsCommand(),
AcceptSuggestionCommand(),
RejectSuggestionCommand(),
NextSuggestionCommand(),
PreviousSuggestionCommand(),
RealtimeSuggestionsCommand(),
PrefetchSuggestionsCommand(),
ChatWithSelectionCommand(),
PromptToCodeCommand(),
SeparatorCommand().named("------"),
].map(makeCommandDefinition)
}
var custom: [[XCSourceEditorCommandDefinitionKey: Any]] {
customCommands()
}
var commandDefinitions: [[XCSourceEditorCommandDefinitionKey: Any]] {
return builtin + custom
}
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.getXPCServiceVersion()
}
#endif
}
}
let identifierPrefix: String = Bundle.main.bundleIdentifier ?? ""
var customCommandMap = [String: String]()
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()
}
func customCommands() -> [[XCSourceEditorCommandDefinitionKey: Any]] {
var definitions = [[XCSourceEditorCommandDefinitionKey: String]]()
for command in UserDefaults.shared.value(for: \.customCommands) {
let identifier = identifierPrefix + "CustomCommand\(command.id)"
definitions.append([
.classNameKey: CustomCommand.className(),
.identifierKey: identifier,
.nameKey: command.name,
])
customCommandMap[identifier] = command.id
}
return definitions
}