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.
UPDATE:
Using the below example data, if I set the TOLERANCE on the collection initialize to 3 or more then it formats the code below as expected, but as soon as I drop the tolerance to 0,1 or 2, then it's not indented as explained above.
class Class1 { public void Test() { int 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 } } }; } } class Person { public int A { get; set; } public int B { get; set; } public int C { get; set; } }
Result with 3 tolerance:
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 } } }; }
Result with 2 tolerance!
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 } } }; }
Sorry for the long post guys, like to give you as much info as I can.
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:
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 } } }; }
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.
Excellent, glad to hear that it's not just me doing something wrong :)