2

I have the following code:

internal class Program
{
    private static bool IsMatchingThing1(IThing thing)
    {
        return thing is
            AThing {
                Children: [
                    BThing
                ]
            };
    }

    private static bool IsMatchingThing2(IThing thing)
    {
        return thing is
            AThing
            {
                Children:
                [
                    BThing
                ]
            };
    }
}

internal interface IThing;

internal class AThing : IThing
{
    public List<IThing> Children { get; set; } = new();
}

internal class BThing : IThing
{
    public List<IThing> Children { get; set; } = new();
}

The methods IsMatchingThing1 and IsMatchingThing2 contain pattern matching statements, with slightly different formatting, both of which are acceptable to me.

However, if I trigger an automatic reformat (e.g. by retyping the semicolon at the end of the pattern matching statements) Visual Studio reformats this to:

private static bool IsMatchingThing1(IThing thing)
{
    return thing is
        AThing
    {
        Children: [
                BThing
            ]
    };
}

private static bool IsMatchingThing2(IThing thing)
{
    return thing is
        AThing
    {
        Children:
            [
                BThing
            ]
    };
}

I don't consider this an improvement and would like to retain the formatting used in both, or at least one of the methods. I don't really mind if I am forced to adopt one of the formats.

If I put everything on one line, then the problem doesn't exist, but some of the patterns I need to match are more deeply hierarchical than this example, so some kind of nested indentation is what I'm after.

What settings can I use in .editorconfig that allow some sensible formatting of these statements, and that doesn't cause dotnet format to reformat the code these methods, yet retains the formatting used elsewhere in the code?

dotnet.exe version 9.0.300

6
  • How about expression-bodies methods ? fiddle. I dislike original snippet formatting tbh. Commented Jun 11 at 15:54
  • I don't have a problem if everything is on one line, using expression bodied methods or otherwise. Some of the pattern matching is more deeply hierarchical than this, a single line isn't really scalable. Commented Jun 11 at 16:14
  • 1
    The auto-formatting is not ideal and certain parts seems to be seen by it as "custom formatted" by you and the formatter is keeping it. Square brackets formatting is lacking. Periodically new questions about formatting are asked (most recent with my comment), but the answer is dissapointing. With enough time invested you can fix everything by making your own plugin, I prefer just to fix formatting manually in few cases. Commented Jun 11 at 16:24
  • 1
    What do you have in your VS Settings -> Text Editor -> C# -> Code Style -> Formatting? Under Indentation and New Lines what is checked? Commented Jun 11 at 17:07
  • I believe this problem is independent of Visual Studio Text Editor settings as running dotnet format reports the formatting as an issue and 'fixes' it as shown above. Commented Jun 12 at 10:21

1 Answer 1

0

I experienced the exact same a little over a year ago. If the whole program is formatted this way the you will just have to put aside a weekend where you don't write code and just format.

I setup a keyboard shortcut to join lines using Options-Environment->Keyboard. I set it to Ctrl+J, Ctrl+P. This only works across two methods.

enter image description here

Start your selection below the first curly brace that appears after the first method signature. Select over the next method and end your selection before the end curly brace of the second method as shown below

enter image description here

Use your shortcut to join lines. it will appear as below

enter image description here

Apply the Ctrl+K, Ctrl+D key combination. The outcome of this is shown below

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.