Looking at the title of this post you’re probably asking yourself “why the hell would I not use the built-in formatter?” And that’s a valid question. In my case I am the only member of my team that uses AppCode and we now are enforcing style using uncrustify. This allows me to guarantee we match.
I sadly can’t share my scripts but if you Google it or look here-ish you will find some guides on how to only run this script against files that have changed in your project. But I can share a very simple script. Save this out as “appcode_uncrusty.sh” somewhere in your project folder.
uncrustify -l OC -c uncrustify.cfg -F files-to-uncrustify.txt --no-backup
*NOTE* This script is looking for a txt of files to process. You can replace that with the file, or if you really want, pass the current open file in as a param during the tool creation below. This is beyond the scope of this post but note it is possible.
You’ll need to put your uncrustify config somewhere now. I leave it in the root of the project.
External Tool Creation
Once you have that done fire up AppCode. In there we’ll go to Preferences -> Tools -> External Tools and add a new tool.
As you can see from the screenshot above it’s pretty simple what we want to do.
- Give the tool a name.
- Next in program give the path to your script from the root of your project. Because it’s a shell script I have the ./ in front of the path to execute the script.
- Finally give the tool a working directory. $ProjectFileDir$ is an AppCode macro that is related to the project you currently have open.
Once you have that you can also add this tool as a menu option and as a keyboard shortcut.
For a menu option go to Preferences->Apperance & Behaviour->Menus and Toolbars. There expand the Main menu folder. Here you can create a menu entry.
To add a keyboard short cut go to Preferences->Keymap and filter the list with “external”. You should see a folder “External Tools”. Expand it and find the Uncrustify tool you created earlier. Double click and you’ll get a menu to add a new shortcut.
And there you go. Uncrustify running from AppCode with menu options and keyboard shortcuts.