dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.87k stars 675 forks source link

List initializers spread to multiple lines don't get formatted when running format command #2958

Closed natanfudge closed 9 hours ago

natanfudge commented 5 years ago

Issue Description

When you declare some kind of list / dictionary with "{}" (list initializer) that takes more than one line to write and try to format it, none of the list will get formatted.

Steps to Reproduce

Create a new .cs file and type:


using System.Collections.Generic;

public class test
{
    List<string> list = new List<string>{"a","b",        "c", "d", "e", "f", "g", "h"
        , "i","j"};

}

And then activate the Format Document command.

Expected Behavior

The list initializer will get formatted to this:

List<string> list = new List<string> { "a", "b", "c", "d", "e", "f", "g", "h",
    "i", "j" };

or this:

    List<string> list = new List<string> { "a", "b", "c", "d", "e", "f", "g", "h",
                                           "i", "j" };

Actual Behavior

The list remains unchanged.

Logs

OmniSharp log

No log.

C# log

No output.

Environment information

VSCode version: 1.32.3 C# Extension: 1.18.0

Dotnet Information .NET Core SDK (reflecting any global.json): Version: 2.1.502 Commit: c74ce8f29f Runtime Environment: OS Name: Windows OS Version: 10.0.17134 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\2.1.502\ Host (useful for support): Version: 2.1.6 Commit: 3f4f8eebd8 .NET Core SDKs installed: 2.1.202 [C:\Program Files\dotnet\sdk] 2.1.500 [C:\Program Files\dotnet\sdk] 2.1.502 [C:\Program Files\dotnet\sdk] .NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download
Visual Studio Code Extensions |Extension|Author|Version| |---|---|---| |auto-using|Fudge|0.7.4| |cs2ts-vscode-extension|chiadem|0.2.1| |csharp|ms-vscode|1.18.0| |csharpextensions|jchannon|1.3.0| |docomment|k--kato|0.1.6| |msbuild-project-tools|tintoy|0.2.54| |vscode-nuget-package-manager|jmrog|1.1.6| |vscode-path-tools|cg-cnu|0.0.4| |vscode-test-explorer|hbenl|2.9.1| |vscode-todo-highlight|wayou|1.0.4|;
vidarasberg commented 1 year ago

I found the same behaviour to be true when multiline initialising Dictionary as well.

var data = new Dictionary<string, string> {
                          { "a", "a" },
                    { "b", "b" },
                                { "c", "c" }
            };

Remains the same. Single line works as expected

JoeRobich commented 9 hours ago

By design the Roslyn Formatter does not format initializers.