Skip to content
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

Tweaks to line breaking in pretty printer. #1292

Open
wants to merge 1 commit 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
10 changes: 5 additions & 5 deletions src/Cryptol/Parser/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ instance (Show name, PPName name) => PP (Decl name) where
ppPrec n decl =
case decl of
DSignature xs s -> commaSep (map ppL xs) <+> text ":" <+> pp s
DPatBind p e -> pp p <+> text "=" <+> pp e
DPatBind p e -> nest 2 (pp p <+> text "=" </> pp e)
DBind b -> ppPrec n b
DRec bs -> nest 2 (vcat ("recursive" : map (ppPrec n) bs))
DFixity f ns -> ppFixity f ns
Expand Down Expand Up @@ -695,7 +695,7 @@ ppPragma xs p =

instance (Show name, PPName name) => PP (Bind name) where
ppPrec _ b = vcat (sig ++ [ ppPragma [f] p | p <- bPragmas b ] ++
[hang (def <+> eq) 4 (pp (thing (bDef b)))])
[nest 2 (def <+> eq </> pp (thing (bDef b)))])
where def | bInfix b = lhsOp
| otherwise = lhs
f = bName b
Expand Down Expand Up @@ -850,7 +850,7 @@ instance (Show name, PPName name) => PP (Expr name) where

-- low prec
EFun _ xs e -> wrap n 0 ((text "\\" <.> hsep (map (ppPrec 3) xs)) <+>
text "->" <+> pp e)
text "->" </> pp e)

EIf e1 e2 e3 -> wrap n 0 $ sep [ text "if" <+> pp e1
, text "then" <+> pp e2
Expand All @@ -869,7 +869,7 @@ instance (Show name, PPName name) => PP (Expr name) where
$ ppInfix 2 isInfix ifix

EApp _ _ -> let (e, es) = asEApps expr in
wrap n 3 (ppPrec 3 e <+> fsep (map (ppPrec 4) es))
nest 2 (wrap n 3 (foldl (</>) (ppPrec 3 e) (map (ppPrec 4) es)))

ELocated e _ -> ppPrec n e

Expand Down Expand Up @@ -959,7 +959,7 @@ instance PPName name => PP (Type name) where
$ ppPrefixName f <+> fsep (map (ppPrec 4) ts)

TFun t1 t2 -> optParens (n > 1)
$ sep [ppPrec 2 t1 <+> text "->", ppPrec 1 t2]
$ ppPrec 2 t1 <+> text "->" </> ppPrec 1 t2

TLocated t _ -> ppPrec n t

Expand Down
2 changes: 1 addition & 1 deletion src/Cryptol/TypeCheck/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ instance PP (WithNames Type) where
$ brackets (go 0 t1) <.> go 4 t2

(TCFun, [t1,t2]) -> optParens (prec > 1)
$ go 2 t1 <+> text "->" <+> go 1 t2
$ go 2 t1 <+> text "->" </> go 1 t2

(TCTuple _, fs) -> ppTuple $ map (go 0) fs

Expand Down
7 changes: 5 additions & 2 deletions src/Cryptol/Utils/PP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ infixl 6 <.>, <+>, </>
(<+>) = liftPP2 (PP.<+>)

(</>) :: Doc -> Doc -> Doc
Doc x </> Doc y = Doc (\e -> x e <> PP.softline <> y e)
Doc x </> Doc y = Doc (\e -> x e <> PP.group (PP.line <> y e))

infixl 5 $$

Expand All @@ -251,7 +251,10 @@ sep :: [Doc] -> Doc
sep = liftSep PP.sep

fsep :: [Doc] -> Doc
fsep = liftSep PP.fillSep
fsep = liftSep fillSep
where
fillSep [] = mempty
fillSep (d0 : ds) = foldl (\a d -> a <> PP.group (PP.line <> d)) d0 ds

hsep :: [Doc] -> Doc
hsep = liftSep PP.hsep
Expand Down