elm-format
elm-format copied to clipboard
Keep preformatted code blocks
I'm not really sure how to name this issue, so let me show it.
elm-format will turn this:
deck : List Card
deck =
[ (Spade, Manille), (Club, Manille), (Diamond, Manille), (Heart, Manille)
, (Spade, Ace), (Club, Ace), (Diamond, Ace), (Heart, Ace)
, (Spade, King), (Club, King), (Diamond, King), (Heart, King)
, (Spade, Queen), (Club, Queen), (Diamond, Queen), (Heart, Queen)
, (Spade, Jack), (Club, Jack), (Diamond, Jack), (Heart, Jack)
, (Spade, Nine), (Club, Nine), (Diamond, Nine), (Heart, Nine)
, (Spade, Eight), (Club, Eight), (Diamond, Eight), (Heart, Eight)
, (Spade, Seven), (Club, Seven), (Diamond, Seven), (Heart, Seven)
]
into this:
deck : List Card
deck =
[ ( Spade, Manille )
, ( Club, Manille )
, ( Diamond, Manille )
, ( Heart, Manille )
, ( Spade, Ace )
, ( Club, Ace )
, ( Diamond, Ace )
, ( Heart, Ace )
, ( Spade, King )
, ( Club, King )
, ( Diamond, King )
, ( Heart, King )
, ( Spade, Queen )
, ( Club, Queen )
, ( Diamond, Queen )
, ( Heart, Queen )
, ( Spade, Jack )
, ( Club, Jack )
, ( Diamond, Jack )
, ( Heart, Jack )
, ( Spade, Nine )
, ( Club, Nine )
, ( Diamond, Nine )
, ( Heart, Nine )
, ( Spade, Eight )
, ( Club, Eight )
, ( Diamond, Eight )
, ( Heart, Eight )
, ( Spade, Seven )
, ( Club, Seven )
, ( Diamond, Seven )
, ( Heart, Seven )
]
It would be great if we could instruct elm-format to not reformat this code. I do not really have a good idea how to do it (maybe surround it with some elm-format ignore comments), but maybe someone else has an idea?
Hello, this is interesting to consider, but is significantly complicated to design how the algorithm would work. How would preformatted blocks be distinguished from blocks that need to be formatted?
As an alternative, I would suggest the following which would allow you to keep things grouped:
deck : List Card
deck =
[ [ ( Spade, Manille ), ( Club, Manille ), ( Diamond, Manille ), ( Heart, Manille ) ]
, [ ( Spade, Ace ), ( Club, Ace ), ( Diamond, Ace ), ( Heart, Ace ) ]
, [ ( Spade, King ), ( Club, King ), ( Diamond, King ), ( Heart, King ) ]
, [ ( Spade, Queen ), ( Club, Queen ), ( Diamond, Queen ), ( Heart, Queen ) ]
, [ ( Spade, Jack ), ( Club, Jack ), ( Diamond, Jack ), ( Heart, Jack ) ]
, [ ( Spade, Nine ), ( Club, Nine ), ( Diamond, Nine ), ( Heart, Nine ) ]
, [ ( Spade, Eight ), ( Club, Eight ), ( Diamond, Eight ), ( Heart, Eight ) ]
, [ ( Spade, Seven ), ( Club, Seven ), ( Diamond, Seven ), ( Heart, Seven ) ]
]
|> List.concat
Alternatively, refactoring the code could lead to something that doesn't need special formatting. For example,
allCardsOfRank rank =
[ Spade, Club, Diamond, Heart ]
|> List.map (\suit -> ( suit, rank ))
deck =
[ Manille, Ace, King, Queen, Jack, Nine, Eight, Seven ]
|> List.concatMap allCardsOfRank
@avh4 for this piece of code, the refactoring would be ok, but I have an other example where I would like to keep the formatting. See https://github.com/tcoopman/game-of-life/blob/master/GameOfLife.elm#L73
I was wondering if surrounding the code -- elm-format disable
and -- elm-format enable
could be options? Or maybe something more fine grained like -- elm-format enable-only
I just happen to stumble on an other example where I would like to disable the formatting for one section:
this is after commenting
fixed =
[ (rotateN 1 elbow)
, (rotateN 2 tee)
, (rotateN 2 tee)
, (rotateN 2 elbow)
, (rotateN 1 tee)
, (rotateN 1 tee)
, (rotateN 2 tee)
, (rotateN 3 tee)
, (rotateN 1 tee)
, (rotateN 0 tee)
, (rotateN 3 tee)
, (rotateN 3 tee)
, (rotateN 0 elbow)
, (rotateN 0 tee)
, (rotateN 0 tee)
, (rotateN 3 elbow)
]
This is how I would like it:
fixed =
[ (rotateN 1 elbow), (rotateN 2 tee), (rotateN 2 tee), (rotateN 2 elbow)
, (rotateN 1 tee), (rotateN 1 tee), (rotateN 2 tee), (rotateN 3 tee)
, (rotateN 1 tee), (rotateN 0 tee), (rotateN 3 tee), (rotateN 3 tee)
, (rotateN 0 elbow), (rotateN 0 tee), (rotateN 0 tee), (rotateN 3 elbow)
]
This is from a board game, which has a 4 x 4 layout.
Here's another example (a game with a set/list of hexagonal tiles):
rings =
[ {- -} (Point 1 0), (Point 2 0), (Point 3 0)
, {- -} (Point 0 1), (Point 1 1), (Point 2 1), (Point 3 1)
, (Point 0 2), (Point 1 2), (Point 2 2), (Point 3 2), (Point 4 2)
, {- -} (Point 0 3), (Point 1 3), (Point 2 3), (Point 3 3)
, {- -} (Point 1 4), (Point 2 4), (Point 3 4)]
Here’s the truth table pattern example from Elm In Action:
case ( link, page ) of
--------------------------------------------
( Gallery, Gallery ) -> True
( Gallery, _ ) -> False
( Folders, Folders ) -> True
( Folders, SelectedPhoto _ ) -> True
( Folders, _ ) -> False
( SelectedPhoto _, _ ) -> False
( NotFound, _ ) -> False
Here it’s used for something a little more complicated, but it could also be used for simply comparing custom types. Anyway, it’s one of the cases where compactness greatly improves readability, IMO.
As for the ignore directives syntax, Prettier seems to simply ignore the next statement after the comment. Maybe elm-format
could ignore expressions (bindings, if
s, case
s etc.)
@avh4 any chance we can get this soon? Where would we need to look in the codebase to implement this?