11import Client
22import Foundation
3- import XcodeKit
43import Preferences
4+ import XcodeKit
55
66class SourceEditorExtension : NSObject , XCSourceEditorExtension {
7- var commandDefinitions : [ [ XCSourceEditorCommandDefinitionKey : Any ] ] {
7+ var builtin : [ [ XCSourceEditorCommandDefinitionKey : Any ] ] {
88 [
99 GetSuggestionsCommand ( ) ,
1010 AcceptSuggestionCommand ( ) ,
@@ -15,11 +15,18 @@ class SourceEditorExtension: NSObject, XCSourceEditorExtension {
1515 RealtimeSuggestionsCommand ( ) ,
1616 PrefetchSuggestionsCommand ( ) ,
1717 ChatWithSelectionCommand ( ) ,
18-
19- SeparatorCommand ( " # Custom Commands: " ) ,
18+ PromptToCodeCommand ( ) ,
19+
20+ SeparatorCommand ( ) . named ( " ------ " ) ,
2021 ] . map ( makeCommandDefinition)
21-
22- + customCommands( ) . map ( makeCommandDefinition)
22+ }
23+
24+ var custom : [ [ XCSourceEditorCommandDefinitionKey : Any ] ] {
25+ customCommands ( )
26+ }
27+
28+ var commandDefinitions : [ [ XCSourceEditorCommandDefinitionKey : Any ] ] {
29+ return builtin + custom
2330 }
2431
2532 func extensionDidFinishLaunching( ) {
@@ -38,7 +45,9 @@ class SourceEditorExtension: NSObject, XCSourceEditorExtension {
3845 }
3946}
4047
41- private let identifierPrefix : String = Bundle . main. bundleIdentifier ?? " "
48+ let identifierPrefix : String = Bundle . main. bundleIdentifier ?? " "
49+
50+ var customCommandMap = [ String: String] ( )
4251
4352protocol CommandType : AnyObject {
4453 var commandClassName : String { get }
@@ -65,8 +74,23 @@ func makeCommandDefinition(_ commandType: CommandType)
6574 commandType. makeCommandDefinition ( )
6675}
6776
68- func customCommands( ) -> [ CustomCommand ] {
69- UserDefaults . shared. value ( for: \. customCommands) . map {
70- CustomCommand ( name: $0. name)
77+ func customCommands( ) -> [ [ XCSourceEditorCommandDefinitionKey : Any ] ] {
78+ let definitions = UserDefaults . shared. value ( for: \. customCommands) . map {
79+ [
80+ XCSourceEditorCommandDefinitionKey . classNameKey: CustomCommand . className ( ) ,
81+ XCSourceEditorCommandDefinitionKey
82+ . identifierKey: identifierPrefix + " CustomCommand \( $0. name. hashValue) " ,
83+ . nameKey: $0. name,
84+ ]
7185 }
86+
87+ for item in definitions {
88+ let name = item [ . nameKey]
89+ let identifier = item [ . identifierKey]
90+ if let identifier {
91+ customCommandMap [ identifier] = name
92+ }
93+ }
94+
95+ return definitions
7296}
0 commit comments