@@ -1,4 +1,5 @@
1{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE MultiWayIf #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE RecordWildCards #-}
4{-# LANGUAGE RecordWildCards #-}
4{-# LANGUAGE TypeApplications #-}
5{-# LANGUAGE TypeApplications #-}
@@ -21,12 +22,12 @@ import Data.Char (isPunctuation, isSymbol)
21import Data.Data hiding (Infix, Prefix)
22import Data.Data hiding (Infix, Prefix)
22import Data.Functor ((<&>))
23import Data.Functor ((<&>))
23import Data.List (intersperse, sortOn)
24import Data.List (intersperse, sortOn)
24−import Data.List.NonEmpty ((<|), NonEmpty (..))
25+import Data.List.NonEmpty (NonEmpty (..), (<|))
25import qualified Data.List.NonEmpty as NE
26import qualified Data.List.NonEmpty as NE
26import Data.Text (Text)
27import Data.Text (Text)
27import qualified Data.Text as Text
28import qualified Data.Text as Text
29−import OccName (mkVarOcc)
30+import OccName (occNameString)
30import Ormolu.Printer.Combinators
31import Ormolu.Printer.Combinators
31import Ormolu.Printer.Internal
32import Ormolu.Printer.Internal
32import Ormolu.Printer.Meat.Common
33import Ormolu.Printer.Meat.Common
@@ -60,7 +61,7 @@ data Placement
60 -- should use it and avoid bumping one level
61 -- should use it and avoid bumping one level
61 -- of indentation
62 -- of indentation
63− deriving (Eq)
64+ deriving (Eq, Show)
65p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
66p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
66p_valDecl = \case
67p_valDecl = \case
@@ -179,25 +180,24 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
179 NoSrcStrict -> return ()
180 NoSrcStrict -> return ()
180 SrcStrict -> txt "!"
181 SrcStrict -> txt "!"
181 SrcLazy -> txt "~"
182 SrcLazy -> txt "~"
182− inci' <- case NE.nonEmpty m_pats of
183+ indentBody <- case NE.nonEmpty m_pats of
183− Nothing -> id <$ case style of
185+ False <$ case style of
184− Function name -> p_rdrName name
186+ Function name -> p_rdrName name
185− _ -> return ()
187+ _ -> return ()
186 Just ne_pats -> do
188 Just ne_pats -> do
187− let combinedSpans =
189+ let combinedSpans = case style of
190+ Function name -> combineSrcSpans (getLoc name) patSpans
188− combineSrcSpans' $
192+ patSpans = combineSrcSpans' (getLoc <$> ne_pats)
191− if isOneLineSpan combinedSpans
193+ indentBody = not (isOneLineSpan combinedSpans)
194 switchLayout [combinedSpans] $ do
194 switchLayout [combinedSpans] $ do
195 let stdCase = sep breakpoint (located' p_pat) m_pats
195 let stdCase = sep breakpoint (located' p_pat) m_pats
196 case style of
196 case style of
197 Function name ->
197 Function name ->
198 p_infixDefHelper
198 p_infixDefHelper
200− inci'
200+ indentBody
201 (p_rdrName name)
201 (p_rdrName name)
202 (located' p_pat <$> m_pats)
202 (located' p_pat <$> m_pats)
203 PatternBind -> stdCase
203 PatternBind -> stdCase
@@ -212,7 +212,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
212 when needsSpace space
212 when needsSpace space
213 sitcc stdCase
213 sitcc stdCase
214 LambdaCase -> stdCase
214 LambdaCase -> stdCase
215− return inci'
215+ return indentBody
216 let -- Calculate position of end of patterns. This is useful when we decide
216 let -- Calculate position of end of patterns. This is useful when we decide
217 -- about putting certain constructions in hanging positions.
217 -- about putting certain constructions in hanging positions.
218 endOfPats = case NE.nonEmpty m_pats of
218 endOfPats = case NE.nonEmpty m_pats of
@@ -224,7 +224,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
224 Case -> True
224 Case -> True
225 LambdaCase -> True
225 LambdaCase -> True
226 _ -> False
226 _ -> False
227− let hasGuards = withGuards grhssGRHSs
227+ hasGuards = withGuards grhssGRHSs
228 grhssSpan =
228 grhssSpan =
229 combineSrcSpans' $
229 combineSrcSpans' $
230 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
230 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
@@ -253,7 +253,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
253 txt "where"
253 txt "where"
254 unless whereIsEmpty breakpoint
254 unless whereIsEmpty breakpoint
255 inci $ located grhssLocalBinds p_hsLocalBinds
255 inci $ located grhssLocalBinds p_hsLocalBinds
256− inci' $ do
256+ inciIf indentBody $ do
257 unless (length grhssGRHSs > 1) $
257 unless (length grhssGRHSs > 1) $
258 case style of
258 case style of
259 Function _ | hasGuards -> return ()
259 Function _ | hasGuards -> return ()
@@ -353,7 +353,14 @@ p_hsCmdTop = \case
353 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
353 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
354 XCmdTop x -> noExtCon x
354 XCmdTop x -> noExtCon x
356+-- | Render an expression preserving blank lines between such consecutive
357+-- expressions found in the original source code.
356−withSpacing :: Data a => (a -> R ()) -> Located a -> R ()
358+withSpacing ::
359+ -- | Rendering function
361+ -- | Entity to render
357withSpacing f l = located l $ \x -> do
364withSpacing f l = located l $ \x -> do
358 case getLoc l of
365 case getLoc l of
359 UnhelpfulSpan _ -> f x
366 UnhelpfulSpan _ -> f x
@@ -464,30 +471,24 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
464p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
471p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
465p_hsLocalBinds = \case
472p_hsLocalBinds = \case
466 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
473 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
469− (srcSpanStart . getLoc)
470− (srcSpanStart . getLoc)
472− (Left <$> bagToList bag) ++ (Right <$> lsigs)
473− p_item (Left x) = located x p_valDecl
474− p_item (Right x) = located x p_sigDecl
475 -- When in a single-line layout, there is a chance that the inner
474 -- When in a single-line layout, there is a chance that the inner
476 -- elements will also contain semicolons and they will confuse the
475 -- elements will also contain semicolons and they will confuse the
477 -- parser. so we request braces around every element except the last.
476 -- parser. so we request braces around every element except the last.
478 br <- layoutToBraces <$> getLayout
477 br <- layoutToBraces <$> getLayout
479− sitcc $
478+ let items =
480− sepSemi
479+ let injectLeft (L l x) = L l (Left x)
481− ( \(p, i) ->
480+ injectRight (L l x) = L l (Right x)
482− ( case p of
481+ in (injectLeft <$> bagToList bag) ++ (injectRight <$> lsigs)
482+ positionToBracing = \case
483− SinglePos -> id
483+ SinglePos -> id
484− FirstPos -> br
484+ FirstPos -> br
485− MiddlePos -> br
485+ MiddlePos -> br
486− LastPos -> id
486+ LastPos -> id
487− )
487+ p_item' (p, item) =
488− (p_item i)
488+ positionToBracing p $
489− )
489+ withSpacing (either p_valDecl p_sigDecl) item
490+ binds = sortOn (srcSpanStart . getLoc) items
490− (attachRelativePos $ sortOn ssStart items)
491+ sitcc $ sepSemi p_item' (attachRelativePos binds)
491 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
492 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
492 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
493 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
493 -- Second argument of IPBind is always Left before type-checking.
494 -- Second argument of IPBind is always Left before type-checking.
@@ -520,12 +521,6 @@ p_hsRecField HsRecField {..} = do
520 else Normal
521 else Normal
521 placeHanging placement (located hsRecFieldArg p_hsExpr)
522 placeHanging placement (located hsRecFieldArg p_hsExpr)
523−p_hsTupArg :: HsTupArg GhcPs -> R ()
525− Present NoExtField x -> located x p_hsExpr
526− Missing NoExtField -> pure ()
527− XTupArg x -> noExtCon x
529p_hsExpr :: HsExpr GhcPs -> R ()
524p_hsExpr :: HsExpr GhcPs -> R ()
530p_hsExpr = p_hsExpr' N
525p_hsExpr = p_hsExpr' N
@@ -589,23 +584,21 @@ p_hsExpr' s = \case
589 -- do-block or case expression it is not a good idea. It seems
584 -- do-block or case expression it is not a good idea. It seems
590 -- to be safe to always bump indentation when the function
585 -- to be safe to always bump indentation when the function
591 -- expression is parenthesised.
586 -- expression is parenthesised.
592− indent =
587+ doIndent =
593 case func of
588 case func of
594− L _ (HsPar NoExtField _) -> inci
589+ L _ (HsPar NoExtField _) -> True
595− L _ (HsAppType NoExtField _ _) -> inci
590+ L _ (HsAppType NoExtField _ _) -> True
596− L _ (HsMultiIf NoExtField _) -> inci
591+ L _ (HsMultiIf NoExtField _) -> True
598− if isOneLineSpan spn
592+ L spn _ -> isOneLineSpan spn
601− ub <- getLayout <&> \case
594+ getLayout <&> \case
602− SingleLine -> useBraces
595+ SingleLine -> useBraces
603− MultiLine -> id
596+ MultiLine -> id
605 located func (p_hsExpr' s)
598 located func (p_hsExpr' s)
606 breakpoint
599 breakpoint
607− indent $ sep breakpoint (located' p_hsExpr) initp
600+ inciIf doIndent $ sep breakpoint (located' p_hsExpr) initp
608− indent $ do
601+ inciIf doIndent $ do
609 unless (null initp) breakpoint
602 unless (null initp) breakpoint
610 located lastp p_hsExpr
603 located lastp p_hsExpr
611 Hanging -> do
604 Hanging -> do
@@ -620,10 +613,15 @@ p_hsExpr' s = \case
620 breakpoint
613 breakpoint
621 inci $ do
614 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
623 located (hswc_body a) p_hsType
621 located (hswc_body a) p_hsType
624 OpApp NoExtField x op y -> do
622 OpApp NoExtField x op y -> do
625 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
623 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
626− p_exprOpTree True s (reassociateOpTree getOpName opTree)
624+ p_exprOpTree s (reassociateOpTree getOpName opTree)
627 NegApp NoExtField e _ -> do
625 NegApp NoExtField e _ -> do
@@ -640,22 +638,27 @@ p_hsExpr' s = \case
640 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
638 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
641 unless (useRecordDot' && isRecordDot') breakpoint
639 unless (useRecordDot' && isRecordDot') breakpoint
642 inci (located x p_hsExpr)
640 inci (located x p_hsExpr)
643− ExplicitTuple NoExtField args boxity -> do
641+ ExplicitTuple NoExtField args boxity ->
644 let isSection = any (isMissing . unLoc) args
642 let isSection = any (isMissing . unLoc) args
645 isMissing = \case
643 isMissing = \case
646 Missing NoExtField -> True
644 Missing NoExtField -> True
647 _ -> False
645 _ -> 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
648− let parens' =
651+ parens' =
649 case boxity of
652 case boxity of
650 Boxed -> parens
653 Boxed -> parens
651 Unboxed -> parensHash
654 Unboxed -> parensHash
652− if isSection
655+ in if isSection
654− switchLayout [] . parens' s $
657+ switchLayout [] . parens' s $
655− sep comma (located' p_hsTupArg) args
658+ sep comma p_larg args
657− switchLayout (getLoc <$> args) . parens' s $
660+ switchLayout (getLoc <$> args) . parens' s $
658− sep commaDel (sitcc . located' p_hsTupArg) args
661+ sep commaDel p_larg args
659 ExplicitSum NoExtField tag arity e ->
662 ExplicitSum NoExtField tag arity e ->
660 p_unboxedSum N tag arity (located e p_hsExpr)
663 p_unboxedSum N tag arity (located e p_hsExpr)
661 HsCase NoExtField e mgroup ->
664 HsCase NoExtField e mgroup ->
@@ -677,7 +680,7 @@ p_hsExpr' s = \case
678 (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
681 (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
679 (unLoc es)
682 (unLoc es)
680− compBody = brackets N $ located es $ \xs -> do
683+ compBody = brackets N . located es $ \xs -> do
681 let p_parBody =
684 let p_parBody =
683 (breakpoint >> txt "|" >> space)
686 (breakpoint >> txt "|" >> space)
@@ -1228,11 +1231,10 @@ exprPlacement = \case
1228 HsCase NoExtField _ _ -> Hanging
1231 HsCase NoExtField _ _ -> Hanging
1229 HsDo NoExtField DoExpr _ -> Hanging
1232 HsDo NoExtField DoExpr _ -> Hanging
1230 HsDo NoExtField MDoExpr _ -> Hanging
1233 HsDo NoExtField MDoExpr _ -> Hanging
1231− -- If the rightmost expression in an operator chain is hanging, make the
1234+ OpApp NoExtField _ op y ->
1232− -- whole block hanging; so that we can use the common @f = foo $ do@
1235+ case (fmap getOpNameStr . getOpName . unLoc) op of
1234− OpApp NoExtField _ _ y -> exprPlacement (unLoc y)
1236+ Just "$" -> exprPlacement (unLoc y)
1235− -- Same thing for function applications (usually with -XBlockArguments)
1237+ _ -> Normal
1236 HsApp NoExtField _ y -> exprPlacement (unLoc y)
1238 HsApp NoExtField _ y -> exprPlacement (unLoc y)
1237 HsProc NoExtField p _ ->
1239 HsProc NoExtField p _ ->
1238 -- Indentation breaks if pattern is longer than one line and left
1240 -- Indentation breaks if pattern is longer than one line and left
@@ -1258,15 +1260,16 @@ getOpName = \case
1258 HsVar NoExtField (L _ a) -> Just a
1260 HsVar NoExtField (L _ a) -> Just a
1259 _ -> Nothing
1261 _ -> Nothing
1263+getOpNameStr :: RdrName -> String
1264+getOpNameStr = occNameString . rdrNameOcc
1261p_exprOpTree ::
1266p_exprOpTree ::
1262− -- | Can use special handling of dollar?
1264 -- | Bracket style to use
1267 -- | Bracket style to use
1265 BracketStyle ->
1268 BracketStyle ->
1266 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
1269 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
1268−p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1271+p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1269−p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
1272+p_exprOpTree s (OpBranch x op y) = do
1270 -- If the beginning of the first argument and the second argument are on
1273 -- If the beginning of the first argument and the second argument are on
1271 -- the same line, and the second argument has a hanging form, use hanging
1274 -- the same line, and the second argument has a hanging form, use hanging
1272 -- placement.
1275 -- placement.
@@ -1288,44 +1291,54 @@ p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
1288 MultiLine -> case placement of
1291 MultiLine -> case placement of
1289 Hanging -> useBraces
1292 Hanging -> useBraces
1290 Normal -> dontUseBraces
1293 Normal -> dontUseBraces
1291− gotDollar = case getOpName (unLoc op) of
1294+ opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1292− Just rname -> mkVarOcc "$" == rdrNameOcc rname
1295+ gotDollar = opNameStr == Just "$"
1293− _ -> False
1296+ gotColon = opNameStr == Just ":"
1297+ gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
1295 switchLayout [opTreeLoc x] $
1299 switchLayout [opTreeLoc x] $
1296− p_exprOpTree (not gotDollar) s x
1300+ p_exprOpTree s x
1297− let p_op = located op (opWrapper . p_hsExpr)
1301+ p_op = located op (opWrapper . p_hsExpr)
1298− p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
1302+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree N y)
1299 isSection = case (opTreeLoc x, getLoc op) of
1303 isSection = case (opTreeLoc x, getLoc op) of
1300 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
1304 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
1301 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
1305 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
1302 _ -> False
1306 _ -> False
1308+ OpNode (L _ HsDo {}) -> True
1303 useRecordDot' <- useRecordDot
1310 useRecordDot' <- useRecordDot
1304− let isRecordDot' = isRecordDot (unLoc op) (opTreeLoc y)
1311+ if
1305− if useRecordDot' && isRecordDot'
1312+ | gotColon -> do
1308− when isSection space
1316− && isOneLineSpan (opTreeLoc x)
1318− useBraces lhs
1313+ lhs
1321− breakpoint
1316+ case placement of
1322− inci p_y
1317+ Hanging -> do
1325− 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
1330-- | Return 'True' if given expression is a record-dot operator expression.
1343-- | Return 'True' if given expression is a record-dot operator expression.
1331isRecordDot ::
1344isRecordDot ::
@@ -1336,14 +1349,10 @@ isRecordDot ::
1337isRecordDot op (RealSrcSpan ySpan) = case op of
1350isRecordDot op (RealSrcSpan ySpan) = case op of
1338 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1351 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1339− isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1352+ (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1340 _ -> False
1353 _ -> False
1341isRecordDot _ _ = False
1354isRecordDot _ _ = False
1343−-- | Check whether a given 'RdrName' is the dot operator.
1344−isDot :: RdrName -> Bool
1345−isDot name = rdrNameOcc name == mkVarOcc "."
1347-- | Get annotations for the enclosing element.
1356-- | Get annotations for the enclosing element.
1348getEnclosingAnns :: R [AnnKeywordId]
1357getEnclosingAnns :: R [AnnKeywordId]
1349getEnclosingAnns = do
1358getEnclosingAnns = do