@@ -1,5 +1,4 @@
11{-# LANGUAGE LambdaCase #-}
2−{-# LANGUAGE MultiWayIf #-}
32{-# LANGUAGE OverloadedStrings #-}
43{-# LANGUAGE RecordWildCards #-}
54{-# LANGUAGE TypeApplications #-}
@@ -22,12 +21,12 @@ import Data.Char (isPunctuation, isSymbol)
2221import Data.Data hiding (Infix, Prefix)
2322import Data.Functor ((<&>))
2423import Data.List (intersperse, sortOn)
25−import Data.List.NonEmpty (NonEmpty (..), (<|))
24+import Data.List.NonEmpty ((<|), NonEmpty ((:|)))
2625import qualified Data.List.NonEmpty as NE
2726import Data.Text (Text)
2827import qualified Data.Text as Text
2928import GHC
30−import OccName (occNameString)
29+import OccName (mkVarOcc)
3130import Ormolu.Printer.Combinators
3231import Ormolu.Printer.Internal
3332import Ormolu.Printer.Meat.Common
@@ -36,6 +35,8 @@ import Ormolu.Printer.Meat.Declaration.Signature
3635import Ormolu.Printer.Meat.Type
3736import Ormolu.Printer.Operators
3837import Ormolu.Utils
38+import RdrName (RdrName (..), rdrNameOcc)
39+import SrcLoc (combineSrcSpans, isOneLineSpan)
3940
4041-- | Style of a group of equations.
4142data MatchGroupStyle
@@ -61,7 +62,7 @@ data Placement
6162 -- should use it and avoid bumping one level
6263 -- of indentation
6364 Hanging
64− deriving (Eq, Show)
65+ deriving (Eq)
6566
6667p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
6768p_valDecl = \case
@@ -180,24 +181,25 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
180181 NoSrcStrict -> return ()
181182 SrcStrict -> txt "!"
182183 SrcLazy -> txt "~"
183− indentBody <- case NE.nonEmpty m_pats of
184− Nothing ->
185− False <$ case style of
186− Function name -> p_rdrName name
187− _ -> return ()
184+ inci' <- case NE.nonEmpty m_pats of
185+ Nothing -> id <$ case style of
186+ Function name -> p_rdrName name
187+ _ -> return ()
188188 Just ne_pats -> do
189− let combinedSpans = case style of
190− Function name -> combineSrcSpans (getLoc name) patSpans
191− _ -> patSpans
192− patSpans = combineSrcSpans' (getLoc <$> ne_pats)
193− indentBody = not (isOneLineSpan combinedSpans)
189+ let combinedSpans =
190+ combineSrcSpans' $
191+ getLoc <$> ne_pats
192+ inci' =
193+ if isOneLineSpan combinedSpans
194+ then id
195+ else inci
194196 switchLayout [combinedSpans] $ do
195197 let stdCase = sep breakpoint (located' p_pat) m_pats
196198 case style of
197199 Function name ->
198200 p_infixDefHelper
199201 isInfix
200− indentBody
202+ inci'
201203 (p_rdrName name)
202204 (located' p_pat <$> m_pats)
203205 PatternBind -> stdCase
@@ -212,32 +214,40 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
212214 when needsSpace space
213215 sitcc stdCase
214216 LambdaCase -> stdCase
215− return indentBody
217+ return inci'
216218 let -- Calculate position of end of patterns. This is useful when we decide
217219 -- about putting certain constructions in hanging positions.
218220 endOfPats = case NE.nonEmpty m_pats of
219221 Nothing -> case style of
220− Function name -> Just (getLoc name)
222+ Function name -> (Just . srcSpanEnd . getLoc) name
221223 _ -> Nothing
222− Just pats -> (Just . getLoc . NE.last) pats
224+ Just pats -> (Just . srcSpanEnd . getLoc . NE.last) pats
223225 isCase = \case
224226 Case -> True
225227 LambdaCase -> True
226228 _ -> False
227− hasGuards = withGuards grhssGRHSs
228− grhssSpan =
229+ let hasGuards = withGuards grhssGRHSs
230+ unless (length grhssGRHSs > 1) $
231+ case style of
232+ Function _ | hasGuards -> return ()
233+ Function _ -> space >> txt "="
234+ PatternBind -> space >> txt "="
235+ s | isCase s && hasGuards -> return ()
236+ _ -> space >> txt "->"
237+ let grhssSpan =
229238 combineSrcSpans' $
230239 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
231240 patGrhssSpan =
232241 maybe
233242 grhssSpan
234− (combineSrcSpans grhssSpan . srcLocSpan . srcSpanEnd)
243+ (combineSrcSpans grhssSpan . srcLocSpan)
235244 endOfPats
236245 placement =
237246 case endOfPats of
238247 Nothing -> blockPlacement placer grhssGRHSs
239248 Just spn ->
240− if onTheSameLine spn grhssSpan
249+ if isOneLineSpan
250+ (mkSrcSpan spn (srcSpanStart grhssSpan))
241251 then blockPlacement placer grhssGRHSs
242252 else Normal
243253 p_body = do
@@ -253,14 +263,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
253263 txt "where"
254264 unless whereIsEmpty breakpoint
255265 inci $ located grhssLocalBinds p_hsLocalBinds
256− inciIf indentBody $ do
257− unless (length grhssGRHSs > 1) $
258− case style of
259− Function _ | hasGuards -> return ()
260− Function _ -> space >> inci equals
261− PatternBind -> space >> inci equals
262− s | isCase s && hasGuards -> return ()
263− _ -> space >> txt "->"
266+ inci' $ do
264267 switchLayout [patGrhssSpan] $
265268 placeHanging placement p_body
266269 inci p_where
@@ -284,24 +287,24 @@ p_grhs' placer render style (GRHS NoExtField guards body) =
284287 xs -> do
285288 txt "|"
286289 space
287− sitcc (sep commaDel (sitcc . located' p_stmt) xs)
290+ sitcc (sep (comma >> breakpoint) (sitcc . located' p_stmt) xs)
288291 space
289− inci $ case style of
290− EqualSign -> equals
291− RightArrow -> txt "->"
292+ txt $ case style of
293+ EqualSign -> "="
294+ RightArrow -> "->"
292295 placeHanging placement p_body
293296 where
294297 placement =
295298 case endOfGuards of
296299 Nothing -> placer (unLoc body)
297300 Just spn ->
298− if onTheSameLine spn (getLoc body)
301+ if isOneLineSpan (mkSrcSpan spn (srcSpanStart (getLoc body)))
299302 then placer (unLoc body)
300303 else Normal
301304 endOfGuards =
302305 case NE.nonEmpty guards of
303306 Nothing -> Nothing
304− Just gs -> (Just . getLoc . NE.last) gs
307+ Just gs -> (Just . srcSpanEnd . getLoc . NE.last) gs
305308 p_body = located body render
306309p_grhs' _ _ _ (XGRHS x) = noExtCon x
307310
@@ -309,14 +312,13 @@ p_hsCmd :: HsCmd GhcPs -> R ()
309312p_hsCmd = \case
310313 HsCmdArrApp NoExtField body input arrType _ -> do
311314 located body p_hsExpr
312− breakpoint
313− inci $ do
314− case arrType of
315− HsFirstOrderApp -> txt "-<"
316− HsHigherOrderApp -> txt "-<<"
317− placeHanging (exprPlacement (unLoc input)) $
318− located input p_hsExpr
319− HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ do
315+ space
316+ case arrType of
317+ HsFirstOrderApp -> txt "-<"
318+ HsHigherOrderApp -> txt "-<<"
319+ placeHanging (exprPlacement (unLoc input)) $
320+ located input p_hsExpr
321+ HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ sitcc $ do
320322 located form p_hsExpr
321323 unless (null cmds) $ do
322324 breakpoint
@@ -344,7 +346,7 @@ p_hsCmd = \case
344346 txt "do"
345347 newline
346348 inci . located es $
347− sitcc . sep newline (sitcc . withSpacing (p_stmt' cmdPlacement p_hsCmd))
349+ sitcc . sep newline (located' (sitcc . p_stmt' cmdPlacement p_hsCmd))
348350 HsCmdWrap {} -> notImplemented "HsCmdWrap"
349351 XCmd x -> noExtCon x
350352
@@ -353,35 +355,6 @@ p_hsCmdTop = \case
353355 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
354356 XCmdTop x -> noExtCon x
355357
356−-- | Render an expression preserving blank lines between such consecutive
357−-- expressions found in the original source code.
358−withSpacing ::
359− -- | Rendering function
360− (a -> R ()) ->
361− -- | Entity to render
362− Located a ->
363− R ()
364−withSpacing f l = located l $ \x -> do
365− case getLoc l of
366− UnhelpfulSpan _ -> f x
367− RealSrcSpan currentSpn -> do
368− getSpanMark >>= \case
369− -- Spacing before comments will be handled by the code
370− -- that prints comments, so we just have to deal with
371− -- blank lines between statements here.
372− Just (StatementSpan lastSpn) ->
373− if srcSpanStartLine currentSpn > srcSpanEndLine lastSpn + 1
374− then newline
375− else return ()
376− _ -> return ()
377− f x
378− -- In some cases the (f x) expression may insert a new mark. We want
379− -- to be careful not to override comment marks.
380− getSpanMark >>= \case
381− Just (HaddockSpan _ _) -> return ()
382− Just (CommentSpan _) -> return ()
383− _ -> setSpanMark (StatementSpan currentSpn)
384−
385358p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
386359p_stmt = p_stmt' exprPlacement p_hsExpr
387360
@@ -453,7 +426,7 @@ p_stmt' placer render = \case
453426 RecStmt {..} -> do
454427 txt "rec"
455428 space
456− sitcc $ sepSemi (withSpacing (p_stmt' placer render)) recS_stmts
429+ sitcc $ sepSemi (located' (p_stmt' placer render)) recS_stmts
457430 XStmtLR c -> noExtCon c
458431
459432gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]]
@@ -471,31 +444,34 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
471444p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
472445p_hsLocalBinds = \case
473446 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
447+ let ssStart =
448+ either
449+ (srcSpanStart . getLoc)
450+ (srcSpanStart . getLoc)
451+ items =
452+ (Left <$> bagToList bag) ++ (Right <$> lsigs)
453+ p_item (Left x) = located x p_valDecl
454+ p_item (Right x) = located x p_sigDecl
455+ -- Assigns 'False' to the last element, 'True' to the rest.
456+ markInit :: [a] -> [(Bool, a)]
457+ markInit [] = []
458+ markInit [x] = [(False, x)]
459+ markInit (x : xs) = (True, x) : markInit xs
474460 -- When in a single-line layout, there is a chance that the inner
475461 -- elements will also contain semicolons and they will confuse the
476462 -- parser. so we request braces around every element except the last.
477463 br <- layoutToBraces <$> getLayout
478− let items =
479− let injectLeft (L l x) = L l (Left x)
480− injectRight (L l x) = L l (Right x)
481− in (injectLeft <$> bagToList bag) ++ (injectRight <$> lsigs)
482− positionToBracing = \case
483− SinglePos -> id
484− FirstPos -> br
485− MiddlePos -> br
486− LastPos -> id
487− p_item' (p, item) =
488− positionToBracing p $
489− withSpacing (either p_valDecl p_sigDecl) item
490− binds = sortOn (srcSpanStart . getLoc) items
491− sitcc $ sepSemi p_item' (attachRelativePos binds)
464+ sitcc $
465+ sepSemi
466+ (\(m, i) -> (if m then br else id) $ p_item i)
467+ (markInit $ sortOn ssStart items)
492468 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
493469 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
494470 -- Second argument of IPBind is always Left before type-checking.
495471 let p_ipBind (IPBind NoExtField (Left name) expr) = do
496472 atom name
497473 space
498− equals
474+ txt "="
499475 breakpoint
500476 useBraces $ inci $ located expr p_hsExpr
501477 p_ipBind (IPBind NoExtField (Right _) _) =
@@ -514,12 +490,15 @@ p_hsRecField HsRecField {..} = do
514490 p_rdrName hsRecFieldLbl
515491 unless hsRecPun $ do
516492 space
517− equals
518− let placement =
519− if onTheSameLine (getLoc hsRecFieldLbl) (getLoc hsRecFieldArg)
520− then exprPlacement (unLoc hsRecFieldArg)
521− else Normal
522− placeHanging placement (located hsRecFieldArg p_hsExpr)
493+ txt "="
494+ let placement = exprPlacement (unLoc hsRecFieldArg)
495+ placeHanging placement $ located hsRecFieldArg p_hsExpr
496+
497+p_hsTupArg :: HsTupArg GhcPs -> R ()
498+p_hsTupArg = \case
499+ Present NoExtField x -> located x p_hsExpr
500+ Missing NoExtField -> pure ()
501+ XTupArg x -> noExtCon x
523502
524503p_hsExpr :: HsExpr GhcPs -> R ()
525504p_hsExpr = p_hsExpr' N
@@ -584,21 +563,23 @@ p_hsExpr' s = \case
584563 -- do-block or case expression it is not a good idea. It seems
585564 -- to be safe to always bump indentation when the function
586565 -- expression is parenthesised.
587− doIndent =
566+ indent =
588567 case func of
589− L _ (HsPar NoExtField _) -> True
590− L _ (HsAppType NoExtField _ _) -> True
591− L _ (HsMultiIf NoExtField _) -> True
592− L spn _ -> isOneLineSpan spn
593− ub <-
594− getLayout <&> \case
595− SingleLine -> useBraces
596− MultiLine -> id
568+ L _ (HsPar NoExtField _) -> inci
569+ L _ (HsAppType NoExtField _ _) -> inci
570+ L _ (HsMultiIf NoExtField _) -> inci
571+ L spn _ ->
572+ if isOneLineSpan spn
573+ then inci
574+ else id
575+ ub <- getLayout <&> \case
576+ SingleLine -> useBraces
577+ MultiLine -> id
597578 ub $ do
598579 located func (p_hsExpr' s)
599580 breakpoint
600− inciIf doIndent $ sep breakpoint (located' p_hsExpr) initp
601− inciIf doIndent $ do
581+ indent $ sep breakpoint (located' p_hsExpr) initp
582+ indent $ do
602583 unless (null initp) breakpoint
603584 located lastp p_hsExpr
604585 Hanging -> do
@@ -613,15 +594,10 @@ p_hsExpr' s = \case
613594 breakpoint
614595 inci $ do
615596 txt "@"
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
620− _ -> return ()
621597 located (hswc_body a) p_hsType
622598 OpApp NoExtField x op y -> do
623599 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
624− p_exprOpTree s (reassociateOpTree getOpName opTree)
600+ p_exprOpTree True s (reassociateOpTree getOpName opTree)
625601 NegApp NoExtField e _ -> do
626602 txt "-"
627603 space
@@ -638,27 +614,22 @@ p_hsExpr' s = \case
638614 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
639615 unless (useRecordDot' && isRecordDot') breakpoint
640616 inci (located x p_hsExpr)
641− ExplicitTuple NoExtField args boxity ->
617+ ExplicitTuple NoExtField args boxity -> do
642618 let isSection = any (isMissing . unLoc) args
643619 isMissing = \case
644620 Missing NoExtField -> True
645621 _ -> False
646− p_arg = \case
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' =
622+ let parens' =
652623 case boxity of
653624 Boxed -> parens
654625 Unboxed -> parensHash
655− in if isSection
656− then
657− switchLayout [] . parens' s $
658− sep comma p_larg args
659− else
660− switchLayout (getLoc <$> args) . parens' s $
661− sep commaDel p_larg args
626+ if isSection
627+ then
628+ switchLayout [] . parens' s $
629+ sep comma (located' p_hsTupArg) args
630+ else
631+ switchLayout (getLoc <$> args) . parens' s . sitcc $
632+ sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
662633 ExplicitSum NoExtField tag arity e ->
663634 p_unboxedSum N tag arity (located e p_hsExpr)
664635 HsCase NoExtField e mgroup ->
@@ -666,9 +637,8 @@ p_hsExpr' s = \case
666637 HsIf NoExtField _ if' then' else' ->
667638 p_if exprPlacement p_hsExpr if' then' else'
668639 HsMultiIf NoExtField guards -> do
669− txt "if"
670− breakpoint
671− inci . inci $ sep newline (located' (p_grhs RightArrow)) guards
640+ txt "if "
641+ inci . inci . sitcc $ sep newline (located' (p_grhs RightArrow)) guards
672642 HsLet NoExtField localBinds e ->
673643 p_let p_hsExpr localBinds e
674644 HsDo NoExtField ctx es -> do
@@ -678,17 +648,17 @@ p_hsExpr' s = \case
678648 ub <- layoutToBraces <$> getLayout
679649 inci $
680650 sepSemi
681− (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
651+ (located' (ub . p_stmt' exprPlacement (p_hsExpr' S)))
682652 (unLoc es)
683− compBody = brackets N . located es $ \xs -> do
653+ compBody = brackets N $ located es $ \xs -> do
684654 let p_parBody =
685655 sep
686− (breakpoint >> txt "|" >> space)
656+ (breakpoint >> txt "| ")
687657 p_seqBody
688658 p_seqBody =
689659 sitcc
690660 . sep
691− commaDel
661+ (comma >> breakpoint)
692662 (located' (sitcc . p_stmt))
693663 stmts = init xs
694664 yield = last xs
@@ -709,8 +679,8 @@ p_hsExpr' s = \case
709679 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
710680 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
711681 ExplicitList _ _ xs ->
712− brackets s $
713− sep commaDel (sitcc . located' p_hsExpr) xs
682+ brackets s . sitcc $
683+ sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs
714684 RecordCon {..} -> do
715685 located rcon_con_name atom
716686 breakpoint
@@ -726,8 +696,8 @@ p_hsExpr' s = \case
726696 case rec_dotdot of
727697 Just {} -> [txt ".."]
728698 Nothing -> []
729− inci . braces N $
730− sep commaDel sitcc (fields <> dotdot)
699+ inci . braces N . sitcc $
700+ sep (comma >> breakpoint) sitcc (fields <> dotdot)
731701 RecordUpd {..} -> do
732702 located rupd_expr p_hsExpr
733703 useRecordDot' <- useRecordDot
@@ -745,9 +715,9 @@ p_hsExpr' s = \case
745715 Unambiguous _ n -> n
746716 XAmbiguousFieldOcc x -> noExtCon x
747717 }
748− inci . braces N $
718+ inci . braces N . sitcc $
749719 sep
750− commaDel
720+ (comma >> breakpoint)
751721 (sitcc . located' (p_hsRecField . updName))
752722 rupd_flds
753723 ExprWithTySig NoExtField x HsWC {hswc_body = HsIB {..}} -> sitcc $ do
@@ -760,22 +730,22 @@ p_hsExpr' s = \case
760730 ExprWithTySig NoExtField _ (XHsWildCardBndrs x) -> noExtCon x
761731 ArithSeq NoExtField _ x ->
762732 case x of
763− From from -> brackets s $ do
733+ From from -> brackets s . sitcc $ do
764734 located from p_hsExpr
765735 breakpoint
766736 txt ".."
767− FromThen from next -> brackets s $ do
768− sep commaDel (located' p_hsExpr) [from, next]
737+ FromThen from next -> brackets s . sitcc $ do
738+ sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
769739 breakpoint
770740 txt ".."
771− FromTo from to -> brackets s $ do
741+ FromTo from to -> brackets s . sitcc $ do
772742 located from p_hsExpr
773743 breakpoint
774744 txt ".."
775745 space
776746 located to p_hsExpr
777− FromThenTo from next to -> brackets s $ do
778− sep commaDel (located' p_hsExpr) [from, next]
747+ FromThenTo from next to -> brackets s . sitcc $ do
748+ sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
779749 breakpoint
780750 txt ".."
781751 space
@@ -825,7 +795,7 @@ p_patSynBind PSB {..} = do
825795 breakpoint
826796 located psb_def p_pat
827797 ImplicitBidirectional -> do
828− equals
798+ txt "="
829799 breakpoint
830800 located psb_def p_pat
831801 ExplicitBidirectional mgroup -> do
@@ -852,8 +822,8 @@ p_patSynBind PSB {..} = do
852822 inci $ do
853823 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
854824 unless (null xs) breakpoint
855− braces N $
856− sep commaDel (p_rdrName . recordPatSynPatVar) xs
825+ braces N . sitcc $
826+ sep (comma >> breakpoint) (p_rdrName . recordPatSynPatVar) xs
857827 rhs
858828 InfixCon l r -> do
859829 switchLayout [getLoc l, getLoc r] $ do
@@ -907,12 +877,10 @@ p_if placer render if' then' else' = do
907877 breakpoint
908878 inci $ do
909879 txt "then"
910− space
911880 located then' $ \x ->
912881 placeHanging (placer x) (render x)
913882 breakpoint
914883 txt "else"
915− space
916884 located else' $ \x ->
917885 placeHanging (placer x) (render x)
918886
@@ -949,13 +917,13 @@ p_pat = \case
949917 txt "!"
950918 located pat p_pat
951919 ListPat NoExtField pats ->
952− brackets S $ sep commaDel (located' p_pat) pats
920+ brackets S . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats
953921 TuplePat NoExtField pats boxing -> do
954− let parens' =
922+ let f =
955923 case boxing of
956924 Boxed -> parens S
957925 Unboxed -> parensHash S
958− parens' $ sep commaDel (sitcc . located' p_pat) pats
926+ f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats
959927 SumPat NoExtField pat tag arity ->
960928 p_unboxedSum S tag arity (located pat p_pat)
961929 ConPatIn pat details ->
@@ -971,7 +939,7 @@ p_pat = \case
971939 let f = \case
972940 Nothing -> txt ".."
973941 Just x -> located x p_pat_hsRecField
974− inci . braces N . sep commaDel f $
942+ inci . braces N . sitcc . sep (comma >> breakpoint) f $
975943 case dotdot of
976944 Nothing -> Just <$> fields
977945 Just (L _ n) -> (Just <$> take n fields) ++ [Nothing]
@@ -1012,7 +980,7 @@ p_pat_hsRecField HsRecField {..} = do
1012980 p_rdrName (rdrNameFieldOcc x)
1013981 unless hsRecPun $ do
1014982 space
1015− equals
983+ txt "="
1016984 breakpoint
1017985 inci (located hsRecFieldArg p_pat)
1018986
@@ -1021,15 +989,17 @@ p_unboxedSum s tag arity m = do
1021989 let before = tag - 1
1022990 after = arity - before - 1
1023991 args = replicate before Nothing <> [Just m] <> replicate after Nothing
1024− f x =
992+ f (x, i) = do
993+ let isFirst = i == 0
994+ isLast = i == arity - 1
1025995 case x :: Maybe (R ()) of
1026996 Nothing ->
1027− space
997+ unless (isFirst || isLast) space
1028998 Just m' -> do
1029− space
999+ unless isFirst space
10301000 m'
1031− space
1032− parensHash s $ sep (txt "|") f args
1001+ unless isLast space
1002+ parensHash s $ sep (txt "|") f (zip args [0 ..])
10331003
10341004p_hsSplice :: HsSplice GhcPs -> R ()
10351005p_hsSplice = \case
@@ -1231,10 +1201,11 @@ exprPlacement = \case
12311201 HsCase NoExtField _ _ -> Hanging
12321202 HsDo NoExtField DoExpr _ -> Hanging
12331203 HsDo NoExtField MDoExpr _ -> Hanging
1234− OpApp NoExtField _ op y ->
1235− case (fmap getOpNameStr . getOpName . unLoc) op of
1236− Just "$" -> exprPlacement (unLoc y)
1237− _ -> Normal
1204+ -- If the rightmost expression in an operator chain is hanging, make the
1205+ -- whole block hanging; so that we can use the common @f = foo $ do@
1206+ -- style.
1207+ OpApp NoExtField _ _ y -> exprPlacement (unLoc y)
1208+ -- Same thing for function applications (usually with -XBlockArguments)
12381209 HsApp NoExtField _ y -> exprPlacement (unLoc y)
12391210 HsProc NoExtField p _ ->
12401211 -- Indentation breaks if pattern is longer than one line and left
@@ -1260,16 +1231,15 @@ getOpName = \case
12601231 HsVar NoExtField (L _ a) -> Just a
12611232 _ -> Nothing
12621233
1263−getOpNameStr :: RdrName -> String
1264−getOpNameStr = occNameString . rdrNameOcc
1265−
12661234p_exprOpTree ::
1235+ -- | Can use special handling of dollar?
1236+ Bool ->
12671237 -- | Bracket style to use
12681238 BracketStyle ->
12691239 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
12701240 R ()
1271−p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1272−p_exprOpTree s (OpBranch x op y) = do
1241+p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1242+p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
12731243 -- If the beginning of the first argument and the second argument are on
12741244 -- the same line, and the second argument has a hanging form, use hanging
12751245 -- placement.
@@ -1291,54 +1261,44 @@ p_exprOpTree s (OpBranch x op y) = do
12911261 MultiLine -> case placement of
12921262 Hanging -> useBraces
12931263 Normal -> dontUseBraces
1294− opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1295− gotDollar = opNameStr == Just "$"
1296− gotColon = opNameStr == Just ":"
1297− gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
1264+ gotDollar = case getOpName (unLoc op) of
1265+ Just rname -> mkVarOcc "$" == rdrNameOcc rname
1266+ _ -> False
12981267 lhs =
12991268 switchLayout [opTreeLoc x] $
1300− p_exprOpTree s x
1301− p_op = located op (opWrapper . p_hsExpr)
1302− p_y = switchLayout [opTreeLoc y] (p_exprOpTree N y)
1269+ p_exprOpTree (not gotDollar) s x
1270+ let p_op = located op (opWrapper . p_hsExpr)
1271+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
13031272 isSection = case (opTreeLoc x, getLoc op) of
13041273 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
13051274 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
13061275 _ -> False
1307− isDoBlock = \case
1308− OpNode (L _ HsDo {}) -> True
1309− _ -> False
13101276 useRecordDot' <- useRecordDot
1311− if
1312− | gotColon -> do
1313− lhs
1314− space
1315− p_op
1316− case placement of
1317− Hanging -> do
1277+ let isRecordDot' = isRecordDot (unLoc op) (opTreeLoc y)
1278+ if useRecordDot' && isRecordDot'
1279+ then do
1280+ lhs
1281+ when isSection space
1282+ p_op
1283+ p_y
1284+ else
1285+ if isDollarSpecial
1286+ && gotDollar
1287+ && placement
1288+ == Normal
1289+ && isOneLineSpan (opTreeLoc x)
1290+ then do
1291+ useBraces lhs
1292+ space
1293+ p_op
1294+ breakpoint
1295+ inci p_y
1296+ else do
1297+ ub lhs
1298+ placeHanging placement $ do
1299+ p_op
13181300 space
13191301 p_y
1320− Normal -> do
1321− breakpoint
1322− inciIf (isDoBlock y) p_y
1323− | gotDollar
1324− && isOneLineSpan (opTreeLoc x)
1325− && placement == Normal -> do
1326− useBraces lhs
1327− space
1328− p_op
1329− breakpoint
1330− inci p_y
1331− | useRecordDot' && gotRecordDot -> do
1332− lhs
1333− when isSection space
1334− p_op
1335− p_y
1336− | otherwise -> do
1337− ub lhs
1338− placeHanging placement $ do
1339− p_op
1340− space
1341− p_y
13421302
13431303-- | Return 'True' if given expression is a record-dot operator expression.
13441304isRecordDot ::
@@ -1349,10 +1309,14 @@ isRecordDot ::
13491309 Bool
13501310isRecordDot op (RealSrcSpan ySpan) = case op of
13511311 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1352− (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1312+ isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
13531313 _ -> False
13541314isRecordDot _ _ = False
13551315
1316+-- | Check whether a given 'RdrName' is the dot operator.
1317+isDot :: RdrName -> Bool
1318+isDot name = rdrNameOcc name == mkVarOcc "."
1319+
13561320-- | Get annotations for the enclosing element.
13571321getEnclosingAnns :: R [AnnKeywordId]
13581322getEnclosingAnns = do