Build an Interactive Xcode Plugin

2023, Apr 01    

Looking at ways of building a Xcode extension with interactive UI. 😛 Using the project I mentioned in my previous blog - nef-plugin.

1. Xcode Extension Iteself

The xcode extension itself is straight forward and simple, which uses the key XCSourceEditorExtensionPrincipalClass info.plist for loading the entrance of the plugin. The class delcared by this field should further implement XCSourceEditorExtension protocol to indicate all the possible command definitions.

class SourceEditorExtension: NSObject, XCSourceEditorExtension {
    var commandDefinitions: [[XCSourceEditorCommandDefinitionKey : Any]] {
        // return the definition of the command to expose
        return [[:]]
    }
}

Each element in the array should contain basic information, such as identifierKey, classNameKey, nameKey.All those keys will be further utilized to initiate the command items. Which will eventually be rendered in [editor] / [${your-extension-name}] / [${commands}].

When each of the command get clicked, the XCSourceEditorCommand instance will be invoked with invocation model of XCSourceEditorCommandInvocation to the instance. From instance, information like UTI, range of selected code and etc. the XCSourceTextBuffer provides all the mutatable source code which the command can edit.

So by default, the extension can only handle synchronous code formatting/template creation jobs. How can we provide a xcode extension with interactive UI? Let’s move on to the next part.

2. The Interactive UI Part

To make it short, in order to support interactive UI, the extension have to delegate out command to Mac app in the form of scheme. Taking the nef-plugin as an example, to see how it works:

In the following blog, I will try to create extra features based on CopilotForXcode.

3. References

TOC