@@ -81,8 +81,15 @@ p_matchGroup'
8181 -> MatchGroupStyle
8282 -> MatchGroup GhcPs (Located body)
8383 -> R ()
84−p_matchGroup' placer pretty style MG {..} =
85− sep newline (located' p_Match) (unLoc mg_alts)
84+p_matchGroup' placer pretty style MG {..} = do
85+ let ob = case style of
86+ Case -> id
87+ LambdaCase -> id
88+ _ -> dontUseBraces
89+ -- NOTE since we are forcing braces on 'sepSemi' based on 'ob', we have to
90+ -- restore the brace state inside the sepsemi.
91+ ub <- bool dontUseBraces useBraces <$> canUseBraces
92+ ob $ sepSemi (located' (ub . p_Match)) (unLoc mg_alts)
8693 where
8794 p_Match m@Match {..} =
8895 p_match'
@@ -179,7 +186,6 @@ p_match' placer pretty style isInfix strictness m_pats m_grhss = do
179186 stdCase
180187 LambdaCase -> stdCase
181188 return inci'
182−
183189 let
184190 -- Calculate position of end of patterns. This is useful when we decide
185191 -- about putting certain constructions in hanging positions.
@@ -192,49 +198,44 @@ p_match' placer pretty style isInfix strictness m_pats m_grhss = do
192198 Case -> True
193199 LambdaCase -> True
194200 _ -> False
201+ let GRHSs {..} = m_grhss
202+ hasGuards = withGuards grhssGRHSs
203+ unless (length grhssGRHSs > 1) $ do
204+ case style of
205+ Function _ | hasGuards -> return ()
206+ Function _ -> txt " ="
207+ PatternBind -> txt " ="
208+ s | isCase s && hasGuards -> return ()
209+ _ -> txt " ->"
210+ let grhssSpan = combineSrcSpans' $
211+ getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
212+ patGrhssSpan = maybe grhssSpan
213+ (combineSrcSpans grhssSpan . srcLocSpan) endOfPats
214+ placement =
215+ case endOfPats of
216+ Nothing -> blockPlacement placer grhssGRHSs
217+ Just spn ->
218+ if isOneLineSpan
219+ (mkSrcSpan spn (srcSpanStart grhssSpan))
220+ then blockPlacement placer grhssGRHSs
221+ else Normal
222+ p_body = do
223+ let groupStyle =
224+ if isCase style && hasGuards
225+ then RightArrow
226+ else EqualSign
227+ sep newline (located' (p_grhs' pretty groupStyle)) grhssGRHSs
228+ p_where = do
229+ let whereIsEmpty = GHC.isEmptyLocalBindsPR (unLoc grhssLocalBinds)
230+ unless (GHC.eqEmptyLocalBinds (unLoc grhssLocalBinds)) $ do
231+ breakpoint
232+ txt "where"
233+ unless whereIsEmpty breakpoint
234+ inci $ located grhssLocalBinds p_hsLocalBinds
195235 inci' $ do
196− let GRHSs {..} = m_grhss
197− hasGuards = withGuards grhssGRHSs
198− unless (length grhssGRHSs > 1) $ do
199− case style of
200− Function _ | hasGuards -> return ()
201− Function _ -> txt " ="
202− PatternBind -> txt " ="
203− s | isCase s && hasGuards -> return ()
204− _ -> txt " ->"
205− let grhssSpan = combineSrcSpans' $
206− getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
207− patGrhssSpan = maybe grhssSpan
208− (combineSrcSpans grhssSpan . srcLocSpan) endOfPats
209− placement =
210− case endOfPats of
211− Nothing -> blockPlacement placer grhssGRHSs
212− Just spn ->
213− if isOneLineSpan
214− (mkSrcSpan spn (srcSpanStart grhssSpan))
215− then blockPlacement placer grhssGRHSs
216− else Normal
217− inciLocalBinds = case placement of
218− Normal -> id
219− Hanging -> inci
220− p_body = do
221− let groupStyle =
222− if isCase style && hasGuards
223− then RightArrow
224− else EqualSign
225− sep newline (located' (p_grhs' pretty groupStyle)) grhssGRHSs
226− let whereLocation = combineSrcSpans patGrhssSpan $ getLoc grhssLocalBinds
227− whereIsEmpty = GHC.isEmptyLocalBindsPR (unLoc grhssLocalBinds)
228− unless (GHC.eqEmptyLocalBinds (unLoc grhssLocalBinds))
229− . inciLocalBinds
230− . switchLayout [whereLocation] $ do
231− if whereIsEmpty then newline else breakpoint
232− txt "where"
233− unless whereIsEmpty $ do
234− breakpoint
235− inci (located grhssLocalBinds p_hsLocalBinds)
236236 switchLayout [patGrhssSpan] $
237237 placeHanging placement p_body
238+ inci p_where
238239
239240p_grhs :: GroupStyle -> GRHS GhcPs (LHsExpr GhcPs) -> R ()
240241p_grhs = p_grhs' p_hsExpr
@@ -386,7 +387,7 @@ p_stmt' pretty = \case
386387 inci (p_hsExpr x)
387388 RecStmt {..} -> do
388389 txt "rec "
389− sitcc $ sep newline (located' (p_stmt' pretty)) recS_stmts
390+ sitcc $ sepSemi (located' (p_stmt' pretty)) recS_stmts
390391 XStmtLR {} -> notImplemented "XStmtLR"
391392
392393gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]]
@@ -411,7 +412,7 @@ p_hsLocalBinds = \case
411412 (Left <$> bagToList bag) ++ (Right <$> lsigs)
412413 p_item (Left x) = located x p_valDecl
413414 p_item (Right x) = located x p_sigDecl
414− sitcc $ sep newline (sitcc . p_item) (sortOn ssStart items)
415+ sitcc $ sepSemi (useBraces . p_item) (sortOn ssStart items)
415416 HsValBinds NoExt _ -> notImplemented "HsValBinds"
416417 HsIPBinds NoExt _ -> notImplemented "HsIPBinds"
417418 EmptyLocalBinds NoExt -> return ()
@@ -468,18 +469,23 @@ p_hsExpr = \case
468469 txt "@"
469470 located (hswc_body a) p_hsType
470471 OpApp NoExt x op y -> do
471− located x p_hsExpr
472− -- NOTE If the end of the first argument and the beginning of the second
473− -- argument are on the same line, and the second argument has a hanging
474− -- form, use hanging placement.
472+ -- NOTE If the beginning of the first argument and the second argument
473+ -- are on the same line, and the second argument has a hanging form, use
474+ -- hanging placement.
475475 let placement =
476476 if isOneLineSpan
477− (mkSrcSpan (srcSpanEnd (getLoc x)) (srcSpanStart (getLoc y)))
477+ (mkSrcSpan (srcSpanStart (getLoc x)) (srcSpanStart (getLoc y)))
478478 then exprPlacement (unLoc y)
479479 else Normal
480480 opWrapper = case unLoc op of
481481 EWildPat NoExt -> backticks
482482 _ -> id
483+ ub <- vlayout
484+ (return useBraces)
485+ (return $ case placement of
486+ Hanging -> useBraces
487+ Normal -> dontUseBraces)
488+ ub $ located x p_hsExpr
483489 placeHanging placement $ do
484490 located op (opWrapper . p_hsExpr)
485491 space
@@ -543,10 +549,9 @@ p_hsExpr = \case
543549 HsDo NoExt ctx es -> do
544550 let doBody header = do
545551 txt header
546− if length (unLoc es) <= 1
547− then breakpoint
548− else newline
549− inci $ located es (sep newline (located' (sitcc . p_stmt)))
552+ breakpoint
553+ ub <- vlayout (return useBraces) (return id)
554+ inci $ sepSemi (located' (ub . p_stmt)) (unLoc es)
550555 compBody = brackets $ located es $ \xs -> do
551556 let p_parBody = sitcc . sep
552557 (breakpoint >> txt "| ")
@@ -864,7 +869,7 @@ p_hsBracket = \case
864869 txt "|"
865870 breakpoint'
866871 inci $ do
867− body
872+ dontUseBraces body
868873 breakpoint'
869874 txt "|]"
870875