@@ -1,5 +1,4 @@
11{-# LANGUAGE LambdaCase #-}
2−{-# LANGUAGE MultiWayIf #-}
32{-# LANGUAGE OverloadedStrings #-}
43{-# LANGUAGE RecordWildCards #-}
54{-# LANGUAGE TypeApplications #-}
@@ -22,12 +21,12 @@ import Data.Char (isPunctuation, isSymbol)
2221import Data.Data hiding (Infix, Prefix)
2322import Data.Functor ((<&>))
2423import Data.List (intersperse, sortOn)
25−import Data.List.NonEmpty (NonEmpty (..), (<|))
24+import Data.List.NonEmpty ((<|), NonEmpty (..))
2625import qualified Data.List.NonEmpty as NE
2726import Data.Text (Text)
2827import qualified Data.Text as Text
2928import GHC
30−import OccName (occNameString)
29+import OccName (mkVarOcc)
3130import Ormolu.Printer.Combinators
3231import Ormolu.Printer.Internal
3332import Ormolu.Printer.Meat.Common
@@ -61,7 +60,7 @@ data Placement
6160 -- should use it and avoid bumping one level
6261 -- of indentation
6362 Hanging
64− deriving (Eq, Show)
63+ deriving (Eq)
6564
6665p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
6766p_valDecl = \case
@@ -180,24 +179,25 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
180179 NoSrcStrict -> return ()
181180 SrcStrict -> txt "!"
182181 SrcLazy -> txt "~"
183− indentBody <- case NE.nonEmpty m_pats of
184− Nothing ->
185− False <$ case style of
186− Function name -> p_rdrName name
187− _ -> return ()
182+ inci' <- case NE.nonEmpty m_pats of
183+ Nothing -> id <$ case style of
184+ Function name -> p_rdrName name
185+ _ -> return ()
188186 Just ne_pats -> do
189− let combinedSpans = case style of
190− Function name -> combineSrcSpans (getLoc name) patSpans
191− _ -> patSpans
192− patSpans = combineSrcSpans' (getLoc <$> ne_pats)
193− indentBody = not (isOneLineSpan combinedSpans)
187+ let combinedSpans =
188+ combineSrcSpans' $
189+ getLoc <$> ne_pats
190+ inci' =
191+ if isOneLineSpan combinedSpans
192+ then id
193+ else inci
194194 switchLayout [combinedSpans] $ do
195195 let stdCase = sep breakpoint (located' p_pat) m_pats
196196 case style of
197197 Function name ->
198198 p_infixDefHelper
199199 isInfix
200− indentBody
200+ inci'
201201 (p_rdrName name)
202202 (located' p_pat <$> m_pats)
203203 PatternBind -> stdCase
@@ -212,7 +212,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
212212 when needsSpace space
213213 sitcc stdCase
214214 LambdaCase -> stdCase
215− return indentBody
215+ return inci'
216216 let -- Calculate position of end of patterns. This is useful when we decide
217217 -- about putting certain constructions in hanging positions.
218218 endOfPats = case NE.nonEmpty m_pats of
@@ -224,7 +224,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
224224 Case -> True
225225 LambdaCase -> True
226226 _ -> False
227− hasGuards = withGuards grhssGRHSs
227+ let hasGuards = withGuards grhssGRHSs
228228 grhssSpan =
229229 combineSrcSpans' $
230230 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
@@ -253,7 +253,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
253253 txt "where"
254254 unless whereIsEmpty breakpoint
255255 inci $ located grhssLocalBinds p_hsLocalBinds
256− inciIf indentBody $ do
256+ inci' $ do
257257 unless (length grhssGRHSs > 1) $
258258 case style of
259259 Function _ | hasGuards -> return ()
@@ -284,7 +284,7 @@ p_grhs' placer render style (GRHS NoExtField guards body) =
284284 xs -> do
285285 txt "|"
286286 space
287− sitcc (sep commaDel (sitcc . located' p_stmt) xs)
287+ sitcc (sep (comma >> breakpoint) (sitcc . located' p_stmt) xs)
288288 space
289289 inci $ case style of
290290 EqualSign -> equals
@@ -316,7 +316,7 @@ p_hsCmd = \case
316316 HsHigherOrderApp -> txt "-<<"
317317 placeHanging (exprPlacement (unLoc input)) $
318318 located input p_hsExpr
319− HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ do
319+ HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ sitcc $ do
320320 located form p_hsExpr
321321 unless (null cmds) $ do
322322 breakpoint
@@ -353,14 +353,7 @@ p_hsCmdTop = \case
353353 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
354354 XCmdTop x -> noExtCon x
355355
356−-- | Render an expression preserving blank lines between such consecutive
357−-- expressions found in the original source code.
358−withSpacing ::
359− -- | Rendering function
360− (a -> R ()) ->
361− -- | Entity to render
362− Located a ->
363− R ()
356+withSpacing :: Data a => (a -> R ()) -> Located a -> R ()
364357withSpacing f l = located l $ \x -> do
365358 case getLoc l of
366359 UnhelpfulSpan _ -> f x
@@ -471,24 +464,30 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
471464p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
472465p_hsLocalBinds = \case
473466 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
467+ let ssStart =
468+ either
469+ (srcSpanStart . getLoc)
470+ (srcSpanStart . getLoc)
471+ items =
472+ (Left <$> bagToList bag) ++ (Right <$> lsigs)
473+ p_item (Left x) = located x p_valDecl
474+ p_item (Right x) = located x p_sigDecl
474475 -- When in a single-line layout, there is a chance that the inner
475476 -- elements will also contain semicolons and they will confuse the
476477 -- parser. so we request braces around every element except the last.
477478 br <- layoutToBraces <$> getLayout
478− let items =
479− let injectLeft (L l x) = L l (Left x)
480− injectRight (L l x) = L l (Right x)
481− in (injectLeft <$> bagToList bag) ++ (injectRight <$> lsigs)
482− positionToBracing = \case
483− SinglePos -> id
484− FirstPos -> br
485− MiddlePos -> br
486− LastPos -> id
487− p_item' (p, item) =
488− positionToBracing p $
489− withSpacing (either p_valDecl p_sigDecl) item
490− binds = sortOn (srcSpanStart . getLoc) items
491− sitcc $ sepSemi p_item' (attachRelativePos binds)
479+ sitcc $
480+ sepSemi
481+ ( \(p, i) ->
482+ ( case p of
483+ SinglePos -> id
484+ FirstPos -> br
485+ MiddlePos -> br
486+ LastPos -> id
487+ )
488+ (p_item i)
489+ )
490+ (attachRelativePos $ sortOn ssStart items)
492491 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
493492 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
494493 -- Second argument of IPBind is always Left before type-checking.
@@ -521,6 +520,12 @@ p_hsRecField HsRecField {..} = do
521520 else Normal
522521 placeHanging placement (located hsRecFieldArg p_hsExpr)
523522
523+p_hsTupArg :: HsTupArg GhcPs -> R ()
524+p_hsTupArg = \case
525+ Present NoExtField x -> located x p_hsExpr
526+ Missing NoExtField -> pure ()
527+ XTupArg x -> noExtCon x
528+
524529p_hsExpr :: HsExpr GhcPs -> R ()
525530p_hsExpr = p_hsExpr' N
526531
@@ -584,21 +589,23 @@ p_hsExpr' s = \case
584589 -- do-block or case expression it is not a good idea. It seems
585590 -- to be safe to always bump indentation when the function
586591 -- expression is parenthesised.
587− doIndent =
592+ indent =
588593 case func of
589− L _ (HsPar NoExtField _) -> True
590− L _ (HsAppType NoExtField _ _) -> True
591− L _ (HsMultiIf NoExtField _) -> True
592− L spn _ -> isOneLineSpan spn
593− ub <-
594− getLayout <&> \case
595− SingleLine -> useBraces
596− MultiLine -> id
594+ L _ (HsPar NoExtField _) -> inci
595+ L _ (HsAppType NoExtField _ _) -> inci
596+ L _ (HsMultiIf NoExtField _) -> inci
597+ L spn _ ->
598+ if isOneLineSpan spn
599+ then inci
600+ else id
601+ ub <- getLayout <&> \case
602+ SingleLine -> useBraces
603+ MultiLine -> id
597604 ub $ do
598605 located func (p_hsExpr' s)
599606 breakpoint
600− inciIf doIndent $ sep breakpoint (located' p_hsExpr) initp
601− inciIf doIndent $ do
607+ indent $ sep breakpoint (located' p_hsExpr) initp
608+ indent $ do
602609 unless (null initp) breakpoint
603610 located lastp p_hsExpr
604611 Hanging -> do
@@ -613,15 +620,10 @@ p_hsExpr' s = \case
613620 breakpoint
614621 inci $ do
615622 txt "@"
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
620− _ -> return ()
621623 located (hswc_body a) p_hsType
622624 OpApp NoExtField x op y -> do
623625 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
624− p_exprOpTree s (reassociateOpTree getOpName opTree)
626+ p_exprOpTree True s (reassociateOpTree getOpName opTree)
625627 NegApp NoExtField e _ -> do
626628 txt "-"
627629 space
@@ -638,27 +640,22 @@ p_hsExpr' s = \case
638640 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
639641 unless (useRecordDot' && isRecordDot') breakpoint
640642 inci (located x p_hsExpr)
641− ExplicitTuple NoExtField args boxity ->
643+ ExplicitTuple NoExtField args boxity -> do
642644 let isSection = any (isMissing . unLoc) args
643645 isMissing = \case
644646 Missing NoExtField -> True
645647 _ -> False
646− p_arg = \case
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' =
648+ let parens' =
652649 case boxity of
653650 Boxed -> parens
654651 Unboxed -> parensHash
655− in if isSection
656− then
657− switchLayout [] . parens' s $
658− sep comma p_larg args
659− else
660− switchLayout (getLoc <$> args) . parens' s $
661− sep commaDel p_larg args
652+ if isSection
653+ then
654+ switchLayout [] . parens' s $
655+ sep comma (located' p_hsTupArg) args
656+ else
657+ switchLayout (getLoc <$> args) . parens' s . sitcc $
658+ sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
662659 ExplicitSum NoExtField tag arity e ->
663660 p_unboxedSum N tag arity (located e p_hsExpr)
664661 HsCase NoExtField e mgroup ->
@@ -668,7 +665,7 @@ p_hsExpr' s = \case
668665 HsMultiIf NoExtField guards -> do
669666 txt "if"
670667 breakpoint
671− inci . inci $ sep newline (located' (p_grhs RightArrow)) guards
668+ inci . inci . sitcc $ sep newline (located' (p_grhs RightArrow)) guards
672669 HsLet NoExtField localBinds e ->
673670 p_let p_hsExpr localBinds e
674671 HsDo NoExtField ctx es -> do
@@ -680,15 +677,15 @@ p_hsExpr' s = \case
680677 sepSemi
681678 (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
682679 (unLoc es)
683− compBody = brackets N . located es $ \xs -> do
680+ compBody = brackets N $ located es $ \xs -> do
684681 let p_parBody =
685682 sep
686− (breakpoint >> txt "|" >> space)
683+ (breakpoint >> txt "| ")
687684 p_seqBody
688685 p_seqBody =
689686 sitcc
690687 . sep
691− commaDel
688+ (comma >> breakpoint)
692689 (located' (sitcc . p_stmt))
693690 stmts = init xs
694691 yield = last xs
@@ -709,8 +706,8 @@ p_hsExpr' s = \case
709706 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
710707 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
711708 ExplicitList _ _ xs ->
712− brackets s $
713− sep commaDel (sitcc . located' p_hsExpr) xs
709+ brackets s . sitcc $
710+ sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs
714711 RecordCon {..} -> do
715712 located rcon_con_name atom
716713 breakpoint
@@ -726,8 +723,8 @@ p_hsExpr' s = \case
726723 case rec_dotdot of
727724 Just {} -> [txt ".."]
728725 Nothing -> []
729− inci . braces N $
730− sep commaDel sitcc (fields <> dotdot)
726+ inci . braces N . sitcc $
727+ sep (comma >> breakpoint) sitcc (fields <> dotdot)
731728 RecordUpd {..} -> do
732729 located rupd_expr p_hsExpr
733730 useRecordDot' <- useRecordDot
@@ -745,9 +742,9 @@ p_hsExpr' s = \case
745742 Unambiguous _ n -> n
746743 XAmbiguousFieldOcc x -> noExtCon x
747744 }
748− inci . braces N $
745+ inci . braces N . sitcc $
749746 sep
750− commaDel
747+ (comma >> breakpoint)
751748 (sitcc . located' (p_hsRecField . updName))
752749 rupd_flds
753750 ExprWithTySig NoExtField x HsWC {hswc_body = HsIB {..}} -> sitcc $ do
@@ -760,22 +757,22 @@ p_hsExpr' s = \case
760757 ExprWithTySig NoExtField _ (XHsWildCardBndrs x) -> noExtCon x
761758 ArithSeq NoExtField _ x ->
762759 case x of
763− From from -> brackets s $ do
760+ From from -> brackets s . sitcc $ do
764761 located from p_hsExpr
765762 breakpoint
766763 txt ".."
767− FromThen from next -> brackets s $ do
768− sep commaDel (located' p_hsExpr) [from, next]
764+ FromThen from next -> brackets s . sitcc $ do
765+ sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
769766 breakpoint
770767 txt ".."
771− FromTo from to -> brackets s $ do
768+ FromTo from to -> brackets s . sitcc $ do
772769 located from p_hsExpr
773770 breakpoint
774771 txt ".."
775772 space
776773 located to p_hsExpr
777− FromThenTo from next to -> brackets s $ do
778− sep commaDel (located' p_hsExpr) [from, next]
774+ FromThenTo from next to -> brackets s . sitcc $ do
775+ sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
779776 breakpoint
780777 txt ".."
781778 space
@@ -852,8 +849,8 @@ p_patSynBind PSB {..} = do
852849 inci $ do
853850 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
854851 unless (null xs) breakpoint
855− braces N $
856− sep commaDel (p_rdrName . recordPatSynPatVar) xs
852+ braces N . sitcc $
853+ sep (comma >> breakpoint) (p_rdrName . recordPatSynPatVar) xs
857854 rhs
858855 InfixCon l r -> do
859856 switchLayout [getLoc l, getLoc r] $ do
@@ -949,13 +946,13 @@ p_pat = \case
949946 txt "!"
950947 located pat p_pat
951948 ListPat NoExtField pats ->
952− brackets S $ sep commaDel (located' p_pat) pats
949+ brackets S . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats
953950 TuplePat NoExtField pats boxing -> do
954− let parens' =
951+ let f =
955952 case boxing of
956953 Boxed -> parens S
957954 Unboxed -> parensHash S
958− parens' $ sep commaDel (sitcc . located' p_pat) pats
955+ f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats
959956 SumPat NoExtField pat tag arity ->
960957 p_unboxedSum S tag arity (located pat p_pat)
961958 ConPatIn pat details ->
@@ -971,7 +968,7 @@ p_pat = \case
971968 let f = \case
972969 Nothing -> txt ".."
973970 Just x -> located x p_pat_hsRecField
974− inci . braces N . sep commaDel f $
971+ inci . braces N . sitcc . sep (comma >> breakpoint) f $
975972 case dotdot of
976973 Nothing -> Just <$> fields
977974 Just (L _ n) -> (Just <$> take n fields) ++ [Nothing]
@@ -1021,15 +1018,17 @@ p_unboxedSum s tag arity m = do
10211018 let before = tag - 1
10221019 after = arity - before - 1
10231020 args = replicate before Nothing <> [Just m] <> replicate after Nothing
1024− f x =
1021+ f (x, i) = do
1022+ let isFirst = i == 0
1023+ isLast = i == arity - 1
10251024 case x :: Maybe (R ()) of
10261025 Nothing ->
1027− space
1026+ unless (isFirst || isLast) space
10281027 Just m' -> do
1029− space
1028+ unless isFirst space
10301029 m'
1031− space
1032− parensHash s $ sep (txt "|") f args
1030+ unless isLast space
1031+ parensHash s $ sep (txt "|") f (zip args [0 ..])
10331032
10341033p_hsSplice :: HsSplice GhcPs -> R ()
10351034p_hsSplice = \case
@@ -1231,10 +1230,11 @@ exprPlacement = \case
12311230 HsCase NoExtField _ _ -> Hanging
12321231 HsDo NoExtField DoExpr _ -> Hanging
12331232 HsDo NoExtField MDoExpr _ -> Hanging
1234− OpApp NoExtField _ op y ->
1235− case (fmap getOpNameStr . getOpName . unLoc) op of
1236− Just "$" -> exprPlacement (unLoc y)
1237− _ -> Normal
1233+ -- If the rightmost expression in an operator chain is hanging, make the
1234+ -- whole block hanging; so that we can use the common @f = foo $ do@
1235+ -- style.
1236+ OpApp NoExtField _ _ y -> exprPlacement (unLoc y)
1237+ -- Same thing for function applications (usually with -XBlockArguments)
12381238 HsApp NoExtField _ y -> exprPlacement (unLoc y)
12391239 HsProc NoExtField p _ ->
12401240 -- Indentation breaks if pattern is longer than one line and left
@@ -1260,16 +1260,15 @@ getOpName = \case
12601260 HsVar NoExtField (L _ a) -> Just a
12611261 _ -> Nothing
12621262
1263−getOpNameStr :: RdrName -> String
1264−getOpNameStr = occNameString . rdrNameOcc
1265−
12661263p_exprOpTree ::
1264+ -- | Can use special handling of dollar?
1265+ Bool ->
12671266 -- | Bracket style to use
12681267 BracketStyle ->
12691268 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
12701269 R ()
1271−p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1272−p_exprOpTree s (OpBranch x op y) = do
1270+p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1271+p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
12731272 -- If the beginning of the first argument and the second argument are on
12741273 -- the same line, and the second argument has a hanging form, use hanging
12751274 -- placement.
@@ -1291,54 +1290,44 @@ p_exprOpTree s (OpBranch x op y) = do
12911290 MultiLine -> case placement of
12921291 Hanging -> useBraces
12931292 Normal -> dontUseBraces
1294− opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1295− gotDollar = opNameStr == Just "$"
1296− gotColon = opNameStr == Just ":"
1297− gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
1293+ gotDollar = case getOpName (unLoc op) of
1294+ Just rname -> mkVarOcc "$" == rdrNameOcc rname
1295+ _ -> False
12981296 lhs =
12991297 switchLayout [opTreeLoc x] $
1300− p_exprOpTree s x
1301− p_op = located op (opWrapper . p_hsExpr)
1302− p_y = switchLayout [opTreeLoc y] (p_exprOpTree N y)
1298+ p_exprOpTree (not gotDollar) s x
1299+ let p_op = located op (opWrapper . p_hsExpr)
1300+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
13031301 isSection = case (opTreeLoc x, getLoc op) of
13041302 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
13051303 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
13061304 _ -> False
1307− isDoBlock = \case
1308− OpNode (L _ HsDo {}) -> True
1309− _ -> False
13101305 useRecordDot' <- useRecordDot
1311− if
1312− | gotColon -> do
1313− lhs
1314− space
1315− p_op
1316− case placement of
1317− Hanging -> do
1306+ let isRecordDot' = isRecordDot (unLoc op) (opTreeLoc y)
1307+ if useRecordDot' && isRecordDot'
1308+ then do
1309+ lhs
1310+ when isSection space
1311+ p_op
1312+ p_y
1313+ else
1314+ if isDollarSpecial
1315+ && gotDollar
1316+ && placement
1317+ == Normal
1318+ && isOneLineSpan (opTreeLoc x)
1319+ then do
1320+ useBraces lhs
1321+ space
1322+ p_op
1323+ breakpoint
1324+ inci p_y
1325+ else do
1326+ ub lhs
1327+ placeHanging placement $ do
1328+ p_op
13181329 space
13191330 p_y
1320− Normal -> do
1321− breakpoint
1322− inciIf (isDoBlock y) p_y
1323− | gotDollar
1324− && isOneLineSpan (opTreeLoc x)
1325− && placement == Normal -> do
1326− useBraces lhs
1327− space
1328− p_op
1329− breakpoint
1330− inci p_y
1331− | useRecordDot' && gotRecordDot -> do
1332− lhs
1333− when isSection space
1334− p_op
1335− p_y
1336− | otherwise -> do
1337− ub lhs
1338− placeHanging placement $ do
1339− p_op
1340− space
1341− p_y
13421331
13431332-- | Return 'True' if given expression is a record-dot operator expression.
13441333isRecordDot ::
@@ -1349,10 +1338,14 @@ isRecordDot ::
13491338 Bool
13501339isRecordDot op (RealSrcSpan ySpan) = case op of
13511340 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1352− (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1341+ isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
13531342 _ -> False
13541343isRecordDot _ _ = False
13551344
1345+-- | Check whether a given 'RdrName' is the dot operator.
1346+isDot :: RdrName -> Bool
1347+isDot name = rdrNameOcc name == mkVarOcc "."
1348+
13561349-- | Get annotations for the enclosing element.
13571350getEnclosingAnns :: R [AnnKeywordId]
13581351getEnclosingAnns = do