on master help sign in
Handle “prefixed blocks” when they go after an operator

The change is obvious from the examples.

commitf937aa7aafdd1886f7142ffbfb12c7f6edc76ab2 authormrkkrp <mark.karpov@tweag.io> dateThu 6 Jun 2019, 22:07 (7 years ago) parent99a345b
4 files changed+26−11
data/examples/declaration/value/function/do-out.hs+6 data/examples/declaration/value/function/do.hs+5 src/Ormolu/Printer/Meat/Declaration/Pat.hs+2−4 src/Ormolu/Printer/Meat/Declaration/Value.hs+13−7
data/examples/declaration/value/function/do-out.hs+6
@@ -20,3 +20,9 @@ baz = do
2020 bar c
2121 let d = c + 2
2222 return d
23+
24+quux =
25+ something $ do
26+ foo
27+ bar
28+ baz
data/examples/declaration/value/function/do.hs+5
@@ -16,3 +16,8 @@ baz = do
1616 bar c
1717 let d = c + 2
1818 return d
19+
20+quux = something $ do
21+ foo
22+ bar
23+ baz
src/Ormolu/Printer/Meat/Declaration/Pat.hs+2−4
@@ -55,10 +55,8 @@ p_pat = \case
5555 Nothing -> txt ".."
5656 Just x -> located x p_hsRecField
5757 inci . braces . velt . withSep comma f $ case dotdot of
58− Nothing ->
59− Just <$> fields
60− Just n -> do
61− (Just <$> take n fields) ++ [Nothing]
58+ Nothing -> Just <$> fields
59+ Just n -> (Just <$> take n fields) ++ [Nothing]
6260 InfixCon x y -> do
6361 located x p_pat
6462 space
src/Ormolu/Printer/Meat/Declaration/Value.hs+13−7
@@ -231,7 +231,9 @@ p_hsExpr = \case
231231 located x p_hsExpr
232232 space
233233 located op p_hsExpr
234− breakpoint
234+ if isPrefixBlockExpr (unL y)
235+ then space
236+ else breakpoint
235237 inci (located y p_hsExpr)
236238 NegApp NoExt e _ -> do
237239 txt "-"
@@ -437,10 +439,14 @@ getGRHSSpan (XGRHS NoExt) = notImplemented "XGRHS"
437439-- case expressions.
438440
439441isPrefixBlock :: [LGRHS GhcPs (LHsExpr GhcPs)] -> Bool
440−isPrefixBlock [(L _ (GRHS NoExt _ (L _ e)))] =
441− case e of
442− HsLam NoExt _ -> True
443− HsDo NoExt _ _ -> True
444− HsLamCase NoExt _ -> True
445− _ -> False
442+isPrefixBlock [(L _ (GRHS NoExt _ (L _ e)))] = isPrefixBlockExpr e
446443isPrefixBlock _ = False
444+
445+-- | Similar to 'isPrefixBlock', but just for checking 'HsExpr' directly.
446+
447+isPrefixBlockExpr :: HsExpr GhcPs -> Bool
448+isPrefixBlockExpr = \case
449+ HsLam NoExt _ -> True
450+ HsDo NoExt _ _ -> True
451+ HsLamCase NoExt _ -> True
452+ _ -> False