Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Xcode plugin for code auto formatting [closed]

I am searching for a plugin to auto format objective-c code within XCode, it would also be very helpful to have a set of optional styling formats,

I'm aiming to follow Google's code convention: http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml as a start, any help is appreciated.

like image 731
Carlos Barbosa Avatar asked Feb 18 '09 15:02

Carlos Barbosa


People also ask

How do I get beautify code in Xcode?

Select first the text you want to format and then press Ctrl + I . Use Cmd + A first if you wish to format all text in the selected file.

Does Xcode have Formatter?

In the Xcode menu click [Editor] -> [Swimat] -> [Format] then the current active file will reformat.


1 Answers

updated April 2013

As Roger Nolan and rcw3 suggested, you can go a lot further with Uncrustify. The latest release version can be download here.


Integration Into Xcode

Integration into Xcode is dependent upon the version of Xcode that is installed.

  • Xcode 4.6 & newer

Benoît Bourdon's BBUncrustifyPlugin-Xcode

This plugin provides access to Uncrustify right from Xcode's "Edit" menu ("Uncrustify Selected Files", "Uncrustify Active File", and "Uncrustify Selected Lines"). Just build the project and the plugin is compiled and installed.

As of April 2, 2013, Uncrustify 0.60 and an Uncrustify configuration file are include with the plugin. To use a custom Uncrustify configuration file, make sure to read the section titled "How to customize the Uncrustify configuration?" of README.md.

Note: To use Uncrustify from the command line, for example as part of a git hook, I recommend building and installing Uncrustify independently. For consistency sake, I also recommend replacing the version of Uncrustify that comes with the plugin with a link to the version Uncrustify that was independently built and installed. The following is how to do that:

cd ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/UncrustifyPlugin.xcplugin/Contents/Resources mv uncrustify uncrustify.orig ln -s /usr/local/bin/uncrustify uncrustify 
  • Xcode 4.x

Jonah Williams' Code Formatting in Xcode 4

  • Xcode 3.x

Jonah Williams' Code Formatting in Xcode


Example Uncrustify Configuration File

The Uncrustify 0.60 configuration file that I use for Objective-C is located here.


Update the Uncrustify Configuration File for a New Version of Uncrustify

To update the Uncrustify configuration file for a new version of Uncrustify, start a Terminal and type:

mv uncrustify.cfg uncrustify.cfg.old uncrustify -c uncrustify.cfg.old --update-config > uncrustify.cfg 

To see the changes made to the Uncrustify configuration, in the terminal type:

diff uncrustify.cfg.old uncrustify.cfg 

To View a Documented Version of the Uncrustify Configuration File

To view a documented version of the Uncrustify configuration file, start a Terminal and type:

uncrustify -c uncrustify.cfg --show-config 
like image 191
mmorris Avatar answered Sep 20 '22 12:09

mmorris