-1

I like to format my if statements like this so the conditions line up, but I can't find a setting in Xcode to do that for me:

if (ptTarget.x > fLeft
 && ptTarget.x < fRight
 && ptTarget.y > fTop
 && ptTarget.y < fBottom)
{
}

Any time I type a } Xcode will re-format it like this, which I don't want:

if (ptTarget.x > fLeft
    && ptTarget.x < fRight
    && ptTarget.y > fTop
    && ptTarget.y < fBottom)
{
}

I don't want to switch auto-formatting off, just would like this option!

4
  • Try putting the "&&" at the end of the (previous) line Commented Jul 25 at 11:25
  • Ah, the classic holy war because of where to put the operator. Love it. :D Let me add in my bit. Putting the operator on the previous line will look the next line like the first line inside the if-block, not a part of the condition. Commented Jul 25 at 11:33
  • I don't think Xcode has built-in instruments for granular enough control of code formatting (yet). I believe your best option is to look for a third-party plug-in. Commented Jul 25 at 11:34
  • i guess xcode doesn’t currently offer a built-in option to align && in if conditions as you shared. It's hardcoded to indent wrapped lines. you’d need to format manually or use an external formatter like clang-format with custom rules. Commented Jul 25 at 13:38

1 Answer 1

2

The way I deal with this is to put the left parenthesis at the end of a line and the right parenthesis at the start of a line, so that the contents of the condition have their own lines, like this:

if (
    ptTarget.x > fLeft
    && ptTarget.x < fRight
    && ptTarget.y > fTop
    && ptTarget.y < fBottom
) {
    // ...
}

You'll find that Xcode won't reformat that by changing the indentation (for example, if you type the curly brace, or if you cut the whole thing and paste it elsewhere in your code).

Now, what lines up in my approach is the line starts, not the repeated ptTarget. It happens that in the case of this particular code, you can get that simply by putting the && at the end of the preceding line:

if (
    ptTarget.x > fLeft &&
    ptTarget.x < fRight &&
    ptTarget.y > fTop &&
    ptTarget.y < fBottom
) {
    // ...
}

You can also get a more elaborately aligned arrangement by inserting your own tabs within each line; this is a bit nutty, but you could write it something like this:

if (
    ptTarget.x   >  fLeft   &&
    ptTarget.x   <  fRight  &&
    ptTarget.y   >  fTop    &&
    ptTarget.y   <  fBottom
) {
    // ...
}

There are many possible variants on this. For example, you could put each && on its own line!

if (
    ptTarget.x > fLeft
    &&
    ptTarget.x < fRight
    &&
    ptTarget.y > fTop
    &&
    ptTarget.y < fBottom
) {
    // ...
}

You can be quite elaborate, all depending on how "clear" or "readable" you want the code to be (for yourself or for other future programmers who may need to read / edit the code).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I'm not really looking for a different way to format my code, this is the way I've done it for nearly 40 years and I'm not about to change! Just wondered if there's a way to get Xcode to format it this way for me.
Yes, and I'm telling you, no, there is not, and this is what I do to work around that fact. What you do is up to you, but you cannot do the impossible, because it is (wait for it) impossible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.