MBeautifier icon indicating copy to clipboard operation
MBeautifier copied to clipboard

Unexpected/Inconsistent behavior for if one liner

Open FlorianPfaff opened this issue 6 years ago • 3 comments
trafficstars

Hi, thanks for addressing my last issue. Since this is so actively maintained, I thought you may be interested in another issue that is, however, not as critical. I'll explain it on MWEs. if 0 == 0, a = 1 + 1, end is kept as it is by the tool. However, if 0 == 0, a = 1 + 1; end is turned into if 0 == 0, a = 1 + 1; end

I think these cases should behave identically. I personally prefer the former behavior because it does allow one liners. However, I can also understand if one liners are eliminated - in this case, however, the a = 1 + 1; expression should also put in a separate line.

Anyways, thanks again. And it's not an important issue because it does not break the code after all.

FlorianPfaff avatar Feb 08 '19 21:02 FlorianPfaff

It's an unfortunate side-effect of "AllowMultipleStatementsPerLine": The idea is, that if this option is enabled:

a = 2; b = 3; is formatted as

a = 2;
b = 3;

At the moment, the "multiple statement" only means that multiple parts can be found where the closing character is semicolon (";").

One liner if-s (and this is also valid to for and while loops) theoretically could be supported, but for example:

if 0 == 0, a = 1 + 1; end -> Should remain one-liner if 0 == 0, a = 1 + 1; b = 2 + 2; end -> Should remain one-liner, or should be formatted as?:

if 0 == 0,
    a = 1 + 1;
    b = 2 + 2;
end

I personally would expect the one-liner form. And I could imagine 3 new options below "AllowMultipleStatementsPerLine":

  • AllowOneLinerIf
  • AllowOneLinerFor
  • AllowOneLinerWhile

What do you think?

davidvarga avatar Feb 10 '19 12:02 davidvarga

That definitely sounds like a thorough solution.

I would expect both of your examples to stay one-liners if "AllowOneLinerIf" if set to true. While I would avoid multiple statements in an if one-liner, I can think of examples that may make sense, e.g., to avoid distracation from plotting commands such as if ishold, hold('off'); cla; end

Anyways, thanks again, your enthusiasm for this project is really commendable.

FlorianPfaff avatar Feb 10 '19 13:02 FlorianPfaff

The new rule was not yet implemented yet, but single-statement lines are now untouched even if "AllowOneLinerIf" is set to false.

So e.g.

if true, a = 1; end

will be untouched.

davidvarga avatar May 12 '19 15:05 davidvarga