on master help sign in
Keep the ($) operator on the same line in certain cases
397fc55mrkkrp
data/examples/declaration/value/function/infix/applicative.hs−2
@@ -1,6 +1,4 @@
1−
2f =
1f =
3 Foo <$> bar
2 Foo <$> bar
4 <*> baz
3 <*> baz
5 <*> baz'
4 <*> baz'
6−
data/examples/declaration/value/function/infix/comments-out.hs+3−5
@@ -1,10 +1,8 @@
1main =
1main =
2− bar
2+ bar $ -- bar
3− $ baz -- bar
4− -- baz
3+ baz -- baz
5
4
6−bar
5+bar $
7− $
8 {- foo
6 {- foo
9 -}
7 -}
10 bar
8 bar
data/examples/declaration/value/function/infix/dollar-chains-out.hsnew file+18
@@ -0,0 +1,18 @@
1+module Main where
2+
3+foo =
4+ fmap escapeLeadingDollar
5+ . dropPaddingSpace
6+ . dropWhileEnd T.null
7+ . fmap (T.stripEnd . T.pack)
8+ . lines
9+ $ unpackHDS docStr
10+
11+foo =
12+ when (GHC.xopt Cpp dynFlags && not cfgTolerateCpp) $
13+ throwIO (OrmoluCppEnabled path)
14+
15+foo =
16+ bar
17+ $ baz
18+ $ quux
data/examples/declaration/value/function/infix/dollar-chains.hsnew file+18
@@ -0,0 +1,18 @@
1+module Main where
2+
3+foo =
4+ fmap escapeLeadingDollar
5+ . dropPaddingSpace
6+ . dropWhileEnd T.null
7+ . fmap (T.stripEnd . T.pack)
8+ . lines
9+ $ unpackHDS docStr
10+
11+foo =
12+ when (GHC.xopt Cpp dynFlags && not cfgTolerateCpp) $
13+ throwIO (OrmoluCppEnabled path)
14+
15+foo =
16+ bar
17+ $ baz
18+ $ quux
data/examples/declaration/value/function/infix/lenses-out.hs+2−2
@@ -1,6 +1,6 @@
1lenses =
1lenses =
2− Just $ M.fromList
2+ Just $ M.fromList $
3− $ "type" .= ("user.connection" :: Text)
3+ "type" .= ("user.connection" :: Text)
4 # "connection" .= uc
4 # "connection" .= uc
5 # "user" .= case name of
5 # "user" .= case name of
6 Just n -> Just $ object ["name" .= n]
6 Just n -> Just $ object ["name" .= n]
data/examples/declaration/value/function/lambda-multi-line1-out.hs+3−3
@@ -9,7 +9,7 @@ bar x = \y ->
9 else 12
9 else 12
10
10
11foo =
11foo =
12− prop "is inverse to closure"
12+ prop "is inverse to closure" $
13− $ \(f :: StaticPtr (Int -> Int))
13+ \(f :: StaticPtr (Int -> Int))
14− (x :: Int) ->
14+ (x :: Int) ->
15 (unclosure . closure) f x == deRefStaticPtr f x
15 (unclosure . closure) f x == deRefStaticPtr f x
data/examples/declaration/value/function/lambda-multi-line2-out.hs+4−4
@@ -3,10 +3,10 @@ tricky0 =
3 canUnify poly_given_ok wt gt || go False wt gt
3 canUnify poly_given_ok wt gt || go False wt gt
4
4
5tricky1 =
5tricky1 =
6− flip all (zip ws gs)
6+ flip all (zip ws gs) $
7− $ \(wt, gt) -> canUnify poly_given_ok wt gt || go False wt gt
7+ \(wt, gt) -> canUnify poly_given_ok wt gt || go False wt gt
8
8
9tricky2 =
9tricky2 =
10− flip all (zip ws gs)
10+ flip all (zip ws gs) $
11− $ \(wt, gt) ->
11+ \(wt, gt) ->
12 canUnify poly_given_ok wt gt || go False wt gt
12 canUnify poly_given_ok wt gt || go False wt gt
data/examples/declaration/value/function/let-multi-line-out.hs+3−3
@@ -23,6 +23,6 @@ implicitParams =
23 in foo cs
23 in foo cs
24
24
25sitting =
25sitting =
26− foo
26+ foo $
27− $ let x = 20
27+ let x = 20
28− in x
28+ in x
src/Ormolu/Printer/Meat/Declaration/Value.hs+33−14
@@ -21,17 +21,19 @@ import Data.Data hiding (Infix, Prefix)
21import Data.List (intersperse, sortOn)
21import Data.List (intersperse, sortOn)
22import Data.List.NonEmpty (NonEmpty ((:|)))
22import Data.List.NonEmpty (NonEmpty ((:|)))
23import Data.Text (Text)
23import Data.Text (Text)
24−import qualified Data.Text as Text
25import GHC
24import GHC
25+import OccName (mkVarOcc)
26import Ormolu.Printer.Combinators
26import Ormolu.Printer.Combinators
27import Ormolu.Printer.Meat.Common
27import Ormolu.Printer.Meat.Common
28import Ormolu.Printer.Meat.Declaration.Signature
28import Ormolu.Printer.Meat.Declaration.Signature
29import Ormolu.Printer.Meat.Type
29import Ormolu.Printer.Meat.Type
30import Ormolu.Printer.Operators
30import Ormolu.Printer.Operators
31import Ormolu.Utils
31import Ormolu.Utils
32+import RdrName (RdrName (..))
32import RdrName (rdrNameOcc)
33import RdrName (rdrNameOcc)
33import SrcLoc (combineSrcSpans, isOneLineSpan)
34import SrcLoc (combineSrcSpans, isOneLineSpan)
34import qualified Data.List.NonEmpty as NE
35import qualified Data.List.NonEmpty as NE
36+import qualified Data.Text as Text
35import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration
37import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration
36
38
37-- | Style of a group of equations.
39-- | Style of a group of equations.
@@ -549,10 +551,7 @@ p_hsExpr' s = \case
549 located (hswc_body a) p_hsType
551 located (hswc_body a) p_hsType
550 OpApp NoExt x op y -> do
552 OpApp NoExt x op y -> do
551 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
553 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)
556 NegApp NoExt e _ -> do
555 NegApp NoExt e _ -> do
557 txt "-"
556 txt "-"
558 space
557 space
@@ -1145,9 +1144,18 @@ exprOpTree :: LHsExpr GhcPs -> OpTree (LHsExpr GhcPs) (LHsExpr GhcPs)
1145exprOpTree (L _ (OpApp NoExt x op y)) = OpBranch (exprOpTree x) op (exprOpTree y)
1144exprOpTree (L _ (OpApp NoExt x op y)) = OpBranch (exprOpTree x) op (exprOpTree y)
1146exprOpTree n = OpNode n
1145exprOpTree n = OpNode n
1147
1146
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
1148−p_exprOpTree :: BracketStyle -> OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) -> R ()
1155+ -> OpTree (LHsExpr GhcPs) (LHsExpr GhcPs)
1156+ -> R ()
1149−p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1157+p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1150−p_exprOpTree s (OpBranch x op y) = do
1158+p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
1151 -- NOTE If the beginning of the first argument and the second argument
1159 -- NOTE If the beginning of the first argument and the second argument
1152 -- are on the same line, and the second argument has a hanging form, use
1160 -- are on the same line, and the second argument has a hanging form, use
1153 -- hanging placement.
1161 -- hanging placement.
@@ -1167,13 +1175,24 @@ p_exprOpTree s (OpBranch x op y) = do
1167 MultiLine -> case placement of
1175 MultiLine -> case placement of
1168 Hanging -> useBraces
1176 Hanging -> useBraces
1169 Normal -> dontUseBraces
1177 Normal -> dontUseBraces
1178+ gotDollar = case getOpName (unLoc op) of
1179+ Just rname -> mkVarOcc "$" == (rdrNameOcc rname)
1180+ _ -> False
1170 switchLayout [opTreeLoc x] $
1181 switchLayout [opTreeLoc x] $
1171− ub $ p_exprOpTree s x
1182+ ub $ p_exprOpTree (not gotDollar) s x
1172− placeHanging placement $ do
1173− located op (opWrapper . p_hsExpr)
1183+ let p_op = located op (opWrapper . p_hsExpr)
1174− space
1175− switchLayout [opTreeLoc y] $
1184+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
1176− p_exprOpTree 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
1177
1196
1178-- | Get annotations for the enclosing element.
1197-- | Get annotations for the enclosing element.
1179
1198