问题
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).
回答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?)
回答2:
Or try xcode-preferences-text editing-indentation: check syntax aware indenting, check automatic indenting for "Return".
来源:https://stackoverflow.com/questions/4177390/indenting-continuation-lines-in-xcode