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

6

44

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.

Idolism answered 18/2, 2009 at 15:23 Comment(1)
objclean.com is a new tool that looks like what you need.Sincere
P
43

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
Philbo answered 6/9, 2011 at 14:46 Comment(5)
After trying on it, it seems very very good. @mmorris, do you have any updated uncrustify_obj_c.cfg file?Verdin
@Verdin Take supplied configuration from the answer and then run uncrustify with the --update-config option (explained in the answer). sort both versions of the configuration file. diff the sorted configuration files. The diff output highlights the new configuration options. Run uncrustify with --show-config to look up the new configuration options. Update the new configuration file with any desired changes.Philbo
I prefer uncrustify as a system service so that it's usable everywhere (TextEdit, BBEdit, etc.!) and you can assign it a global hotkey (command-option-control-I). I just used Automator to create a "Run Shell Script" workflow: "/usr/local/bin/uncrustify -q -c /usr/local/share/uncrustify/uncrustify.cfg -l OC+" with 'Service receives selected "text"' and check 'output replaces selected text'. Then put the workflow in your '~/Library/Services/' folder.Herisau
The link to the Example Uncrustify 0.60 config is broken.Windward
@Windward The link to the Example Uncrustify 0.60 config has been restored.Philbo
W
8

XCode has a code formatter built in - it's a little basic but can re-indent your code. First set your indentation preferences in Xcode (as mouviciel suggests). Then select the source you want to reintend and choose Edit | Format | Re-indent.

If you want to go further, Hackertoys has instructions for adding uncrustify support to Xcode. I have not tried this.

Wintergreen answered 17/4, 2009 at 7:33 Comment(0)
C
3

AppCode from Jetbrains has a very good code formatter. For my taste uncrustify feels much too hacky.

Catholicism answered 26/5, 2013 at 7:6 Comment(0)
K
2

After tinkering with multiple external formatters and the weak internal xcode formatter, I finally settled with uncrustify. Uncrustify has fairly good Objective-C support, can easily be integrated with xcode as a user script, and provides a centralized formatter for pretty much all languages that xcode natively supports.

The biggest hurdle with xcode is the daunting configuration file. My recommendation, take one of the supplied sample configs (ben2.cfg is very good), merge in the objc.cfg sample, and tweak as necessary.

Kebab answered 28/2, 2010 at 21:52 Comment(0)
G
1

Reportedly, bcpp works just fine on obj-c, and it has a TextMate plugin, too.

Genarogendarme answered 19/2, 2009 at 16:33 Comment(0)
F
0

A partial answer is that some little things can be set from within Xcode, as 2-space indents. See Indentation Preferences in Xcode Workspace Guide.

Ferrick answered 19/2, 2009 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.