on master help sign in
Format unboxed sum pattern
commitbe53b3e5049c6173daf0ced0f2243ee5b1b6297c authorgabriele.lana <gabriele.lana@gmail.com> dateSat 20 Jul 2019, 16:49 (7 years ago) parent54749b8
3 files changed+68−18
data/examples/declaration/value/function/pattern/unboxed-sum-pattern-out.hs+24 data/examples/declaration/value/function/pattern/unboxed-sum-pattern.hs+23 src/Ormolu/Printer/Meat/Declaration/Value.hs+21−18
data/examples/declaration/value/function/pattern/unboxed-sum-pattern-out.hsnew file+24
@@ -0,0 +1,24 @@
1+{-# LANGUAGE UnboxedSums #-}
2+
3+v = True
4+ where
5+ (# _x #) = (# True #)
6+
7+p = True
8+ where
9+ (# _x | #) = (# | True #)
10+
11+q = True
12+ where
13+ (# | _x | #) = (# | True | #)
14+
15+z = True
16+ where
17+ (# | | _x #) = (# | | True #)
18+
19+z_multiline = True
20+ where
21+ (# | | _x
22+ #) =
23+ (# | | True
24+ #)
data/examples/declaration/value/function/pattern/unboxed-sum-pattern.hsnew file+23
@@ -0,0 +1,23 @@
1+{-# LANGUAGE UnboxedSums #-}
2+
3+v = True
4+ where
5+ (# _x #) = (# True #)
6+
7+p = True
8+ where
9+ (# _x | #) = (# | True #)
10+
11+q = True
12+ where
13+ (# | _x | #) = (# | True | #)
14+
15+z = True
16+ where
17+ (# | | _x #) = (# | | True #)
18+
19+z_multiline = True
20+ where
21+ (# |
22+ | _x #) = (# |
23+ | True #)
src/Ormolu/Printer/Meat/Declaration/Value.hs+21−18
@@ -484,21 +484,8 @@ p_hsExpr = \case
484484 sep comma (located' p_hsTupArg) args
485485 else switchLayout (getLoc <$> args) . parens' . sitcc $
486486 sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
487− ExplicitSum NoExt tag arity e -> do
488− let before = tag - 1
489− after = arity - before - 1
490− args = replicate before Nothing <> [Just e] <> replicate after Nothing
491− f (x,i) = do
492− let isFirst = i == 0
493− isLast = i == arity - 1
494− case x of
495− Nothing ->
496− unless (isFirst || isLast) space
497− Just l -> do
498− unless isFirst space
499− located l p_hsExpr
500− unless isLast space
501− parensHash $ sep (txt "|") f (zip args [0..])
487+ ExplicitSum NoExt tag arity e ->
488+ p_unboxedSum tag arity (located e p_hsExpr)
502489 HsCase NoExt e mgroup -> do
503490 txt "case "
504491 located e p_hsExpr
@@ -710,9 +697,8 @@ p_pat = \case
710697 Boxed -> parens
711698 Unboxed -> parensHash
712699 f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats
713− SumPat NoExt pat _ _ -> do
714− -- XXX I'm not sure about this one.
715− located pat p_pat
700+ SumPat NoExt pat tag arity ->
701+ p_unboxedSum tag arity (located pat p_pat)
716702 ConPatIn pat details ->
717703 case details of
718704 PrefixCon xs -> sitcc $ do
@@ -765,6 +751,23 @@ p_pat_hsRecField HsRecField {..} = do
765751 breakpoint
766752 inci (located hsRecFieldArg p_pat)
767753
754+p_unboxedSum :: ConTag -> Arity -> R () -> R ()
755+p_unboxedSum tag arity m = do
756+ let before = tag - 1
757+ after = arity - before - 1
758+ args = replicate before Nothing <> [Just m] <> replicate after Nothing
759+ f (x,i) = do
760+ let isFirst = i == 0
761+ isLast = i == arity - 1
762+ case x :: Maybe (R ()) of
763+ Nothing ->
764+ unless (isFirst || isLast) space
765+ Just m' -> do
766+ unless isFirst space
767+ m'
768+ unless isLast space
769+ parensHash $ sep (txt "|") f (zip args [0..])
770+
768771p_hsSplice :: HsSplice GhcPs -> R ()
769772p_hsSplice = \case
770773 HsTypedSplice NoExt deco _ expr -> p_hsSpliceTH True expr deco