@@ -1,5 +1,4 @@
1{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE LambdaCase #-}
2−{-# LANGUAGE MultiWayIf #-}
3{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE TypeApplications #-}
4{-# LANGUAGE TypeApplications #-}
@@ -27,7 +26,7 @@ import qualified Data.List.NonEmpty as NE
27import Data.Text (Text)
26import Data.Text (Text)
28import qualified Data.Text as Text
27import qualified Data.Text as Text
30−import OccName (occNameString)
29+import OccName (mkVarOcc)
31import Ormolu.Printer.Combinators
30import Ormolu.Printer.Combinators
32import Ormolu.Printer.Internal
31import Ormolu.Printer.Internal
33import Ormolu.Printer.Meat.Common
32import Ormolu.Printer.Meat.Common
@@ -61,7 +60,7 @@ data Placement
61 -- should use it and avoid bumping one level
60 -- should use it and avoid bumping one level
62 -- of indentation
61 -- of indentation
64− deriving (Eq, Show)
63+ deriving (Eq)
66p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
65p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
67p_valDecl = \case
66p_valDecl = \case
@@ -181,15 +180,13 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
181 SrcStrict -> txt "!"
180 SrcStrict -> txt "!"
182 SrcLazy -> txt "~"
181 SrcLazy -> txt "~"
183 indentBody <- case NE.nonEmpty m_pats of
182 indentBody <- case NE.nonEmpty m_pats of
185− False <$ case style of
183+ Nothing -> False <$ case style of
186− Function name -> p_rdrName name
184+ Function name -> p_rdrName name
187− _ -> return ()
185+ _ -> return ()
188 Just ne_pats -> do
186 Just ne_pats -> do
189− let combinedSpans = case style of
187+ let combinedSpans =
190− Function name -> combineSrcSpans (getLoc name) patSpans
192− patSpans = combineSrcSpans' (getLoc <$> ne_pats)
188+ combineSrcSpans' $
193 indentBody = not (isOneLineSpan combinedSpans)
190 indentBody = not (isOneLineSpan combinedSpans)
194 switchLayout [combinedSpans] $ do
191 switchLayout [combinedSpans] $ do
195 let stdCase = sep breakpoint (located' p_pat) m_pats
192 let stdCase = sep breakpoint (located' p_pat) m_pats
@@ -353,14 +350,7 @@ p_hsCmdTop = \case
353 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
350 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
354 XCmdTop x -> noExtCon x
351 XCmdTop x -> noExtCon x
356−-- | Render an expression preserving blank lines between such consecutive
357−-- expressions found in the original source code.
358−withSpacing ::
353+withSpacing :: Data a => (a -> R ()) -> Located a -> R ()
359− -- | Rendering function
361− -- | Entity to render
364withSpacing f l = located l $ \x -> do
354withSpacing f l = located l $ \x -> do
365 case getLoc l of
355 case getLoc l of
366 UnhelpfulSpan _ -> f x
356 UnhelpfulSpan _ -> f x
@@ -471,24 +461,30 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
471p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
461p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
472p_hsLocalBinds = \case
462p_hsLocalBinds = \case
473 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
463 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
466+ (srcSpanStart . getLoc)
467+ (srcSpanStart . getLoc)
469+ (Left <$> bagToList bag) ++ (Right <$> lsigs)
470+ p_item (Left x) = located x p_valDecl
471+ p_item (Right x) = located x p_sigDecl
474 -- When in a single-line layout, there is a chance that the inner
472 -- When in a single-line layout, there is a chance that the inner
475 -- elements will also contain semicolons and they will confuse the
473 -- elements will also contain semicolons and they will confuse the
476 -- parser. so we request braces around every element except the last.
474 -- parser. so we request braces around every element except the last.
477 br <- layoutToBraces <$> getLayout
475 br <- layoutToBraces <$> getLayout
478− let items =
476+ sitcc $
479− let injectLeft (L l x) = L l (Left x)
477+ sepSemi
480− injectRight (L l x) = L l (Right x)
478+ ( \(p, i) ->
481− in (injectLeft <$> bagToList bag) ++ (injectRight <$> lsigs)
479+ ( case p of
482− positionToBracing = \case
483− SinglePos -> id
480+ SinglePos -> id
484− FirstPos -> br
481+ FirstPos -> br
485− MiddlePos -> br
482+ MiddlePos -> br
486− LastPos -> id
483+ LastPos -> id
487− p_item' (p, item) =
484+ )
488− positionToBracing p $
485+ (p_item i)
489− withSpacing (either p_valDecl p_sigDecl) item
486+ )
490− binds = sortOn (srcSpanStart . getLoc) items
487+ (attachRelativePos $ sortOn ssStart items)
491− sitcc $ sepSemi p_item' (attachRelativePos binds)
492 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
488 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
493 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
489 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
494 -- Second argument of IPBind is always Left before type-checking.
490 -- Second argument of IPBind is always Left before type-checking.
@@ -521,6 +517,12 @@ p_hsRecField HsRecField {..} = do
521 else Normal
517 else Normal
522 placeHanging placement (located hsRecFieldArg p_hsExpr)
518 placeHanging placement (located hsRecFieldArg p_hsExpr)
520+p_hsTupArg :: HsTupArg GhcPs -> R ()
522+ Present NoExtField x -> located x p_hsExpr
523+ Missing NoExtField -> pure ()
524+ XTupArg x -> noExtCon x
524p_hsExpr :: HsExpr GhcPs -> R ()
526p_hsExpr :: HsExpr GhcPs -> R ()
525p_hsExpr = p_hsExpr' N
527p_hsExpr = p_hsExpr' N
@@ -590,10 +592,9 @@ p_hsExpr' s = \case
590 L _ (HsAppType NoExtField _ _) -> True
592 L _ (HsAppType NoExtField _ _) -> True
591 L _ (HsMultiIf NoExtField _) -> True
593 L _ (HsMultiIf NoExtField _) -> True
592 L spn _ -> isOneLineSpan spn
594 L spn _ -> isOneLineSpan spn
594− getLayout <&> \case
595+ ub <- getLayout <&> \case
595− SingleLine -> useBraces
596+ SingleLine -> useBraces
596− MultiLine -> id
597+ MultiLine -> id
598 located func (p_hsExpr' s)
599 located func (p_hsExpr' s)
599 breakpoint
600 breakpoint
@@ -613,15 +614,10 @@ p_hsExpr' s = \case
613 breakpoint
614 breakpoint
614 inci $ do
615 inci $ do
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
621 located (hswc_body a) p_hsType
617 located (hswc_body a) p_hsType
622 OpApp NoExtField x op y -> do
618 OpApp NoExtField x op y -> do
623 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
619 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
624− p_exprOpTree s (reassociateOpTree getOpName opTree)
620+ p_exprOpTree True s (reassociateOpTree getOpName opTree)
625 NegApp NoExtField e _ -> do
621 NegApp NoExtField e _ -> do
@@ -638,27 +634,22 @@ p_hsExpr' s = \case
638 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
634 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
639 unless (useRecordDot' && isRecordDot') breakpoint
635 unless (useRecordDot' && isRecordDot') breakpoint
640 inci (located x p_hsExpr)
636 inci (located x p_hsExpr)
641− ExplicitTuple NoExtField args boxity ->
637+ ExplicitTuple NoExtField args boxity -> do
642 let isSection = any (isMissing . unLoc) args
638 let isSection = any (isMissing . unLoc) args
643 isMissing = \case
639 isMissing = \case
644 Missing NoExtField -> True
640 Missing NoExtField -> True
645 _ -> False
641 _ -> False
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' =
642+ let parens' =
652 case boxity of
643 case boxity of
653 Boxed -> parens
644 Boxed -> parens
654 Unboxed -> parensHash
645 Unboxed -> parensHash
655− in if isSection
646+ if isSection
657− switchLayout [] . parens' s $
648+ switchLayout [] . parens' s $
658− sep comma p_larg args
649+ sep comma (located' p_hsTupArg) args
660− switchLayout (getLoc <$> args) . parens' s $
651+ switchLayout (getLoc <$> args) . parens' s $
661− sep commaDel p_larg args
652+ sep commaDel (sitcc . located' p_hsTupArg) args
662 ExplicitSum NoExtField tag arity e ->
653 ExplicitSum NoExtField tag arity e ->
663 p_unboxedSum N tag arity (located e p_hsExpr)
654 p_unboxedSum N tag arity (located e p_hsExpr)
664 HsCase NoExtField e mgroup ->
655 HsCase NoExtField e mgroup ->
@@ -680,7 +671,7 @@ p_hsExpr' s = \case
681 (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
672 (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
682 (unLoc es)
673 (unLoc es)
683− compBody = brackets N . located es $ \xs -> do
674+ compBody = brackets N $ located es $ \xs -> do
684 let p_parBody =
675 let p_parBody =
686 (breakpoint >> txt "|" >> space)
677 (breakpoint >> txt "|" >> space)
@@ -1231,10 +1222,11 @@ exprPlacement = \case
1231 HsCase NoExtField _ _ -> Hanging
1222 HsCase NoExtField _ _ -> Hanging
1232 HsDo NoExtField DoExpr _ -> Hanging
1223 HsDo NoExtField DoExpr _ -> Hanging
1233 HsDo NoExtField MDoExpr _ -> Hanging
1224 HsDo NoExtField MDoExpr _ -> Hanging
1234− OpApp NoExtField _ op y ->
1225+ -- If the rightmost expression in an operator chain is hanging, make the
1235− case (fmap getOpNameStr . getOpName . unLoc) op of
1226+ -- whole block hanging; so that we can use the common @f = foo $ do@
1236− Just "$" -> exprPlacement (unLoc y)
1228+ OpApp NoExtField _ _ y -> exprPlacement (unLoc y)
1237− _ -> Normal
1229+ -- Same thing for function applications (usually with -XBlockArguments)
1238 HsApp NoExtField _ y -> exprPlacement (unLoc y)
1230 HsApp NoExtField _ y -> exprPlacement (unLoc y)
1239 HsProc NoExtField p _ ->
1231 HsProc NoExtField p _ ->
1240 -- Indentation breaks if pattern is longer than one line and left
1232 -- Indentation breaks if pattern is longer than one line and left
@@ -1260,16 +1252,15 @@ getOpName = \case
1260 HsVar NoExtField (L _ a) -> Just a
1252 HsVar NoExtField (L _ a) -> Just a
1261 _ -> Nothing
1253 _ -> Nothing
1263−getOpNameStr :: RdrName -> String
1264−getOpNameStr = occNameString . rdrNameOcc
1266p_exprOpTree ::
1255p_exprOpTree ::
1256+ -- | Can use special handling of dollar?
1267 -- | Bracket style to use
1258 -- | Bracket style to use
1268 BracketStyle ->
1259 BracketStyle ->
1269 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
1260 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
1271−p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1262+p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1272−p_exprOpTree s (OpBranch x op y) = do
1263+p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
1273 -- If the beginning of the first argument and the second argument are on
1264 -- If the beginning of the first argument and the second argument are on
1274 -- the same line, and the second argument has a hanging form, use hanging
1265 -- the same line, and the second argument has a hanging form, use hanging
1275 -- placement.
1266 -- placement.
@@ -1291,54 +1282,44 @@ p_exprOpTree s (OpBranch x op y) = do
1291 MultiLine -> case placement of
1282 MultiLine -> case placement of
1292 Hanging -> useBraces
1283 Hanging -> useBraces
1293 Normal -> dontUseBraces
1284 Normal -> dontUseBraces
1294− opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1285+ gotDollar = case getOpName (unLoc op) of
1295− gotDollar = opNameStr == Just "$"
1286+ Just rname -> mkVarOcc "$" == rdrNameOcc rname
1296− gotColon = opNameStr == Just ":"
1287+ _ -> False
1297− gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
1299 switchLayout [opTreeLoc x] $
1289 switchLayout [opTreeLoc x] $
1300− p_exprOpTree s x
1290+ p_exprOpTree (not gotDollar) s x
1301− p_op = located op (opWrapper . p_hsExpr)
1291+ let p_op = located op (opWrapper . p_hsExpr)
1302− p_y = switchLayout [opTreeLoc y] (p_exprOpTree N y)
1292+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
1303 isSection = case (opTreeLoc x, getLoc op) of
1293 isSection = case (opTreeLoc x, getLoc op) of
1304 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
1294 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
1305 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
1295 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
1306 _ -> False
1296 _ -> False
1308− OpNode (L _ HsDo {}) -> True
1310 useRecordDot' <- useRecordDot
1297 useRecordDot' <- useRecordDot
1311− if
1298+ let isRecordDot' = isRecordDot (unLoc op) (opTreeLoc y)
1312− | gotColon -> do
1299+ if useRecordDot' && isRecordDot'
1302+ when isSection space
1310+ && isOneLineSpan (opTreeLoc x)
1316− case placement of
1315+ breakpoint
1317− Hanging -> do
1316+ inci p_y
1319+ placeHanging placement $ do
1322− inciIf (isDoBlock y) p_y
1324− && isOneLineSpan (opTreeLoc x)
1325− && placement == Normal -> do
1331− | useRecordDot' && gotRecordDot -> do
1333− when isSection space
1338− placeHanging placement $ do
1343-- | Return 'True' if given expression is a record-dot operator expression.
1324-- | Return 'True' if given expression is a record-dot operator expression.
1344isRecordDot ::
1325isRecordDot ::
@@ -1349,10 +1330,14 @@ isRecordDot ::
1350isRecordDot op (RealSrcSpan ySpan) = case op of
1331isRecordDot op (RealSrcSpan ySpan) = case op of
1351 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1332 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1352− (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1333+ isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1353 _ -> False
1334 _ -> False
1354isRecordDot _ _ = False
1335isRecordDot _ _ = False
1337+-- | Check whether a given 'RdrName' is the dot operator.
1338+isDot :: RdrName -> Bool
1339+isDot name = rdrNameOcc name == mkVarOcc "."
1356-- | Get annotations for the enclosing element.
1341-- | Get annotations for the enclosing element.
1357getEnclosingAnns :: R [AnnKeywordId]
1342getEnclosingAnns :: R [AnnKeywordId]
1358getEnclosingAnns = do
1343getEnclosingAnns = do