Skip to content

Sort case branches to match Msg constructor ordering (only in update functions?) #823

@KasMA1990

Description

@KasMA1990

In my day-to-day writing Elm code, I can only think of one significant place where I don't have autoformatting today, but wish I had: specifying the order of my Msg constructors in my update functions. That is, if my update function looks like this:

update msg model =
    case msg of
        A -> ...
        B -> ...
        C -> ...

then I know that the A,B,C ordering is something I've defined somewhere.

This usually comes up when I want to add new constructors to an existing Msg type. Often those new constructors relate to some existing messages, so I probably want to group them. The most natural place to do so, I find, is with the type definition itself. So although a type like:

type Msg 
    = A
    | B
    | C

looks fine, and it's easy to keep update using the same ordering, it becomes more annoying to keep them in sync when the type has morphed into

type Msg 
    = A
    -- Initialisation messages 
    | B
    | D
    -- Drag and drop impl messages
    | C
    | E
    | F
...

and it would be really nice if there was a way to just keep update in sync with an ordering like this. Some caveats though:

  • I think I would only want this for Msg types and the update function, and not in general for all custom types everywhere.
  • I get that this may not be the right decision for everyone, so it might be worth having as something you opt into.

Of course, those two points can make it tricky to unite this feature with elm-format, because it eschews configuration and special cases.

On the other hand, elm-format already has this feature for a different kind of ordering: formatting exports as specified in module docs. So maybe there is room for something similar for Msg and update? There's lots of room for bikeshedding here, so I won't make concrete proposals for how it should look before testing if this is something anyone besides me would be interested in 😊

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions