Roslyn: Add brace new line code style for switch expressions

Created on 2 Jul 2019  路  22Comments  路  Source: dotnet/roslyn

Currently in Visual Studio 2019 release, there is no option to specify new line options for braces when using the new switch expression. Visual Studio will always place the opening brace on a new line, regardless of what has been specified for a switch statement, and there is no switch statement specific setting to change the option to having the opening brace on the same line. I typically change all of my code style options for all braces to begin on the same line as the statement/expression creating the brace, but this is one expression where I am unable to configure currently.

_This issue has been moved from https://developercommunity.visualstudio.com/content/idea/524975/add-brace-new-line-code-style-for-switch-expressio.html
VSTS ticketId: 843465_
_These are the original issue comments:_

Jane Wu [MSFT] on 4/10/2019, 00:52 AM (82 days ago):

Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. We'll provide an update once the issue has been triaged by the product team.

Area-IDE Bug Developer Community IDE-Formatter Resolution-Fixed

Most helpful comment

I think this will be 16.8 Preview 4.

All 22 comments

I was the person to initially post this issue. Based on what you have shown, this option should be the one listed as Place open brace on new line for control blocks. It appears that it still isn't working for me. With the option enabled, I get this:

            var msg = 2 switch
            {
                1 => "One",
                2 => "Two",
                _ => "Not one or two"
            };
            for (var a = 1; a < 1; a++)
            {

            }

With the option disabled, I get this:

            var msg = 2 switch
            {
                1 => "One",
                2 => "Two",
                _ => "Not one or two"
            };
            for (var a = 1; a < 1; a++) {

            }

Currently, I am using .NET Core 3.0, CSharp 8.0, and Visual Studio 16.3.0 (I haven't had time to update to 16.3.1 yet).

I was the person to initially post this issue. Based on what you have shown, this option should be the one listed as Place open brace on new line for control blocks. It appears that it still isn't working for me.

Yes. The issue has not been fixed.

@sharwell @CyrusNajmabadi I'm going to take a stab at this one, let me know, if I shouldn't for some reason. I'm going to report back here, if I get stuck, or have questions.

@tiesmaster It should be fine to implement this. There is a chance the specific option changes following a design review, but it should be easy to update the implementation and tests to match regardless.

Good luck! Happy to help out if you need it :)

@CyrusNajmabadi Awesome!! Thnx, I'm probably going to need it ;)

I've managed to reproduce the bug, but so far, I haven't been able to get it fixed. I have this so far, but that doesn't "push the brace back".

So I need to do some more digging, but it's getting late, so I'm stopping for today. Just let me know if I'm on the right track (both tests, as the fixes).

Design review conclusion:

  • For fixing this issue (#39396), we should use NewLinesForBracesInObjectCollectionArrayInitializers.
  • We should rename NewLinesForBracesInObjectCollectionArrayInitializers to NewLinesForBracesInExpressions, and update the Tools → Options and .editorconfig strings to reflect this change. @tiesmaster has the option of implementing this in #39396, or we can implement it separately.
  • We should verify that pattern matching braces are also controlled by NewLinesForBracesInExpressions.

@sharwell Thanks for the update. I'm updating the PR with your suggestions, and like to leave that PR as it is, and open new issues for the other changes, validations. I can't guarantee a constant flow of time on these things, and then at least I can finish these things that are already set in motion. Is that ok?

@tiesmaster Yes that sounds good to me

This formatting option should also cover CollectionInitializerExpression, ArrayInitializerExpression, ImplicitArrayCreationExpression and StackAllocArrayCreationExpression.

It currently inconsistently affects CollectionInitializerExpression where it will preserve lines but not force spaces.

https://github.com/dotnet/roslyn/blob/fed2d6e6925c0c91b6ff0c3af225318e8ed4afdd/src/Workspaces/CSharp/Portable/Formatting/Rules/NewLineUserSettingFormattingRule.cs#L107

vs

https://github.com/dotnet/roslyn/blob/fed2d6e6925c0c91b6ff0c3af225318e8ed4afdd/src/Workspaces/CSharp/Portable/Formatting/Rules/NewLineUserSettingFormattingRule.cs#L282

Bump
Has there been any new developments on this? I see the conversation trailed off around November, 2019 with a remark in February. It seems there's also a lot of duplicate issues pointing back to this one.

Has there been any new developments on this?

We took this to a design meeting and decided in the design and behavior we want here. The issue is currently 'up for grabs' for anyone to fix, although @tiesmaster Expressed interest in doing that work.

We would take a well written pr that implements the design specified. Thanks!

Hey @tiesmaster, are you still up for it? Otherwise, how about the product owners take care of it?

@ChainReactive We haven't heard from @tiesmaster in some time. Would you be interested in taking a look and contributing a PR here?

@CyrusNajmabadi Sorry, I'm too busy working on my own products. I can't afford spending the time it'd take to learn Roslyn and contribute to it. Seems it'd be best if the product owners took responsibility.

Seems it'd be best if the product owners took responsibility.

We have responsibility for many issues. This is not more important than all the issues currently assigned to everyone currently. As this is a low impact issue, it likely won't get fixed until current critical work is done, or unless someone wants to take their spare time to fix it. :)

I can't afford spending the time it'd take to learn Roslyn and contribute to it

I'm happy to take my spare time to try to help you learn this :) We have many external contributers already, and you might like being able to be in a position to immediately address small paper cuts like this when they affect you instead of hoping that it might make the cut for someone else in teh future :)

We have responsibility for many issues. This is not more important than all the issues currently assigned to everyone currently. As this is a low impact issue, it likely won't get fixed until current critical work is done, or unless someone wants to take their spare time to fix it. :)

Thanks for the explanation.

I'm happy to take my spare time to try to help you learn this :) We have many external contributers already, and you might like being able to be in a position to immediately address small paper cuts like this when they affect you instead of hoping that it might make the cut for someone else in teh future :)

There was a time in my life.... but not now. Good luck!

@tiesmaster @ChainReactive @CyrusNajmabadi I was the one supposed to finalize #39396 for merge. I'll update it now so this should already be fixed.

@here Indeed, as @sharwell indicated, I already put in a PR to fix this. I left that in an intermediate state, as there were still loose ends (or maybe just a single loose end, I'm not sure), so that was also a bit on me, I suppose.

@sharwell From all the other issues that point back to this issue, it's apparent that there is desire to get this in. Let me know if you need anything from me on the PR. Though, I'd need to dive into that again, if there is anything that needs to be change, so that would take a bit of time.

@CyrusNajmabadi Indeed, you haven't heard from me for some time. I started out with a new job, and together with some demanding things in my personal life (happy stuff, but still time consuming), I didn't have any time to continue with my contributing. I suppose I should have mentioned that, sorry about that.

I do like to start contributing again, but didn't really knew where to start. Getting help on certain issues would definitely help. I know the Gitter channel, but that didn't really work for me, somehow. I see there is Discord now, that might be a better fit for me. Perhaps having a dedicated person to ask questions to, would also help, something like a mentor.

@sharwell Thanks for finishing the PR! I assume it is too late to get this in 16.8, right? Will it be in 16.9?

I think this will be 16.8 Preview 4.

Was this page helpful?
0 / 5 - 0 ratings