@@ -21,17 +21,19 @@ import Data.Data hiding (Infix, Prefix)
2121import Data.List (intersperse, sortOn)
2222import Data.List.NonEmpty (NonEmpty ((:|)))
2323import Data.Text (Text)
24−import qualified Data.Text as Text
2524import GHC
25+import OccName (mkVarOcc)
2626import Ormolu.Printer.Combinators
2727import Ormolu.Printer.Meat.Common
2828import Ormolu.Printer.Meat.Declaration.Signature
2929import Ormolu.Printer.Meat.Type
3030import Ormolu.Printer.Operators
3131import Ormolu.Utils
32+import RdrName (RdrName (..))
3233import RdrName (rdrNameOcc)
3334import SrcLoc (combineSrcSpans, isOneLineSpan)
3435import qualified Data.List.NonEmpty as NE
36+import qualified Data.Text as Text
3537import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration
3638
3739-- | Style of a group of equations.
@@ -549,10 +551,7 @@ p_hsExpr' s = \case
549551 located (hswc_body a) p_hsType
550552 OpApp NoExt x op y -> do
551553 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
552− getOpName = \case
553− HsVar NoExt (L _ a) -> Just a
554− _ -> Nothing
555− p_exprOpTree s (reassociateOpTree getOpName opTree)
554+ p_exprOpTree True s (reassociateOpTree getOpName opTree)
556555 NegApp NoExt e _ -> do
557556 txt "-"
558557 space
@@ -1145,9 +1144,18 @@ exprOpTree :: LHsExpr GhcPs -> OpTree (LHsExpr GhcPs) (LHsExpr GhcPs)
11451144exprOpTree (L _ (OpApp NoExt x op y)) = OpBranch (exprOpTree x) op (exprOpTree y)
11461145exprOpTree n = OpNode n
11471146
1148−p_exprOpTree :: BracketStyle -> OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) -> R ()
1149−p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1150−p_exprOpTree s (OpBranch x op y) = do
1147+getOpName :: HsExpr GhcPs -> Maybe RdrName
1148+getOpName = \case
1149+ HsVar NoExt (L _ a) -> Just a
1150+ _ -> Nothing
1151+
1152+p_exprOpTree
1153+ :: Bool -- ^ Can use special handling of dollar?
1154+ -> BracketStyle -- ^ Bracket style to use
1155+ -> OpTree (LHsExpr GhcPs) (LHsExpr GhcPs)
1156+ -> R ()
1157+p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1158+p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
11511159 -- NOTE If the beginning of the first argument and the second argument
11521160 -- are on the same line, and the second argument has a hanging form, use
11531161 -- hanging placement.
@@ -1167,13 +1175,24 @@ p_exprOpTree s (OpBranch x op y) = do
11671175 MultiLine -> case placement of
11681176 Hanging -> useBraces
11691177 Normal -> dontUseBraces
1178+ gotDollar = case getOpName (unLoc op) of
1179+ Just rname -> mkVarOcc "$" == (rdrNameOcc rname)
1180+ _ -> False
11701181 switchLayout [opTreeLoc x] $
1171− ub $ p_exprOpTree s x
1172− placeHanging placement $ do
1173− located op (opWrapper . p_hsExpr)
1174− space
1175− switchLayout [opTreeLoc y] $
1176− p_exprOpTree N y
1182+ ub $ p_exprOpTree (not gotDollar) s x
1183+ let p_op = located op (opWrapper . p_hsExpr)
1184+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
1185+ if isDollarSpecial && gotDollar && placement == Normal && isOneLineSpan (opTreeLoc x)
1186+ then do
1187+ space
1188+ p_op
1189+ breakpoint
1190+ inci p_y
1191+ else
1192+ placeHanging placement $ do
1193+ p_op
1194+ space
1195+ p_y
11771196
11781197-- | Get annotations for the enclosing element.
11791198