How to declare commands in Xcode extenstions

Issue #638

Use commandDefinitions in XCSourceEditorExtension.

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

Written by

I’m open source contributor, writer, speaker and product maker.

Start the conversation