@@ -1,5 +1,4 @@
1{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE LambdaCase #-}
2−{-# LANGUAGE MultiWayIf #-}
3{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE TypeApplications #-}
4{-# LANGUAGE TypeApplications #-}
@@ -22,12 +21,12 @@ import Data.Char (isPunctuation, isSymbol)
22import Data.Data hiding (Infix, Prefix)
21import Data.Data hiding (Infix, Prefix)
23import Data.Functor ((<&>))
22import Data.Functor ((<&>))
24import Data.List (intersperse, sortOn)
23import Data.List (intersperse, sortOn)
25−import Data.List.NonEmpty (NonEmpty (..), (<|))
24+import Data.List.NonEmpty ((<|), NonEmpty (..))
26import qualified Data.List.NonEmpty as NE
25import qualified Data.List.NonEmpty as NE
27import Data.Text (Text)
26import Data.Text (Text)
28import qualified Data.Text as Text
27import qualified Data.Text as Text
30−import OccName (occNameString)
29+import OccName (mkVarOcc)
31import Ormolu.Printer.Combinators
30import Ormolu.Printer.Combinators
32import Ormolu.Printer.Internal
31import Ormolu.Printer.Internal
33import Ormolu.Printer.Meat.Common
32import Ormolu.Printer.Meat.Common
@@ -61,7 +60,7 @@ data Placement
61 -- should use it and avoid bumping one level
60 -- should use it and avoid bumping one level
62 -- of indentation
61 -- of indentation
64− deriving (Eq, Show)
63+ deriving (Eq)
66p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
65p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
67p_valDecl = \case
66p_valDecl = \case
@@ -180,24 +179,25 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
180 NoSrcStrict -> return ()
179 NoSrcStrict -> return ()
181 SrcStrict -> txt "!"
180 SrcStrict -> txt "!"
182 SrcLazy -> txt "~"
181 SrcLazy -> txt "~"
183− indentBody <- case NE.nonEmpty m_pats of
182+ inci' <- case NE.nonEmpty m_pats of
185− False <$ case style of
183+ Nothing -> id <$ case style of
186− Function name -> p_rdrName name
184+ Function name -> p_rdrName name
187− _ -> return ()
185+ _ -> return ()
188 Just ne_pats -> do
186 Just ne_pats -> do
189− let combinedSpans = case style of
187+ let combinedSpans =
190− Function name -> combineSrcSpans (getLoc name) patSpans
188+ combineSrcSpans' $
192− patSpans = combineSrcSpans' (getLoc <$> ne_pats)
189+ getLoc <$> ne_pats
193− indentBody = not (isOneLineSpan combinedSpans)
191+ if isOneLineSpan combinedSpans
194 switchLayout [combinedSpans] $ do
194 switchLayout [combinedSpans] $ do
195 let stdCase = sep breakpoint (located' p_pat) m_pats
195 let stdCase = sep breakpoint (located' p_pat) m_pats
196 case style of
196 case style of
197 Function name ->
197 Function name ->
198 p_infixDefHelper
198 p_infixDefHelper
200− indentBody
200+ inci'
201 (p_rdrName name)
201 (p_rdrName name)
202 (located' p_pat <$> m_pats)
202 (located' p_pat <$> m_pats)
203 PatternBind -> stdCase
203 PatternBind -> stdCase
@@ -212,32 +212,40 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
212 when needsSpace space
212 when needsSpace space
213 sitcc stdCase
213 sitcc stdCase
214 LambdaCase -> stdCase
214 LambdaCase -> stdCase
215− return indentBody
215+ return inci'
216 let -- Calculate position of end of patterns. This is useful when we decide
216 let -- Calculate position of end of patterns. This is useful when we decide
217 -- about putting certain constructions in hanging positions.
217 -- about putting certain constructions in hanging positions.
218 endOfPats = case NE.nonEmpty m_pats of
218 endOfPats = case NE.nonEmpty m_pats of
219 Nothing -> case style of
219 Nothing -> case style of
220− Function name -> Just (getLoc name)
220+ Function name -> (Just . srcSpanEnd . getLoc) name
221 _ -> Nothing
221 _ -> Nothing
222− Just pats -> (Just . getLoc . NE.last) pats
222+ Just pats -> (Just . srcSpanEnd . getLoc . NE.last) pats
223 isCase = \case
223 isCase = \case
224 Case -> True
224 Case -> True
225 LambdaCase -> True
225 LambdaCase -> True
226 _ -> False
226 _ -> False
227− hasGuards = withGuards grhssGRHSs
227+ let hasGuards = withGuards grhssGRHSs
228+ unless (length grhssGRHSs > 1) $
230+ Function _ | hasGuards -> return ()
231+ Function _ -> space >> txt "="
232+ PatternBind -> space >> txt "="
233+ s | isCase s && hasGuards -> return ()
234+ _ -> space >> txt "->"
228− grhssSpan =
235+ let grhssSpan =
229 combineSrcSpans' $
236 combineSrcSpans' $
230 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
237 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
231 patGrhssSpan =
238 patGrhssSpan =
233 grhssSpan
240 grhssSpan
234− (combineSrcSpans grhssSpan . srcLocSpan . srcSpanEnd)
241+ (combineSrcSpans grhssSpan . srcLocSpan)
235 endOfPats
242 endOfPats
236 placement =
243 placement =
237 case endOfPats of
244 case endOfPats of
238 Nothing -> blockPlacement placer grhssGRHSs
245 Nothing -> blockPlacement placer grhssGRHSs
239 Just spn ->
246 Just spn ->
240− if onTheSameLine spn grhssSpan
248+ (mkSrcSpan spn (srcSpanStart grhssSpan))
241 then blockPlacement placer grhssGRHSs
249 then blockPlacement placer grhssGRHSs
242 else Normal
250 else Normal
243 p_body = do
251 p_body = do
@@ -253,14 +261,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
253 txt "where"
261 txt "where"
254 unless whereIsEmpty breakpoint
262 unless whereIsEmpty breakpoint
255 inci $ located grhssLocalBinds p_hsLocalBinds
263 inci $ located grhssLocalBinds p_hsLocalBinds
256− inciIf indentBody $ do
264+ inci' $ do
257− unless (length grhssGRHSs > 1) $
259− Function _ | hasGuards -> return ()
260− Function _ -> space >> inci equals
261− PatternBind -> space >> inci equals
262− s | isCase s && hasGuards -> return ()
263− _ -> space >> txt "->"
264 switchLayout [patGrhssSpan] $
265 switchLayout [patGrhssSpan] $
265 placeHanging placement p_body
266 placeHanging placement p_body
266 inci p_where
267 inci p_where
@@ -284,24 +285,24 @@ p_grhs' placer render style (GRHS NoExtField guards body) =
287− sitcc (sep commaDel (sitcc . located' p_stmt) xs)
288+ sitcc (sep (comma >> breakpoint) (sitcc . located' p_stmt) xs)
289− inci $ case style of
290+ txt $ case style of
290− EqualSign -> equals
291+ EqualSign -> "="
291− RightArrow -> txt "->"
292+ RightArrow -> "->"
292 placeHanging placement p_body
293 placeHanging placement p_body
294 placement =
295 placement =
295 case endOfGuards of
296 case endOfGuards of
296 Nothing -> placer (unLoc body)
297 Nothing -> placer (unLoc body)
297 Just spn ->
298 Just spn ->
298− if onTheSameLine spn (getLoc body)
299+ if isOneLineSpan (mkSrcSpan spn (srcSpanStart (getLoc body)))
299 then placer (unLoc body)
300 then placer (unLoc body)
300 else Normal
301 else Normal
301 endOfGuards =
302 endOfGuards =
302 case NE.nonEmpty guards of
303 case NE.nonEmpty guards of
303 Nothing -> Nothing
304 Nothing -> Nothing
304− Just gs -> (Just . getLoc . NE.last) gs
305+ Just gs -> (Just . srcSpanEnd . getLoc . NE.last) gs
305 p_body = located body render
306 p_body = located body render
306p_grhs' _ _ _ (XGRHS x) = noExtCon x
307p_grhs' _ _ _ (XGRHS x) = noExtCon x
@@ -316,7 +317,7 @@ p_hsCmd = \case
316 HsHigherOrderApp -> txt "-<<"
317 HsHigherOrderApp -> txt "-<<"
317 placeHanging (exprPlacement (unLoc input)) $
318 placeHanging (exprPlacement (unLoc input)) $
318 located input p_hsExpr
319 located input p_hsExpr
319− HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ do
320+ HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ sitcc $ do
320 located form p_hsExpr
321 located form p_hsExpr
321 unless (null cmds) $ do
322 unless (null cmds) $ do
322 breakpoint
323 breakpoint
@@ -353,14 +354,7 @@ p_hsCmdTop = \case
353 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
354 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
354 XCmdTop x -> noExtCon x
355 XCmdTop x -> noExtCon x
356−-- | Render an expression preserving blank lines between such consecutive
357−-- expressions found in the original source code.
358−withSpacing ::
357+withSpacing :: Data a => (a -> R ()) -> Located a -> R ()
359− -- | Rendering function
361− -- | Entity to render
364withSpacing f l = located l $ \x -> do
358withSpacing f l = located l $ \x -> do
365 case getLoc l of
359 case getLoc l of
366 UnhelpfulSpan _ -> f x
360 UnhelpfulSpan _ -> f x
@@ -369,7 +363,7 @@ withSpacing f l = located l $ \x -> do
369 -- Spacing before comments will be handled by the code
363 -- Spacing before comments will be handled by the code
370 -- that prints comments, so we just have to deal with
364 -- that prints comments, so we just have to deal with
371 -- blank lines between statements here.
365 -- blank lines between statements here.
372− Just (StatementSpan lastSpn) ->
366+ Just (OtherSpan lastSpn) ->
373 if srcSpanStartLine currentSpn > srcSpanEndLine lastSpn + 1
367 if srcSpanStartLine currentSpn > srcSpanEndLine lastSpn + 1
374 then newline
368 then newline
375 else return ()
369 else return ()
@@ -380,7 +374,7 @@ withSpacing f l = located l $ \x -> do
380 getSpanMark >>= \case
374 getSpanMark >>= \case
381 Just (HaddockSpan _ _) -> return ()
375 Just (HaddockSpan _ _) -> return ()
382 Just (CommentSpan _) -> return ()
376 Just (CommentSpan _) -> return ()
383− _ -> setSpanMark (StatementSpan currentSpn)
377+ _ -> setSpanMark (OtherSpan currentSpn)
385p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
379p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
386p_stmt = p_stmt' exprPlacement p_hsExpr
380p_stmt = p_stmt' exprPlacement p_hsExpr
@@ -471,31 +465,37 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
471p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
465p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
472p_hsLocalBinds = \case
466p_hsLocalBinds = \case
473 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
467 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
470+ (srcSpanStart . getLoc)
471+ (srcSpanStart . getLoc)
473+ (Left <$> bagToList bag) ++ (Right <$> lsigs)
474+ p_item (Left x) = located x p_valDecl
475+ p_item (Right x) = located x p_sigDecl
474 -- When in a single-line layout, there is a chance that the inner
476 -- When in a single-line layout, there is a chance that the inner
475 -- elements will also contain semicolons and they will confuse the
477 -- elements will also contain semicolons and they will confuse the
476 -- parser. so we request braces around every element except the last.
478 -- parser. so we request braces around every element except the last.
477 br <- layoutToBraces <$> getLayout
479 br <- layoutToBraces <$> getLayout
478− let items =
480+ sitcc $
479− let injectLeft (L l x) = L l (Left x)
481+ sepSemi
480− injectRight (L l x) = L l (Right x)
482+ ( \(p, i) ->
481− in (injectLeft <$> bagToList bag) ++ (injectRight <$> lsigs)
483+ ( case p of
482− positionToBracing = \case
483− SinglePos -> id
484+ SinglePos -> id
484− FirstPos -> br
485+ FirstPos -> br
485− MiddlePos -> br
486+ MiddlePos -> br
486− LastPos -> id
487+ LastPos -> id
487− p_item' (p, item) =
488+ )
488− positionToBracing p $
489+ (p_item i)
489− withSpacing (either p_valDecl p_sigDecl) item
490+ )
490− binds = sortOn (srcSpanStart . getLoc) items
491+ (attachRelativePos $ sortOn ssStart items)
491− sitcc $ sepSemi p_item' (attachRelativePos binds)
492 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
492 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
493 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
493 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
494 -- Second argument of IPBind is always Left before type-checking.
494 -- Second argument of IPBind is always Left before type-checking.
495 let p_ipBind (IPBind NoExtField (Left name) expr) = do
495 let p_ipBind (IPBind NoExtField (Left name) expr) = do
496 atom name
496 atom name
499 breakpoint
499 breakpoint
500 useBraces $ inci $ located expr p_hsExpr
500 useBraces $ inci $ located expr p_hsExpr
501 p_ipBind (IPBind NoExtField (Right _) _) =
501 p_ipBind (IPBind NoExtField (Right _) _) =
@@ -514,12 +514,15 @@ p_hsRecField HsRecField {..} = do
514 p_rdrName hsRecFieldLbl
514 p_rdrName hsRecFieldLbl
515 unless hsRecPun $ do
515 unless hsRecPun $ do
519− if onTheSameLine (getLoc hsRecFieldLbl) (getLoc hsRecFieldArg)
520− then exprPlacement (unLoc hsRecFieldArg)
518+ let placement = exprPlacement (unLoc hsRecFieldArg)
522− placeHanging placement (located hsRecFieldArg p_hsExpr)
519+ placeHanging placement $ located hsRecFieldArg p_hsExpr
521+p_hsTupArg :: HsTupArg GhcPs -> R ()
523+ Present NoExtField x -> located x p_hsExpr
524+ Missing NoExtField -> pure ()
525+ XTupArg x -> noExtCon x
524p_hsExpr :: HsExpr GhcPs -> R ()
527p_hsExpr :: HsExpr GhcPs -> R ()
525p_hsExpr = p_hsExpr' N
528p_hsExpr = p_hsExpr' N
@@ -584,21 +587,23 @@ p_hsExpr' s = \case
584 -- do-block or case expression it is not a good idea. It seems
587 -- do-block or case expression it is not a good idea. It seems
585 -- to be safe to always bump indentation when the function
588 -- to be safe to always bump indentation when the function
586 -- expression is parenthesised.
589 -- expression is parenthesised.
587− doIndent =
590+ indent =
588 case func of
591 case func of
589− L _ (HsPar NoExtField _) -> True
592+ L _ (HsPar NoExtField _) -> inci
590− L _ (HsAppType NoExtField _ _) -> True
593+ L _ (HsAppType NoExtField _ _) -> inci
591− L _ (HsMultiIf NoExtField _) -> True
594+ L _ (HsMultiIf NoExtField _) -> inci
592− L spn _ -> isOneLineSpan spn
596+ if isOneLineSpan spn
594− getLayout <&> \case
599+ ub <- getLayout <&> \case
595− SingleLine -> useBraces
600+ SingleLine -> useBraces
596− MultiLine -> id
601+ MultiLine -> id
598 located func (p_hsExpr' s)
603 located func (p_hsExpr' s)
599 breakpoint
604 breakpoint
600− inciIf doIndent $ sep breakpoint (located' p_hsExpr) initp
605+ indent $ sep breakpoint (located' p_hsExpr) initp
601− inciIf doIndent $ do
606+ indent $ do
602 unless (null initp) breakpoint
607 unless (null initp) breakpoint
603 located lastp p_hsExpr
608 located lastp p_hsExpr
604 Hanging -> do
609 Hanging -> do
@@ -613,15 +618,10 @@ p_hsExpr' s = \case
613 breakpoint
618 breakpoint
614 inci $ do
619 inci $ do
616− -- Insert a space when the type is represented as a TH splice to avoid
617− -- gluing @ and $ together.
618− case unLoc (hswc_body a) of
619− HsSpliceTy {} -> space
621 located (hswc_body a) p_hsType
621 located (hswc_body a) p_hsType
622 OpApp NoExtField x op y -> do
622 OpApp NoExtField x op y -> do
623 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
623 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
624− p_exprOpTree s (reassociateOpTree getOpName opTree)
624+ p_exprOpTree True s (reassociateOpTree getOpName opTree)
625 NegApp NoExtField e _ -> do
625 NegApp NoExtField e _ -> do
@@ -638,27 +638,22 @@ p_hsExpr' s = \case
638 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
638 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
639 unless (useRecordDot' && isRecordDot') breakpoint
639 unless (useRecordDot' && isRecordDot') breakpoint
640 inci (located x p_hsExpr)
640 inci (located x p_hsExpr)
641− ExplicitTuple NoExtField args boxity ->
641+ ExplicitTuple NoExtField args boxity -> do
642 let isSection = any (isMissing . unLoc) args
642 let isSection = any (isMissing . unLoc) args
643 isMissing = \case
643 isMissing = \case
644 Missing NoExtField -> True
644 Missing NoExtField -> True
645 _ -> False
645 _ -> False
647− Present NoExtField x -> located x p_hsExpr
648− Missing NoExtField -> pure ()
649− XTupArg x -> noExtCon x
650− p_larg = sitcc . located' p_arg
651− parens' =
646+ let parens' =
652 case boxity of
647 case boxity of
653 Boxed -> parens
648 Boxed -> parens
654 Unboxed -> parensHash
649 Unboxed -> parensHash
655− in if isSection
650+ if isSection
657− switchLayout [] . parens' s $
652+ switchLayout [] . parens' s $
658− sep comma p_larg args
653+ sep comma (located' p_hsTupArg) args
660− switchLayout (getLoc <$> args) . parens' s $
655+ switchLayout (getLoc <$> args) . parens' s . sitcc $
661− sep commaDel p_larg args
656+ sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
662 ExplicitSum NoExtField tag arity e ->
657 ExplicitSum NoExtField tag arity e ->
663 p_unboxedSum N tag arity (located e p_hsExpr)
658 p_unboxedSum N tag arity (located e p_hsExpr)
664 HsCase NoExtField e mgroup ->
659 HsCase NoExtField e mgroup ->
@@ -666,9 +661,8 @@ p_hsExpr' s = \case
666 HsIf NoExtField _ if' then' else' ->
661 HsIf NoExtField _ if' then' else' ->
667 p_if exprPlacement p_hsExpr if' then' else'
662 p_if exprPlacement p_hsExpr if' then' else'
668 HsMultiIf NoExtField guards -> do
663 HsMultiIf NoExtField guards -> do
669− txt "if"
664+ txt "if "
671− inci . inci $ sep newline (located' (p_grhs RightArrow)) guards
665+ inci . inci . sitcc $ sep newline (located' (p_grhs RightArrow)) guards
672 HsLet NoExtField localBinds e ->
666 HsLet NoExtField localBinds e ->
673 p_let p_hsExpr localBinds e
667 p_let p_hsExpr localBinds e
674 HsDo NoExtField ctx es -> do
668 HsDo NoExtField ctx es -> do
@@ -680,15 +674,15 @@ p_hsExpr' s = \case
681 (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
675 (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
682 (unLoc es)
676 (unLoc es)
683− compBody = brackets N . located es $ \xs -> do
677+ compBody = brackets N $ located es $ \xs -> do
684 let p_parBody =
678 let p_parBody =
686− (breakpoint >> txt "|" >> space)
680+ (breakpoint >> txt "| ")
687 p_seqBody
681 p_seqBody
688 p_seqBody =
682 p_seqBody =
691− commaDel
685+ (comma >> breakpoint)
692 (located' (sitcc . p_stmt))
686 (located' (sitcc . p_stmt))
693 stmts = init xs
687 stmts = init xs
694 yield = last xs
688 yield = last xs
@@ -709,8 +703,8 @@ p_hsExpr' s = \case
709 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
703 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
710 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
704 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
711 ExplicitList _ _ xs ->
705 ExplicitList _ _ xs ->
712− brackets s $
706+ brackets s . sitcc $
713− sep commaDel (sitcc . located' p_hsExpr) xs
707+ sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs
714 RecordCon {..} -> do
708 RecordCon {..} -> do
715 located rcon_con_name atom
709 located rcon_con_name atom
716 breakpoint
710 breakpoint
@@ -726,8 +720,8 @@ p_hsExpr' s = \case
726 case rec_dotdot of
720 case rec_dotdot of
727 Just {} -> [txt ".."]
721 Just {} -> [txt ".."]
728 Nothing -> []
722 Nothing -> []
729− inci . braces N $
723+ inci . braces N . sitcc $
730− sep commaDel sitcc (fields <> dotdot)
724+ sep (comma >> breakpoint) sitcc (fields <> dotdot)
731 RecordUpd {..} -> do
725 RecordUpd {..} -> do
732 located rupd_expr p_hsExpr
726 located rupd_expr p_hsExpr
733 useRecordDot' <- useRecordDot
727 useRecordDot' <- useRecordDot
@@ -745,9 +739,9 @@ p_hsExpr' s = \case
745 Unambiguous _ n -> n
739 Unambiguous _ n -> n
746 XAmbiguousFieldOcc x -> noExtCon x
740 XAmbiguousFieldOcc x -> noExtCon x
748− inci . braces N $
742+ inci . braces N . sitcc $
750− commaDel
744+ (comma >> breakpoint)
751 (sitcc . located' (p_hsRecField . updName))
745 (sitcc . located' (p_hsRecField . updName))
752 rupd_flds
746 rupd_flds
753 ExprWithTySig NoExtField x HsWC {hswc_body = HsIB {..}} -> sitcc $ do
747 ExprWithTySig NoExtField x HsWC {hswc_body = HsIB {..}} -> sitcc $ do
@@ -760,22 +754,22 @@ p_hsExpr' s = \case
760 ExprWithTySig NoExtField _ (XHsWildCardBndrs x) -> noExtCon x
754 ExprWithTySig NoExtField _ (XHsWildCardBndrs x) -> noExtCon x
761 ArithSeq NoExtField _ x ->
755 ArithSeq NoExtField _ x ->
762 case x of
756 case x of
763− From from -> brackets s $ do
757+ From from -> brackets s . sitcc $ do
764 located from p_hsExpr
758 located from p_hsExpr
765 breakpoint
759 breakpoint
767− FromThen from next -> brackets s $ do
761+ FromThen from next -> brackets s . sitcc $ do
768− sep commaDel (located' p_hsExpr) [from, next]
762+ sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
769 breakpoint
763 breakpoint
771− FromTo from to -> brackets s $ do
765+ FromTo from to -> brackets s . sitcc $ do
772 located from p_hsExpr
766 located from p_hsExpr
773 breakpoint
767 breakpoint
776 located to p_hsExpr
770 located to p_hsExpr
777− FromThenTo from next to -> brackets s $ do
771+ FromThenTo from next to -> brackets s . sitcc $ do
778− sep commaDel (located' p_hsExpr) [from, next]
772+ sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
779 breakpoint
773 breakpoint
@@ -825,7 +819,7 @@ p_patSynBind PSB {..} = do
825 breakpoint
819 breakpoint
826 located psb_def p_pat
820 located psb_def p_pat
827 ImplicitBidirectional -> do
821 ImplicitBidirectional -> do
829 breakpoint
823 breakpoint
830 located psb_def p_pat
824 located psb_def p_pat
831 ExplicitBidirectional mgroup -> do
825 ExplicitBidirectional mgroup -> do
@@ -852,8 +846,8 @@ p_patSynBind PSB {..} = do
852 inci $ do
846 inci $ do
853 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
847 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
854 unless (null xs) breakpoint
848 unless (null xs) breakpoint
855− braces N $
849+ braces N . sitcc $
856− sep commaDel (p_rdrName . recordPatSynPatVar) xs
850+ sep (comma >> breakpoint) (p_rdrName . recordPatSynPatVar) xs
858 InfixCon l r -> do
852 InfixCon l r -> do
859 switchLayout [getLoc l, getLoc r] $ do
853 switchLayout [getLoc l, getLoc r] $ do
@@ -907,12 +901,10 @@ p_if placer render if' then' else' = do
907 breakpoint
901 breakpoint
908 inci $ do
902 inci $ do
909 txt "then"
903 txt "then"
911 located then' $ \x ->
904 located then' $ \x ->
912 placeHanging (placer x) (render x)
905 placeHanging (placer x) (render x)
913 breakpoint
906 breakpoint
914 txt "else"
907 txt "else"
916 located else' $ \x ->
908 located else' $ \x ->
917 placeHanging (placer x) (render x)
909 placeHanging (placer x) (render x)
@@ -949,13 +941,13 @@ p_pat = \case
950 located pat p_pat
942 located pat p_pat
951 ListPat NoExtField pats ->
943 ListPat NoExtField pats ->
952− brackets S $ sep commaDel (located' p_pat) pats
944+ brackets S . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats
953 TuplePat NoExtField pats boxing -> do
945 TuplePat NoExtField pats boxing -> do
954− let parens' =
946+ let f =
955 case boxing of
947 case boxing of
956 Boxed -> parens S
948 Boxed -> parens S
957 Unboxed -> parensHash S
949 Unboxed -> parensHash S
958− parens' $ sep commaDel (sitcc . located' p_pat) pats
950+ f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats
959 SumPat NoExtField pat tag arity ->
951 SumPat NoExtField pat tag arity ->
960 p_unboxedSum S tag arity (located pat p_pat)
952 p_unboxedSum S tag arity (located pat p_pat)
961 ConPatIn pat details ->
953 ConPatIn pat details ->
@@ -971,7 +963,7 @@ p_pat = \case
971 let f = \case
963 let f = \case
972 Nothing -> txt ".."
964 Nothing -> txt ".."
973 Just x -> located x p_pat_hsRecField
965 Just x -> located x p_pat_hsRecField
974− inci . braces N . sep commaDel f $
966+ inci . braces N . sitcc . sep (comma >> breakpoint) f $
975 case dotdot of
967 case dotdot of
976 Nothing -> Just <$> fields
968 Nothing -> Just <$> fields
977 Just (L _ n) -> (Just <$> take n fields) ++ [Nothing]
969 Just (L _ n) -> (Just <$> take n fields) ++ [Nothing]
@@ -1012,7 +1004,7 @@ p_pat_hsRecField HsRecField {..} = do
1012 p_rdrName (rdrNameFieldOcc x)
1004 p_rdrName (rdrNameFieldOcc x)
1013 unless hsRecPun $ do
1005 unless hsRecPun $ do
1015− equals
1007+ txt "="
1016 breakpoint
1008 breakpoint
1017 inci (located hsRecFieldArg p_pat)
1009 inci (located hsRecFieldArg p_pat)
@@ -1021,15 +1013,17 @@ p_unboxedSum s tag arity m = do
1021 let before = tag - 1
1013 let before = tag - 1
1022 after = arity - before - 1
1014 after = arity - before - 1
1023 args = replicate before Nothing <> [Just m] <> replicate after Nothing
1015 args = replicate before Nothing <> [Just m] <> replicate after Nothing
1024− f x =
1016+ f (x, i) = do
1017+ let isFirst = i == 0
1018+ isLast = i == arity - 1
1025 case x :: Maybe (R ()) of
1019 case x :: Maybe (R ()) of
1026 Nothing ->
1020 Nothing ->
1027− space
1021+ unless (isFirst || isLast) space
1028 Just m' -> do
1022 Just m' -> do
1029− space
1023+ unless isFirst space
1031− space
1025+ unless isLast space
1032− parensHash s $ sep (txt "|") f args
1026+ parensHash s $ sep (txt "|") f (zip args [0 ..])
1034p_hsSplice :: HsSplice GhcPs -> R ()
1028p_hsSplice :: HsSplice GhcPs -> R ()
1035p_hsSplice = \case
1029p_hsSplice = \case
@@ -1231,10 +1225,11 @@ exprPlacement = \case
1231 HsCase NoExtField _ _ -> Hanging
1225 HsCase NoExtField _ _ -> Hanging
1232 HsDo NoExtField DoExpr _ -> Hanging
1226 HsDo NoExtField DoExpr _ -> Hanging
1233 HsDo NoExtField MDoExpr _ -> Hanging
1227 HsDo NoExtField MDoExpr _ -> Hanging
1234− OpApp NoExtField _ op y ->
1228+ -- If the rightmost expression in an operator chain is hanging, make the
1235− case (fmap getOpNameStr . getOpName . unLoc) op of
1229+ -- whole block hanging; so that we can use the common @f = foo $ do@
1236− Just "$" -> exprPlacement (unLoc y)
1231+ OpApp NoExtField _ _ y -> exprPlacement (unLoc y)
1237− _ -> Normal
1232+ -- Same thing for function applications (usually with -XBlockArguments)
1238 HsApp NoExtField _ y -> exprPlacement (unLoc y)
1233 HsApp NoExtField _ y -> exprPlacement (unLoc y)
1239 HsProc NoExtField p _ ->
1234 HsProc NoExtField p _ ->
1240 -- Indentation breaks if pattern is longer than one line and left
1235 -- Indentation breaks if pattern is longer than one line and left
@@ -1260,16 +1255,15 @@ getOpName = \case
1260 HsVar NoExtField (L _ a) -> Just a
1255 HsVar NoExtField (L _ a) -> Just a
1261 _ -> Nothing
1256 _ -> Nothing
1263−getOpNameStr :: RdrName -> String
1264−getOpNameStr = occNameString . rdrNameOcc
1266p_exprOpTree ::
1258p_exprOpTree ::
1259+ -- | Can use special handling of dollar?
1267 -- | Bracket style to use
1261 -- | Bracket style to use
1268 BracketStyle ->
1262 BracketStyle ->
1269 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
1263 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
1271−p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1265+p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1272−p_exprOpTree s (OpBranch x op y) = do
1266+p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
1273 -- If the beginning of the first argument and the second argument are on
1267 -- If the beginning of the first argument and the second argument are on
1274 -- the same line, and the second argument has a hanging form, use hanging
1268 -- the same line, and the second argument has a hanging form, use hanging
1275 -- placement.
1269 -- placement.
@@ -1291,54 +1285,44 @@ p_exprOpTree s (OpBranch x op y) = do
1291 MultiLine -> case placement of
1285 MultiLine -> case placement of
1292 Hanging -> useBraces
1286 Hanging -> useBraces
1293 Normal -> dontUseBraces
1287 Normal -> dontUseBraces
1294− opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1288+ gotDollar = case getOpName (unLoc op) of
1295− gotDollar = opNameStr == Just "$"
1289+ Just rname -> mkVarOcc "$" == rdrNameOcc rname
1296− gotColon = opNameStr == Just ":"
1290+ _ -> False
1297− gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
1299 switchLayout [opTreeLoc x] $
1292 switchLayout [opTreeLoc x] $
1300− p_exprOpTree s x
1293+ p_exprOpTree (not gotDollar) s x
1301− p_op = located op (opWrapper . p_hsExpr)
1294+ let p_op = located op (opWrapper . p_hsExpr)
1302− p_y = switchLayout [opTreeLoc y] (p_exprOpTree N y)
1295+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
1303 isSection = case (opTreeLoc x, getLoc op) of
1296 isSection = case (opTreeLoc x, getLoc op) of
1304 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
1297 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
1305 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
1298 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
1306 _ -> False
1299 _ -> False
1308− OpNode (L _ HsDo {}) -> True
1310 useRecordDot' <- useRecordDot
1300 useRecordDot' <- useRecordDot
1311− if
1301+ let isRecordDot' = isRecordDot (unLoc op) (opTreeLoc y)
1312− | gotColon -> do
1302+ if useRecordDot' && isRecordDot'
1305+ when isSection space
1313+ && isOneLineSpan (opTreeLoc x)
1316− case placement of
1318+ breakpoint
1317− Hanging -> do
1319+ inci p_y
1322+ placeHanging placement $ do
1322− inciIf (isDoBlock y) p_y
1324− && isOneLineSpan (opTreeLoc x)
1325− && placement == Normal -> do
1331− | useRecordDot' && gotRecordDot -> do
1333− when isSection space
1338− placeHanging placement $ do
1343-- | Return 'True' if given expression is a record-dot operator expression.
1327-- | Return 'True' if given expression is a record-dot operator expression.
1344isRecordDot ::
1328isRecordDot ::
@@ -1349,10 +1333,14 @@ isRecordDot ::
1350isRecordDot op (RealSrcSpan ySpan) = case op of
1334isRecordDot op (RealSrcSpan ySpan) = case op of
1351 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1335 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1352− (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1336+ isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1353 _ -> False
1337 _ -> False
1354isRecordDot _ _ = False
1338isRecordDot _ _ = False
1340+-- | Check whether a given 'RdrName' is the dot operator.
1341+isDot :: RdrName -> Bool
1342+isDot name = rdrNameOcc name == mkVarOcc "."
1356-- | Get annotations for the enclosing element.
1344-- | Get annotations for the enclosing element.
1357getEnclosingAnns :: R [AnnKeywordId]
1345getEnclosingAnns :: R [AnnKeywordId]
1358getEnclosingAnns = do
1346getEnclosingAnns = do