diff --git a/primer/src/Primer/Pretty.hs b/primer/src/Primer/Pretty.hs index bc70c1293..126634c26 100644 --- a/primer/src/Primer/Pretty.hs +++ b/primer/src/Primer/Pretty.hs @@ -60,6 +60,10 @@ data PrettyOptions = PrettyOptions -- ^ Attempt to print λs and Λs on one line , inlineForAll :: Bool -- ^ Attempt to print @for all@ and associated type sig on one line + , inlineCon :: Bool + -- ^ Attempt to print constructors and all arguments on one line + , inlineAnn :: Bool + -- ^ Attempt to print annotations on one line } -- | Default PrettyOptions - makes no attempt to group text @@ -71,6 +75,8 @@ sparse = , inlineLet = False , inlineLambda = False , inlineForAll = False + , inlineCon = False + , inlineAnn = False } -- | Groups whenever possible @@ -82,6 +88,8 @@ compact = , inlineLet = True , inlineLambda = True , inlineForAll = True + , inlineCon = True + , inlineAnn = True } -- | Pretty prints @Expr'@ using Prettyprinter library @@ -91,7 +99,7 @@ prettyExpr opts = \case EmptyHole _ -> col Red "?" Con _ n tms -> let prettyTms = brac Round White . pE <$> tms - in vsep $ col Green (gname opts n) : prettyTms + in (if inlineCon opts then group else identity) $ vsep $ col Green (gname opts n) : prettyTms Var _ v -> case v of GlobalVarRef n -> col Blue (gname opts n) LocalVarRef n -> lname n @@ -161,7 +169,7 @@ prettyExpr opts = \case <> inlineblock opts (pE e) <> col Yellow "in" <> line - <> indent' 2 (pE e') + <> indent' 2 (brac Round White $ pE e') LetType _ v t e -> col Yellow "let type" <+> lname v @@ -169,7 +177,7 @@ prettyExpr opts = \case <> inlineblock opts (pT t) <> col Yellow "in" <> line - <> indent' 2 (pE e) + <> indent' 2 (brac Round White $ pE e) Letrec _ v e t e' -> col Yellow "let rec" <+> lname v @@ -177,7 +185,7 @@ prettyExpr opts = \case <> inlineblock opts (typeann e t) <> col Yellow "in" <> line - <> indent' 2 (pE e') + <> indent' 2 (brac Round White $ pE e') PrimCon _ p -> prim p where pT = prettyType opts @@ -185,7 +193,7 @@ prettyExpr opts = \case prim = \case PrimChar c -> "Char" <+> pretty @Text (show c) PrimInt n -> "Int" <+> pretty @Text (show n) - typeann e t = brac Round Yellow (pE e) <+> col Yellow "::" <> line <> brac Round Yellow (pT t) + typeann e t = brac Round Yellow (pE e) <+> (if inlineAnn opts then group else identity) (col Yellow "::" <> line <> brac Round Yellow (pT t)) -- When grouped: " x " -- When ungrouped: "\n\tx\n" @@ -226,10 +234,15 @@ rBrac Curly = "?}" -- Adds brackets of type b around "doc" with color c brac :: BracketType -> Color -> Doc AnsiStyle -> Doc AnsiStyle -brac b c doc = col c (lBrac b) <> line' <> flatAlt (indent 2 doc) doc <> line' <> col c (rBrac b) +--brac b c doc = col c (lBrac b) <> line' <> flatAlt (indent 2 doc) doc <> line' <> col c (rBrac b) +brac b c doc = if 1 == length (T.words $ show doc) -- TODO: hack to avoid superfluous brackets + then doc + else col c (lBrac b) <> line' <> flatAlt (indent 2 doc) doc <> line' <> col c (rBrac b) col :: Color -> Doc AnsiStyle -> Doc AnsiStyle -col = annotate . color +col = \case + White -> identity -- TODO: hack! we use "White" to mean "terminal default" + c -> annotate $ color c -- | Pretty prints @Type'@ using Prettyprinter library prettyType :: PrettyOptions -> Type' b -> Doc AnsiStyle @@ -254,7 +267,7 @@ prettyType opts typ = case typ of <> inlineblock opts (pT t) <> col Yellow "in" <> line - <> indent' 2 (pT b) + <> indent' 2 (brac Round White $ pT b) where pT = prettyType opts diff --git a/primer/test/outputs/Pretty/comprehensive/Expr (Compact).ansi b/primer/test/outputs/Pretty/comprehensive/Expr (Compact).ansi index a9d43dcc9..a88a3741b 100644 --- a/primer/test/outputs/Pretty/comprehensive/Expr (Compact).ansi +++ b/primer/test/outputs/Pretty/comprehensive/Expr (Compact).ansi @@ -1,44 +1,27 @@ -let x = (True) :: (Bool) in - let rec y = - ( - ( - {?(Just (?)) :: ((Maybe) (?))?} - ) - ( - {?unboundName?} - ) - ) :: - ( - {?Maybe?} - ) - in - ( - λi. - Λβ. - ( - ( - let type b = Bool in - Left - ) @β - ) - ( - match i with - Zero → False - Succ n → match n with - Zero → ( - ( - ? - ) - ( - x - ) - ) - ( - y - ) - _ → ? - ) - ) :: - ( - Nat -> ∀ α. ((Either) (Bool)) (α) - ) +let x = True :: Bool in + ( + let rec y = (({?(Just ?) :: (Maybe ?)?}) unboundName) :: Maybe in + ( + ( + λi. + Λβ. + ( + ( + let type b = Bool in + Left + ) @β + ) + ( + match i with + Zero → False + Succ n → match n with + Zero → ( + ? + x + ) + y + _ → ? + ) + ) :: (Nat -> ∀ α. (Either Bool) α) + ) + ) diff --git a/primer/test/outputs/Pretty/comprehensive/Expr (Sparse).ansi b/primer/test/outputs/Pretty/comprehensive/Expr (Sparse).ansi index 3dc91420d..bca91e299 100644 --- a/primer/test/outputs/Pretty/comprehensive/Expr (Sparse).ansi +++ b/primer/test/outputs/Pretty/comprehensive/Expr (Sparse).ansi @@ -1,83 +1,57 @@ let x = - ( - True - ) :: - ( - Bool - ) + True :: + Bool in - let rec y = - ( - ( - {? - ( - Just - ( + ( + let rec y = + ( + ( + {? + ( + Just ? - ) - ) :: - ( - ( + ) :: + ( Maybe - ) - ( ? - ) - ) - ?} - ) - ( - {? - unboundName - ?} - ) - ) :: - ( - {? - Maybe - ?} - ) - in - ( - λi. - Λβ. - ( - ( - let type b = - Bool - in - Left - ) @β - ) - ( - match i with - Zero → False - Succ n → match n with - Zero → ( - ( - ? - ) - ( - x - ) - ) - ( - y - ) - _ → ? - ) - ) :: - ( - Nat -> ∀ α. - ( - ( - Either - ) - ( - Bool - ) - ) - ( - α - ) - ) + ) + ?} + ) + unboundName + ) :: + Maybe + in + ( + ( + λi. + Λβ. + ( + ( + let type b = + Bool + in + Left + ) @β + ) + ( + match i with + Zero → False + Succ n → match n with + Zero → ( + ? + x + ) + y + _ → ? + ) + ) :: + ( + Nat -> ∀ α. + ( + Either + Bool + ) + α + ) + ) + ) diff --git a/primer/test/outputs/Pretty/comprehensive/Type (Compact).ansi b/primer/test/outputs/Pretty/comprehensive/Type (Compact).ansi index 5c8a9cf03..9776b5749 100644 --- a/primer/test/outputs/Pretty/comprehensive/Type (Compact).ansi +++ b/primer/test/outputs/Pretty/comprehensive/Type (Compact).ansi @@ -1 +1 @@ -Nat -> ∀ a. ({?(List) (?)?}) (a) +Nat -> ∀ a. ({?List ??}) a diff --git a/primer/test/outputs/Pretty/comprehensive/Type (Sparse).ansi b/primer/test/outputs/Pretty/comprehensive/Type (Sparse).ansi index 3b4f7ce2f..46a5db3e9 100644 --- a/primer/test/outputs/Pretty/comprehensive/Type (Sparse).ansi +++ b/primer/test/outputs/Pretty/comprehensive/Type (Sparse).ansi @@ -1,14 +1,8 @@ Nat -> ∀ a. - ( + ( {? - ( - List - ) - ( - ? - ) + List + ? ?} - ) - ( - a - ) + ) + a