views:

1772

answers:

5
+8  Q: 

Objective-C Tidy

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?

+6  A: 

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.
Vinko Vrsalovic
It's on my OS X 10.5.5 machine. I imagine it may be installed as part of the developer tools.
dbr
It is installed on 10.6
Joe
+12  A: 

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
dig up solely because of the name of the project
slf
+5  A: 

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

Ahruman
+4  A: 

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
+1  A: 

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