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

Allow unaligned cases in case statements

Open Janiczek opened this issue 9 years ago • 4 comments

This has probably much to do with whether elm-compiler deems file syntactically correct, but feels wrong in the context of elm-format.

update : Msg -> Model -> Model
update msg model =
    case msg of
        NoOp ->
            model
      RouteTo page ->
          { model | currentPage = page }

doesn't format - I have to change the RouteTo on the same column as NoOp. It feels like elm-format could do that without my help.

Similar issue is with top-level definitions:

foo : Int
             foo = 1

or even

             foo = 1

Janiczek avatar May 13 '16 17:05 Janiczek

This will be tricky to implement because of the following example:

foo x =
    case x of
        Just y ->
            case y of
                Just _ ->
                    ()
            Nothing ->
                    ()

which case does the Nothing belong with?

However, this seems good to do, despite the difficulty.

avh4 avatar May 13 '16 18:05 avh4

Understood. There will have to be more thought put into this, or at least discussion. As a first shot, it would make sense to me to put it to the most nested case (ie. to the right in your example).

Janiczek avatar May 13 '16 18:05 Janiczek

Yeah, that seems good... I think if it matches the indentation level of a case, then it goes with that one, if not then it goes with the inner-most one.

Also, let's have separate issues for each type of syntax. I renamed this issue to focus on case statements. Feel free to open other issues for other places (like the top-level definitions).

avh4 avatar May 14 '16 03:05 avh4

Most nested or not is going to be ambiguous, it't be safer to not match if it's ambiguous. But match when there's only one case statement.

My reasoning is: Most people will only have the one case statement and this will fix their needs. In the rarer event that there is nested cases and ambiguity, draw user attention to the code rather than possibly create a logic issue or a compile issue (when the case does not match the switch statement)

mordrax avatar May 30 '16 03:05 mordrax