roslyn: Incorrect formatting of C# comments

When you want to disable (but not delete) a field etc in a C# class, you comment it out by writing "//" at the start of the line, but this causes VS 15.8.0 to incorrectly format the line when the previous line ends with a comment. You can understand this much easier when you look at my screenshot attached.

To reproduce this bug, first put this class in a .cs file:

class ExampleClass1
{
    int ExampleField1;
    int ExampleField2;  // Some comment here.
    //int ExampleField3;
    int ExampleField4;
}

Then select the text (select the entire class). Then click menubar -> Edit -> Advanced -> Format Selection. VS changes the class to:

class ExampleClass1
{
    int ExampleField1;
    int ExampleField2;  // Some comment here.
                        //int ExampleField3;
    int ExampleField4;
}

image

The bug also occurs when VS formats the class at other times, not only the "Format Selection" command. See the "Automatically format when/on XXXXX" options in menubar -> Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting -> General.

Thanks for investigating this!

This issue has been moved from https://developercommunity.visualstudio.com/content/problem/317225/incorrect-formatting-of-c-comments.html VSTS ticketId: 668245 These are the original issue comments:

Etienne Poirier on 8/21/2018, 04:35 PM (2 days ago):

Same issue as

https://developercommunity.visualstudio.com/content/problem/20554/line-comments-unwantedly-align-to-comment-above-in.html

Thank you to check this.

These are the original issue solutions: (no solutions)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 54 (32 by maintainers)

Most upvoted comments

This seems by design to me… it’s a common pattern to have multi-line comments at the end of constructs, and to align them across multiple lines.

In this case, we don’t really have any way to know or believe that this isn’t one of those cases, and aligning the comments as shown seems reasonable in terms of respecting the common pattern that is out there.

@verelpode @ChainReactive I’m not sure what else can be stated at this point. The team has put forth their position on this issue and how it is currently prioritized (i.e. in the help-wanted bucket). I’m available to help out if you, or anyone else, is interested in tackling this from the community.

As a FOSS project, Roslyn thrives on shared efforts by dedicated teams at MS, outside of MS and from community efforts. It’s part and parcel of that that not all work is done internally. There is effectively interest from many thousands of individuals and groups about what is important, and the way Roslyn effectively manages that is to make it so that the core team itself is not the limiting factor for improvements happening. This is a virtue of the project, and one of the very specific reasons it is open source.

At this point, i don’t think I can add any more help here. My offers of assistance seem to not be welcome, and the discussion seems to not be providing any actual value toward solving the originally reported issue.

If anyone is interested in solving this issue in the future, see the links provided above for a large group of community members (internal and external) and resources that can help out here.

Thanks all!

@CyrusNajmabadi, @sharwell Visual Studio is a commercial product with a dedicated team on staff at Microsoft.

It’s also an open source project with a community that actively contributes to it and provides PRs when their needs don’t align with the priorities of the main team itself. I’m one of those people and have contributed as an external community member a large number of times.

As i’ve already pointed out, the stated view of the team is that there is an easy workaround if hte current behavior is undesirable to you. If that workaround is not tenable and you want to see this fixed, it will have to come from a PR from someone willing to view the change as valuable enough to invest their own time.

I personally don’t feel like it’s worth fixing myself, but i’d be happy to help you with a PR to change if it is something that is affecting you.

The people who’s job it is to improve Visual Studio’s user experience

As an open source project, “the people” includes the community (including you, me, and other interested parties).

would be better equipped to fix this.

Yes, that’s likely true. But they’re also tasked with work felt to be more important and a more valuable use of their time. And so here we are.

This seems by design to me…

I can’t really agree with you because VS 15.8.2 also does it when the first comment is single-line // and the second comment is multi-line /* */ The following shows the result after executing the “Format Document” command:

int ExampleField2;  // Some comment here.
                    /*
                    int ExampleField3;
                    int ExampleField4;
                    int ExampleField5;
                    */

I don’t believe that anyone wants the above formatting.

it’s a common pattern to have multi-line comments at the end of constructs, and to align them across multiple lines.

I never do that, and apparently multiple other people also never do that, therefore I suggest making a checkbox in the Options window, so that people can simply switch this behavior on or off as desired. However, in my opinion, the mixed example above seems to be a bug not a feature.

It’s unethical and inappropriate if you frequently try to push or manipulate forum participants into joining your religion/philosophy.

Continuing to offering help is not pushing religion. Nor is it unethical to provide free support to people who can choose to take it or not.

If I followed your recommendation and adopted your religion/philosophy, then it would substantially damage the upbringing of my children.

Then don’t. Nothing about what i’ve said insists that you do this. It merely offers you the choice of doing this. It’s not unethical to make ones time available to help people freely if they are interested. I do this in many avenues in life. I freely offer tutoring for many different subjects. I freely offer assistance with learning different programming languages. I freely offer time and information toward contributing to some FOSS projects.

it would be monstrous to harm my children in that way.

So you’re saying i’m operating unethically for offering my time and knowledge to people who may want it because if you were to take me up on it it would hurt you? Does that not strike you as ridiculous?

Should i also not offer free tutoring for interested students because if some were to take it they might be negatively impacted in other avenues of life?

Does it not make more sense (and seem more ethical) to have this be available and to leave it entirely to you (and others) to decide if you want to take it or not? I don’t see how this is at all a religion, but i certainly can see how it’s a philosophy. And it’s a philosophy that has worked well for dozens of people out there. I think it’s a good thing that I can help out, and I’ll continue to do so and will continue to announce this possibility so that if there are people out there (even if its not you) will know and can take advantage of it if they wish.

Cheers!

Sacrifice ethical behavior.

Working with a community to improve an open source project sacrifices no ethical behavior as far as i can tell.

My personal opinion is that your behavior appears to be unethical

It’s unethical to provide support to people to show how they can contribute to the Roslyn open source project?

Being an app developer (or likewise) is already a challenging, time-consuming, and exhausting job. Thus it would be very unreasonable to expect app developers to do even more work in the form of unpaid casual side-jobs

This is how much of the open source world works. Projects are made freely available for people to use and modify and contribute back to. When the priorities of the team don’t align with your particular desires, you can choose to petition that they change, and/or you can choose to contribute your own efforts to align the project with your own desires.

However, it would be wildly unreasonable to expect @ChainReactive to sacrifice himself like you do.

I have never expected @ChainReactive to do anything. I have simply pointed out that this is something he can choose to contribute to if he wishes. And, if he does wish to do so, i am 100% willing to provide my own time and experience toward aiding that effort. There are many other community members who are willing to do that as well, and this has led to hundreds of community PRs and thousands of commits to the Roslyn repo that have improved the project for everyone. This is the power and value of it being an open source project.

Cheers! 😃

I’m sure they’re working on new and great things, but some house keeping of low hanging but high value fruit like this issue would be nice.

Many issues like that are fixed in any given release. You can just look at the issues closed with merged PRs over a certain time period. That doesn’t mean all the things you care about will be addressed though.

You have a knack for justifying inaction.

I’ve already offered my time toward helping you fix this in a paired manner. If you view my willingness to work with you to fix hte issues you care about as “inaction” then that’s on you.

As I mentioned above, I don’t have time.

If that changes in the future, let me know. Still happy to help you out here to reach a resolution you would like! 😃

is neglecting a lot of users by not fixing this bug.

And if they fix this and this do not fix another because they focused here, are they then neglecting the users affected by those other issues?

Framing the decision to prioritize bug fixing in a way you don’t like as ‘neglect’ is not helpful. Any project will have bugs and every team will fix using a system that will not align with the desires of all members of their user base.

And, as mentioned above, if that order doesn’t fit your desires, you actually have something you can do about it.

But if your Roslyn skills aren’t up to stuff, then don’t worry about it.

When it comes to this area, they may not be. Hence why I was offering to work through it with you. Two minds being better than one and all that 😃

Cheers and good luck! If you change your mind, just let me know! 😃

@CyrusNajmabadi I’d welcome your assistance in making up for Microsoft’s negligence.

Teams prioritizing things different from you is not negligence. There are millions of users and thousands of signals indicating what is valuable and important to actually address with the resources available. This minor issue simply isn’t one of those. I can imagine you probably have to make similar decisions in your own work when deciding where to allocate your own resources.

How about I send you my kudos once you fix this bug?

I am personally not interesting in contributing a PR here. Primarily because i don’t find the behavior particularly problematic, nor do i feel that the workaround is insufficient.

Still happy to help you out at any point if you change you mind. The links to the channels still apply. Cheers!

@ChainReactive as mentioned in this post here: https://github.com/dotnet/roslyn/issues/29482#issuecomment-481007848

The current general recommended approach is to add a blank line before the comment which should not have the extra indentation. However, to address the specific issue here, we would take a pull request which added a conditional formatting rule where comments already aligned where code would be indented on the same line will anchor at that location instead of the current behavior of anchoring to the trailing comment on the preceding line.

In other words, if a community member wanted to contribute such a PR, roslyn would take it. however, absent that, the advice would be to just put in a blank line in the code.

If this is something you are passionate about (which seem evidenced by https://github.com/dotnet/roslyn/issues/29482#issuecomment-491030328 and https://github.com/dotnet/roslyn/issues/29482#issuecomment-548212118) perhaps you would be willing to help out here with a PR yourself?

Note that there are a couple of good channels to help out people working to contribute toward roslyn (internal and external alike):

gitter.im/dotnet/roslyn and aka.ms/csharp-discord

Cheers!

Wait, here is another alternative solution, better than my previous brainstorming. VS would only increase the indenting of a comment if it is already indented by at least one space or tab char. Example:

int x;  // First comment.
    // VS would align this comment with the first comment because it is already indented.

int y; // Blah blah.
// This comment would never be aligned with the previous comment.

Thus the final result is:

int x;  // First comment.
        // VS would align this comment with the first comment because it is already indented.

int y; // Blah blah.
// This comment would never be aligned with the previous comment.

In other words, if a comment is aligned with “int x;” then it remains at that level, but if it already indented beyond “int x;” by one or more space or tabs, then VS aligns it with the previous comment.

@dibarbet This one is still in the queue