16

I have a somewhat messily-formatted Objective-C code base. Is there a way to have Xcode reformat an entire project to conform to a coding standard (i.e., properly indent, spaces v. tabs, etc.)? Are there other tools that might accomplish this?

logancautrell
  • 8,762
  • 3
  • 39
  • 50
drewh
  • 10,077
  • 7
  • 34
  • 43

8 Answers8

23

Uncrustify: http://uncrustify.sourceforge.net/

Source Code Beautifier for C, C++, C#, ObjectiveC, D, Java, Pawn and VALA

If you want something simpler, you could probably get some way by simply stripping out all the white-space/line-breaks, and adding a new line-break on ; { }, and manually re-indenting the code. It won't be anywhere near perfectly laid out code, and reindenting could be a pain on large code, but it will be consistent.

dbr
  • 165,801
  • 69
  • 278
  • 343
8

A simple, but limited, solution is Edit->Format->Re-Indent in Xcode, which will apply your current indentation settings (Xcode->Preferences…->Indentation).

Jens Ayton
  • 14,532
  • 3
  • 33
  • 47
7

According to this blog post, bcpp works with Objective-C.

In addition, the tool indent might help you. It's aimed at plain C but has a gazillion options that could help. I don't know if it comes by default on OS X though.

NAME
       indent - changes the appearance of a C program by inserting or deleting
       whitespace.

SYNOPSIS
       indent [options] [input-files]

       indent [options] [single-input-file] [-o output-file]

       indent --version

DESCRIPTION
       This man page is generated from the file indent.texinfo.  This is  Ediâ
       tion  of "The indent Manual", for Indent Version , last updated .

       The  indent  program  can  be used to make code easier to read.  It can
       also convert from one style of writing C to another.

       indent understands a substantial amount about the syntax of C,  but  it
       also attempts to cope with incomplete and misformed syntax.
logancautrell
  • 8,762
  • 3
  • 39
  • 50
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
  • It's on my OS X 10.5.5 machine. I imagine it may be installed as part of the developer tools. – dbr Oct 04 '08 at 16:46
5

From Xcode:

1) Change the Indentation preferences to match what you want.

2) Select a file to work on and Select All (cmd-A)

3) Shift Left (cmd-[) several times until all lines are at the left edge of the window.

4) Use Re-Indent Selection (from Edit->Format-> or from the right-click contextual menu)

Only works on one file at a time, not the whole project. Also only deals with indentation.

Nathan Kinsinger
  • 23,641
  • 2
  • 30
  • 19
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 uncrustify 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.

rcw3
  • 3,034
  • 1
  • 27
  • 24
0

The sole feature of the "GTMXcodePlugin", aka Google Toolbox For Mac Xcode Plugin - does a great job of tidying objective-c code, by effectively, and safely trimming whitespaces.

The GTM Xcode 4 plugin currently only adds a "Clean Up Whitespace" menu item to the end of the "Edit" menu to remove unnecessary end of line white space from text files. Hopefully we will add more features soon. It has only been tested against Xcode 4.2.

Alex Gray
  • 16,007
  • 9
  • 96
  • 118
0

The newest, bestest way of doing this, as of this writing, is SpaceCommander. It's based on top of clang, and has lots of other neat features, and is maintained and active as of this writing.

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
0

One way to go that uses uncrustify in a different context is

http://universalindent.sourceforge.net/

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421