Skip to content

More liberal way of aligning matches #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions src/Floskell/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
if nl
then do
printCommentsBefore True ast
onside $ prettyPrint ast
onside $ cut $ prettyPrint ast
printCommentsAfter ast
else onside $ pretty ast

Expand Down Expand Up @@ -181,7 +181,7 @@

printCommentsAfter :: Annotated ast => ast NodeInfo -> Printer ()
printCommentsAfter ast = unless (null comments) $ suppressOnside $ do
let distance = srcSpanStartLine (commentSpan (head comments))

Check warning on line 184 in src/Floskell/Pretty.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 (ubuntu-latest)

In the use of ‘head’

Check warning on line 184 in src/Floskell/Pretty.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 (macOS-latest)

In the use of ‘head’
- srcSpanEndLine (nodeSpan ast)
when (distance > 0) $ do
ensureNewline
Expand All @@ -193,8 +193,8 @@

printCommentsInternal :: Int -> [Comment] -> Printer ()
printCommentsInternal correction comments = do
printComment correction (head comments)

Check warning on line 196 in src/Floskell/Pretty.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 (ubuntu-latest)

In the use of ‘head’

Check warning on line 196 in src/Floskell/Pretty.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 (macOS-latest)

In the use of ‘head’
forM_ (zip (tail comments) (map (srcSpanEndLine . commentSpan) comments)) $

Check warning on line 197 in src/Floskell/Pretty.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 (ubuntu-latest)

In the use of ‘tail’

Check warning on line 197 in src/Floskell/Pretty.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 (macOS-latest)

In the use of ‘tail’
\(comment, prevLine) -> do
let nextLine = srcSpanStartLine $ commentSpan comment
replicateM_ (nextLine - prevLine) newline
Expand Down Expand Up @@ -414,7 +414,9 @@
measure' p = fmap (: []) <$> measure p

measureMatch :: Match NodeInfo -> Printer (Maybe [Int])
measureMatch (Match _ name pats _ Nothing) = measure' (prettyApp name pats)
measureMatch (Match _ name pats rhs _) = case rhs of
UnGuardedRhs _ _ -> measure' (prettyApp name pats)
GuardedRhss _ grhss -> fmap sequence (mapM measureGuardedRhs grhss)
measureMatch (InfixMatch _ pat name pats _ Nothing) = measure' go
where
go = do
Expand All @@ -426,6 +428,12 @@
inter spaceOrNewline $ map pretty pats
measureMatch _ = return Nothing

measureGuardedRhs :: GuardedRhs NodeInfo -> Printer (Maybe Int)
measureGuardedRhs (GuardedRhs _ stmts _) = measure $
withIndentConfig cfgIndentMultiIf (space >> aligned p) (flip indented p)
where
p = prettyGuard stmts

measureDecl :: Decl NodeInfo -> Printer (Maybe [Int])
measureDecl (PatBind _ pat _ Nothing) = measure' (pretty pat)
measureDecl (FunBind _ matches) =
Expand Down Expand Up @@ -524,7 +532,9 @@
_ -> False

skipBlankDecl :: Decl NodeInfo -> Decl NodeInfo -> Bool
skipBlankDecl a _ = skipBlankAfterDecl a
skipBlankDecl a b = case (a, b) of
(PatBind{}, PatBind{}) -> True
_ -> skipBlankAfterDecl a

skipBlankClassDecl :: ClassDecl NodeInfo -> ClassDecl NodeInfo -> Bool
skipBlankClassDecl a _ = case a of
Expand Down Expand Up @@ -712,6 +722,16 @@
write "where"
withIndent cfgIndentWhereBinds $ pretty binds

prettyGuard :: [Stmt NodeInfo] -> Printer ()
prettyGuard stmts = do
operatorSectionR Pattern "|" $ write "|"
withLayout cfgLayoutDeclaration flex vertical
where
flex = listAutoWrap' pat sep stmts
vertical = list' pat sep stmts
pat = Pattern
sep = ","

instance Pretty Module where
prettyPrint (Module _ mhead pragmas imports decls) = inter blankline $
catMaybes [ ifNotEmpty prettyPragmas pragmas
Expand Down Expand Up @@ -1294,20 +1314,14 @@
withIndent cfgIndentMultiIf $ linedOnside guardedrhss

instance Pretty GuardedRhs where
prettyPrint (GuardedRhs _ stmts expr) =
withLayout cfgLayoutDeclaration flex vertical
prettyPrint (GuardedRhs _ stmts expr) = do
prettyGuard stmts
atTabStop stopRhs
withLayout cfgLayoutDeclaration (operator d op) (operatorV d op)
pretty expr
where
flex = do
operatorSectionR Pattern "|" $ write "|"
listAutoWrap' Pattern "," stmts
operator Declaration "="
pretty expr

vertical = do
operatorSectionR Pattern "|" $ write "|"
list' Pattern "," stmts
operatorV Declaration "="
pretty expr
d = Declaration
op = "="

instance Pretty Context where
prettyPrint (CxSingle _ asst) = do
Expand Down
3 changes: 3 additions & 0 deletions src/Floskell/Printers.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

module Floskell.Printers
Expand Down Expand Up @@ -65,7 +66,9 @@ import Control.Monad.State.Strict ( get, gets, modify )

import Data.List ( intersperse )
import qualified Data.Map.Strict as Map
#if __GLASGOW_HASKELL__ <= 802
import Data.Monoid ( (<>) )
#endif
import Data.Text ( Text )
import qualified Data.Text as T

Expand Down
Loading