Need to know how to stop indentation

Xcode 16 unindents lines if I choose a code-completion and I cannot find a way to stop this - does anybody know how to stop it?

Here's a link to a gif demonstrating this problem - it's incredibly frustrating.

https://i.imgur.com/jaDpzpb.gif

Thanks in advance

Forgot to mention that the option "Automatically reformat when completing code is NOT checked"

Might be that you got some override from EditorConfig settings?

Xcode 16 unindents lines if I choose a code-completion and I cannot find a way to stop this - does anybody know how to stop it?

Here's a link to a gif demonstrating this problem - it's incredibly frustrating.

That's strange. I see the exact opposite. I have all indentation turned off, yet any use of code completion adds an unwanted indent.

I filed two feedbacks about this in June. Both were closed with nonsensical replies.

FB13893391

FB14102219

But I've definitely never seen it unindent the way you describe. But I'm mainly using Swift these days.

I just tried with a C++ test project and I can't reproduce what you are seeing when I have all indentation settings turned off. I also tried turning on various settings to trigger what you are seeing, but I couldn't do it.

File a feedback. Maybe you'll get better results than I did. Then turn off all indentation. At least for C++, that seems to work. I guess I should have stuck with C++ instead of switching to Swift.

I've never seen a "feedback" work -- as far as I can tell, Apple QA is just lousy these days. As you observed, yours just got closed.

I guess I'm just stuck with this issue. I wish there was an alternative to Xcode

Posting your feedback ID in these discussion forum questions does seem to result in more attention from people who can affect change.

I would like to know what happens in your case if you turn off all indentation. I would also like to know what your Xcode editing and indentation settings are. You are one of the only people I've seen who has mentioned this problem. Since I can't reproduce your specific experience, if I could reproduce it, I might be able to tell you what causes it. And maybe I could figure out a way to fix my version of the indentation problem.

I'm away from my office for a few days and I'll check when I get back but at one point I unchecked absolutely every option, including one that is called "Automatically reformat when completing code"

By the way, a google search shows quite a few people reporting this issue.

I posted my example in reddit after finding several mentions of the issue there.

https://www.reddit.com/r/Xcode/comments/1dvnhn1 how_to_disable_automatic_indentation_for_code/

I have the same issue and I also reported a bug report for that already 16. October 2024 as FB15510573. This was still for Xcode 16.0.

Meanwhile there is Xcode 16.2 and the issue has not yet been addressed. There is now a new option "Automatically reformat when completing code" but this option does simply nothing.

At the moment I keep using Xcode 15.4, as it's just imposisble to work with Xcode 16 in its current state and as I'm still using Sonoma, 15.4 is still an option.

Here are my settings:

And here is the problem (Xcode 16.2 below):

Here's how Xcode 15.4 behaves:

Here is what Apple said in reply to my bug report in June.

Please know that there is a setting that controls this (which should be off by default). In Xcode > Settings > Text Editing > Editing, it’s called “Automatically reformat when completing code”.

I don't know what "controls this" means. Does "on" mean add unwanted indentation and "off" mean do not add unwanted indentation?

But regardless, I filed another report regarding this setting having no effect. Apple's reply:

It only reformats code that’s wider than the specified column, and only if the code is made up of nested expressions (function calls, etc), that can be wrapped to new lines.

In any case, my code never extends to the "Reformat code at column" location, as shown in my screen recordings. Every line jumps around. Mine go in the opposite direction from yours. But it's never where I want it.

Obviously I don't understand the logic that Apple's using for these indentations. But it seems clear that there is only one coding style that Apple now allows - this one:

- (void) applicationDidFinishLaunching: (NSNotification *) aNotification {
    int value = 1;
}

The key is having that opening brace on the previous line. Nothing else is allowed.

I still don't understand why your indentation is going backwards from mine. But thanks for posting your settings screenshots. I'll try to reproduce this later on Sequoia. I'm still using Ventura for production work. 😄

Putting { on the same line as the method declaration doesn't change the behavior at all. It behaves exactly the same way when I do that.

And as for "Obviously I don't understand the logic that Apple uses for these indentations". How it changes the alignment depends on where you are in the code.

For a method, Apple thinks it should not be intended, and so the code of a method should always have an indention of one. In my case, the code has an indention of two (because the method itself is already indented), so Xcode reduces that to one every time you autocomplete, so the code jumps to the left.

But then I break the function call into two lines, and when you do that, Apple thinks that the next line should align with the parenthesis of the previous line, so this time the code jumps to the right to align with the parenthesis.

Basically, Apple has a weird idea of how code should be aligned, even though no one outside of the Apple universe would align code that way, and whenever you autocomplete, Xcode aligns the code the way Apple thinks it should be aligned. Also note that Apple has different ideas about alignment in Obj-C, Swift, and other languages, so this also plays a role.

However, that our company has been using the same code alignment for over 20 years and we will not allow Apple or Xcode to force a new alignment on us. If this issue is not resolved, we will stop using Xcode for development and even abandon native coding if we have to.

even abandon native coding if we have to.

Hmm, I'd love to abandon Xcode, I've never loved it, but I'm not aware of any other tool that I can use to develop and compile (and sign) code for the Mac.

And I have absolutely no interest in that dated and horrible coding style!

The key is having that opening brace on the previous line. Nothing else is allowed.

Putting { on the same line as the method declaration doesn't change the behavior at all.

It does for me. But my indentation is going the other way. I haven't been able to reproduce your unwanted indentation direction yet.

The thing that bothers me is that there are something like 30 million registered developers, and some unknown number of download-only Xcode users. I have seen two people report unwarned right indentation and two people report unwanted left indentation. What are the other millions of people doing? Are our preferences just corrupted or something?

And yet, people seem to be super excited about the new EditorConfig option. I had hoped it would allow users to control indentation settings. But no, it does virtually nothing. Instead of choosing spaces or tabs in Xcode settings, we can create a new file, add it to an Xcode project, and that will specify whether we want to use spaces or tabs. Hallelujah! That's literally all it does.

In my case, I've turned off all indentation options. But this is the one indentation that I can't disable. Since you are going the other way, try turning off your indentation settings.

I think I'm making some progress. Here is what I've learned.

  1. Indentation cannot be disabled. End of story. So there's that.
  2. The most important setting is "Indent solo { by _ spaces". This has nothing to do with the "{" character. It affects every line.
  3. There is a concept of indentation context. This is the "indent width" setting. It is the number of spaces after an opening "{" to indent the next line of code.
  4. The "indent solo { by _ spaces" defines a negative indent from the indentation context.

So, if I want to code Objective-C in this style:

- (void) foo
  {
  handleValue(3);

  if(YES)
    {
    handleValue(4);
    }
  }

I need an "Indent width" of 2 and an "indent solo { by _ spaces" of 2. They cancel each other out and my code lines up.

  1. The indentation context in Swift is more complicated. At the top level, it works like Objective-C.
  2. After the top level, Swift ignores the position of the opening brace. The context is defined in terms of the parent expression. if "indent width" and "indent solo {" are the same, then they cancel each other out and my subordinate block is indented at the same level as the parent, which is wrong in any language. In order to get my code to work (after the top level), I need to set "indent solo { by _ spaces" to zero. Luckily this field does accept zero. "Indent width" does not allow zero.

What this means for people dealing with leftward indent problems, you may be able to fix the problem by setting "indent solo { by _ spaces" to zero, but only in Objective-C/C++.

C++ seems to work like Swift's inner block logic. Set "indent solo { by _ spaces" to zero and it should fix it everywhere.

But alas, poor me. I waited ten years to switch to Swift, and I didn't wait long enough to iron out the daily bugs.

Need to know how to stop indentation
 
 
Q