Force line breaks when pretty-printing case statements

So

   case x of
     12 -> foo
     13 -> bar

instead of

   case x of 12 -> foo; 13 -> bar

(The latter doesn't parse.)
This commit is contained in:
Chris Gibbs 2018-11-14 22:48:03 +00:00
parent 55d4eaa17a
commit 2b2b0d0482
2 changed files with 17 additions and 5 deletions

View File

@ -29,8 +29,6 @@ import qualified Unison.PrettyPrintEnv as PrettyPrintEnv
--TODO let suppression (eg console.u `simulate`, delay blocks (eg ability-keyword.u)
--TODO "in cases where let is needed, let has higher precedence than fn application"
--TODO in demo/2.u `merge`, surplus parens in pattern, `((Optional.None), _)`, and surplus parens around lambda body (a case statement) (and in `sort` around a case statement as else body); ditto surplus parens around if/then/else in lambda body
--TODO type annotations above let bindings, not appended to them, eg tictactoe.u `isWin`
--TODO case alternatives are separated by ; if not being line broken - correct? or force line breaks?
--TODO `sum = Stream.fold-left 0 (+) t` being rendered as `sum = Stream.fold-left 0 + t`
--TODO precedence comment and double check in type printer
@ -126,9 +124,9 @@ pretty n p term = specialCases term $ \case
Or' x y -> paren (p >= 10) $ l"or" <> b" " <> pretty n 10 x <> b" " <> pretty n 10 y
LetRecNamed' bs e -> printLet bs e
Lets' bs e -> printLet (map (\(_, v, binding) -> (v, binding)) bs) e
Match' scrutinee branches -> paren (p >= 2) $
Match' scrutinee branches -> paren (p >= 2) $ PP.BrokenGroup $
PP.Group (l"case" <> b" " <> pretty n 2 scrutinee <> b" " <> l"of") <> b" " <>
(PP.Nest " " $ PP.Group $ fold (intersperse (b"; ") (map printCase branches)))
(PP.Nest " " $ fold (intersperse (b"; ") (map printCase branches)))
t -> l"error: " <> l (show t)
where specialCases term go =
case (term, binaryOpsPred) of

View File

@ -149,7 +149,13 @@ test = scope "termprinter" . tests $
, tc "case x of +1 -> foo"
, tc "case x of -1 -> foo"
, tc "case x of 3.14159 -> foo"
, tc "case x of true -> foo"
, tc_diff_rtt False "case x of\n\
\ true -> foo\n\
\ false -> bar"
"case x of true -> foo; false -> bar" 0
, tc_breaks 50 "case x of\n\
\ true -> foo\n\
\ false -> bar"
, tc "case x of false -> foo"
, tc "case x of y@() -> y"
, tc "case x of a@(b@(c@())) -> c"
@ -178,6 +184,14 @@ test = scope "termprinter" . tests $
, tc "case x of 12 -> x -> f x"
, tc_diff "case x of (12) -> x" $ "case x of 12 -> x"
, tc_diff "case (x) of 12 -> x" $ "case x of 12 -> x"
, tc_breaks 50 "case x of\n\
\ 12 -> x"
, pending $ tc_breaks 50 "if true\n\
\then\n\
\ case x of\n\
\ 12 -> x\n\
\else\n\
\ x" -- TODO parser bug? 'unexpected else' (and parens round case doesn't work either)
, tc_breaks 15 "case x of\n\
\ 12 -> x\n\
\ 13 -> y\n\