Crystal: Formatter add newlines after comments following a `when then` including a semicolon

Created on 19 Aug 2018  路  8Comments  路  Source: crystal-lang/crystal

The formatter adds one line and then another one line after comments following a when then that includes a semicolon.

case 0
when 0 then 1; 2
# Comments
end

will format firstly to

case 0
when 0 then 1; 2
# Comments

end

and then reformat again to

case 0
when 0 then 1; 2
# Comments


end

Most helpful comment

What about begin? Do you forbid it? I think restricting this is unnecessary complexity in the compiler.

All 8 comments

Should that even compile?

case x
when 0 then 1
  2
end

doesn't make sense to me.

It doesn't have less sense than #6367 . I have put here integers to reduce the problem, but it can be anything else. I use a big case with quite a lot of whens, that have small operations to do for each.

case var
# This do things
when "a" then dosome; puts "done"
# This do other
when "test" then other; puts "also done"
# to continue...
end

It doesn't have less sense than #6367

It does IMO have much less sense than having an empty case which in itself is... just that, empty. Not incomprehensible like the code posted above.

I don't get @Sija, what's incomprehensible? Each when do 2 operations.

The actual code is https://github.com/DFabric/dppm/blob/master/src/dppm/cmd.cr . Its clearer on a single line, because they are similar, and also most in term of size (so we can see "columns")

It doesn't matter if there is a real use case and if that is better than it's alternatives.

A semicolon should always be usable to delimit an expression, as an alternative for a line break. This should just work an the same line as when ... then. It would be inconsistent and very awkward if this was a syntax error.

This just needs a fix in the formatter.

No, this should be a compile error because the code above is unreadable and anyone who wants to put two separate expressions inside a when should be using the standard case syntax, not the inline then syntax.

The compiler exists to detect bad or ambiguous (to the compiler or the human) code and reject it, and I can't think of any reason why this code couldn't always be replaced with

case x
when 0
  1
  2
end

Hence, I think it should be a compile error.

6367 was merged because it helped when macros were generating case blocks. This is unrelated. The very precedent that meant that an empty case was rejected in the first place implies that this syntax should be rejected.

If people really want to use this syntax, they can use

case 0
when 0 then (1; 2)
# Comments
end

which works and formats right now. The extra brackets help in showing that something weird is coming up.

What about begin? Do you forbid it? I think restricting this is unnecessary complexity in the compiler.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

will picture will  路  3Comments

ArthurZ picture ArthurZ  路  3Comments

nabeelomer picture nabeelomer  路  3Comments

Papierkorb picture Papierkorb  路  3Comments

RX14 picture RX14  路  3Comments