roslyn: Format document should not add braces by default

Formatting a document by default now has started adding braces around single line blocks:

using System;

namespace ConsoleApp75
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
                throw new Exception();
        }
    }
}

To:

using System;

namespace ConsoleApp75
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                throw new Exception();
            }
        }
    }
}

However there is no way to enforce whether or not braces are optional for single line statements in editorconfig. This is going to lead to unneeded changes to my tree without a way to enforce it. This option should be turned off by default unless there is a way to enforce it.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 43 (40 by maintainers)

Most upvoted comments

I’d personally want this not only off by default for formatting, but as a separate command from formatting. There are many reasons why I’d like to format the document but not cleanup (yet), for example:

I agree with @Neme12 . I think it’s totally fine to tie these two things together. However, i also think it would make sense to have a dedicated keystroke for this purpose that someone could use instead.

So, the changes i would have would be:

  1. introduce a specific command for cleanup-document.
  2. expose that command in Keybindings.
  3. expose that command on edit|advanced next to format document.
  4. have a way to configure what that command does in tools options.
  5. have an option under the formatting page to say: run code cleanup when i format document.
  6. still have the gold-bar

This seems to address nearly all the use cases i can consider. People who want to keep the commands separate can do so. People who want a single command can have it (albeit opt-in). People can control what runs when the command executes. And it’s easy to discover the commands through the gold-bar and the edit menu.

And code cleanup is considered Configured if user goes to the Formatting setting page and click “Ok” to exit.

This feels wrong to me. There is not explicit consent being given when the user just navigates somewhere, then hits ‘ok’ to close a page. What would probably make sense would be to change this list:

image

Into something more like:

image

This “perform additional cleanup during formatting” would be unchecked by default. It would only be checked if the user explicitly clicked it, or if they accepted through the gold-bar.

Thoughts @kuhlenh @sharwell @jinujoseph @JieCarolHu ?