@@ -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
@@ -35,8 +36,6 @@ import Ormolu.Printer.Meat.Declaration.Signature
35import Ormolu.Printer.Meat.Type
36import Ormolu.Printer.Meat.Type
36import Ormolu.Printer.Operators
37import Ormolu.Printer.Operators
37import Ormolu.Utils
38import Ormolu.Utils
38−import RdrName (RdrName (..), rdrNameOcc)
39−import SrcLoc (combineSrcSpans, isOneLineSpan)
41-- | Style of a group of equations.
40-- | Style of a group of equations.
42data MatchGroupStyle
41data MatchGroupStyle
@@ -62,7 +61,7 @@ data Placement
62 -- should use it and avoid bumping one level
61 -- should use it and avoid bumping one level
63 -- of indentation
62 -- of indentation
65− deriving (Eq)
64+ deriving (Eq, Show)
67p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
66p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
68p_valDecl = \case
67p_valDecl = \case
@@ -181,25 +180,24 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
181 NoSrcStrict -> return ()
180 NoSrcStrict -> return ()
182 SrcStrict -> txt "!"
181 SrcStrict -> txt "!"
183 SrcLazy -> txt "~"
182 SrcLazy -> txt "~"
184− inci' <- case NE.nonEmpty m_pats of
183+ indentBody <- case NE.nonEmpty m_pats of
185− Nothing -> id <$ case style of
185+ False <$ case style of
186− Function name -> p_rdrName name
186+ Function name -> p_rdrName name
187− _ -> return ()
187+ _ -> return ()
188 Just ne_pats -> do
188 Just ne_pats -> do
189− let combinedSpans =
189+ let combinedSpans = case style of
190+ Function name -> combineSrcSpans (getLoc name) patSpans
190− combineSrcSpans' $
192+ patSpans = combineSrcSpans' (getLoc <$> ne_pats)
193− if isOneLineSpan combinedSpans
193+ indentBody = not (isOneLineSpan combinedSpans)
196 switchLayout [combinedSpans] $ do
194 switchLayout [combinedSpans] $ do
197 let stdCase = sep breakpoint (located' p_pat) m_pats
195 let stdCase = sep breakpoint (located' p_pat) m_pats
198 case style of
196 case style of
199 Function name ->
197 Function name ->
200 p_infixDefHelper
198 p_infixDefHelper
202− inci'
200+ indentBody
203 (p_rdrName name)
201 (p_rdrName name)
204 (located' p_pat <$> m_pats)
202 (located' p_pat <$> m_pats)
205 PatternBind -> stdCase
203 PatternBind -> stdCase
@@ -214,40 +212,32 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
214 when needsSpace space
212 when needsSpace space
215 sitcc stdCase
213 sitcc stdCase
216 LambdaCase -> stdCase
214 LambdaCase -> stdCase
217− return inci'
215+ return indentBody
218 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
219 -- about putting certain constructions in hanging positions.
217 -- about putting certain constructions in hanging positions.
220 endOfPats = case NE.nonEmpty m_pats of
218 endOfPats = case NE.nonEmpty m_pats of
221 Nothing -> case style of
219 Nothing -> case style of
222− Function name -> (Just . srcSpanEnd . getLoc) name
220+ Function name -> Just (getLoc name)
223 _ -> Nothing
221 _ -> Nothing
224− Just pats -> (Just . srcSpanEnd . getLoc . NE.last) pats
222+ Just pats -> (Just . getLoc . NE.last) pats
225 isCase = \case
223 isCase = \case
226 Case -> True
224 Case -> True
227 LambdaCase -> True
225 LambdaCase -> True
228 _ -> False
226 _ -> False
229− let hasGuards = withGuards grhssGRHSs
227+ hasGuards = withGuards grhssGRHSs
230− unless (length grhssGRHSs > 1) $
232− Function _ | hasGuards -> return ()
233− Function _ -> space >> txt "="
234− PatternBind -> space >> txt "="
235− s | isCase s && hasGuards -> return ()
236− _ -> space >> txt "->"
237− let grhssSpan =
228+ grhssSpan =
238 combineSrcSpans' $
229 combineSrcSpans' $
239 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
230 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
240 patGrhssSpan =
231 patGrhssSpan =
242 grhssSpan
233 grhssSpan
243− (combineSrcSpans grhssSpan . srcLocSpan)
234+ (combineSrcSpans grhssSpan . srcLocSpan . srcSpanEnd)
244 endOfPats
235 endOfPats
245 placement =
236 placement =
246 case endOfPats of
237 case endOfPats of
247 Nothing -> blockPlacement placer grhssGRHSs
238 Nothing -> blockPlacement placer grhssGRHSs
248 Just spn ->
239 Just spn ->
250− (mkSrcSpan spn (srcSpanStart grhssSpan))
240+ if onTheSameLine spn grhssSpan
251 then blockPlacement placer grhssGRHSs
241 then blockPlacement placer grhssGRHSs
252 else Normal
242 else Normal
253 p_body = do
243 p_body = do
@@ -263,7 +253,14 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
263 txt "where"
253 txt "where"
264 unless whereIsEmpty breakpoint
254 unless whereIsEmpty breakpoint
265 inci $ located grhssLocalBinds p_hsLocalBinds
255 inci $ located grhssLocalBinds p_hsLocalBinds
266− inci' $ do
256+ inciIf indentBody $ do
257+ unless (length grhssGRHSs > 1) $
259+ Function _ | hasGuards -> return ()
260+ Function _ -> space >> inci equals
261+ PatternBind -> space >> inci equals
262+ s | isCase s && hasGuards -> return ()
263+ _ -> space >> txt "->"
267 switchLayout [patGrhssSpan] $
264 switchLayout [patGrhssSpan] $
268 placeHanging placement p_body
265 placeHanging placement p_body
269 inci p_where
266 inci p_where
@@ -287,24 +284,24 @@ p_grhs' placer render style (GRHS NoExtField guards body) =
290− sitcc (sep (comma >> breakpoint) (sitcc . located' p_stmt) xs)
287+ sitcc (sep commaDel (sitcc . located' p_stmt) xs)
292− txt $ case style of
289+ inci $ case style of
293− EqualSign -> "="
290+ EqualSign -> equals
294− RightArrow -> "->"
291+ RightArrow -> txt "->"
295 placeHanging placement p_body
292 placeHanging placement p_body
297 placement =
294 placement =
298 case endOfGuards of
295 case endOfGuards of
299 Nothing -> placer (unLoc body)
296 Nothing -> placer (unLoc body)
300 Just spn ->
297 Just spn ->
301− if isOneLineSpan (mkSrcSpan spn (srcSpanStart (getLoc body)))
298+ if onTheSameLine spn (getLoc body)
302 then placer (unLoc body)
299 then placer (unLoc body)
303 else Normal
300 else Normal
304 endOfGuards =
301 endOfGuards =
305 case NE.nonEmpty guards of
302 case NE.nonEmpty guards of
306 Nothing -> Nothing
303 Nothing -> Nothing
307− Just gs -> (Just . srcSpanEnd . getLoc . NE.last) gs
304+ Just gs -> (Just . getLoc . NE.last) gs
308 p_body = located body render
305 p_body = located body render
309p_grhs' _ _ _ (XGRHS x) = noExtCon x
306p_grhs' _ _ _ (XGRHS x) = noExtCon x
@@ -312,13 +309,14 @@ p_hsCmd :: HsCmd GhcPs -> R ()
312p_hsCmd = \case
309p_hsCmd = \case
313 HsCmdArrApp NoExtField body input arrType _ -> do
310 HsCmdArrApp NoExtField body input arrType _ -> do
314 located body p_hsExpr
311 located body p_hsExpr
315− space
312+ breakpoint
316− case arrType of
314+ case arrType of
317− HsFirstOrderApp -> txt "-<"
315+ HsFirstOrderApp -> txt "-<"
318− HsHigherOrderApp -> txt "-<<"
316+ HsHigherOrderApp -> txt "-<<"
319− placeHanging (exprPlacement (unLoc input)) $
317+ placeHanging (exprPlacement (unLoc input)) $
320− located input p_hsExpr
318+ located input p_hsExpr
321− HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ sitcc $ do
319+ HsCmdArrForm NoExtField form Prefix _ cmds -> banana $ do
322 located form p_hsExpr
320 located form p_hsExpr
323 unless (null cmds) $ do
321 unless (null cmds) $ do
324 breakpoint
322 breakpoint
@@ -346,7 +344,7 @@ p_hsCmd = \case
348 inci . located es $
346 inci . located es $
349− sitcc . sep newline (located' (sitcc . p_stmt' cmdPlacement p_hsCmd))
347+ sitcc . sep newline (sitcc . withSpacing (p_stmt' cmdPlacement p_hsCmd))
350 HsCmdWrap {} -> notImplemented "HsCmdWrap"
348 HsCmdWrap {} -> notImplemented "HsCmdWrap"
351 XCmd x -> noExtCon x
349 XCmd x -> noExtCon x
@@ -355,6 +353,35 @@ p_hsCmdTop = \case
355 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
353 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
356 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.
359+ -- | Rendering function
361+ -- | Entity to render
364+withSpacing f l = located l $ \x -> do
366+ UnhelpfulSpan _ -> f x
367+ RealSrcSpan currentSpn -> do
368+ getSpanMark >>= \case
369+ -- Spacing before comments will be handled by the code
370+ -- that prints comments, so we just have to deal with
371+ -- blank lines between statements here.
372+ Just (StatementSpan lastSpn) ->
373+ if srcSpanStartLine currentSpn > srcSpanEndLine lastSpn + 1
378+ -- In some cases the (f x) expression may insert a new mark. We want
379+ -- to be careful not to override comment marks.
380+ getSpanMark >>= \case
381+ Just (HaddockSpan _ _) -> return ()
382+ Just (CommentSpan _) -> return ()
383+ _ -> setSpanMark (StatementSpan currentSpn)
358p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
385p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
359p_stmt = p_stmt' exprPlacement p_hsExpr
386p_stmt = p_stmt' exprPlacement p_hsExpr
@@ -426,7 +453,7 @@ p_stmt' placer render = \case
426 RecStmt {..} -> do
453 RecStmt {..} -> do
427 txt "rec"
454 txt "rec"
429− sitcc $ sepSemi (located' (p_stmt' placer render)) recS_stmts
456+ sitcc $ sepSemi (withSpacing (p_stmt' placer render)) recS_stmts
430 XStmtLR c -> noExtCon c
457 XStmtLR c -> noExtCon c
432gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]]
459gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]]
@@ -444,34 +471,31 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
444p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
471p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
445p_hsLocalBinds = \case
472p_hsLocalBinds = \case
446 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
473 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
449− (srcSpanStart . getLoc)
450− (srcSpanStart . getLoc)
452− (Left <$> bagToList bag) ++ (Right <$> lsigs)
453− p_item (Left x) = located x p_valDecl
454− p_item (Right x) = located x p_sigDecl
455− -- Assigns 'False' to the last element, 'True' to the rest.
456− markInit :: [a] -> [(Bool, a)]
458− markInit [x] = [(False, x)]
459− markInit (x : xs) = (True, x) : markInit xs
460 -- 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
461 -- elements will also contain semicolons and they will confuse the
475 -- elements will also contain semicolons and they will confuse the
462 -- parser. so we request braces around every element except the last.
476 -- parser. so we request braces around every element except the last.
463 br <- layoutToBraces <$> getLayout
477 br <- layoutToBraces <$> getLayout
464− sitcc $
478+ let items =
465− sepSemi
479+ let injectLeft (L l x) = L l (Left x)
466− (\(m, i) -> (if m then br else id) $ p_item i)
480+ injectRight (L l x) = L l (Right x)
467− (markInit $ sortOn ssStart items)
481+ in (injectLeft <$> bagToList bag) ++ (injectRight <$> lsigs)
482+ positionToBracing = \case
488+ positionToBracing p $
489+ withSpacing (either p_valDecl p_sigDecl) item
490+ binds = sortOn (srcSpanStart . getLoc) items
491+ sitcc $ sepSemi p_item' (attachRelativePos binds)
468 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
492 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
469 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
493 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
470 -- Second argument of IPBind is always Left before type-checking.
494 -- Second argument of IPBind is always Left before type-checking.
471 let p_ipBind (IPBind NoExtField (Left name) expr) = do
495 let p_ipBind (IPBind NoExtField (Left name) expr) = do
472 atom name
496 atom name
475 breakpoint
499 breakpoint
476 useBraces $ inci $ located expr p_hsExpr
500 useBraces $ inci $ located expr p_hsExpr
477 p_ipBind (IPBind NoExtField (Right _) _) =
501 p_ipBind (IPBind NoExtField (Right _) _) =
@@ -490,15 +514,12 @@ p_hsRecField HsRecField {..} = do
490 p_rdrName hsRecFieldLbl
514 p_rdrName hsRecFieldLbl
491 unless hsRecPun $ do
515 unless hsRecPun $ do
519+ if onTheSameLine (getLoc hsRecFieldLbl) (getLoc hsRecFieldArg)
494− let placement = exprPlacement (unLoc hsRecFieldArg)
520+ then exprPlacement (unLoc hsRecFieldArg)
495− placeHanging placement $ located hsRecFieldArg p_hsExpr
522+ placeHanging placement (located hsRecFieldArg p_hsExpr)
497−p_hsTupArg :: HsTupArg GhcPs -> R ()
499− Present NoExtField x -> located x p_hsExpr
500− Missing NoExtField -> pure ()
501− XTupArg x -> noExtCon x
503p_hsExpr :: HsExpr GhcPs -> R ()
524p_hsExpr :: HsExpr GhcPs -> R ()
504p_hsExpr = p_hsExpr' N
525p_hsExpr = p_hsExpr' N
@@ -563,23 +584,21 @@ p_hsExpr' s = \case
563 -- 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
564 -- to be safe to always bump indentation when the function
585 -- to be safe to always bump indentation when the function
565 -- expression is parenthesised.
586 -- expression is parenthesised.
566− indent =
587+ doIndent =
567 case func of
588 case func of
568− L _ (HsPar NoExtField _) -> inci
589+ L _ (HsPar NoExtField _) -> True
569− L _ (HsAppType NoExtField _ _) -> inci
590+ L _ (HsAppType NoExtField _ _) -> True
570− L _ (HsMultiIf NoExtField _) -> inci
591+ L _ (HsMultiIf NoExtField _) -> True
572− if isOneLineSpan spn
592+ L spn _ -> isOneLineSpan spn
575− ub <- getLayout <&> \case
594+ getLayout <&> \case
576− SingleLine -> useBraces
595+ SingleLine -> useBraces
577− MultiLine -> id
596+ MultiLine -> id
579 located func (p_hsExpr' s)
598 located func (p_hsExpr' s)
580 breakpoint
599 breakpoint
581− indent $ sep breakpoint (located' p_hsExpr) initp
600+ inciIf doIndent $ sep breakpoint (located' p_hsExpr) initp
582− indent $ do
601+ inciIf doIndent $ do
583 unless (null initp) breakpoint
602 unless (null initp) breakpoint
584 located lastp p_hsExpr
603 located lastp p_hsExpr
585 Hanging -> do
604 Hanging -> do
@@ -594,10 +613,15 @@ p_hsExpr' s = \case
594 breakpoint
613 breakpoint
595 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
597 located (hswc_body a) p_hsType
621 located (hswc_body a) p_hsType
598 OpApp NoExtField x op y -> do
622 OpApp NoExtField x op y -> do
599 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
623 let opTree = OpBranch (exprOpTree x) op (exprOpTree y)
600− p_exprOpTree True s (reassociateOpTree getOpName opTree)
624+ p_exprOpTree s (reassociateOpTree getOpName opTree)
601 NegApp NoExtField e _ -> do
625 NegApp NoExtField e _ -> do
@@ -614,22 +638,27 @@ p_hsExpr' s = \case
614 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
638 let isRecordDot' = isRecordDot (unLoc op) (getLoc x)
615 unless (useRecordDot' && isRecordDot') breakpoint
639 unless (useRecordDot' && isRecordDot') breakpoint
616 inci (located x p_hsExpr)
640 inci (located x p_hsExpr)
617− ExplicitTuple NoExtField args boxity -> do
641+ ExplicitTuple NoExtField args boxity ->
618 let isSection = any (isMissing . unLoc) args
642 let isSection = any (isMissing . unLoc) args
619 isMissing = \case
643 isMissing = \case
620 Missing NoExtField -> True
644 Missing NoExtField -> True
621 _ -> 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
622− let parens' =
651+ parens' =
623 case boxity of
652 case boxity of
624 Boxed -> parens
653 Boxed -> parens
625 Unboxed -> parensHash
654 Unboxed -> parensHash
626− if isSection
655+ in if isSection
628− switchLayout [] . parens' s $
657+ switchLayout [] . parens' s $
629− sep comma (located' p_hsTupArg) args
658+ sep comma p_larg args
631− switchLayout (getLoc <$> args) . parens' s . sitcc $
660+ switchLayout (getLoc <$> args) . parens' s $
632− sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
661+ sep commaDel p_larg args
633 ExplicitSum NoExtField tag arity e ->
662 ExplicitSum NoExtField tag arity e ->
634 p_unboxedSum N tag arity (located e p_hsExpr)
663 p_unboxedSum N tag arity (located e p_hsExpr)
635 HsCase NoExtField e mgroup ->
664 HsCase NoExtField e mgroup ->
@@ -637,8 +666,9 @@ p_hsExpr' s = \case
637 HsIf NoExtField _ if' then' else' ->
666 HsIf NoExtField _ if' then' else' ->
638 p_if exprPlacement p_hsExpr if' then' else'
667 p_if exprPlacement p_hsExpr if' then' else'
639 HsMultiIf NoExtField guards -> do
668 HsMultiIf NoExtField guards -> do
640− txt "if "
669+ txt "if"
641− inci . inci . sitcc $ sep newline (located' (p_grhs RightArrow)) guards
671+ inci . inci $ sep newline (located' (p_grhs RightArrow)) guards
642 HsLet NoExtField localBinds e ->
672 HsLet NoExtField localBinds e ->
643 p_let p_hsExpr localBinds e
673 p_let p_hsExpr localBinds e
644 HsDo NoExtField ctx es -> do
674 HsDo NoExtField ctx es -> do
@@ -648,17 +678,17 @@ p_hsExpr' s = \case
648 ub <- layoutToBraces <$> getLayout
678 ub <- layoutToBraces <$> getLayout
651− (located' (ub . p_stmt' exprPlacement (p_hsExpr' S)))
681+ (ub . withSpacing (p_stmt' exprPlacement (p_hsExpr' S)))
652 (unLoc es)
682 (unLoc es)
653− compBody = brackets N $ located es $ \xs -> do
683+ compBody = brackets N . located es $ \xs -> do
654 let p_parBody =
684 let p_parBody =
656− (breakpoint >> txt "| ")
686+ (breakpoint >> txt "|" >> space)
657 p_seqBody
687 p_seqBody
658 p_seqBody =
688 p_seqBody =
661− (comma >> breakpoint)
691+ commaDel
662 (located' (sitcc . p_stmt))
692 (located' (sitcc . p_stmt))
663 stmts = init xs
693 stmts = init xs
664 yield = last xs
694 yield = last xs
@@ -679,8 +709,8 @@ p_hsExpr' s = \case
679 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
709 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
680 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
710 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
681 ExplicitList _ _ xs ->
711 ExplicitList _ _ xs ->
682− brackets s . sitcc $
712+ brackets s $
683− sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs
713+ sep commaDel (sitcc . located' p_hsExpr) xs
684 RecordCon {..} -> do
714 RecordCon {..} -> do
685 located rcon_con_name atom
715 located rcon_con_name atom
686 breakpoint
716 breakpoint
@@ -696,8 +726,8 @@ p_hsExpr' s = \case
696 case rec_dotdot of
726 case rec_dotdot of
697 Just {} -> [txt ".."]
727 Just {} -> [txt ".."]
698 Nothing -> []
728 Nothing -> []
699− inci . braces N . sitcc $
729+ inci . braces N $
700− sep (comma >> breakpoint) sitcc (fields <> dotdot)
730+ sep commaDel sitcc (fields <> dotdot)
701 RecordUpd {..} -> do
731 RecordUpd {..} -> do
702 located rupd_expr p_hsExpr
732 located rupd_expr p_hsExpr
703 useRecordDot' <- useRecordDot
733 useRecordDot' <- useRecordDot
@@ -715,9 +745,9 @@ p_hsExpr' s = \case
715 Unambiguous _ n -> n
745 Unambiguous _ n -> n
716 XAmbiguousFieldOcc x -> noExtCon x
746 XAmbiguousFieldOcc x -> noExtCon x
718− inci . braces N . sitcc $
748+ inci . braces N $
720− (comma >> breakpoint)
750+ commaDel
721 (sitcc . located' (p_hsRecField . updName))
751 (sitcc . located' (p_hsRecField . updName))
722 rupd_flds
752 rupd_flds
723 ExprWithTySig NoExtField x HsWC {hswc_body = HsIB {..}} -> sitcc $ do
753 ExprWithTySig NoExtField x HsWC {hswc_body = HsIB {..}} -> sitcc $ do
@@ -730,22 +760,22 @@ p_hsExpr' s = \case
730 ExprWithTySig NoExtField _ (XHsWildCardBndrs x) -> noExtCon x
760 ExprWithTySig NoExtField _ (XHsWildCardBndrs x) -> noExtCon x
731 ArithSeq NoExtField _ x ->
761 ArithSeq NoExtField _ x ->
732 case x of
762 case x of
733− From from -> brackets s . sitcc $ do
763+ From from -> brackets s $ do
734 located from p_hsExpr
764 located from p_hsExpr
735 breakpoint
765 breakpoint
737− FromThen from next -> brackets s . sitcc $ do
767+ FromThen from next -> brackets s $ do
738− sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
768+ sep commaDel (located' p_hsExpr) [from, next]
739 breakpoint
769 breakpoint
741− FromTo from to -> brackets s . sitcc $ do
771+ FromTo from to -> brackets s $ do
742 located from p_hsExpr
772 located from p_hsExpr
743 breakpoint
773 breakpoint
746 located to p_hsExpr
776 located to p_hsExpr
747− FromThenTo from next to -> brackets s . sitcc $ do
777+ FromThenTo from next to -> brackets s $ do
748− sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
778+ sep commaDel (located' p_hsExpr) [from, next]
749 breakpoint
779 breakpoint
@@ -795,7 +825,7 @@ p_patSynBind PSB {..} = do
795 breakpoint
825 breakpoint
796 located psb_def p_pat
826 located psb_def p_pat
797 ImplicitBidirectional -> do
827 ImplicitBidirectional -> do
799 breakpoint
829 breakpoint
800 located psb_def p_pat
830 located psb_def p_pat
801 ExplicitBidirectional mgroup -> do
831 ExplicitBidirectional mgroup -> do
@@ -822,8 +852,8 @@ p_patSynBind PSB {..} = do
822 inci $ do
852 inci $ do
823 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
853 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
824 unless (null xs) breakpoint
854 unless (null xs) breakpoint
825− braces N . sitcc $
855+ braces N $
826− sep (comma >> breakpoint) (p_rdrName . recordPatSynPatVar) xs
856+ sep commaDel (p_rdrName . recordPatSynPatVar) xs
828 InfixCon l r -> do
858 InfixCon l r -> do
829 switchLayout [getLoc l, getLoc r] $ do
859 switchLayout [getLoc l, getLoc r] $ do
@@ -877,10 +907,12 @@ p_if placer render if' then' else' = do
877 breakpoint
907 breakpoint
878 inci $ do
908 inci $ do
879 txt "then"
909 txt "then"
880 located then' $ \x ->
911 located then' $ \x ->
881 placeHanging (placer x) (render x)
912 placeHanging (placer x) (render x)
882 breakpoint
913 breakpoint
883 txt "else"
914 txt "else"
884 located else' $ \x ->
916 located else' $ \x ->
885 placeHanging (placer x) (render x)
917 placeHanging (placer x) (render x)
@@ -917,13 +949,13 @@ p_pat = \case
918 located pat p_pat
950 located pat p_pat
919 ListPat NoExtField pats ->
951 ListPat NoExtField pats ->
920− brackets S . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats
952+ brackets S $ sep commaDel (located' p_pat) pats
921 TuplePat NoExtField pats boxing -> do
953 TuplePat NoExtField pats boxing -> do
922− let f =
954+ let parens' =
923 case boxing of
955 case boxing of
924 Boxed -> parens S
956 Boxed -> parens S
925 Unboxed -> parensHash S
957 Unboxed -> parensHash S
926− f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats
958+ parens' $ sep commaDel (sitcc . located' p_pat) pats
927 SumPat NoExtField pat tag arity ->
959 SumPat NoExtField pat tag arity ->
928 p_unboxedSum S tag arity (located pat p_pat)
960 p_unboxedSum S tag arity (located pat p_pat)
929 ConPatIn pat details ->
961 ConPatIn pat details ->
@@ -939,7 +971,7 @@ p_pat = \case
939 let f = \case
971 let f = \case
940 Nothing -> txt ".."
972 Nothing -> txt ".."
941 Just x -> located x p_pat_hsRecField
973 Just x -> located x p_pat_hsRecField
942− inci . braces N . sitcc . sep (comma >> breakpoint) f $
974+ inci . braces N . sep commaDel f $
943 case dotdot of
975 case dotdot of
944 Nothing -> Just <$> fields
976 Nothing -> Just <$> fields
945 Just (L _ n) -> (Just <$> take n fields) ++ [Nothing]
977 Just (L _ n) -> (Just <$> take n fields) ++ [Nothing]
@@ -980,7 +1012,7 @@ p_pat_hsRecField HsRecField {..} = do
980 p_rdrName (rdrNameFieldOcc x)
1012 p_rdrName (rdrNameFieldOcc x)
981 unless hsRecPun $ do
1013 unless hsRecPun $ do
984 breakpoint
1016 breakpoint
985 inci (located hsRecFieldArg p_pat)
1017 inci (located hsRecFieldArg p_pat)
@@ -989,17 +1021,15 @@ p_unboxedSum s tag arity m = do
989 let before = tag - 1
1021 let before = tag - 1
990 after = arity - before - 1
1022 after = arity - before - 1
991 args = replicate before Nothing <> [Just m] <> replicate after Nothing
1023 args = replicate before Nothing <> [Just m] <> replicate after Nothing
992− f (x, i) = do
1024+ f x =
993− let isFirst = i == 0
994− isLast = i == arity - 1
995 case x :: Maybe (R ()) of
1025 case x :: Maybe (R ()) of
996 Nothing ->
1026 Nothing ->
997− unless (isFirst || isLast) space
1027+ space
998 Just m' -> do
1028 Just m' -> do
999− unless isFirst space
1029+ space
1001− unless isLast space
1031+ space
1002− parensHash s $ sep (txt "|") f (zip args [0 ..])
1032+ parensHash s $ sep (txt "|") f args
1004p_hsSplice :: HsSplice GhcPs -> R ()
1034p_hsSplice :: HsSplice GhcPs -> R ()
1005p_hsSplice = \case
1035p_hsSplice = \case
@@ -1201,11 +1231,10 @@ exprPlacement = \case
1201 HsCase NoExtField _ _ -> Hanging
1231 HsCase NoExtField _ _ -> Hanging
1202 HsDo NoExtField DoExpr _ -> Hanging
1232 HsDo NoExtField DoExpr _ -> Hanging
1203 HsDo NoExtField MDoExpr _ -> Hanging
1233 HsDo NoExtField MDoExpr _ -> Hanging
1204− -- If the rightmost expression in an operator chain is hanging, make the
1234+ OpApp NoExtField _ op y ->
1205− -- whole block hanging; so that we can use the common @f = foo $ do@
1235+ case (fmap getOpNameStr . getOpName . unLoc) op of
1207− OpApp NoExtField _ _ y -> exprPlacement (unLoc y)
1236+ Just "$" -> exprPlacement (unLoc y)
1208− -- Same thing for function applications (usually with -XBlockArguments)
1237+ _ -> Normal
1209 HsApp NoExtField _ y -> exprPlacement (unLoc y)
1238 HsApp NoExtField _ y -> exprPlacement (unLoc y)
1210 HsProc NoExtField p _ ->
1239 HsProc NoExtField p _ ->
1211 -- Indentation breaks if pattern is longer than one line and left
1240 -- Indentation breaks if pattern is longer than one line and left
@@ -1231,15 +1260,16 @@ getOpName = \case
1231 HsVar NoExtField (L _ a) -> Just a
1260 HsVar NoExtField (L _ a) -> Just a
1232 _ -> Nothing
1261 _ -> Nothing
1263+getOpNameStr :: RdrName -> String
1264+getOpNameStr = occNameString . rdrNameOcc
1234p_exprOpTree ::
1266p_exprOpTree ::
1235− -- | Can use special handling of dollar?
1237 -- | Bracket style to use
1267 -- | Bracket style to use
1238 BracketStyle ->
1268 BracketStyle ->
1239 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
1269 OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
1241−p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
1271+p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1242−p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
1272+p_exprOpTree s (OpBranch x op y) = do
1243 -- 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
1244 -- 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
1245 -- placement.
1275 -- placement.
@@ -1261,44 +1291,54 @@ p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
1261 MultiLine -> case placement of
1291 MultiLine -> case placement of
1262 Hanging -> useBraces
1292 Hanging -> useBraces
1263 Normal -> dontUseBraces
1293 Normal -> dontUseBraces
1264− gotDollar = case getOpName (unLoc op) of
1294+ opNameStr = (fmap getOpNameStr . getOpName . unLoc) op
1265− Just rname -> mkVarOcc "$" == rdrNameOcc rname
1295+ gotDollar = opNameStr == Just "$"
1266− _ -> False
1296+ gotColon = opNameStr == Just ":"
1297+ gotRecordDot = isRecordDot (unLoc op) (opTreeLoc y)
1268 switchLayout [opTreeLoc x] $
1299 switchLayout [opTreeLoc x] $
1269− p_exprOpTree (not gotDollar) s x
1300+ p_exprOpTree s x
1270− let p_op = located op (opWrapper . p_hsExpr)
1301+ p_op = located op (opWrapper . p_hsExpr)
1271− p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
1302+ p_y = switchLayout [opTreeLoc y] (p_exprOpTree N y)
1272 isSection = case (opTreeLoc x, getLoc op) of
1303 isSection = case (opTreeLoc x, getLoc op) of
1273 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
1304 (RealSrcSpan treeSpan, RealSrcSpan opSpan) ->
1274 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
1305 srcSpanEndCol treeSpan /= srcSpanStartCol opSpan
1275 _ -> False
1306 _ -> False
1308+ OpNode (L _ HsDo {}) -> True
1276 useRecordDot' <- useRecordDot
1310 useRecordDot' <- useRecordDot
1277− let isRecordDot' = isRecordDot (unLoc op) (opTreeLoc y)
1311+ if
1278− if useRecordDot' && isRecordDot'
1312+ | gotColon -> do
1281− when isSection space
1289− && isOneLineSpan (opTreeLoc x)
1291− useBraces lhs
1313+ lhs
1294− breakpoint
1316+ case placement of
1295− inci p_y
1317+ Hanging -> do
1298− 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
1303-- | Return 'True' if given expression is a record-dot operator expression.
1343-- | Return 'True' if given expression is a record-dot operator expression.
1304isRecordDot ::
1344isRecordDot ::
@@ -1309,14 +1349,10 @@ isRecordDot ::
1310isRecordDot op (RealSrcSpan ySpan) = case op of
1350isRecordDot op (RealSrcSpan ySpan) = case op of
1311 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1351 HsVar NoExtField (L (RealSrcSpan opSpan) opName) ->
1312− isDot opName && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1352+ (getOpNameStr opName == ".") && (srcSpanEndCol opSpan == srcSpanStartCol ySpan)
1313 _ -> False
1353 _ -> False
1314isRecordDot _ _ = False
1354isRecordDot _ _ = False
1316−-- | Check whether a given 'RdrName' is the dot operator.
1317−isDot :: RdrName -> Bool
1318−isDot name = rdrNameOcc name == mkVarOcc "."
1320-- | Get annotations for the enclosing element.
1356-- | Get annotations for the enclosing element.
1321getEnclosingAnns :: R [AnnKeywordId]
1357getEnclosingAnns :: R [AnnKeywordId]
1322getEnclosingAnns = do
1358getEnclosingAnns = do