@@ -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
@@ -61,7 +60,7 @@ data Placement
6160 -- should use it and avoid bumping one level
6261 -- of indentation
6362 Hanging
64− deriving (Eq, Show)
63+ deriving (Eq)
6564
6665p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
6766p_valDecl = \case
@@ -180,24 +179,25 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
180179 NoSrcStrict -> return ()
181180 SrcStrict -> txt "!"
182181 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 ()
182+ inci' <- case NE.nonEmpty m_pats of
183+ Nothing -> id <$ case style of
184+ Function name -> p_rdrName name
185+ _ -> return ()
188186 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)
187+ let combinedSpans =
188+ combineSrcSpans' $
189+ getLoc <$> ne_pats
190+ inci' =
191+ if isOneLineSpan combinedSpans
192+ then id
193+ else inci
194194 switchLayout [combinedSpans] $ do
195195 let stdCase = sep breakpoint (located' p_pat) m_pats
196196 case style of
197197 Function name ->
198198 p_infixDefHelper
199199 isInfix
200− indentBody
200+ inci'
201201 (p_rdrName name)
202202 (located' p_pat <$> m_pats)
203203 PatternBind -> stdCase
@@ -212,32 +212,40 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
212212 when needsSpace space
213213 sitcc stdCase
214214 LambdaCase -> stdCase
215− return indentBody
215+ return inci'
216216 let -- Calculate position of end of patterns. This is useful when we decide
217217 -- about putting certain constructions in hanging positions.
218218 endOfPats = case NE.nonEmpty m_pats of
219219 Nothing -> case style of
220− Function name -> Just (getLoc name)
220+ Function name -> (Just . srcSpanEnd . getLoc) name
221221 _ -> Nothing
222− Just pats -> (Just . getLoc . NE.last) pats
222+ Just pats -> (Just . srcSpanEnd . getLoc . NE.last) pats
223223 isCase = \case
224224 Case -> True
225225 LambdaCase -> True
226226 _ -> False
227− hasGuards = withGuards grhssGRHSs
228− grhssSpan =
227+ let hasGuards = withGuards grhssGRHSs
228+ unless (length grhssGRHSs > 1) $
229+ case style of
230+ Function _ | hasGuards -> return ()
231+ Function _ -> space >> txt "="
232+ PatternBind -> space >> txt "="
233+ s | isCase s && hasGuards -> return ()
234+ _ -> space >> txt "->"
235+ let grhssSpan =
229236 combineSrcSpans' $
230237 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
231238 patGrhssSpan =
232239 maybe
233240 grhssSpan
234− (combineSrcSpans grhssSpan . srcLocSpan . srcSpanEnd)
241+ (combineSrcSpans grhssSpan . srcLocSpan)
235242 endOfPats
236243 placement =
237244 case endOfPats of
238245 Nothing -> blockPlacement placer grhssGRHSs
239246 Just spn ->
240− if onTheSameLine spn grhssSpan
247+ if isOneLineSpan
248+ (mkSrcSpan spn (srcSpanStart grhssSpan))
241249 then blockPlacement placer grhssGRHSs
242250 else Normal
243251 p_body = do
@@ -253,14 +261,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
253261 txt "where"
254262 unless whereIsEmpty breakpoint
255263 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 "->"
264+ inci' $ do
264265 switchLayout [patGrhssSpan] $
265266 placeHanging placement p_body
266267 inci p_where
@@ -284,24 +285,24 @@ p_grhs' placer render style (GRHS NoExtField guards body) =
284285 xs -> do
285286 txt "|"
286287 space
287− sitcc (sep commaDel (sitcc . located' p_stmt) xs)
288+ sitcc (sep (comma >> breakpoint) (sitcc . located' p_stmt) xs)
288289 space
289− inci $ case style of
290− EqualSign -> equals
291− RightArrow -> txt "->"
290+ txt $ case style of
291+ EqualSign -> "="
292+ RightArrow -> "->"
292293 placeHanging placement p_body
293294 where
294295 placement =
295296 case endOfGuards of
296297 Nothing -> placer (unLoc body)
297298 Just spn ->
298− if onTheSameLine spn (getLoc body)
299+ if isOneLineSpan (mkSrcSpan spn (srcSpanStart (getLoc body)))
299300 then placer (unLoc body)
300301 else Normal
301302 endOfGuards =
302303 case NE.nonEmpty guards of
303304 Nothing -> Nothing
304− Just gs -> (Just . getLoc . NE.last) gs
305+ Just gs -> (Just . srcSpanEnd . getLoc . NE.last) gs
305306 p_body = located body render
306307p_grhs' _ _ _ (XGRHS x) = noExtCon x
307308
@@ -316,7 +317,7 @@ p_hsCmd = \case
316317 HsHigherOrderApp -> txt "-<<"
317318 placeHanging (exprPlacement (unLoc input)) $
318319 located input p_hsExpr
319− HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ do
320+ HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ sitcc $ do
320321 located form p_hsExpr
321322 unless (null cmds) $ do
322323 breakpoint
@@ -344,7 +345,7 @@ p_hsCmd = \case
344345 txt "do"
345346 newline
346347 inci . located es $
347− sitcc . sep newline (sitcc . withSpacing (p_stmt' cmdPlacement p_hsCmd))
348+ sitcc . sep newline (located' (sitcc . p_stmt' cmdPlacement p_hsCmd))
348349 HsCmdWrap {} -> notImplemented "HsCmdWrap"
349350 XCmd x -> noExtCon x
350351
@@ -353,35 +354,6 @@ p_hsCmdTop = \case
353354 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
354355 XCmdTop x -> noExtCon x
355356
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−
385357p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
386358p_stmt = p_stmt' exprPlacement p_hsExpr
387359
@@ -453,7 +425,7 @@ p_stmt' placer render = \case
453425 RecStmt {..} -> do
454426 txt "rec"
455427 space
456− sitcc $ sepSemi (withSpacing (p_stmt' placer render)) recS_stmts
428+ sitcc $ sepSemi (located' (p_stmt' placer render)) recS_stmts
457429 XStmtLR c -> noExtCon c
458430
459431gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]]
@@ -471,31 +443,34 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
471443p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
472444p_hsLocalBinds = \case
473445 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
446+ let ssStart =
447+ either
448+ (srcSpanStart . getLoc)
449+ (srcSpanStart . getLoc)
450+ items =
451+ (Left <$> bagToList bag) ++ (Right <$> lsigs)
452+ p_item (Left x) = located x p_valDecl
453+ p_item (Right x) = located x p_sigDecl
454+ -- Assigns 'False' to the last element, 'True' to the rest.
455+ markInit :: [a] -> [(Bool, a)]
456+ markInit [] = []
457+ markInit [x] = [(False, x)]
458+ markInit (x : xs) = (True, x) : markInit xs
474459 -- When in a single-line layout, there is a chance that the inner
475460 -- elements will also contain semicolons and they will confuse the
476461 -- parser. so we request braces around every element except the last.
477462 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)
463+ sitcc $
464+ sepSemi
465+ (\(m, i) -> (if m then br else id) $ p_item i)
466+ (markInit $ sortOn ssStart items)
492467 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
493468 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
494469 -- Second argument of IPBind is always Left before type-checking.
495470 let p_ipBind (IPBind NoExtField (Left name) expr) = do
496471 atom name
497472 space
498− equals
473+ txt "="
499474 breakpoint
500475 useBraces $ inci $ located expr p_hsExpr
501476 p_ipBind (IPBind NoExtField (Right _) _) =
@@ -514,12 +489,15 @@ p_hsRecField HsRecField {..} = do
514489 p_rdrName hsRecFieldLbl
515490 unless hsRecPun $ do
516491 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)
492+ txt "="
493+ let placement = exprPlacement (unLoc hsRecFieldArg)
494+ placeHanging placement $ located hsRecFieldArg p_hsExpr
495+
496+p_hsTupArg :: HsTupArg GhcPs -> R ()
497+p_hsTupArg = \case
498+ Present NoExtField x -> located x p_hsExpr
499+ Missing NoExtField -> pure ()
500+ XTupArg x -> noExtCon x
523501
524502p_hsExpr :: HsExpr GhcPs -> R ()
525503p_hsExpr = p_hsExpr' N
@@ -584,21 +562,23 @@ p_hsExpr' s = \case
584562 -- do-block or case expression it is not a good idea. It seems
585563 -- to be safe to always bump indentation when the function
586564 -- expression is parenthesised.
587− doIndent =
565+ indent =
588566 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
567+ L _ (HsPar NoExtField _) -> inci
568+ L _ (HsAppType NoExtField _ _) -> inci
569+ L _ (HsMultiIf NoExtField _) -> inci
570+ L spn _ ->
571+ if isOneLineSpan spn
572+ then inci
573+ else id
574+ ub <- getLayout <&> \case
575+ SingleLine -> useBraces
576+ MultiLine -> id
597577 ub $ do
598578 located func (p_hsExpr' s)
599579 breakpoint
600− inciIf doIndent $ sep breakpoint (located' p_hsExpr) initp
601− inciIf doIndent $ do
580+ indent $ sep breakpoint (located' p_hsExpr) initp
581+ indent $ do
602582 unless (null initp) breakpoint
603583 located lastp p_hsExpr
604584 Hanging -> do
@@ -613,15 +593,10 @@ p_hsExpr' s = \case
613593 breakpoint
614594 inci $ do
615595 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 ()
621596 located (hswc_body a) p_hsType
622597 OpApp NoExtField x op y -> do
623598 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
624− p_exprOpTree s (reassociateOpTree getOpName opTree)
599+ p_exprOpTree True s (reassociateOpTree getOpName opTree)
625600 NegApp NoExtField e _ -> do
626601 txt "-"
627602 space
@@ -638,27 +613,22 @@ p_hsExpr' s = \case
638613 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
639614 unless (useRecordDot' && isRecordDot') breakpoint
640615 inci (located x p_hsExpr)
641− ExplicitTuple NoExtField args boxity ->
616+ ExplicitTuple NoExtField args boxity -> do
642617 let isSection = any (isMissing . unLoc) args
643618 isMissing = \case
644619 Missing NoExtField -> True
645620 _ -> 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' =
621+ let parens' =
652622 case boxity of
653623 Boxed -> parens
654624 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
625+ if isSection
626+ then
627+ switchLayout [] . parens' s $
628+ sep comma (located' p_hsTupArg) args
629+ else
630+ switchLayout (getLoc <$> args) . parens' s . sitcc $
631+ sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
662632 ExplicitSum NoExtField tag arity e ->
663633 p_unboxedSum N tag arity (located e p_hsExpr)
664634 HsCase NoExtField e mgroup ->
@@ -666,9 +636,8 @@ p_hsExpr' s = \case
666636 HsIf NoExtField _ if' then' else' ->
667637 p_if exprPlacement p_hsExpr if' then' else'
668638 HsMultiIf NoExtField guards -> do
669− txt "if"
670− breakpoint
671− inci . inci $ sep newline (located' (p_grhs RightArrow)) guards
639+ txt "if "
640+ inci . inci . sitcc $ sep newline (located' (p_grhs RightArrow)) guards
672641 HsLet NoExtField localBinds e ->
673642 p_let p_hsExpr localBinds e
674643 HsDo NoExtField ctx es -> do
@@ -678,17 +647,17 @@ p_hsExpr' s = \case
678647 ub <- layoutToBraces <$> getLayout
679648 inci $
680649 sepSemi
681− (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
650+ (located' (ub . p_stmt' exprPlacement (p_hsExpr' S)))
682651 (unLoc es)
683− compBody = brackets N . located es $ \xs -> do
652+ compBody = brackets N $ located es $ \xs -> do
684653 let p_parBody =
685654 sep
686− (breakpoint >> txt "|" >> space)
655+ (breakpoint >> txt "| ")
687656 p_seqBody
688657 p_seqBody =
689658 sitcc
690659 . sep
691− commaDel
660+ (comma >> breakpoint)
692661 (located' (sitcc . p_stmt))
693662 stmts = init xs
694663 yield = last xs
@@ -709,8 +678,8 @@ p_hsExpr' s = \case
709678 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
710679 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
711680 ExplicitList _ _ xs ->
712− brackets s $
713− sep commaDel (sitcc . located' p_hsExpr) xs
681+ brackets s . sitcc $
682+ sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs
714683 RecordCon {..} -> do
715684 located rcon_con_name atom
716685 breakpoint
@@ -726,8 +695,8 @@ p_hsExpr' s = \case
726695 case rec_dotdot of
727696 Just {} -> [txt ".."]
728697 Nothing -> []
729− inci . braces N $
730− sep commaDel sitcc (fields <> dotdot)
698+ inci . braces N . sitcc $
699+ sep (comma >> breakpoint) sitcc (fields <> dotdot)
731700 RecordUpd {..} -> do
732701 located rupd_expr p_hsExpr
733702 useRecordDot' <- useRecordDot
@@ -745,9 +714,9 @@ p_hsExpr' s = \case
745714 Unambiguous _ n -> n
746715 XAmbiguousFieldOcc x -> noExtCon x
747716 }
748− inci . braces N $
717+ inci . braces N . sitcc $
749718 sep
750− commaDel
719+ (comma >> breakpoint)
751720 (sitcc . located' (p_hsRecField . updName))
752721 rupd_flds
753722 ExprWithTySig NoExtField x HsWC {hswc_body = HsIB {..}} -> sitcc $ do
@@ -760,22 +729,22 @@ p_hsExpr' s = \case
760729 ExprWithTySig NoExtField _ (XHsWildCardBndrs x) -> noExtCon x
761730 ArithSeq NoExtField _ x ->
762731 case x of
763− From from -> brackets s $ do
732+ From from -> brackets s . sitcc $ do
764733 located from p_hsExpr
765734 breakpoint
766735 txt ".."
767− FromThen from next -> brackets s $ do
768− sep commaDel (located' p_hsExpr) [from, next]
736+ FromThen from next -> brackets s . sitcc $ do
737+ sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
769738 breakpoint
770739 txt ".."
771− FromTo from to -> brackets s $ do
740+ FromTo from to -> brackets s . sitcc $ do
772741 located from p_hsExpr
773742 breakpoint
774743 txt ".."
775744 space
776745 located to p_hsExpr
777− FromThenTo from next to -> brackets s $ do
778− sep commaDel (located' p_hsExpr) [from, next]
746+ FromThenTo from next to -> brackets s . sitcc $ do
747+ sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
779748 breakpoint
780749 txt ".."
781750 space
@@ -825,7 +794,7 @@ p_patSynBind PSB {..} = do
825794 breakpoint
826795 located psb_def p_pat
827796 ImplicitBidirectional -> do
828− equals
797+ txt "="
829798 breakpoint
830799 located psb_def p_pat
831800 ExplicitBidirectional mgroup -> do
@@ -852,8 +821,8 @@ p_patSynBind PSB {..} = do
852821 inci $ do
853822 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
854823 unless (null xs) breakpoint
855− braces N $
856− sep commaDel (p_rdrName . recordPatSynPatVar) xs
824+ braces N . sitcc $
825+ sep (comma >> breakpoint) (p_rdrName . recordPatSynPatVar) xs
857826 rhs
858827 InfixCon l r -> do
859828 switchLayout [getLoc l, getLoc r] $ do
@@ -907,12 +876,10 @@ p_if placer render if' then' else' = do
907876 breakpoint
908877 inci $ do
909878 txt "then"
910− space
911879 located then' $ \x ->
912880 placeHanging (placer x) (render x)
913881 breakpoint
914882 txt "else"
915− space
916883 located else' $ \x ->
917884 placeHanging (placer x) (render x)
918885
@@ -949,13 +916,13 @@ p_pat = \case
949916 txt "!"
950917 located pat p_pat
951918 ListPat NoExtField pats ->
952− brackets S $ sep commaDel (located' p_pat) pats
919+ brackets S . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats
953920 TuplePat NoExtField pats boxing -> do
954− let parens' =
921+ let f =
955922 case boxing of
956923 Boxed -> parens S
957924 Unboxed -> parensHash S
958− parens' $ sep commaDel (sitcc . located' p_pat) pats
925+ f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats
959926 SumPat NoExtField pat tag arity ->
960927 p_unboxedSum S tag arity (located pat p_pat)
961928 ConPatIn pat details ->
@@ -971,7 +938,7 @@ p_pat = \case
971938 let f = \case
972939 Nothing -> txt ".."
973940 Just x -> located x p_pat_hsRecField
974− inci . braces N . sep commaDel f $
941+ inci . braces N . sitcc . sep (comma >> breakpoint) f $
975942 case dotdot of
976943 Nothing -> Just <$> fields
977944 Just (L _ n) -> (Just <$> take n fields) ++ [Nothing]
@@ -1012,7 +979,7 @@ p_pat_hsRecField HsRecField {..} = do
1012979 p_rdrName (rdrNameFieldOcc x)
1013980 unless hsRecPun $ do
1014981 space
1015− equals
982+ txt "="
1016983 breakpoint
1017984 inci (located hsRecFieldArg p_pat)
1018985
@@ -1021,15 +988,17 @@ p_unboxedSum s tag arity m = do
1021988 let before = tag - 1
1022989 after = arity - before - 1
1023990 args = replicate before Nothing <> [Just m] <> replicate after Nothing
1024− f x =
991+ f (x, i) = do
992+ let isFirst = i == 0
993+ isLast = i == arity - 1
1025994 case x :: Maybe (R ()) of
1026995 Nothing ->
1027− space
996+ unless (isFirst || isLast) space
1028997 Just m' -> do
1029− space
998+ unless isFirst space
1030999 m'
1031− space
1032− parensHash s $ sep (txt "|") f args
1000+ unless isLast space
1001+ parensHash s $ sep (txt "|") f (zip args [0 ..])
10331002
10341003p_hsSplice :: HsSplice GhcPs -> R ()
10351004p_hsSplice = \case
@@ -1231,10 +1200,11 @@ exprPlacement = \case
12311200 HsCase NoExtField _ _ -> Hanging
12321201 HsDo NoExtField DoExpr _ -> Hanging
12331202 HsDo NoExtField MDoExpr _ -> Hanging
1234− OpApp NoExtField _ op y ->
1235− case (fmap getOpNameStr . getOpName . unLoc) op of
1236− Just "$" -> exprPlacement (unLoc y)
1237− _ -> Normal
1203+ -- If the rightmost expression in an operator chain is hanging, make the
1204+ -- whole block hanging; so that we can use the common @f = foo $ do@
1205+ -- style.
1206+ OpApp NoExtField _ _ y -> exprPlacement (unLoc y)
1207+ -- Same thing for function applications (usually with -XBlockArguments)
12381208 HsApp NoExtField _ y -> exprPlacement (unLoc y)
12391209 HsProc NoExtField p _ ->
12401210 -- Indentation breaks if pattern is longer than one line and left
@@ -1260,16 +1230,15 @@ getOpName = \case
12601230 HsVar NoExtField (L _ a) -> Just a
12611231 _ -> Nothing
12621232
1263−getOpNameStr :: RdrName -> String
1264−getOpNameStr = occNameString . rdrNameOcc
1265−
12661233p_exprOpTree ::
1234+ -- | Can use special handling of dollar?
1235+ Bool ->
12671236 -- | Bracket style to use
12681237 BracketStyle ->
12691238 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
12701239 R ()
1271−p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1272−p_exprOpTree s (OpBranch x op y) = do
1240+p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1241+p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
12731242 -- If the beginning of the first argument and the second argument are on
12741243 -- the same line, and the second argument has a hanging form, use hanging
12751244 -- placement.
@@ -1291,54 +1260,44 @@ p_exprOpTree s (OpBranch x op y) = do
12911260 MultiLine -> case placement of
12921261 Hanging -> useBraces
12931262 Normal -> dontUseBraces
1294− opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1295− gotDollar = opNameStr == Just "$"
1296− gotColon = opNameStr == Just ":"
1297− gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
1263+ gotDollar = case getOpName (unLoc op) of
1264+ Just rname -> mkVarOcc "$" == rdrNameOcc rname
1265+ _ -> False
12981266 lhs =
12991267 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)
1268+ p_exprOpTree (not gotDollar) s x
1269+ let p_op = located op (opWrapper . p_hsExpr)
1270+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
13031271 isSection = case (opTreeLoc x, getLoc op) of
13041272 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
13051273 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
13061274 _ -> False
1307− isDoBlock = \case
1308− OpNode (L _ HsDo {}) -> True
1309− _ -> False
13101275 useRecordDot' <- useRecordDot
1311− if
1312− | gotColon -> do
1313− lhs
1314− space
1315− p_op
1316− case placement of
1317− Hanging -> do
1276+ let isRecordDot' = isRecordDot (unLoc op) (opTreeLoc y)
1277+ if useRecordDot' && isRecordDot'
1278+ then do
1279+ lhs
1280+ when isSection space
1281+ p_op
1282+ p_y
1283+ else
1284+ if isDollarSpecial
1285+ && gotDollar
1286+ && placement
1287+ == Normal
1288+ && isOneLineSpan (opTreeLoc x)
1289+ then do
1290+ useBraces lhs
1291+ space
1292+ p_op
1293+ breakpoint
1294+ inci p_y
1295+ else do
1296+ ub lhs
1297+ placeHanging placement $ do
1298+ p_op
13181299 space
13191300 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
13421301
13431302-- | Return 'True' if given expression is a record-dot operator expression.
13441303isRecordDot ::
@@ -1349,10 +1308,14 @@ isRecordDot ::
13491308 Bool
13501309isRecordDot op (RealSrcSpan ySpan) = case op of
13511310 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1352− (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1311+ isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
13531312 _ -> False
13541313isRecordDot _ _ = False
13551314
1315+-- | Check whether a given 'RdrName' is the dot operator.
1316+isDot :: RdrName -> Bool
1317+isDot name = rdrNameOcc name == mkVarOcc "."
1318+
13561319-- | Get annotations for the enclosing element.
13571320getEnclosingAnns :: R [AnnKeywordId]
13581321getEnclosingAnns = do