Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indentation bug in Xcode [closed]

I've come across this a few times lately. I cannot isolate what causes it, but it happens if there's a line with a cout statement. Not always of course, it's kind of rare, but when it does happen, it is caused by a line that begins with cout.

It works like this: after the "faulty" cout line, Xcode automatically indents the code one or two levels more than it should. Every time I press the enter key to start writing on a new line, the code will be unnecessarily indented. There's nothing wrong with the code itself, there are no grammatical errors or anything. Just a simple cout << someString << endl; line. But if I get rid of that line, or comment it out, Xcode will use the right amount of indentation, so everything is back to normal. If I uncomment it, the indentation of the following lines is screwed up once again. Even if I delete that whole section of code and rewrite it (in case there's a funky invisible unicode in character in there or something), the same thing happens.

The most annoying thing is that this cannot be reproduced quite easily. I write C++ code fairly often, and only came across this two or three times, in totally unrelated contexts (different projects, different kinds of code, etc.). The only common thing is that it's always a cout statement that messes with the indentation of the following lines.

Btw, I recently did a clean install of OS X on my mac, and downloaded a fresh copy of Xcode from the Mac App Store, so I don't think that anything is corrupted or something.

Has anyone else noticed this? I can't seem to find a single mention of anything like this on the internet. Should I contact Apple about it or something?

Edit (2015. 11. 29.):

The problem still exists in Xcode 7.1.1 on OS X 10.11.1. Here's a picture:

Code

As you can see, the indentation of the lines that follow the cout line is messed up. Even if I select this entire piece of code and select "Re-Indent", Xcode will not fix it. In fact, even if I fix it by hand, the "Re-Indent" option will revert it back to this messed up state. And again, this happens to me in completely different projects, all created at different times, on different versions of Xcode. I have re-installed OS X (cleanly!) since I posted this question originally, and things haven't changed. This is why I'm sure it's some kind of bug in Xcode itself.

like image 935
notadam Avatar asked Dec 04 '25 12:12

notadam


1 Answers

I can confirm this bug still exists in Xcode Version 9.3 (9E145). Consider following test code snippet:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;


class Date
{

private:

    int Day;
    int Month;
    int Year;
    string DateInString;

public:

    Date(int InputDay, int InputMonth, int InputYear):
    Day (InputDay), Month (InputMonth), Year (InputYear) {};

    operator const char*()
    {
        ostringstream formattedDate;
        formattedDate << Day << " / " << Month << " / " << Year;
        DateInString = formattedDate.str();
        return DateInString.c_str();
        }

        };


        int main()
        {
            Date Holiday (25, 12, 2011);
            cout << "Holiday is on: " << Holiday << endl;
            return 0;
        }

The formattedDate << Day << " / " << Month << " / " << Year; is causing wrong indentation. When commented out the Xcode can re-indent code correctly.

like image 71
Petr Javorik Avatar answered Dec 06 '25 00:12

Petr Javorik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!