Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Visual Studio 2010 C# Express format nested code correctly?

Consider this code...

Create(x,y,z,
null, null, null,
new Class1[     ] {
    new Class1(a,b,c),
    new Class1(a,b,c)
},
new Class2[] {
    new Class2(d,e,f    )
},
true);

Notice the lack of spacing after the parameter commas, and the extra spacing in the Class1 array definition and Class2 call.

If I format my document -- Ctrl+E, D or Ctrl+K, D -- the code is formatted to this...

Create(x, y, z,
null, null, null,
new Class1[] {
    new Class1(a,b,c),
    new Class1(a,b,c)
},
new Class2[] {
    new Class2(d,e,f    )
},
true);

Notice that the parameter commas in the "outer" code have been properly formatted with spaces (according to my preferences), and the extra spaces have been removed from the Class1 array definition (again, my preferences). However, the parameter commas in the "inner" code HAVE NOT been correctly formatted and the extra spacing remains in the Class2 call.

There is absolutely nothing wrong with the code -- it compiles and runs without errors.

This happens every time I create nested, complex calls using braces. I suppose I could just make my code longer and more complex by breaking it all apart and using temporary variables but that seems like overkill.

Why can't Visual Studio 2010 C# Express correctly format such nested code? Has anyone else experienced this? I can provide my Formatting preferences if necessary. Thanks in advance! :)

like image 719
tvwxyz Avatar asked Aug 30 '26 08:08

tvwxyz


1 Answers

@tvwxyz: I could reproduce this issue with Ctrl+K, D.

But, interestingly if I enable "Automatically format completed block on }" it works fine (gets intended as expected) when I close the function.

Tools -> Options -> Text Editor-> C#-> Formatting -> General -> Automatically format completed block on }

like image 74
jacob aloysious Avatar answered Aug 31 '26 22:08

jacob aloysious



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!