@@ -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
@@ -35,8 +36,6 @@ import Ormolu.Printer.Meat.Declaration.Signature
3536import Ormolu.Printer.Meat.Type
3637import Ormolu.Printer.Operators
3738import Ormolu.Utils
38−import RdrName (RdrName (..), rdrNameOcc)
39−import SrcLoc (combineSrcSpans, isOneLineSpan)
4039
4140-- | Style of a group of equations.
4241data MatchGroupStyle
@@ -62,7 +61,7 @@ data Placement
6261 -- should use it and avoid bumping one level
6362 -- of indentation
6463 Hanging
65− deriving (Eq)
64+ deriving (Eq, Show)
6665
6766p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
6867p_valDecl = \case
@@ -181,25 +180,24 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
181180 NoSrcStrict -> return ()
182181 SrcStrict -> txt "!"
183182 SrcLazy -> txt "~"
184− inci' <- case NE.nonEmpty m_pats of
185− Nothing -> id <$ case style of
186− Function name -> p_rdrName name
187− _ -> return ()
183+ indentBody <- case NE.nonEmpty m_pats of
184+ Nothing ->
185+ False <$ case style of
186+ Function name -> p_rdrName name
187+ _ -> return ()
188188 Just ne_pats -> do
189− let combinedSpans =
190− combineSrcSpans' $
191− getLoc <$> ne_pats
192− inci' =
193− if isOneLineSpan combinedSpans
194− then id
195− 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)
196194 switchLayout [combinedSpans] $ do
197195 let stdCase = sep breakpoint (located' p_pat) m_pats
198196 case style of
199197 Function name ->
200198 p_infixDefHelper
201199 isInfix
202− inci'
200+ indentBody
203201 (p_rdrName name)
204202 (located' p_pat <$> m_pats)
205203 PatternBind -> stdCase
@@ -214,40 +212,32 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
214212 when needsSpace space
215213 sitcc stdCase
216214 LambdaCase -> stdCase
217− return inci'
215+ return indentBody
218216 let -- Calculate position of end of patterns. This is useful when we decide
219217 -- about putting certain constructions in hanging positions.
220218 endOfPats = case NE.nonEmpty m_pats of
221219 Nothing -> case style of
222− Function name -> (Just . srcSpanEnd . getLoc) name
220+ Function name -> Just (getLoc name)
223221 _ -> Nothing
224− Just pats -> (Just . srcSpanEnd . getLoc . NE.last) pats
222+ Just pats -> (Just . getLoc . NE.last) pats
225223 isCase = \case
226224 Case -> True
227225 LambdaCase -> True
228226 _ -> False
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 =
227+ hasGuards = withGuards grhssGRHSs
228+ grhssSpan =
238229 combineSrcSpans' $
239230 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
240231 patGrhssSpan =
241232 maybe
242233 grhssSpan
243− (combineSrcSpans grhssSpan . srcLocSpan)
234+ (combineSrcSpans grhssSpan . srcLocSpan . srcSpanEnd)
244235 endOfPats
245236 placement =
246237 case endOfPats of
247238 Nothing -> blockPlacement placer grhssGRHSs
248239 Just spn ->
249− if isOneLineSpan
250− (mkSrcSpan spn (srcSpanStart grhssSpan))
240+ if onTheSameLine spn grhssSpan
251241 then blockPlacement placer grhssGRHSs
252242 else Normal
253243 p_body = do
@@ -263,7 +253,14 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
263253 txt "where"
264254 unless whereIsEmpty breakpoint
265255 inci $ located grhssLocalBinds p_hsLocalBinds
266− 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 "->"
267264 switchLayout [patGrhssSpan] $
268265 placeHanging placement p_body
269266 inci p_where
@@ -287,24 +284,24 @@ p_grhs' placer render style (GRHS NoExtField guards body) =
287284 xs -> do
288285 txt "|"
289286 space
290− sitcc (sep (comma >> breakpoint) (sitcc . located' p_stmt) xs)
287+ sitcc (sep commaDel (sitcc . located' p_stmt) xs)
291288 space
292− txt $ case style of
293− EqualSign -> "="
294− RightArrow -> "->"
289+ inci $ case style of
290+ EqualSign -> equals
291+ RightArrow -> txt "->"
295292 placeHanging placement p_body
296293 where
297294 placement =
298295 case endOfGuards of
299296 Nothing -> placer (unLoc body)
300297 Just spn ->
301− if isOneLineSpan (mkSrcSpan spn (srcSpanStart (getLoc body)))
298+ if onTheSameLine spn (getLoc body)
302299 then placer (unLoc body)
303300 else Normal
304301 endOfGuards =
305302 case NE.nonEmpty guards of
306303 Nothing -> Nothing
307− Just gs -> (Just . srcSpanEnd . getLoc . NE.last) gs
304+ Just gs -> (Just . getLoc . NE.last) gs
308305 p_body = located body render
309306p_grhs' _ _ _ (XGRHS x) = noExtCon x
310307
@@ -312,13 +309,14 @@ p_hsCmd :: HsCmd GhcPs -> R ()
312309p_hsCmd = \case
313310 HsCmdArrApp NoExtField body input arrType _ -> do
314311 located body p_hsExpr
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
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
322320 located form p_hsExpr
323321 unless (null cmds) $ do
324322 breakpoint
@@ -346,7 +344,7 @@ p_hsCmd = \case
346344 txt "do"
347345 newline
348346 inci . located es $
349− sitcc . sep newline (located' (sitcc . p_stmt' cmdPlacement p_hsCmd))
347+ sitcc . sep newline (sitcc . withSpacing (p_stmt' cmdPlacement p_hsCmd))
350348 HsCmdWrap {} -> notImplemented "HsCmdWrap"
351349 XCmd x -> noExtCon x
352350
@@ -355,6 +353,35 @@ p_hsCmdTop = \case
355353 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
356354 XCmdTop x -> noExtCon x
357355
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+
358385p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
359386p_stmt = p_stmt' exprPlacement p_hsExpr
360387
@@ -426,7 +453,7 @@ p_stmt' placer render = \case
426453 RecStmt {..} -> do
427454 txt "rec"
428455 space
429− sitcc $ sepSemi (located' (p_stmt' placer render)) recS_stmts
456+ sitcc $ sepSemi (withSpacing (p_stmt' placer render)) recS_stmts
430457 XStmtLR c -> noExtCon c
431458
432459gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]]
@@ -444,34 +471,31 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
444471p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
445472p_hsLocalBinds = \case
446473 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
460474 -- When in a single-line layout, there is a chance that the inner
461475 -- elements will also contain semicolons and they will confuse the
462476 -- parser. so we request braces around every element except the last.
463477 br <- layoutToBraces <$> getLayout
464− sitcc $
465− sepSemi
466− (\(m, i) -> (if m then br else id) $ p_item i)
467− (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)
468492 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
469493 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
470494 -- Second argument of IPBind is always Left before type-checking.
471495 let p_ipBind (IPBind NoExtField (Left name) expr) = do
472496 atom name
473497 space
474− txt "="
498+ equals
475499 breakpoint
476500 useBraces $ inci $ located expr p_hsExpr
477501 p_ipBind (IPBind NoExtField (Right _) _) =
@@ -490,15 +514,12 @@ p_hsRecField HsRecField {..} = do
490514 p_rdrName hsRecFieldLbl
491515 unless hsRecPun $ do
492516 space
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
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)
502523
503524p_hsExpr :: HsExpr GhcPs -> R ()
504525p_hsExpr = p_hsExpr' N
@@ -563,23 +584,21 @@ p_hsExpr' s = \case
563584 -- do-block or case expression it is not a good idea. It seems
564585 -- to be safe to always bump indentation when the function
565586 -- expression is parenthesised.
566− indent =
587+ doIndent =
567588 case func of
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
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
578597 ub $ do
579598 located func (p_hsExpr' s)
580599 breakpoint
581− indent $ sep breakpoint (located' p_hsExpr) initp
582− indent $ do
600+ inciIf doIndent $ sep breakpoint (located' p_hsExpr) initp
601+ inciIf doIndent $ do
583602 unless (null initp) breakpoint
584603 located lastp p_hsExpr
585604 Hanging -> do
@@ -594,10 +613,15 @@ p_hsExpr' s = \case
594613 breakpoint
595614 inci $ do
596615 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 ()
597621 located (hswc_body a) p_hsType
598622 OpApp NoExtField x op y -> do
599623 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
600− p_exprOpTree True s (reassociateOpTree getOpName opTree)
624+ p_exprOpTree s (reassociateOpTree getOpName opTree)
601625 NegApp NoExtField e _ -> do
602626 txt "-"
603627 space
@@ -614,22 +638,27 @@ p_hsExpr' s = \case
614638 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
615639 unless (useRecordDot' && isRecordDot') breakpoint
616640 inci (located x p_hsExpr)
617− ExplicitTuple NoExtField args boxity -> do
641+ ExplicitTuple NoExtField args boxity ->
618642 let isSection = any (isMissing . unLoc) args
619643 isMissing = \case
620644 Missing NoExtField -> True
621645 _ -> False
622− 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' =
623652 case boxity of
624653 Boxed -> parens
625654 Unboxed -> parensHash
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
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
633662 ExplicitSum NoExtField tag arity e ->
634663 p_unboxedSum N tag arity (located e p_hsExpr)
635664 HsCase NoExtField e mgroup ->
@@ -637,8 +666,9 @@ p_hsExpr' s = \case
637666 HsIf NoExtField _ if' then' else' ->
638667 p_if exprPlacement p_hsExpr if' then' else'
639668 HsMultiIf NoExtField guards -> do
640− txt "if "
641− 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
642672 HsLet NoExtField localBinds e ->
643673 p_let p_hsExpr localBinds e
644674 HsDo NoExtField ctx es -> do
@@ -648,17 +678,17 @@ p_hsExpr' s = \case
648678 ub <- layoutToBraces <$> getLayout
649679 inci $
650680 sepSemi
651− (located' (ub . p_stmt' exprPlacement (p_hsExpr' S)))
681+ (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
652682 (unLoc es)
653− compBody = brackets N $ located es $ \xs -> do
683+ compBody = brackets N . located es $ \xs -> do
654684 let p_parBody =
655685 sep
656− (breakpoint >> txt "| ")
686+ (breakpoint >> txt "|" >> space)
657687 p_seqBody
658688 p_seqBody =
659689 sitcc
660690 . sep
661− (comma >> breakpoint)
691+ commaDel
662692 (located' (sitcc . p_stmt))
663693 stmts = init xs
664694 yield = last xs
@@ -679,8 +709,8 @@ p_hsExpr' s = \case
679709 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
680710 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
681711 ExplicitList _ _ xs ->
682− brackets s . sitcc $
683− sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs
712+ brackets s $
713+ sep commaDel (sitcc . located' p_hsExpr) xs
684714 RecordCon {..} -> do
685715 located rcon_con_name atom
686716 breakpoint
@@ -696,8 +726,8 @@ p_hsExpr' s = \case
696726 case rec_dotdot of
697727 Just {} -> [txt ".."]
698728 Nothing -> []
699− inci . braces N . sitcc $
700− sep (comma >> breakpoint) sitcc (fields <> dotdot)
729+ inci . braces N $
730+ sep commaDel sitcc (fields <> dotdot)
701731 RecordUpd {..} -> do
702732 located rupd_expr p_hsExpr
703733 useRecordDot' <- useRecordDot
@@ -715,9 +745,9 @@ p_hsExpr' s = \case
715745 Unambiguous _ n -> n
716746 XAmbiguousFieldOcc x -> noExtCon x
717747 }
718− inci . braces N . sitcc $
748+ inci . braces N $
719749 sep
720− (comma >> breakpoint)
750+ commaDel
721751 (sitcc . located' (p_hsRecField . updName))
722752 rupd_flds
723753 ExprWithTySig NoExtField x HsWC {hswc_body = HsIB {..}} -> sitcc $ do
@@ -730,22 +760,22 @@ p_hsExpr' s = \case
730760 ExprWithTySig NoExtField _ (XHsWildCardBndrs x) -> noExtCon x
731761 ArithSeq NoExtField _ x ->
732762 case x of
733− From from -> brackets s . sitcc $ do
763+ From from -> brackets s $ do
734764 located from p_hsExpr
735765 breakpoint
736766 txt ".."
737− FromThen from next -> brackets s . sitcc $ do
738− sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
767+ FromThen from next -> brackets s $ do
768+ sep commaDel (located' p_hsExpr) [from, next]
739769 breakpoint
740770 txt ".."
741− FromTo from to -> brackets s . sitcc $ do
771+ FromTo from to -> brackets s $ do
742772 located from p_hsExpr
743773 breakpoint
744774 txt ".."
745775 space
746776 located to p_hsExpr
747− FromThenTo from next to -> brackets s . sitcc $ do
748− 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]
749779 breakpoint
750780 txt ".."
751781 space
@@ -795,7 +825,7 @@ p_patSynBind PSB {..} = do
795825 breakpoint
796826 located psb_def p_pat
797827 ImplicitBidirectional -> do
798− txt "="
828+ equals
799829 breakpoint
800830 located psb_def p_pat
801831 ExplicitBidirectional mgroup -> do
@@ -822,8 +852,8 @@ p_patSynBind PSB {..} = do
822852 inci $ do
823853 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
824854 unless (null xs) breakpoint
825− braces N . sitcc $
826− sep (comma >> breakpoint) (p_rdrName . recordPatSynPatVar) xs
855+ braces N $
856+ sep commaDel (p_rdrName . recordPatSynPatVar) xs
827857 rhs
828858 InfixCon l r -> do
829859 switchLayout [getLoc l, getLoc r] $ do
@@ -877,10 +907,12 @@ p_if placer render if' then' else' = do
877907 breakpoint
878908 inci $ do
879909 txt "then"
910+ space
880911 located then' $ \x ->
881912 placeHanging (placer x) (render x)
882913 breakpoint
883914 txt "else"
915+ space
884916 located else' $ \x ->
885917 placeHanging (placer x) (render x)
886918
@@ -917,13 +949,13 @@ p_pat = \case
917949 txt "!"
918950 located pat p_pat
919951 ListPat NoExtField pats ->
920− brackets S . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats
952+ brackets S $ sep commaDel (located' p_pat) pats
921953 TuplePat NoExtField pats boxing -> do
922− let f =
954+ let parens' =
923955 case boxing of
924956 Boxed -> parens S
925957 Unboxed -> parensHash S
926− f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats
958+ parens' $ sep commaDel (sitcc . located' p_pat) pats
927959 SumPat NoExtField pat tag arity ->
928960 p_unboxedSum S tag arity (located pat p_pat)
929961 ConPatIn pat details ->
@@ -939,7 +971,7 @@ p_pat = \case
939971 let f = \case
940972 Nothing -> txt ".."
941973 Just x -> located x p_pat_hsRecField
942− inci . braces N . sitcc . sep (comma >> breakpoint) f $
974+ inci . braces N . sep commaDel f $
943975 case dotdot of
944976 Nothing -> Just <$> fields
945977 Just (L _ n) -> (Just <$> take n fields) ++ [Nothing]
@@ -980,7 +1012,7 @@ p_pat_hsRecField HsRecField {..} = do
9801012 p_rdrName (rdrNameFieldOcc x)
9811013 unless hsRecPun $ do
9821014 space
983− txt "="
1015+ equals
9841016 breakpoint
9851017 inci (located hsRecFieldArg p_pat)
9861018
@@ -989,17 +1021,15 @@ p_unboxedSum s tag arity m = do
9891021 let before = tag - 1
9901022 after = arity - before - 1
9911023 args = replicate before Nothing <> [Just m] <> replicate after Nothing
992− f (x, i) = do
993− let isFirst = i == 0
994− isLast = i == arity - 1
1024+ f x =
9951025 case x :: Maybe (R ()) of
9961026 Nothing ->
997− unless (isFirst || isLast) space
1027+ space
9981028 Just m' -> do
999− unless isFirst space
1029+ space
10001030 m'
1001− unless isLast space
1002− parensHash s $ sep (txt "|") f (zip args [0 ..])
1031+ space
1032+ parensHash s $ sep (txt "|") f args
10031033
10041034p_hsSplice :: HsSplice GhcPs -> R ()
10051035p_hsSplice = \case
@@ -1201,11 +1231,10 @@ exprPlacement = \case
12011231 HsCase NoExtField _ _ -> Hanging
12021232 HsDo NoExtField DoExpr _ -> Hanging
12031233 HsDo NoExtField MDoExpr _ -> Hanging
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)
1234+ OpApp NoExtField _ op y ->
1235+ case (fmap getOpNameStr . getOpName . unLoc) op of
1236+ Just "$" -> exprPlacement (unLoc y)
1237+ _ -> Normal
12091238 HsApp NoExtField _ y -> exprPlacement (unLoc y)
12101239 HsProc NoExtField p _ ->
12111240 -- Indentation breaks if pattern is longer than one line and left
@@ -1231,15 +1260,16 @@ getOpName = \case
12311260 HsVar NoExtField (L _ a) -> Just a
12321261 _ -> Nothing
12331262
1263+getOpNameStr :: RdrName -> String
1264+getOpNameStr = occNameString . rdrNameOcc
1265+
12341266p_exprOpTree ::
1235− -- | Can use special handling of dollar?
1236− Bool ->
12371267 -- | Bracket style to use
12381268 BracketStyle ->
12391269 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
12401270 R ()
1241−p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1242−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
12431273 -- If the beginning of the first argument and the second argument are on
12441274 -- the same line, and the second argument has a hanging form, use hanging
12451275 -- placement.
@@ -1261,44 +1291,54 @@ p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
12611291 MultiLine -> case placement of
12621292 Hanging -> useBraces
12631293 Normal -> dontUseBraces
1264− gotDollar = case getOpName (unLoc op) of
1265− Just rname -> mkVarOcc "$" == rdrNameOcc rname
1266− _ -> False
1294+ opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1295+ gotDollar = opNameStr == Just "$"
1296+ gotColon = opNameStr == Just ":"
1297+ gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
12671298 lhs =
12681299 switchLayout [opTreeLoc x] $
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)
1300+ p_exprOpTree s x
1301+ p_op = located op (opWrapper . p_hsExpr)
1302+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree N y)
12721303 isSection = case (opTreeLoc x, getLoc op) of
12731304 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
12741305 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
12751306 _ -> False
1307+ isDoBlock = \case
1308+ OpNode (L _ HsDo {}) -> True
1309+ _ -> False
12761310 useRecordDot' <- useRecordDot
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
1311+ if
1312+ | gotColon -> do
1313+ lhs
1314+ space
1315+ p_op
1316+ case placement of
1317+ Hanging -> do
13001318 space
13011319 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
13021342
13031343-- | Return 'True' if given expression is a record-dot operator expression.
13041344isRecordDot ::
@@ -1309,14 +1349,10 @@ isRecordDot ::
13091349 Bool
13101350isRecordDot op (RealSrcSpan ySpan) = case op of
13111351 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1312− isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1352+ (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
13131353 _ -> False
13141354isRecordDot _ _ = False
13151355
1316−-- | Check whether a given 'RdrName' is the dot operator.
1317−isDot :: RdrName -> Bool
1318−isDot name = rdrNameOcc name == mkVarOcc "."
1319−
13201356-- | Get annotations for the enclosing element.
13211357getEnclosingAnns :: R [AnnKeywordId]
13221358getEnclosingAnns = do