Skip to content

"Convert to class" refactoring loses trivia and produces a not-so-pretty result #45792

@mahalex

Description

@mahalex

Version Used:
Verified in VS 16.7.0 Preview 3.1
Steps to Reproduce:

Ctrl+. on "new" -> "Convert to class" in the following fragment

            var t1 = new
            {
                // Hi.
                a = 1,
                b = 2
            };

Expected Behavior:

            var t1 = new NewClass(
                // Hi.
                1,
                2);

Actual Behavior:

            var t1 = new NewClass(
1,
2
            );

Note the lost comment. Preserving leading trivia from the initializers should help with indentation as well.
Regarding the whitespace after "2", note that the current behavior is similar to what happens in "Convert to tuple":

            var t1 = (
                // Hi.
                a: 1,
                b: 2
            );

As you can see, indentation and comment are preserved, but the ugly whitespace after "2" remains.
I believe that having the closing parenthesis on the same line corresponds to the common style expected by users (as does having closing curly bracket on a separate line).

Metadata

Metadata

Assignees

No one assigned

    Labels

    4 - In ReviewA fix for the issue is submitted for review.Area-IDEBugIDE-CodeStyleBuilt-in analyzers, fixes, and refactorings

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions