5

Can I get Xcode's automatic indentation to indent continuation lines?

I want:

BOOL someLongVariableName = someLongValue
    | someOtherLongValue
    | moreLongValues

BOOL someOtherLongVariableName =
    someEvenLongerValue;

[someLongVariableName
    performSomeAction:someLongArgument]

I currently get:

BOOL someLongVariableName = someLongValue
| someOtherLongValue
| moreLongValues

BOOL someOtherLongVariableName =
someEvenLongerValue;

[someLongVariableName
 performSomeAction:someLongArgument]

To be clear:

  • I'm using explicit line breaks not automatic wrapping.
  • I want the correct indent while editing and immediately after pressing return, not after running an external program (like uncrustify).
nschum
  • 15,322
  • 5
  • 58
  • 56
  • For the first one: consider using () around the statement. For the rest: consider renaming :) – w-m Nov 15 '10 at 13:20

2 Answers2

1

I ended up integrating uncrustify to partially get what I wanted. (Case 3 is still off, though.)

Xcode integration

To get Xcode to indent the code automatically, I've created an "Aggregate" target with a "Run Script" phase:

find . -name '*.[mh]' -print0 \
    | xargs -0 git diff-index HEAD -- | grep -v "D\t" | cut -c100- \
    | xargs uncrustify -l OC --replace --no-backup -c uncrustify.cfg

This runs uncrustify on all files that are marked as changed in git. I've added my app target as a dependency to the format target, so it only formats if compilation succeeds. (Important, since uncrustify would be confused by broken syntax.) Finally, I've added the format target to my scheme, so every build starts a format. Xcode usually reloads the formatted file on its own.

The relevant setting my uncrustify.cfg is indent_continue = 4.

Problems

Undo information is lost when Xcode reloads the formatted file. I could run the script from a git pre-commit hook, but I prefer quicker results.

Another downside is that Objective-C support in uncrustify isn't perfect, but there seems to be no alternative. (Maybe clang-format someday?)

nschum
  • 15,322
  • 5
  • 58
  • 56
  • Could use both. For example run `clang-format` first, then `uncrustify` on top, or the other way around. I find that clang-format handles few ObjC things better than uncrustify, for example spaces in array literals and space after `@property` (maybe uncrustify can handle that too). – i4niac May 21 '15 at 00:52
  • Yes, since writing the answer, `clang-format` has matured a lot. I would probably be using it exclusively now (had I not moved on to Swift …). – nschum May 21 '15 at 06:03
-1

Or try xcode-preferences-text editing-indentation: check syntax aware indenting, check automatic indenting for "Return".

Roland
  • 388
  • 1
  • 4
  • 21
  • -1 This ignores the premise of the question (which is about modifying the indentation depth). In fact this is even the default setting. – nschum Sep 14 '11 at 18:36