🧽 Magic Sponge
Magic Sponge Code Completion from Andrew Eades on Vimeo.
Add Magic Sponge to Xcode
- Go to System Preferences in the Apple menu.
- Choose Extensions (🧩).
- Enable Magic Sponge.
Set Up a Hot Key In Xcode
- From Preferences go to the Key Bindings.
- Search for Magic Sponge.
- Set the key you want to use to invoke Magic Sponge . I use ⌥` (option-backtick).
Summon the Sponge
- Whenever you want to summon the sponge, just hit the hot key and watch the magic happen.
What does it do?
Magic Sponge enhances code completion by adding some smarts to a single hot key press. It uses the position of possibly multiple cursor selections to determine what your intention was and fills in for you. It can fix typos, create variables with sensible names, make init functions super-quick to write, and extract variables intelligently.
Typo Corrections
retrun
→return
swicth
→switch
Self Equal
init(magic: Int) {
magicâ–Š
becomes:
init(magic: Int) {
self.magic = magicâ–Š
Instance Variable
Magicâ–Š
becomes:
let magic = Magicâ–Š
Replace Expression with Extracted Variable
Now it gets more complicated using Xcode’s multiple selection feature. This is the real power of Magic Sponge and the motivation for making it. If you have ever found yourself creating a variable from an exisitng expression and then having to paste the variable back in place of the expression, this is for you.
Select an expression and set a cursor to where you want to create a variable that you want to use in place of the expression:
e.g.
I want to convert:
if magic.isHappening() {
print("Yay!")
}
into:
let isMagicHappening = magic.isHappening()
if isMagicHappening {
print("Yay!")
}
First put a cursor in the position you want to create the new variable, hold control-⇧ and select magic.isHappening()
. Now summon the sponge.
You will be given:
let â–Š = magic.isHappening()
if â–Š {
print("Yay!")
}
with 2 cursor points to type in the new variable name.
Try it out. It’s really cool.
What Next?
I’ll be adding new features to Magic Sponge as I think of them, but if there is something you think would be really useful, let me know. Features should leverage the existing power of Xcode’s code completion and multiple cursor selections.