Bug Report T668685
Visible to All Users

Formatting - The Dictionary initialization is aligned incorrectly

created 6 years ago (modified 6 years ago)

Hi

I'm not sure the collection initializer is working as intended. I have the below example code:

C#
class Class1 { public void Test() { int value = 1; var parameters = new Dictionary<string, object> { { "Key1", value }, { "Key2", value }, { "Key3", value }, { "Key4", value }, { "Key5", value }, { "Key6", value } }; } }

However when I run the code cleanup to format the document I get this result:

C#
public void Test() { var value = 1; var parameters = new Dictionary<string, object> { { "Key1", value }, { "Key2", value }, { "Key3", value }, { "Key4", value }, { "Key5", value }, { "Key6", value } }; }

As you can see I have a dictionary object containing a list of object, but key,value objects should be indented one more level: e.g.

Expected:

C#
public void Test() { var value = 1; var parameters = new Dictionary<string, object> { { "Key1", value }, { "Key2", value }, { "Key3", value }, { "Key4", value }, { "Key5", value }, { "Key6", value } }; }

My settings are set as such:  (See attached image)

I've tried changing tolerance, and all the different settings in the 3 sections in the image (array, collection, object) but can't get it to look like the above.

If I have the tolerance set to more than the number of values (e.g. 3) then I get this:

C#
public void Test() { var value = 1; var parameters = new Dictionary<string, object> { { "Key1", value }, { "Key2", value }, { "Key3", value }, { "Key4", value }, { "Key5", value }, { "Key6", value } }; }

So it looks like the inner objects are indented, at this point.

Show previous comments (1)
AM AM
Alan Middleton 6 years ago

    Update #2:

    I've changed the Collection Initializer Align By from Expression to Statement in all 4 areas of this option, the result is acceptable as they are indented correctly - they are just all aligned to the left:  (With zero tolerance)

    Result:

    C#
    public void Test() { var value = 1; var parameters1 = new Dictionary<string, object> { { "Key1", value } }; var parameters2 = new Dictionary<string, object> { { "Key1", value }, { "Key2", value } }; var parameters3 = new Dictionary<string, object> { { "Key1", value }, { "Key2", value }, { "Key3", value } }; var parameters4 = new Dictionary<string, object> { { "Key1", value }, { "Key2", value }, { "Key3", value }, { "Key4", value } }; var parameters5 = new Dictionary<string, object> { { "Key1", value }, { "Key2", value }, { "Key3", value }, { "Key4", value }, { "Key5", value } }; var parametersp1 = new Dictionary<string, object> { { "Key1", new Person() { A = 1, B = 2, C = 3 } } }; var parametersp2 = new Dictionary<string, object> { { "Key1", new Person() { A = 1, B = 2, C = 3 } }, { "Key2", new Person() { A = 1, B = 2, C = 3 } } }; var parametersp3 = new Dictionary<string, object> { { "Key1", new Person() { A = 1, B = 2, C = 3 } }, { "Key2", new Person() { A = 1, B = 2, C = 3 } }, { "Key3", new Person() { A = 1, B = 2, C = 3 } } }; var parametersp4 = new Dictionary<string, object> { { "Key1", new Person() { A = 1, B = 2, C = 3 } }, { "Key2", new Person() { A = 1, B = 2, C = 3 } }, { "Key3", new Person() { A = 1, B = 2, C = 3 } }, { "Key4", new Person() { A = 1, B = 2, C = 3 } } }; }
    DevExpress Support Team 6 years ago

      Hi Alan,

      Thank you for pointing out this issue and for such a detailed report. I was able to reproduce the issue on my side and can confirm that the Formatting feature incorrectly places the surrounding braces of Dictionary object initialization when Align by option is set to Expression. I passed this ticket to our developers for further research. We will update this thread once we have any news.

      AM AM
      Alan Middleton 6 years ago

        Excellent, glad to hear that it's not just me doing something wrong :)

        Answers approved by DevExpress Support

        created 6 years ago (modified 6 years ago)

        We have fixed the issue described in this ticket and will include the fix in our next maintenance update. To apply this solution before the official update, request a hotfix by clicking the corresponding link for product versions you require.

        Note: Hotfixes may be unavailable for beta versions and updates that are about to be released.

        Additional information:

        We have fixed the issue with indenting the initializer's elements.
        The build with the fix will be available in the next release.
        Note that you should set the "Align content by" option to Expression and the "Indent" option to 1.
        The attached screenshot illustrates how options should be set to manage the required behavior.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.