elm-format icon indicating copy to clipboard operation
elm-format copied to clipboard

allow more whitespace inconsistencies in case statements

Open avh4 opened this issue 3 years ago • 2 comments

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
  • [ ] how does this interact with other whitespace-sensitive syntax?
    • [ ] let expressions
    • things that response to "fresh lines"
      • [ ] top-level definitions

avh4 avatar Feb 02 '21 21:02 avh4

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 -> () ?

avh4 avatar Feb 02 '21 21:02 avh4

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

avh4 avatar Feb 02 '21 21:02 avh4