@@ -81,8 +81,15 @@ p_matchGroup'
81 -> MatchGroupStyle
81 -> MatchGroupStyle
82 -> MatchGroup GhcPs (Located body)
82 -> MatchGroup GhcPs (Located body)
84−p_matchGroup' placer pretty style MG {..} =
84+p_matchGroup' placer pretty style MG {..} = do
85+ let ob = case style of
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
85− sep newline (located' p_Match) (unLoc mg_alts)
92+ ob $ sepSemi (located' (ub . p_Match)) (unLoc mg_alts)
87 p_Match m@Match {..} =
94 p_Match m@Match {..} =
@@ -179,7 +186,6 @@ p_match' placer pretty style isInfix strictness m_pats m_grhss = do
180 LambdaCase -> stdCase
187 LambdaCase -> stdCase
181 return inci'
188 return inci'
184 -- Calculate position of end of patterns. This is useful when we decide
190 -- Calculate position of end of patterns. This is useful when we decide
185 -- about putting certain constructions in hanging positions.
191 -- about putting certain constructions in hanging positions.
@@ -192,49 +198,44 @@ p_match' placer pretty style isInfix strictness m_pats m_grhss = do
192 Case -> True
198 Case -> True
193 LambdaCase -> True
199 LambdaCase -> True
194 _ -> False
200 _ -> False
201+ let GRHSs {..} = m_grhss
202+ hasGuards = withGuards grhssGRHSs
203+ unless (length grhssGRHSs > 1) $ do
205+ Function _ | hasGuards -> return ()
206+ Function _ -> txt " ="
207+ PatternBind -> txt " ="
208+ s | isCase s && hasGuards -> return ()
210+ let grhssSpan = combineSrcSpans' $
211+ getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
212+ patGrhssSpan = maybe grhssSpan
213+ (combineSrcSpans grhssSpan . srcLocSpan) endOfPats
216+ Nothing -> blockPlacement placer grhssGRHSs
219+ (mkSrcSpan spn (srcSpanStart grhssSpan))
220+ then blockPlacement placer grhssGRHSs
224+ if isCase style && hasGuards
227+ sep newline (located' (p_grhs' pretty groupStyle)) grhssGRHSs
229+ let whereIsEmpty = GHC.isEmptyLocalBindsPR (unLoc grhssLocalBinds)
230+ unless (GHC.eqEmptyLocalBinds (unLoc grhssLocalBinds)) $ do
233+ unless whereIsEmpty breakpoint
234+ inci $ located grhssLocalBinds p_hsLocalBinds
195 inci' $ do
235 inci' $ do
196− let GRHSs {..} = m_grhss
197− hasGuards = withGuards grhssGRHSs
198− unless (length grhssGRHSs > 1) $ do
200− Function _ | hasGuards -> return ()
201− Function _ -> txt " ="
202− PatternBind -> txt " ="
203− s | isCase s && hasGuards -> return ()
205− let grhssSpan = combineSrcSpans' $
206− getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
207− patGrhssSpan = maybe grhssSpan
208− (combineSrcSpans grhssSpan . srcLocSpan) endOfPats
211− Nothing -> blockPlacement placer grhssGRHSs
214− (mkSrcSpan spn (srcSpanStart grhssSpan))
215− then blockPlacement placer grhssGRHSs
217− inciLocalBinds = case placement of
222− if isCase style && hasGuards
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))
230− . switchLayout [whereLocation] $ do
231− if whereIsEmpty then newline else breakpoint
233− unless whereIsEmpty $ do
235− inci (located grhssLocalBinds p_hsLocalBinds)
236 switchLayout [patGrhssSpan] $
236 switchLayout [patGrhssSpan] $
237 placeHanging placement p_body
237 placeHanging placement p_body
239p_grhs :: GroupStyle -> GRHS GhcPs (LHsExpr GhcPs) -> R ()
240p_grhs :: GroupStyle -> GRHS GhcPs (LHsExpr GhcPs) -> R ()
240p_grhs = p_grhs' p_hsExpr
241p_grhs = p_grhs' p_hsExpr
@@ -386,7 +387,7 @@ p_stmt' pretty = \case
386 inci (p_hsExpr x)
387 inci (p_hsExpr x)
387 RecStmt {..} -> do
388 RecStmt {..} -> do
388 txt "rec "
389 txt "rec "
389− sitcc $ sep newline (located' (p_stmt' pretty)) recS_stmts
390+ sitcc $ sepSemi (located' (p_stmt' pretty)) recS_stmts
390 XStmtLR {} -> notImplemented "XStmtLR"
391 XStmtLR {} -> notImplemented "XStmtLR"
392gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]]
393gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]]
@@ -411,7 +412,7 @@ p_hsLocalBinds = \case
411 (Left <$> bagToList bag) ++ (Right <$> lsigs)
412 (Left <$> bagToList bag) ++ (Right <$> lsigs)
412 p_item (Left x) = located x p_valDecl
413 p_item (Left x) = located x p_valDecl
413 p_item (Right x) = located x p_sigDecl
414 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)
415 HsValBinds NoExt _ -> notImplemented "HsValBinds"
416 HsValBinds NoExt _ -> notImplemented "HsValBinds"
416 HsIPBinds NoExt _ -> notImplemented "HsIPBinds"
417 HsIPBinds NoExt _ -> notImplemented "HsIPBinds"
417 EmptyLocalBinds NoExt -> return ()
418 EmptyLocalBinds NoExt -> return ()
@@ -468,18 +469,23 @@ p_hsExpr = \case
469 located (hswc_body a) p_hsType
470 located (hswc_body a) p_hsType
470 OpApp NoExt x op y -> do
471 OpApp NoExt x op y -> do
472− -- NOTE If the end of the first argument and the beginning of the second
472+ -- NOTE If the beginning of the first argument and the second argument
473− -- argument are on the same line, and the second argument has a hanging
473+ -- are on the same line, and the second argument has a hanging form, use
474− -- form, use hanging placement.
474+ -- hanging placement.
475 let placement =
475 let placement =
476 if isOneLineSpan
476 if isOneLineSpan
477− (mkSrcSpan (srcSpanEnd (getLoc x)) (srcSpanStart (getLoc y)))
477+ (mkSrcSpan (srcSpanStart (getLoc x)) (srcSpanStart (getLoc y)))
478 then exprPlacement (unLoc y)
478 then exprPlacement (unLoc y)
479 else Normal
479 else Normal
480 opWrapper = case unLoc op of
480 opWrapper = case unLoc op of
481 EWildPat NoExt -> backticks
481 EWildPat NoExt -> backticks
485+ (return $ case placement of
486+ Hanging -> useBraces
487+ Normal -> dontUseBraces)
488+ ub $ located x p_hsExpr
483 placeHanging placement $ do
489 placeHanging placement $ do
484 located op (opWrapper . p_hsExpr)
490 located op (opWrapper . p_hsExpr)
@@ -543,10 +549,9 @@ p_hsExpr = \case
543 HsDo NoExt ctx es -> do
549 HsDo NoExt ctx es -> do
544 let doBody header = do
550 let doBody header = do
545 txt header
551 txt header
546− if length (unLoc es) <= 1
547− then breakpoint
552+ breakpoint
548− else newline
553+ ub <- vlayout (return useBraces) (return id)
549− inci $ located es (sep newline (located' (sitcc . p_stmt)))
554+ inci $ sepSemi (located' (ub . p_stmt)) (unLoc es)
550 compBody = brackets $ located es $ \xs -> do
555 compBody = brackets $ located es $ \xs -> do
551 let p_parBody = sitcc . sep
556 let p_parBody = sitcc . sep
552 (breakpoint >> txt "| ")
557 (breakpoint >> txt "| ")
@@ -864,7 +869,7 @@ p_hsBracket = \case
865 breakpoint'
870 breakpoint'
866 inci $ do
871 inci $ do
867− body
872+ dontUseBraces body
868 breakpoint'
873 breakpoint'