Use dotnet-format to change to file-scoped namespaces in C#10

450 Views Asked by At

I want to format a file to "file-scoped namespaces". I want to make that change only.

My .editorconfig contains this only:

[*.cs]
csharp_style_namespace_declarations = file_scoped:warning     # config for IDE0161

A test file Foo.cs:

namespace MyNamespace
{

    public class Foo
    {

        public Foo(int someInt, string someString)
        {
            _someInt    = someInt;                    // aligned
            _someString = someString;                 // aligned
        }

        private readonly int _someInt;
        private readonly string _someString;

    }
}

I run this:

dotnet format MyProject.csproj --severity info --diagnostics=IDE0161 --include=Foo.cs

The result:

namespace MyNamespace;

public class Foo
{

    public Foo(int someInt, string someString)
    {
        _someInt = someInt;                     // not aligned
        _someString = someString;               // not aligned
    }

    private readonly int _someInt;
    private readonly string _someString;

}

The alignment is changed as shown. There are various other places where alignment is changed too.

I only want to run one analyser/fixer - IDE0161. I don't want the format command to do anything else.

How do I do that?

(Note if I run the code fix in vscode, it works. It's only via dotnet format that it fails.)

0

There are 0 best solutions below