@@ -1,4 +1,5 @@
11{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE MultiWayIf #-}
23{-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE RecordWildCards #-}
45{-# LANGUAGE TypeApplications #-}
@@ -21,12 +22,12 @@ import Data.Char (isPunctuation, isSymbol)
2122import Data.Data hiding (Infix, Prefix)
2223import Data.Functor ((<&>))
2324import Data.List (intersperse, sortOn)
24−import Data.List.NonEmpty ((<|), NonEmpty ((:|)))
25+import Data.List.NonEmpty (NonEmpty (..), (<|))
2526import qualified Data.List.NonEmpty as NE
2627import Data.Text (Text)
2728import qualified Data.Text as Text
2829import GHC
29−import OccName (mkVarOcc)
30+import OccName (occNameString)
3031import Ormolu.Printer.Combinators
3132import Ormolu.Printer.Internal
3233import Ormolu.Printer.Meat.Common
@@ -60,7 +61,7 @@ data Placement
6061 -- should use it and avoid bumping one level
6162 -- of indentation
6263 Hanging
63− deriving (Eq)
64+ deriving (Eq, Show)
6465
6566p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
6667p_valDecl = \case
@@ -179,25 +180,24 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
179180 NoSrcStrict -> return ()
180181 SrcStrict -> txt "!"
181182 SrcLazy -> txt "~"
182− inci' <- case NE.nonEmpty m_pats of
183− Nothing -> id <$ case style of
184− Function name -> p_rdrName name
185− _ -> return ()
183+ indentBody <- case NE.nonEmpty m_pats of
184+ Nothing ->
185+ False <$ case style of
186+ Function name -> p_rdrName name
187+ _ -> return ()
186188 Just ne_pats -> do
187− let combinedSpans =
188− combineSrcSpans' $
189− getLoc <$> ne_pats
190− inci' =
191− if isOneLineSpan combinedSpans
192− then id
193− else inci
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)
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− inci'
200+ indentBody
201201 (p_rdrName name)
202202 (located' p_pat <$> m_pats)
203203 PatternBind -> stdCase
@@ -212,40 +212,32 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
212212 when needsSpace space
213213 sitcc stdCase
214214 LambdaCase -> stdCase
215− return inci'
215+ return indentBody
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 . srcSpanEnd . getLoc) name
220+ Function name -> Just (getLoc name)
221221 _ -> Nothing
222− Just pats -> (Just . srcSpanEnd . getLoc . NE.last) pats
222+ Just pats -> (Just . getLoc . NE.last) pats
223223 isCase = \case
224224 Case -> True
225225 LambdaCase -> True
226226 _ -> False
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 =
227+ hasGuards = withGuards grhssGRHSs
228+ grhssSpan =
236229 combineSrcSpans' $
237230 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
238231 patGrhssSpan =
239232 maybe
240233 grhssSpan
241− (combineSrcSpans grhssSpan . srcLocSpan)
234+ (combineSrcSpans grhssSpan . srcLocSpan . srcSpanEnd)
242235 endOfPats
243236 placement =
244237 case endOfPats of
245238 Nothing -> blockPlacement placer grhssGRHSs
246239 Just spn ->
247− if isOneLineSpan
248− (mkSrcSpan spn (srcSpanStart grhssSpan))
240+ if onTheSameLine spn grhssSpan
249241 then blockPlacement placer grhssGRHSs
250242 else Normal
251243 p_body = do
@@ -261,7 +253,14 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
261253 txt "where"
262254 unless whereIsEmpty breakpoint
263255 inci $ located grhssLocalBinds p_hsLocalBinds
264− inci' $ do
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 "->"
265264 switchLayout [patGrhssSpan] $
266265 placeHanging placement p_body
267266 inci p_where
@@ -285,24 +284,24 @@ p_grhs' placer render style (GRHS NoExtField guards body) =
285284 xs -> do
286285 txt "|"
287286 space
288− sitcc (sep (comma >> breakpoint) (sitcc . located' p_stmt) xs)
287+ sitcc (sep commaDel (sitcc . located' p_stmt) xs)
289288 space
290− txt $ case style of
291− EqualSign -> "="
292− RightArrow -> "->"
289+ inci $ case style of
290+ EqualSign -> equals
291+ RightArrow -> txt "->"
293292 placeHanging placement p_body
294293 where
295294 placement =
296295 case endOfGuards of
297296 Nothing -> placer (unLoc body)
298297 Just spn ->
299− if isOneLineSpan (mkSrcSpan spn (srcSpanStart (getLoc body)))
298+ if onTheSameLine spn (getLoc body)
300299 then placer (unLoc body)
301300 else Normal
302301 endOfGuards =
303302 case NE.nonEmpty guards of
304303 Nothing -> Nothing
305− Just gs -> (Just . srcSpanEnd . getLoc . NE.last) gs
304+ Just gs -> (Just . getLoc . NE.last) gs
306305 p_body = located body render
307306p_grhs' _ _ _ (XGRHS x) = noExtCon x
308307
@@ -310,13 +309,14 @@ p_hsCmd :: HsCmd GhcPs -> R ()
310309p_hsCmd = \case
311310 HsCmdArrApp NoExtField body input arrType _ -> do
312311 located body p_hsExpr
313− space
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 $ sitcc $ do
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
320320 located form p_hsExpr
321321 unless (null cmds) $ do
322322 breakpoint
@@ -344,7 +344,7 @@ p_hsCmd = \case
344344 txt "do"
345345 newline
346346 inci . located es $
347− sitcc . sep newline (located' (sitcc . p_stmt' cmdPlacement p_hsCmd))
347+ sitcc . sep newline (sitcc . withSpacing (p_stmt' cmdPlacement p_hsCmd))
348348 HsCmdWrap {} -> notImplemented "HsCmdWrap"
349349 XCmd x -> noExtCon x
350350
@@ -353,6 +353,35 @@ p_hsCmdTop = \case
353353 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
354354 XCmdTop x -> noExtCon x
355355
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+
356385p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
357386p_stmt = p_stmt' exprPlacement p_hsExpr
358387
@@ -424,7 +453,7 @@ p_stmt' placer render = \case
424453 RecStmt {..} -> do
425454 txt "rec"
426455 space
427− sitcc $ sepSemi (located' (p_stmt' placer render)) recS_stmts
456+ sitcc $ sepSemi (withSpacing (p_stmt' placer render)) recS_stmts
428457 XStmtLR c -> noExtCon c
429458
430459gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]]
@@ -442,34 +471,31 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
442471p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
443472p_hsLocalBinds = \case
444473 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
445− let ssStart =
446− either
447− (srcSpanStart . getLoc)
448− (srcSpanStart . getLoc)
449− items =
450− (Left <$> bagToList bag) ++ (Right <$> lsigs)
451− p_item (Left x) = located x p_valDecl
452− p_item (Right x) = located x p_sigDecl
453− -- Assigns 'False' to the last element, 'True' to the rest.
454− markInit :: [a] -> [(Bool, a)]
455− markInit [] = []
456− markInit [x] = [(False, x)]
457− markInit (x : xs) = (True, x) : markInit xs
458474 -- When in a single-line layout, there is a chance that the inner
459475 -- elements will also contain semicolons and they will confuse the
460476 -- parser. so we request braces around every element except the last.
461477 br <- layoutToBraces <$> getLayout
462− sitcc $
463− sepSemi
464− (\(m, i) -> (if m then br else id) $ p_item i)
465− (markInit $ sortOn ssStart items)
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)
466492 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
467493 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
468494 -- Second argument of IPBind is always Left before type-checking.
469495 let p_ipBind (IPBind NoExtField (Left name) expr) = do
470496 atom name
471497 space
472− txt "="
498+ equals
473499 breakpoint
474500 useBraces $ inci $ located expr p_hsExpr
475501 p_ipBind (IPBind NoExtField (Right _) _) =
@@ -488,15 +514,12 @@ p_hsRecField HsRecField {..} = do
488514 p_rdrName hsRecFieldLbl
489515 unless hsRecPun $ do
490516 space
491− txt "="
492− let placement = exprPlacement (unLoc hsRecFieldArg)
493− placeHanging placement $ located hsRecFieldArg p_hsExpr
494−
495−p_hsTupArg :: HsTupArg GhcPs -> R ()
496−p_hsTupArg = \case
497− Present NoExtField x -> located x p_hsExpr
498− Missing NoExtField -> pure ()
499− XTupArg x -> noExtCon x
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)
500523
501524p_hsExpr :: HsExpr GhcPs -> R ()
502525p_hsExpr = p_hsExpr' N
@@ -561,23 +584,21 @@ p_hsExpr' s = \case
561584 -- do-block or case expression it is not a good idea. It seems
562585 -- to be safe to always bump indentation when the function
563586 -- expression is parenthesised.
564− indent =
587+ doIndent =
565588 case func of
566− L _ (HsPar NoExtField _) -> inci
567− L _ (HsAppType NoExtField _ _) -> inci
568− L _ (HsMultiIf NoExtField _) -> inci
569− L spn _ ->
570− if isOneLineSpan spn
571− then inci
572− else id
573− ub <- getLayout <&> \case
574− SingleLine -> useBraces
575− MultiLine -> id
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
576597 ub $ do
577598 located func (p_hsExpr' s)
578599 breakpoint
579− indent $ sep breakpoint (located' p_hsExpr) initp
580− indent $ do
600+ inciIf doIndent $ sep breakpoint (located' p_hsExpr) initp
601+ inciIf doIndent $ do
581602 unless (null initp) breakpoint
582603 located lastp p_hsExpr
583604 Hanging -> do
@@ -592,10 +613,15 @@ p_hsExpr' s = \case
592613 breakpoint
593614 inci $ do
594615 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 ()
595621 located (hswc_body a) p_hsType
596622 OpApp NoExtField x op y -> do
597623 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
598− p_exprOpTree True s (reassociateOpTree getOpName opTree)
624+ p_exprOpTree s (reassociateOpTree getOpName opTree)
599625 NegApp NoExtField e _ -> do
600626 txt "-"
601627 space
@@ -612,22 +638,27 @@ p_hsExpr' s = \case
612638 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
613639 unless (useRecordDot' && isRecordDot') breakpoint
614640 inci (located x p_hsExpr)
615− ExplicitTuple NoExtField args boxity -> do
641+ ExplicitTuple NoExtField args boxity ->
616642 let isSection = any (isMissing . unLoc) args
617643 isMissing = \case
618644 Missing NoExtField -> True
619645 _ -> False
620− let parens' =
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' =
621652 case boxity of
622653 Boxed -> parens
623654 Unboxed -> parensHash
624− if isSection
625− then
626− switchLayout [] . parens' s $
627− sep comma (located' p_hsTupArg) args
628− else
629− switchLayout (getLoc <$> args) . parens' s . sitcc $
630− sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
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
631662 ExplicitSum NoExtField tag arity e ->
632663 p_unboxedSum N tag arity (located e p_hsExpr)
633664 HsCase NoExtField e mgroup ->
@@ -635,8 +666,9 @@ p_hsExpr' s = \case
635666 HsIf NoExtField _ if' then' else' ->
636667 p_if exprPlacement p_hsExpr if' then' else'
637668 HsMultiIf NoExtField guards -> do
638− txt "if "
639− inci . inci . sitcc $ sep newline (located' (p_grhs RightArrow)) guards
669+ txt "if"
670+ breakpoint
671+ inci . inci $ sep newline (located' (p_grhs RightArrow)) guards
640672 HsLet NoExtField localBinds e ->
641673 p_let p_hsExpr localBinds e
642674 HsDo NoExtField ctx es -> do
@@ -646,17 +678,17 @@ p_hsExpr' s = \case
646678 ub <- layoutToBraces <$> getLayout
647679 inci $
648680 sepSemi
649− (located' (ub . p_stmt' exprPlacement (p_hsExpr' S)))
681+ (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
650682 (unLoc es)
651− compBody = brackets N $ located es $ \xs -> do
683+ compBody = brackets N . located es $ \xs -> do
652684 let p_parBody =
653685 sep
654− (breakpoint >> txt "| ")
686+ (breakpoint >> txt "|" >> space)
655687 p_seqBody
656688 p_seqBody =
657689 sitcc
658690 . sep
659− (comma >> breakpoint)
691+ commaDel
660692 (located' (sitcc . p_stmt))
661693 stmts = init xs
662694 yield = last xs
@@ -677,8 +709,8 @@ p_hsExpr' s = \case
677709 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
678710 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
679711 ExplicitList _ _ xs ->
680− brackets s . sitcc $
681− sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs
712+ brackets s $
713+ sep commaDel (sitcc . located' p_hsExpr) xs
682714 RecordCon {..} -> do
683715 located rcon_con_name atom
684716 breakpoint
@@ -694,8 +726,8 @@ p_hsExpr' s = \case
694726 case rec_dotdot of
695727 Just {} -> [txt ".."]
696728 Nothing -> []
697− inci . braces N . sitcc $
698− sep (comma >> breakpoint) sitcc (fields <> dotdot)
729+ inci . braces N $
730+ sep commaDel sitcc (fields <> dotdot)
699731 RecordUpd {..} -> do
700732 located rupd_expr p_hsExpr
701733 useRecordDot' <- useRecordDot
@@ -713,9 +745,9 @@ p_hsExpr' s = \case
713745 Unambiguous _ n -> n
714746 XAmbiguousFieldOcc x -> noExtCon x
715747 }
716− inci . braces N . sitcc $
748+ inci . braces N $
717749 sep
718− (comma >> breakpoint)
750+ commaDel
719751 (sitcc . located' (p_hsRecField . updName))
720752 rupd_flds
721753 ExprWithTySig NoExtField x HsWC {hswc_body = HsIB {..}} -> sitcc $ do
@@ -728,22 +760,22 @@ p_hsExpr' s = \case
728760 ExprWithTySig NoExtField _ (XHsWildCardBndrs x) -> noExtCon x
729761 ArithSeq NoExtField _ x ->
730762 case x of
731− From from -> brackets s . sitcc $ do
763+ From from -> brackets s $ do
732764 located from p_hsExpr
733765 breakpoint
734766 txt ".."
735− FromThen from next -> brackets s . sitcc $ do
736− sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
767+ FromThen from next -> brackets s $ do
768+ sep commaDel (located' p_hsExpr) [from, next]
737769 breakpoint
738770 txt ".."
739− FromTo from to -> brackets s . sitcc $ do
771+ FromTo from to -> brackets s $ do
740772 located from p_hsExpr
741773 breakpoint
742774 txt ".."
743775 space
744776 located to p_hsExpr
745− FromThenTo from next to -> brackets s . sitcc $ do
746− sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
777+ FromThenTo from next to -> brackets s $ do
778+ sep commaDel (located' p_hsExpr) [from, next]
747779 breakpoint
748780 txt ".."
749781 space
@@ -793,7 +825,7 @@ p_patSynBind PSB {..} = do
793825 breakpoint
794826 located psb_def p_pat
795827 ImplicitBidirectional -> do
796− txt "="
828+ equals
797829 breakpoint
798830 located psb_def p_pat
799831 ExplicitBidirectional mgroup -> do
@@ -820,8 +852,8 @@ p_patSynBind PSB {..} = do
820852 inci $ do
821853 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
822854 unless (null xs) breakpoint
823− braces N . sitcc $
824− sep (comma >> breakpoint) (p_rdrName . recordPatSynPatVar) xs
855+ braces N $
856+ sep commaDel (p_rdrName . recordPatSynPatVar) xs
825857 rhs
826858 InfixCon l r -> do
827859 switchLayout [getLoc l, getLoc r] $ do
@@ -875,10 +907,12 @@ p_if placer render if' then' else' = do
875907 breakpoint
876908 inci $ do
877909 txt "then"
910+ space
878911 located then' $ \x ->
879912 placeHanging (placer x) (render x)
880913 breakpoint
881914 txt "else"
915+ space
882916 located else' $ \x ->
883917 placeHanging (placer x) (render x)
884918
@@ -915,13 +949,13 @@ p_pat = \case
915949 txt "!"
916950 located pat p_pat
917951 ListPat NoExtField pats ->
918− brackets S . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats
952+ brackets S $ sep commaDel (located' p_pat) pats
919953 TuplePat NoExtField pats boxing -> do
920− let f =
954+ let parens' =
921955 case boxing of
922956 Boxed -> parens S
923957 Unboxed -> parensHash S
924− f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats
958+ parens' $ sep commaDel (sitcc . located' p_pat) pats
925959 SumPat NoExtField pat tag arity ->
926960 p_unboxedSum S tag arity (located pat p_pat)
927961 ConPatIn pat details ->
@@ -937,7 +971,7 @@ p_pat = \case
937971 let f = \case
938972 Nothing -> txt ".."
939973 Just x -> located x p_pat_hsRecField
940− inci . braces N . sitcc . sep (comma >> breakpoint) f $
974+ inci . braces N . sep commaDel f $
941975 case dotdot of
942976 Nothing -> Just <$> fields
943977 Just (L _ n) -> (Just <$> take n fields) ++ [Nothing]
@@ -978,7 +1012,7 @@ p_pat_hsRecField HsRecField {..} = do
9781012 p_rdrName (rdrNameFieldOcc x)
9791013 unless hsRecPun $ do
9801014 space
981− txt "="
1015+ equals
9821016 breakpoint
9831017 inci (located hsRecFieldArg p_pat)
9841018
@@ -987,17 +1021,15 @@ p_unboxedSum s tag arity m = do
9871021 let before = tag - 1
9881022 after = arity - before - 1
9891023 args = replicate before Nothing <> [Just m] <> replicate after Nothing
990− f (x, i) = do
991− let isFirst = i == 0
992− isLast = i == arity - 1
1024+ f x =
9931025 case x :: Maybe (R ()) of
9941026 Nothing ->
995− unless (isFirst || isLast) space
1027+ space
9961028 Just m' -> do
997− unless isFirst space
1029+ space
9981030 m'
999− unless isLast space
1000− parensHash s $ sep (txt "|") f (zip args [0 ..])
1031+ space
1032+ parensHash s $ sep (txt "|") f args
10011033
10021034p_hsSplice :: HsSplice GhcPs -> R ()
10031035p_hsSplice = \case
@@ -1199,11 +1231,10 @@ exprPlacement = \case
11991231 HsCase NoExtField _ _ -> Hanging
12001232 HsDo NoExtField DoExpr _ -> Hanging
12011233 HsDo NoExtField MDoExpr _ -> Hanging
1202− -- If the rightmost expression in an operator chain is hanging, make the
1203− -- whole block hanging; so that we can use the common @f = foo $ do@
1204− -- style.
1205− OpApp NoExtField _ _ y -> exprPlacement (unLoc y)
1206− -- Same thing for function applications (usually with -XBlockArguments)
1234+ OpApp NoExtField _ op y ->
1235+ case (fmap getOpNameStr . getOpName . unLoc) op of
1236+ Just "$" -> exprPlacement (unLoc y)
1237+ _ -> Normal
12071238 HsApp NoExtField _ y -> exprPlacement (unLoc y)
12081239 HsProc NoExtField p _ ->
12091240 -- Indentation breaks if pattern is longer than one line and left
@@ -1229,15 +1260,16 @@ getOpName = \case
12291260 HsVar NoExtField (L _ a) -> Just a
12301261 _ -> Nothing
12311262
1263+getOpNameStr :: RdrName -> String
1264+getOpNameStr = occNameString . rdrNameOcc
1265+
12321266p_exprOpTree ::
1233− -- | Can use special handling of dollar?
1234− Bool ->
12351267 -- | Bracket style to use
12361268 BracketStyle ->
12371269 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
12381270 R ()
1239−p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1240−p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
1271+p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1272+p_exprOpTree s (OpBranch x op y) = do
12411273 -- If the beginning of the first argument and the second argument are on
12421274 -- the same line, and the second argument has a hanging form, use hanging
12431275 -- placement.
@@ -1259,44 +1291,54 @@ p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
12591291 MultiLine -> case placement of
12601292 Hanging -> useBraces
12611293 Normal -> dontUseBraces
1262− gotDollar = case getOpName (unLoc op) of
1263− Just rname -> mkVarOcc "$" == rdrNameOcc rname
1264− _ -> False
1294+ opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1295+ gotDollar = opNameStr == Just "$"
1296+ gotColon = opNameStr == Just ":"
1297+ gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
12651298 lhs =
12661299 switchLayout [opTreeLoc x] $
1267− p_exprOpTree (not gotDollar) s x
1268− let p_op = located op (opWrapper . p_hsExpr)
1269− p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
1300+ p_exprOpTree s x
1301+ p_op = located op (opWrapper . p_hsExpr)
1302+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree N y)
12701303 isSection = case (opTreeLoc x, getLoc op) of
12711304 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
12721305 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
12731306 _ -> False
1307+ isDoBlock = \case
1308+ OpNode (L _ HsDo {}) -> True
1309+ _ -> False
12741310 useRecordDot' <- useRecordDot
1275− let isRecordDot' = isRecordDot (unLoc op) (opTreeLoc y)
1276− if useRecordDot' && isRecordDot'
1277− then do
1278− lhs
1279− when isSection space
1280− p_op
1281− p_y
1282− else
1283− if isDollarSpecial
1284− && gotDollar
1285− && placement
1286− == Normal
1287− && isOneLineSpan (opTreeLoc x)
1288− then do
1289− useBraces lhs
1290− space
1291− p_op
1292− breakpoint
1293− inci p_y
1294− else do
1295− ub lhs
1296− placeHanging placement $ do
1297− p_op
1311+ if
1312+ | gotColon -> do
1313+ lhs
1314+ space
1315+ p_op
1316+ case placement of
1317+ Hanging -> do
12981318 space
12991319 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
13001342
13011343-- | Return 'True' if given expression is a record-dot operator expression.
13021344isRecordDot ::
@@ -1307,14 +1349,10 @@ isRecordDot ::
13071349 Bool
13081350isRecordDot op (RealSrcSpan ySpan) = case op of
13091351 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1310− isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1352+ (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
13111353 _ -> False
13121354isRecordDot _ _ = False
13131355
1314−-- | Check whether a given 'RdrName' is the dot operator.
1315−isDot :: RdrName -> Bool
1316−isDot name = rdrNameOcc name == mkVarOcc "."
1317−
13181356-- | Get annotations for the enclosing element.
13191357getEnclosingAnns :: R [AnnKeywordId]
13201358getEnclosingAnns = do