How to declare commands in Xcode extenstions

Issue #638

Use commandDefinitions in XCSourceEditorExtension.

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
import Foundation
import XcodeKit

class SourceEditorExtension: NSObject, XCSourceEditorExtension {
func extensionDidFinishLaunching() {

}

var commandDefinitions: [[XCSourceEditorCommandDefinitionKey: Any]] {
func makeDef(
_ className: String,
_ commandName: String
) -> [XCSourceEditorCommandDefinitionKey: Any] {
guard let bundleId = Bundle(for: type(of: self)).bundleIdentifier else { return [:] }

return [
XCSourceEditorCommandDefinitionKey.identifierKey: bundleId + className,
XCSourceEditorCommandDefinitionKey.classNameKey: className,
XCSourceEditorCommandDefinitionKey.nameKey: commandName
]
}

return [
makeDef(TypeCommand.className(), "Type"),
makeDef(ReloadCommand.className(), "Reload"),
]
}
}

There is a weird crash that we can’t seem to declare functions or use commandDefinitions, the workaround is to declare in plist

Read more

Comments