Roslyn: Add an option to apply code formatting to collection initializers

Created on 31 Jan 2016  路  16Comments  路  Source: dotnet/roslyn

At this moment VS code formatter never touches the collection initializers. I would guess this is done to preserve a hypothetical manual item alignment, but as someone who never does that, I find it very annoying that I have to format the code inside the collection initializers manually. So I'd like to suggest adding an option to specify whether the formatter should format the collection initializers (just like there is Ignore spaces in declaration statements to allow/disallow manual declaration alignment).

Area-IDE Developer Community Feature Request IDE-Formatter Need Design Review

Most helpful comment

+1. Actually it is partially an accessibility issue, as for a blind person like me it's non obvious whether something is misindented. Most people can look it up easily, I have to turn on indentation announcements in a screenreader and actually walk through the whole thing. And it is easy to misindent by accident.

All 16 comments

Yes, I agree. I'd like an option too.

Hi, I'm adding my voice to the voting for this feature.

I think this would be a good idea, too.

And it is a problem of multi-line initializer.

+1 Would love this feature!

+1, I don't like manual formatting

This went to an initial design review, but we believe it needs a more specific proposal before it can move forward. Two areas of particular interest are the following:

  • Even if formatting of collection initializers is disabled, users likely want the indentation of these lines to be updated to match project settings regarding tabs/spaces.
  • It may be possible to restrict the formatting suppression to the set of spaces following a , within the collection initializer (to allow for column layouts). This hypothesis would need to be validated at scale to determine if it's consistent with user expectations for formatting.

/cc @heejaechang to help form a more concrete proposal including a specific option or options that can be specified in .editorconfig.

@sharwell @heejaechang

Even if formatting of collection initializers is disabled, users likely want the indentation of these lines to be updated to match project settings regarding tabs/spaces.

If the concept of "disabling collection initializers" existed and the user still wanted to respect the other settings, what does "disabling collection initializers" even mean?

Also, if using manual alignment is the real issue here, then I think the real question would be, "Why are people manually aligning? And what settings need to be introduced to undo that need?"

I would propose an experimental setting csharp_preserve_collection_initializers, defaulted to true, which would represent no change in current behavior. Opting in (setting to false) would apply all rules to the collection initializers. Then we can collect data and see what kinds of issues arise for users.

This issue will eventually offer a specific resolution to #779

It makes sense that some (very few) users would want to do their own manual formatting in object initializers and that they wouldn't want the formatter to interfere, but I'd argue that the same could be said of all code. If you don't want your code to be formatted, just turn off auto-formatting.

Also, even in those cases when people don't want Visual Studio to interfere with their whitespace, they probably still want their indentation to line up correctly. The problem I'm having is that my initializers aren't getting indented when I paste.

+1 from me too

Please count me in. It's ugly to see the formatting not applied as well as frustrating to have to do it manually

This looks so wrong ;)

image

+1. Actually it is partially an accessibility issue, as for a blind person like me it's non obvious whether something is misindented. Most people can look it up easily, I have to turn on indentation announcements in a screenreader and actually walk through the whole thing. And it is easy to misindent by accident.

Was this page helpful?
0 / 5 - 0 ratings