Visual Studio 2019 closing curly brace not under the opening brace (it's fully left aligned instead)

Viewed 5815

Recently the closing curly brace start to align on the fully left instead of under the opening brace, but only when the braces are empties :

(I'm in Visual Studio 2019, in C# code) Steps :

write this :

namespace myNameSpace
{
    class myClass
    { }
}

Then hit return while in between the class braces, it did this :

namespace myNameSpace
{
    class myClass
    { 
 }
}

Note the class closing brace not being under the opening one.

How can I have this instead, where is the formatting option ?

namespace myNameSpace
{
    class myClass
    { 
    }
}

Thanks,

4 Answers

Indentation and bracing styles tend to be religious issues. When you move from team to team, you end up having to learn the local brace/indent style. There's a whole Wikipedia article on it (https://en.wikipedia.org/wiki/Indentation_style).

In Visual Studio, you can setup your preferred brace/indent style. My guess is that your bracing/indentation is busted and needs to be reset.

To get there:

Tools ->> Options ->> Text Editor ->> C# ->> Code Style ->> Formatting ->> Indentation

Once you are there, if thing are the way you want them, change them to something else before OK-ing out of the dialog. Then do it again and change it back. If they don't match your desired style, just change things (the idea being - you need to make a change so that your change gets saved).

Good luck

I've seen this happen before in various versions of VS. It's not really reproducible, but sometimes for no explainable reason, VS just starts acting this way.

There are a couple of ways to handle this. The quick fix is to just hit

ctrl+k+d

And that will auto-align all of the code (and comments) on the open file. While that will fix the misaligned brackets after the fact, it won't stop VS from still doing it.

If it gets too annoying (which it can!) then what you can do is a ctrl+a to copy everything. Then paste it into a text file (so you don't accidentally lose it). Remove the file from the project. Then, delete the actual file from the working folder. Close and re-open VS. Then re-create the file and paste all of the contents back into it.

That should solve the problem.

Ok, it’s appeared, but I might be wrong, that the smart tab management had changed lately and that now it is no longer inserting tab at the beggining of a second empty line in a bloc (like some other code editor). Therefor leaving only a space on the second line, leading to my “problem”.

I always used smart tab indenting for all languagse and then lately it seen to have changed then causing this. So the solution for me was to go back to Block Indenting and it work well. I don’t know what other differences that may cause, but...

NOTE: That this only occur if using the Keep tabs option

Thanks all, your comments lead me to a solution (or at least a work-around).

Auto-formatting as you type looks to be turned off by default in Visual Studio 2019. To auto-format your code, go to Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting and select the option Automatically format when typing then select your preferences as to when auto-format should be triggered. Example screenshot:

Auto-format option in Visual Studio 2019

Related