ennocramer / floskell

Floskell is a flexible Haskell source code pretty printer.
BSD 3-Clause "New" or "Revised" License
178 stars 22 forks source link

Option to align let bindings and in expression #58

Closed mpilgrem closed 3 years ago

mpilgrem commented 3 years ago

I could not see an existing option to allow the popular style of formatting where the in expression is aligned with the let bindings, for example (taken from Learn You a Haskell for Great Good!):

cylinder :: (RealFloat a) => a -> a -> a  
cylinder r h = 
    let sideArea = 2 * pi * r * h  
        topArea = pi * r ^2  
    in  sideArea + 2 * topArea

If there is not an existing option, I think this can be achieved by adding an option to OptionConfig (say cfgOptionAlignLetBindsAndInExpr :: !Bool) and then modifying the relevant part of prettyPrint to be:

    prettyPrint (Let _ binds expr) = withLayout cfgLayoutLet flex vertical
      where
        flex = do
            write "let "
            prettyOnside (CompactBinds binds)
            spaceOrNewline
            nl <- gets psNewline
            alignP <- getOption cfgOptionAlignLetBindsAndInExpr
            write $ if nl && alignP then "in  " else "in "
            prettyOnside expr

        vertical = withIndentAfter
                       cfgIndentLet
                       (do
                            write "let"
                            withIndent cfgIndentLetBinds $
                                pretty (CompactBinds binds))
                       (do
                            newline
                            alignP <- getOption cfgOptionAlignLetBindsAndInExpr
                            write $ if alignP then "in " else "in"
                            withIndent cfgIndentLetIn $ pretty expr)

If that is of interest, I can propose a pull request.

ennocramer commented 3 years ago

Apologies for the late reply, but please do open that PR. This is a very nice and welcome addition!

mpilgrem commented 3 years ago

I have opened a pull request.