elm-format
elm-format copied to clipboard
allow more whitespace inconsistencies in case statements
Elm itself requires the following of case
branches:
- all branches have the same indentation
- the indentation of branches is greater than the indentation of
case
Ideally, elm-format should relax both of those requirements as much as possible (while maintaining good parsing performance for already-formatted code, and having decent parser performance for lenient code), allowing:
f x =
case x of
_ -> ()
_ -> {- different indentation -} ()
f x =
case x of
_ -> {- indentation less than `case` -} ()
This one might be harder to do safely (performantly), as "fresh lines" (newline with no trailing whitespace) are handled specially when parsing whitespace:
f x =
case x of
_ -> {- branch starts after a fresh line -} ()
Ambiguity
When case statements are nested, relaxing these rules can result in ambiguity.
f x =
case x of
Just y ->
case y of
_ -> ()
_ -> {- which case is this attached to? -} ()
f x =
case x of
Just y ->
case y of
_ -> ()
_ -> {- which case is this attached to? -} ()
f x =
case x of
Just y -> case y of
_ -> ()
_ -> {- which case is this attached to? -} ()
f x =
case x of
Just y -> case y of
_ -> ()
_ -> {- which case is this attached to? -} ()
... other examples?
Open questions
- [ ] are there other possible parser ambiguities that could result from relaxing these rules?
- other syntax that uses
->
- [ ] lambda functions
- other syntax that uses
- [ ] how does this interact with other whitespace-sensitive syntax?
- [ ]
let
expressions - things that response to "fresh lines"
- [ ] top-level definitions
- [ ]
Here's an example of an ambiguity that would be introduced by relaxing the whitespace requirements:
case x of
Nothing -> f
Just
y -> ()
does this mean Nothing -> f Just; y -> ()
,
or does it mean Nothing -> f; Just y -> ()
?
use cases via lydell:
- Pasting (parts of) a case expression into another, and ending up with messed up code
- Adding a new branch and accidentally adding one space too much or too little