-
Notifications
You must be signed in to change notification settings - Fork 623
/
Copy pathSourceEditorExtension.swift
90 lines (75 loc) · 2.46 KB
/
SourceEditorExtension.swift
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
90
import Client
import Foundation
import GitHubCopilotService
import Preferences
import XcodeKit
#if canImport(PreferencesPlus)
import PreferencesPlus
#endif
class SourceEditorExtension: NSObject, XCSourceEditorExtension {
var builtin: [[XCSourceEditorCommandDefinitionKey: Any]] {
[
AcceptSuggestionCommand(),
RejectSuggestionCommand(),
GetSuggestionsCommand(),
NextSuggestionCommand(),
PreviousSuggestionCommand(),
SyncTextSettingsCommand(),
ToggleRealtimeSuggestionsCommand(),
].map(makeCommandDefinition)
}
var chat: [[XCSourceEditorCommandDefinitionKey: Any]] {
[
OpenChatCommand()
].map(makeCommandDefinition)
}
var additionalBuiltin: [[XCSourceEditorCommandDefinitionKey: Any]] {
[
OpenSettingsCommand(),
].map(makeCommandDefinition)
}
var commandDefinitions: [[XCSourceEditorCommandDefinitionKey: Any]] {
var definitions = builtin
if FeatureFlagNotifierImpl.shared.featureFlags.chat {
definitions += chat
}
definitions += additionalBuiltin
return definitions
}
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()
}