@@ -1,4 +1,5 @@
11{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE MultiWayIf #-}
23{-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE RecordWildCards #-}
45{-# LANGUAGE TypeApplications #-}
@@ -26,7 +27,7 @@ import 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
@@ -180,9 +181,10 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
180181 SrcStrict -> txt "!"
181182 SrcLazy -> txt "~"
182183 indentBody <- case NE.nonEmpty m_pats of
183− Nothing -> False <$ case style of
184− Function name -> p_rdrName name
185− _ -> return ()
184+ Nothing ->
185+ False <$ case style of
186+ Function name -> p_rdrName name
187+ _ -> return ()
186188 Just ne_pats -> do
187189 let combinedSpans =
188190 combineSrcSpans' $
@@ -592,9 +594,10 @@ p_hsExpr' s = \case
592594 L _ (HsAppType NoExtField _ _) -> True
593595 L _ (HsMultiIf NoExtField _) -> True
594596 L spn _ -> isOneLineSpan spn
595− ub <- getLayout <&> \case
596− SingleLine -> useBraces
597− MultiLine -> id
597+ ub <-
598+ getLayout <&> \case
599+ SingleLine -> useBraces
600+ MultiLine -> id
598601 ub $ do
599602 located func (p_hsExpr' s)
600603 breakpoint
@@ -617,7 +620,7 @@ p_hsExpr' s = \case
617620 located (hswc_body a) p_hsType
618621 OpApp NoExtField x op y -> do
619622 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
620− p_exprOpTree True s (reassociateOpTree getOpName opTree)
623+ p_exprOpTree s (reassociateOpTree getOpName opTree)
621624 NegApp NoExtField e _ -> do
622625 txt "-"
623626 space
@@ -671,7 +674,7 @@ p_hsExpr' s = \case
671674 sepSemi
672675 (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
673676 (unLoc es)
674− compBody = brackets N $ located es $ \xs -> do
677+ compBody = brackets N . located es $ \xs -> do
675678 let p_parBody =
676679 sep
677680 (breakpoint >> txt "|" >> space)
@@ -1222,11 +1225,10 @@ exprPlacement = \case
12221225 HsCase NoExtField _ _ -> Hanging
12231226 HsDo NoExtField DoExpr _ -> Hanging
12241227 HsDo NoExtField MDoExpr _ -> Hanging
1225− -- If the rightmost expression in an operator chain is hanging, make the
1226− -- whole block hanging; so that we can use the common @f = foo $ do@
1227− -- style.
1228− OpApp NoExtField _ _ y -> exprPlacement (unLoc y)
1229− -- Same thing for function applications (usually with -XBlockArguments)
1228+ OpApp NoExtField _ op y ->
1229+ case (fmap getOpNameStr . getOpName . unLoc) op of
1230+ Just "$" -> exprPlacement (unLoc y)
1231+ _ -> Normal
12301232 HsApp NoExtField _ y -> exprPlacement (unLoc y)
12311233 HsProc NoExtField p _ ->
12321234 -- Indentation breaks if pattern is longer than one line and left
@@ -1252,15 +1254,16 @@ getOpName = \case
12521254 HsVar NoExtField (L _ a) -> Just a
12531255 _ -> Nothing
12541256
1257+getOpNameStr :: RdrName -> String
1258+getOpNameStr = occNameString . rdrNameOcc
1259+
12551260p_exprOpTree ::
1256− -- | Can use special handling of dollar?
1257− Bool ->
12581261 -- | Bracket style to use
12591262 BracketStyle ->
12601263 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
12611264 R ()
1262−p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1263−p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
1265+p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1266+p_exprOpTree s (OpBranch x op y) = do
12641267 -- If the beginning of the first argument and the second argument are on
12651268 -- the same line, and the second argument has a hanging form, use hanging
12661269 -- placement.
@@ -1282,44 +1285,54 @@ p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
12821285 MultiLine -> case placement of
12831286 Hanging -> useBraces
12841287 Normal -> dontUseBraces
1285− gotDollar = case getOpName (unLoc op) of
1286− Just rname -> mkVarOcc "$" == rdrNameOcc rname
1287− _ -> False
1288+ opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1289+ gotDollar = opNameStr == Just "$"
1290+ gotColon = opNameStr == Just ":"
1291+ gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
12881292 lhs =
12891293 switchLayout [opTreeLoc x] $
1290− p_exprOpTree (not gotDollar) s x
1291− let p_op = located op (opWrapper . p_hsExpr)
1292− p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
1294+ p_exprOpTree s x
1295+ p_op = located op (opWrapper . p_hsExpr)
1296+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree N y)
12931297 isSection = case (opTreeLoc x, getLoc op) of
12941298 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
12951299 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
12961300 _ -> False
1301+ isDoBlock = \case
1302+ OpNode (L _ HsDo {}) -> True
1303+ _ -> False
12971304 useRecordDot' <- useRecordDot
1298− let isRecordDot' = isRecordDot (unLoc op) (opTreeLoc y)
1299− if useRecordDot' && isRecordDot'
1300− then do
1301− lhs
1302− when isSection space
1303− p_op
1304− p_y
1305− else
1306− if isDollarSpecial
1307− && gotDollar
1308− && placement
1309− == Normal
1310− && isOneLineSpan (opTreeLoc x)
1311− then do
1312− useBraces lhs
1313− space
1314− p_op
1315− breakpoint
1316− inci p_y
1317− else do
1318− ub lhs
1319− placeHanging placement $ do
1320− p_op
1305+ if
1306+ | gotColon -> do
1307+ lhs
1308+ space
1309+ p_op
1310+ case placement of
1311+ Hanging -> do
13211312 space
13221313 p_y
1314+ Normal -> do
1315+ breakpoint
1316+ inciIf (isDoBlock y) p_y
1317+ | gotDollar
1318+ && isOneLineSpan (opTreeLoc x)
1319+ && placement == Normal -> do
1320+ useBraces lhs
1321+ space
1322+ p_op
1323+ breakpoint
1324+ inci p_y
1325+ | useRecordDot' && gotRecordDot -> do
1326+ lhs
1327+ when isSection space
1328+ p_op
1329+ p_y
1330+ | otherwise -> do
1331+ ub lhs
1332+ placeHanging placement $ do
1333+ p_op
1334+ space
1335+ p_y
13231336
13241337-- | Return 'True' if given expression is a record-dot operator expression.
13251338isRecordDot ::
@@ -1330,14 +1343,10 @@ isRecordDot ::
13301343 Bool
13311344isRecordDot op (RealSrcSpan ySpan) = case op of
13321345 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1333− isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1346+ (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
13341347 _ -> False
13351348isRecordDot _ _ = False
13361349
1337−-- | Check whether a given 'RdrName' is the dot operator.
1338−isDot :: RdrName -> Bool
1339−isDot name = rdrNameOcc name == mkVarOcc "."
1340−
13411350-- | Get annotations for the enclosing element.
13421351getEnclosingAnns :: R [AnnKeywordId]
13431352getEnclosingAnns = do