Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indenting Continuation Lines in Xcode

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).
like image 235
nschum Avatar asked Nov 25 '22 17:11

nschum


1 Answers

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?)

like image 101
nschum Avatar answered Dec 22 '22 23:12

nschum