What started out as an impulse to build my own ChatGPT client turned into a full-blown coding assistant integrated with Xcode.
There’s more I’d like to add but here’s what it does so far:
- AI autocomplete
- LLM Chat
The code completion is comparable to other copilots on the market. Powered by Codeium.com’s generous free plan (no affiliation), I find it no better or worse than Github Copilot, in both latency and quality.
My favorite feature is Chat. It uses GPT-4 (adding Claude support as soon as I’m done writing this post.) If you summon CMD-G while inside Xcode, it automatically picks up which file you’re looking at and any code you’ve highlighted and inserts it into the LLM context. (Bonus: you can call CMD-G outside of Xcode to ask anything).
This is only possible because of the macOS Accessibility API. Xcode has a suffocatingly limited plugin framework called XcodeKit. Plugins are limited to manipulating the current source code buffer and cursor position. To work around this, Command-G uses the AX API to detect files by inspecting the Xcode application view hierarchy and to detect editor activity by listening to AX events.
To make this more clear, here’s how autocomplete is implemented:
First, the app subscribes to AX notifications from Xcode.
When notified of an edit, a request is sent to the Codeium API (direct, no middleman). After receiving a response, the code suggestion is presented to the user in an overlay.
When the user presses Tab to accept the suggestion, the app invokes the AX API to simulate activating the plugin from within Xcode.
Note: We could do without AX for this step but since developers can’t set default shortcuts for plugins, users would need to set the hotkey themselves. That ability is buried deep inside Xcode settings.
Finally, the plugin uses XcodeKit to apply the code suggestion.
If you spend time in Xcode, I hope you’ll try it: http://www.commandg.app