Additional setting for SP2001 (CodeLineMustBeginWithIdenticalWhitespaces)

Coordinator
Jan 20, 2011 at 9:55 PM

Dear Harald,

I'm surely going to implement an additional feature for rule SP2001 (CodeLineMustBeginWithIdenticalWhitespaces) as you requested.

It will look like additional checkbox "allow right-padding spaces for multiline expressions" that will be enabled when "mode" option is "Tabs" or "Both".
Criteria: right-padding spaces are allowed when
1. the line holds an expression (or its part)
2. the expression is placed on more than one line
3. the line in subject is not the first line of the expression 

In order to make the result more predictable, I suggest discussing its functionality here (before implementing it in a wrong way ;).
Please have a look at my examples below.
Don't forget that rule SP2001 is only about characters used for indentation and doesn't check overall indentation size (I plan to create a new rule for that).

So, when this checkbox is enabled, the following will be allowed:

(1)
→   if·(a·||·b·||
→   ····c·||·d)
→   {
→   }

(2)
→   if·(a·||·b·||
→   ········c·||·d)
→   {
→   }

(3)
→   if·(a·||·b·||
→   c·||·d)
→   {
→   }

(4)
→   if·(a·||·b·||
→   →   →   c·||·d)
→   {
→   }

(5)
→   if
→   
(
→   a·||·b·||
····c·||·d
→   )

→   {
→   }

The following will not be allowed:

(6)
→   if
→   (
····a·||·b·||
····c·||·d
→   )

→   {
→   }

(7)
→   ·if·(a·||·b·||
→   ····c·||·d)
→   {
→   }

(8)
→   if·(a·||·b·||
→   ····c·||·d)
→   ··{
→   ··}

As I said before, this feature will allow to pad any multiline expression.
For example, the following will be allowed too.

(9)
→   bool·a·=·b·||
→   →   ·····(c·&&
→   →   ······d)

Do these examples fully describe your idea?
If no, could you please provide more test-cases, specifying you request in a more detailed way?

Looking forward for you feedback.

Best regards,
Oleg Shuruev

Jan 20, 2011 at 10:56 PM

Dear Oleg,

almost perfect! It seems that this simple issue is getting more and more complex ;-)

Ad Don't forget that rule SP2001 is only about characters used for indentation and doesn't check overall indentation size (I plan to create a new rule for that).
Considering this (2) etc. will be OK.

Are you sure that (5) should be allowed?

I think you're right with (9) even I use such intending only for: if, for, foreach, while, do, catch - but to be honest such situation may only occur for "if" statements.

Idea for another rule (similar to an existing standard rule - all parameters of a method call must be in one line OR all parameters must be on their own line):

CallSomeMethod(a, b, c, d, e, f);

OR:

CallSomeMethod(
a,
b,
c,
d,
e,
f);

For the second case above (each parameter on its own line) I use double-intention. Also for declaring arrays and their values:

int[] someLocalArray = new[] {
1, 2, 3, 4, 
5, 6, 7, 8 };

etc.

Maybe it should be discussed in a separate thread.

Best Regards,
Harald

 

 

Coordinator
Jan 21, 2011 at 12:46 PM
Edited Jan 21, 2011 at 12:46 PM

Yes, let's discuss indentation size later.

Regarding your answers, maybe (5) was not very clear.
Of course, this code looks ugly - and it violates SA1008 and SA1009 (rules about parenthesis).
What I tried to ask there - should padding be allowed on the very fist line of the expression?

In other words, let's look through the proposed criteria:
Right-padding spaces are allowed when
1. the line holds an expression (or its part)
2. the expression is placed on more than one line
3. the line in subject is not the first line of the expression

Is #3 required or not?

Here are more concrete examples (they don't violate any other StyleCop rules):

(10)
→   bool·z·=
→   →   →   a·||·((b && c)

→   →   →   ·······|| d)

(11)
→   bool·z·=
→   →   →   ··a·||·((b && c)

→   →   →   ·········|| d)

Obviously, (10) should be allowed.
What about (11)?
Could someone want to pad an expression starting from its first line?

Personally, I thought for a while and now I think that both of them could be allowed by this setting.

What do you think?

Best regards,
Oleg Shuruev

Coordinator
Jan 25, 2011 at 12:02 PM

I have finally implemented the following in StyleCop+ 1.0:

Right space padding is allowed when
1. the line uses the same amount of tabs as the previous line
2. the line holds an expression that is placed on more than one line

Thus, only the following examples described above will be allowed:
(1), (2), (3), (4) 

Could you please give it a try?
Don't forget to enable this additional setting!

Best regards,
Oleg Shuruev

Jan 25, 2011 at 3:37 PM

Great, thank you!

I'll give it a try asap. At the moment I am very busy.

Best regards,
Harald-René Flasch (aka hfrmobile)