Search code examples
c#visual-studioindentationvisual-studio-2022

Format document - indentation on object initialization is wrong


The problem is the indentation on object initializers' indentation when Format document is run (CTRL + K, CTRL + D) in Visual Studio 2022.

Current:

Person person = new Person
    {
        FirstName = "John",
        LastName = "Doe"
    };

Expected:

Person person = new Person
{
    FirstName = "John",
    LastName = "Doe"
};

When doing arrays/list object initialization, the indentation works as I want it.

What setting in Visual Studio is it that causes this?

I have tried turning on/off these settings with no luck getting the expected:

Text Editor -> C# -> Code Style -> Formatting -> Indentation -> Indent block contents

Text Editor -> C# -> Code Style -> Formatting -> Indentation -> Indent open and close braces

I have tried resetting all Visual Studio settings, and tried creating a new solution, and no luck either.


Solution

  • The problem was that I used .razor files and not .cs files. .cs files work fine.

    The razor team has a problem with correct indentation when formatting document.