Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 621
drewh Avatar asked Oct 04 '08 16:10

drewh


5 Answers

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.

like image 102
dbr Avatar answered Oct 02 '22 12:10

dbr


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

like image 22
Jens Ayton Avatar answered Oct 02 '22 10:10

Jens Ayton


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.
like image 36
Vinko Vrsalovic Avatar answered Oct 02 '22 10:10

Vinko Vrsalovic


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.

like image 24
Nathan Kinsinger Avatar answered Oct 02 '22 10:10

Nathan Kinsinger


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.

like image 23
rcw3 Avatar answered Oct 02 '22 12:10

rcw3