How to get Parenthesis to mimic Braces Layout [ At next like (BSD style) ]? Follow
I am an OCD formatting freak; I'll admit it. With [ Options > Code Editing > C# ] I can CURRENTLY do the following with array, object, and collection initializers:
var fooBar = new FooBar
{
Style = new Style,
Message = String.Empty
};
fooBar.Style.Setters.Add(new Setter
{
Property = HeightProperty,
Value = 0
});
As you can see, the BSD style BRACES LAYOUT works awesome for those initializers, but for PARENTHESIS, not so much.
I want to know how you can do the same with the PARENTHESIS?
Thus making the second piece of code above:
fooBar.Style.Setters.Add
(
new Setter
{
Property = HeightProperty,
Value = 0
}
);
I guess what I'm really trying to say is that it's almost as if we need "Collapsed," "Expanded," and even "Mixed" default coding style settings added ( and maybe the ability is already there, I just don't know how to do it ).
Let me show you what I mean:
This is nice and concise and can already be done based through [ Options > Code Editing > C# > Formatting Style > Line Breaks and Wrapping ].
- Under Preserve Existing Formatting, set Break line in single embedded statement and Break line in a block with a single statement to [ Force put on single line ].
- Under Line Wrapping, you can either set Wrap long lines to [ unchecked ] or set the Right margin ( columns ) to a high number ( mine is ~200, can't remember exactly ).
Here's an example of what that looks like:
private const int MAX_FOOBARS_ALLOWED = 5;
private bool AddFooBarIfMaxHasNotBeenExceeded(IList<FooBar> fooBars, FooBar fooBar, bool collapseFooBar)
{
if (fooBar == null || fooBars == null || fooBars.Count >= MAX_FOOBARS_ALLOWED) { return false; }
if (collapseFooBar) { fooBar.Style.Setters.Add(new Setter { Property = HeightProperty, Value = 0 }); }
fooBars.Add(fooBar);
return true;
}
EXPANDED
We already have [At next line ( BSD style ) ] for BRACES. What is MISSING ( or I just don't know how to configure it to make it work ) is [ At next line ( BSD style ) ] for PARENTHESIS.
So to be clear, that would mean that:
-
Both OPEN and CLOSING PARENTHESIS would ALWAYS LINE BREAK and ALIGN with the statement on the PREVIOUS LINE.
-
Their CONTENTS ( parameters, objects, conditions, nested parenthesis, etc. ) would ALWAYS CHOP and ALIGN with 1 TAB INDENT from the PARENTHESIS.
- If the CONTENTS ( usually conditions inside if statements ) contain OPERATORS, there would be an option to ALIGN BEFORE or ALIGN AFTER OPERATOR.
-
Their CONTENTS ( parameters, objects, conditions, nested parenthesis, etc. ) would ALWAYS CHOP and ALIGN with 1 TAB INDENT from the PARENTHESIS.
Here's an example of what that would look like:
private const int MAX_FOOBARS_ALLOWED = 5; private bool AddFooBarIfMaxHasNotBeenExceeded ( IList<FooBar> fooBars, FooBar fooBar, bool collapseFooBar ) {
// This obviously uses the ALIGN AFTER OPERATOR option mentioned above.
if
( fooBar == null || fooBars == null || fooBars.Count >= MAX_FOOBARS_ALLOWED ) {
return false;
} if (collapseFooBar) { fooBar.Style.Setters.Add
( new Setter { Property = HeightProperty, Value = 0 } ); } fooBars.Add(fooBar); return true; }
MIXED
Sometimes you neither want "Collapsed" or "Expanded" coding style settings for all your code, but you want a MIX of both based on the number of CONTENTS inside the PARENTHESIS and / or BRACES.
So when CONTENTS ( parameters, objects, conditions, nested parenthesis, etc. ) between the PARENTHESIS and / or BRACES have:
- ONLY ONE ITEM, then it should use the "Collapsed" coding style settings for that piece of code.
- MORE THAN ONE ITEM, then it should use "Expanded" coding style settings for that piece of code.
Here's an example of what that would look like:
private const int MAX_FOOBARS_ALLOWED = 5;
new Setter { Property = HeightProperty, Value = 0 } );
// Method signature CONTENTS have MORE THAN ONE ITEM, uses "Expanded" coding style settings for PARENTHESIS.
private bool AddFooBarIfMaxHasNotBeenExceeded ( IList<FooBar> fooBars, FooBar fooBar, bool collapseFooBar ) { // IF statement has MORE THAN ONE ITEM, uses "Expanded" coding style settings for PARENTHESIS.
// IF statement is also using the ALIGN AFTER OPERATOR option.
// BRACES CONTENTS have ONLY ONE ITEM, uses "Collapsed" coding style settings for BRACES LAYOUT.
if ( fooBar == null || fooBars == null || fooBars.Count >= MAX_FOOBARS_ALLOWED ) { return false; }
// IF statement has ONLY ONE ITEM, uses "Collapsed" coding style settings for PARENTHESIS.
if (collapseFooBar) { // BRACES CONTENTS have MORE THAN ONE ITEM, therefore uses "Expanded" for PARENTHESIS and BRACES LAYOUT.
fooBar.Style.Setters.Add
(}
// Method call CONTENTS has ONLY ONE ITEM, uses "Collapsed" coding style settings.
fooBars.Add(fooBar);
return true; }
So the question of the day is: "Is there are way to CURRENTLY do either 'Expanded' or 'Mixed' coding style settings above?"
Message was edited by: Scott Elliott ( cleaned things up and added another coding style setting "MIXED" that is obviously a mixture of "COLLAPSED" and "EXPANDED" based on the number of CONTENTS inside the PARENTHESIS ).
Please sign in to leave a comment.
Hello Scott
As I see my colleague has already created a ticket for your request https://youtrack.jetbrains.com/issue/RSRP-427585.
Thanks!
Yes I had contacted them through ReSharper Feedback and we had a back and forth and they created a ticket. So, hopefully it gets voted up! I do have a question though, it seems that every time I'd try to fix the formatting in my original post here, if I went from Editor to HTML view and back to Editor to check my changes it acted as though I had already submitted my edit and I could never get my actual edit to submit.