Skip to content

Commit fc9d516

Browse files
committed
Fix extension command
1 parent f3bb9cc commit fc9d516

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

EditorExtension/CustomCommand.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ import Foundation
44
import XcodeKit
55

66
class CustomCommand: NSObject, XCSourceEditorCommand, CommandType {
7-
let name: String
8-
9-
init(name: String) {
10-
self.name = name
11-
}
12-
7+
var name: String = ""
8+
139
func perform(
1410
with invocation: XCSourceEditorCommandInvocation,
1511
completionHandler: @escaping (Error?) -> Void
@@ -18,7 +14,7 @@ class CustomCommand: NSObject, XCSourceEditorCommand, CommandType {
1814
do {
1915
let service = try getService()
2016
if let content = try await service.customCommand(
21-
name: name,
17+
name: customCommandMap[invocation.commandIdentifier] ?? "",
2218
editorContent: .init(invocation)
2319
) {
2420
invocation.accept(content)

EditorExtension/SeparatorCommand.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import Foundation
44
import XcodeKit
55

66
class SeparatorCommand: NSObject, XCSourceEditorCommand, CommandType {
7-
let name: String
7+
var name: String = ""
88

9-
init(_ name: String) {
10-
self.name = name
11-
}
12-
139
func perform(
1410
with invocation: XCSourceEditorCommandInvocation,
1511
completionHandler: @escaping (Error?) -> Void
1612
) {
1713
completionHandler(nil)
1814
}
15+
16+
func named(_ name: String) -> Self {
17+
self.name = name
18+
return self
19+
}
1920
}

EditorExtension/SourceEditorExtension.swift

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import Client
22
import Foundation
3-
import XcodeKit
43
import Preferences
4+
import XcodeKit
55

66
class 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

4352
protocol 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

Comments
 (0)