From b1299795166365970a15c6d73b7d9287737d9b0a Mon Sep 17 00:00:00 2001 From: mrkkrp Date: Thu, 23 May 2019 12:18:40 +0200 Subject: [PATCH] Fix guards --- .../declaration/value/function/guards-out.hs | 14 ++++++++++++++ .../declaration/value/function/guards.hs | 12 ++++++++++++ src/Ormolu/Printer/Meat/Declaration/Value.hs | 19 +++++++++++-------- 3 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 data/examples/declaration/value/function/guards-out.hs create mode 100644 data/examples/declaration/value/function/guards.hs diff --git a/data/examples/declaration/value/function/guards-out.hs b/data/examples/declaration/value/function/guards-out.hs new file mode 100644 index 0000000..ef57761 --- /dev/null +++ b/data/examples/declaration/value/function/guards-out.hs @@ -0,0 +1,14 @@ +foo :: Int -> Int +foo x + | x == 5 = 10 + | otherwise = 12 + +bar :: Int -> Int +bar x + | x == 5 = + foo x + + foo 10 + | x == 6 = + foo x + + foo 20 + | otherwise = foo 100 diff --git a/data/examples/declaration/value/function/guards.hs b/data/examples/declaration/value/function/guards.hs new file mode 100644 index 0000000..7f71f62 --- /dev/null +++ b/data/examples/declaration/value/function/guards.hs @@ -0,0 +1,12 @@ +foo :: Int -> Int +foo x + | x == 5 = 10 + | otherwise = 12 + +bar :: Int -> Int +bar x + | x == 5 = foo x + + foo 10 + | x == 6 = foo x + + foo 20 + | otherwise = foo 100 diff --git a/src/Ormolu/Printer/Meat/Declaration/Value.hs b/src/Ormolu/Printer/Meat/Declaration/Value.hs index c620c29..ff9397f 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Value.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Value.hs @@ -80,10 +80,10 @@ p_match style Match {..} = do inci' (velt' (located' p_pat <$> m_pats)) return inci' inci' $ do - space let GRHSs {..} = m_grhss - unless (length grhssGRHSs > 1) . txt $ - case style of + unless (length grhssGRHSs > 1) $ do + space + txt $ case style of Function _ -> "=" _ -> "->" let combinedSpans = combineSrcSpans' $ @@ -101,9 +101,6 @@ p_match style Match {..} = do line (txt "where") inci (located grhssLocalBinds p_hsLocalBinds) -getGRHSSpan :: GRHS GhcPs (LHsExpr GhcPs) -> SrcSpan -getGRHSSpan (GRHS _ body) = getSpan body - p_grhs :: GRHS GhcPs (LHsExpr GhcPs) -> R () p_grhs (GRHS guards body) = case guards of @@ -111,9 +108,9 @@ p_grhs (GRHS guards body) = xs -> do txt "| " velt $ withSep comma (located' p_stmt) xs - txt " ->" + txt " =" breakpoint - p_body + inci p_body where p_body = located body p_hsExpr @@ -237,3 +234,9 @@ p_hsExpr = \case EViewPat {} -> notImplemented "EViewPat" ELazyPat {} -> notImplemented "ELazyPat" HsWrap {} -> notImplemented "HsWrap" + +---------------------------------------------------------------------------- +-- Helpers + +getGRHSSpan :: GRHS GhcPs (LHsExpr GhcPs) -> SrcSpan +getGRHSSpan (GRHS _ body) = getSpan body