@@ -1,13 +1,13 @@
1−{-# LANGUAGE LambdaCase #-}
1+{-# LANGUAGE LambdaCase #-}
22{-# LANGUAGE OverloadedStrings #-}
3−{-# LANGUAGE RecordWildCards #-}
3+{-# LANGUAGE RecordWildCards #-}
44
55module Ormolu.Printer.Meat.Declaration.Value
6− ( p_valDecl
7− , p_pat
8− , p_hsExpr
9− , p_hsSplice
10− , p_stringLit
6+ ( p_valDecl,
7+ p_pat,
8+ p_hsExpr,
9+ p_hsSplice,
10+ p_stringLit,
1111 )
1212where
1313
@@ -20,11 +20,14 @@ import Data.Char (isPunctuation, isSymbol)
2020import Data.Data hiding (Infix, Prefix)
2121import Data.List (intersperse, sortOn)
2222import Data.List.NonEmpty (NonEmpty ((:|)))
23+import qualified Data.List.NonEmpty as NE
2324import Data.Text (Text)
25+import qualified Data.Text as Text
2426import GHC
2527import OccName (mkVarOcc)
2628import Ormolu.Printer.Combinators
2729import Ormolu.Printer.Meat.Common
30+import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration
2831import Ormolu.Printer.Meat.Declaration.Signature
2932import Ormolu.Printer.Meat.Type
3033import Ormolu.Printer.Operators
@@ -32,12 +35,8 @@ import Ormolu.Utils
3235import RdrName (RdrName (..))
3336import RdrName (rdrNameOcc)
3437import SrcLoc (combineSrcSpans, isOneLineSpan)
35−import qualified Data.List.NonEmpty as NE
36−import qualified Data.Text as Text
37−import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration
3838
3939-- | Style of a group of equations.
40−
4140data MatchGroupStyle
4241 = Function (Located RdrName)
4342 | PatternBind
@@ -46,21 +45,21 @@ data MatchGroupStyle
4645 | LambdaCase
4746
4847-- | Style of equations in a group.
49−
5048data GroupStyle
5149 = EqualSign
5250 | RightArrow
5351
5452-- | Expression placement. This marks the places where expressions that
5553-- implement handing forms may use them.
56−
5754data Placement
58− = Normal -- ^ Multi-line layout should cause
59− -- insertion of a newline and indentation
60− -- bump
61− | Hanging -- ^ Expressions that have hanging form
62− -- should use it and avoid bumping one level
63− -- of indentation
55+ = -- | Multi-line layout should cause
56+ -- insertion of a newline and indentation
57+ -- bump
58+ Normal
59+ | -- | Expressions that have hanging form
60+ -- should use it and avoid bumping one level
61+ -- of indentation
62+ Hanging
6463 deriving (Eq)
6564
6665p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
@@ -72,25 +71,29 @@ p_valDecl = \case
7271 PatSynBind NoExt psb -> p_patSynBind psb
7372 XHsBindsLR NoExt -> notImplemented "XHsBindsLR"
7473
75−p_funBind
76− :: Located RdrName
77− -> MatchGroup GhcPs (LHsExpr GhcPs)
78− -> R ()
74+p_funBind ::
75+ Located RdrName ->
76+ MatchGroup GhcPs (LHsExpr GhcPs) ->
77+ R ()
7978p_funBind name = p_matchGroup (Function name)
8079
81−p_matchGroup
82− :: MatchGroupStyle
83− -> MatchGroup GhcPs (LHsExpr GhcPs)
84− -> R ()
80+p_matchGroup ::
81+ MatchGroupStyle ->
82+ MatchGroup GhcPs (LHsExpr GhcPs) ->
83+ R ()
8584p_matchGroup = p_matchGroup' exprPlacement p_hsExpr
8685
87−p_matchGroup'
88− :: Data body
89− => (body -> Placement) -- ^ How to get body placement
90− -> (body -> R ()) -- ^ How to print body
91− -> MatchGroupStyle -- ^ Style of this group of equations
92− -> MatchGroup GhcPs (Located body) -- ^ Match group
93− -> R ()
86+p_matchGroup' ::
87+ Data body =>
88+ -- | How to get body placement
89+ (body -> Placement) ->
90+ -- | How to print body
91+ (body -> R ()) ->
92+ -- | Style of this group of equations
93+ MatchGroupStyle ->
94+ -- | Match group
95+ MatchGroup GhcPs (Located body) ->
96+ R ()
9497p_matchGroup' placer render style MG {..} = do
9598 let ob = case style of
9699 Case -> id
@@ -119,11 +122,10 @@ p_matchGroup' _ _ _ (XMatchGroup NoExt) = notImplemented "XMatchGroup"
119122-- and paretheses) associated with them. It is necessary to use per-equation
120123-- names obtained from 'm_ctxt' of 'Match'. This function replaces function
121124-- name inside of 'Function' accordingly.
122−
123−adjustMatchGroupStyle
124− :: Match GhcPs body
125− -> MatchGroupStyle
126− -> MatchGroupStyle
125+adjustMatchGroupStyle ::
126+ Match GhcPs body ->
127+ MatchGroupStyle ->
128+ MatchGroupStyle
127129adjustMatchGroupStyle m = \case
128130 Function _ -> (Function . mc_fun . m_ctxt) m
129131 style -> style
@@ -131,28 +133,40 @@ adjustMatchGroupStyle m = \case
131133matchStrictness :: Match id body -> SrcStrictness
132134matchStrictness match =
133135 case m_ctxt match of
134− FunRhs{mc_strictness=s} -> s
136+ FunRhs {mc_strictness = s} -> s
135137 _ -> NoSrcStrict
136138
137−p_match
138− :: MatchGroupStyle -- ^ Style of the group
139− -> Bool -- ^ Is this an infix match?
140− -> SrcStrictness -- ^ Strictness prefix (FunBind)
141− -> [LPat GhcPs] -- ^ Argument patterns
142− -> GRHSs GhcPs (LHsExpr GhcPs) -- ^ Equations
143− -> R ()
139+p_match ::
140+ -- | Style of the group
141+ MatchGroupStyle ->
142+ -- | Is this an infix match?
143+ Bool ->
144+ -- | Strictness prefix (FunBind)
145+ SrcStrictness ->
146+ -- | Argument patterns
147+ [LPat GhcPs] ->
148+ -- | Equations
149+ GRHSs GhcPs (LHsExpr GhcPs) ->
150+ R ()
144151p_match = p_match' exprPlacement p_hsExpr
145152
146−p_match'
147− :: Data body
148− => (body -> Placement) -- ^ How to get body placement
149− -> (body -> R ()) -- ^ How to print body
150− -> MatchGroupStyle -- ^ Style of this group of equations
151− -> Bool -- ^ Is this an infix match?
152− -> SrcStrictness -- ^ Strictness prefix (FunBind)
153− -> [LPat GhcPs] -- ^ Argument patterns
154− -> GRHSs GhcPs (Located body) -- ^ Equations
155− -> R ()
153+p_match' ::
154+ Data body =>
155+ -- | How to get body placement
156+ (body -> Placement) ->
157+ -- | How to print body
158+ (body -> R ()) ->
159+ -- | Style of this group of equations
160+ MatchGroupStyle ->
161+ -- | Is this an infix match?
162+ Bool ->
163+ -- | Strictness prefix (FunBind)
164+ SrcStrictness ->
165+ -- | Argument patterns
166+ [LPat GhcPs] ->
167+ -- | Equations
168+ GRHSs GhcPs (Located body) ->
169+ R ()
156170p_match' placer render style isInfix strictness m_pats m_grhss = do
157171 -- NOTE Normally, since patterns may be placed in a multi-line layout, it
158172 -- is necessary to bump indentation for the pattern group so it's more
@@ -170,11 +184,13 @@ p_match' placer render style isInfix strictness m_pats m_grhss = do
170184 Function name -> p_rdrName name
171185 _ -> return ()
172186 Just ne_pats -> do
173− let combinedSpans = combineSrcSpans' $
174− getLoc <$> ne_pats
175− inci' = if isOneLineSpan combinedSpans
176− then id
177− else inci
187+ let combinedSpans =
188+ combineSrcSpans' $
189+ getLoc <$> ne_pats
190+ inci' =
191+ if isOneLineSpan combinedSpans
192+ then id
193+ else inci
178194 switchLayout [combinedSpans] $ do
179195 let stdCase = sep breakpoint (located' p_pat) m_pats
180196 case style of
@@ -197,18 +213,17 @@ p_match' placer render style isInfix strictness m_pats m_grhss = do
197213 sitcc stdCase
198214 LambdaCase -> stdCase
199215 return inci'
200− let
201− -- Calculate position of end of patterns. This is useful when we decide
202− -- about putting certain constructions in hanging positions.
203− endOfPats = case NE.nonEmpty m_pats of
204− Nothing -> case style of
205− Function name -> (Just . srcSpanEnd . getLoc) name
206− _ -> Nothing
207− Just pats -> (Just . srcSpanEnd . getLoc . NE.last) pats
208− isCase = \case
209− Case -> True
210− LambdaCase -> True
211− _ -> False
216+ let -- Calculate position of end of patterns. This is useful when we decide
217+ -- about putting certain constructions in hanging positions.
218+ endOfPats = case NE.nonEmpty m_pats of
219+ Nothing -> case style of
220+ Function name -> (Just . srcSpanEnd . getLoc) name
221+ _ -> Nothing
222+ Just pats -> (Just . srcSpanEnd . getLoc . NE.last) pats
223+ isCase = \case
224+ Case -> True
225+ LambdaCase -> True
226+ _ -> False
212227 let GRHSs {..} = m_grhss
213228 hasGuards = withGuards grhssGRHSs
214229 unless (length grhssGRHSs > 1) $ do
@@ -218,10 +233,13 @@ p_match' placer render style isInfix strictness m_pats m_grhss = do
218233 PatternBind -> space >> txt "="
219234 s | isCase s && hasGuards -> return ()
220235 _ -> space >> txt "->"
221− let grhssSpan = combineSrcSpans' $
222− getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
223− patGrhssSpan = maybe grhssSpan
224− (combineSrcSpans grhssSpan . srcLocSpan) endOfPats
236+ let grhssSpan =
237+ combineSrcSpans' $
238+ getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
239+ patGrhssSpan =
240+ maybe grhssSpan
241+ (combineSrcSpans grhssSpan . srcLocSpan)
242+ endOfPats
225243 placement =
226244 case endOfPats of
227245 Nothing -> blockPlacement placer grhssGRHSs
@@ -233,8 +251,8 @@ p_match' placer render style isInfix strictness m_pats m_grhss = do
233251 p_body = do
234252 let groupStyle =
235253 if isCase style && hasGuards
236− then RightArrow
237− else EqualSign
254+ then RightArrow
255+ else EqualSign
238256 sep newline (located' (p_grhs' placer render groupStyle)) grhssGRHSs
239257 p_where = do
240258 let whereIsEmpty = GHC.isEmptyLocalBindsPR (unLoc grhssLocalBinds)
@@ -251,13 +269,15 @@ p_match' placer render style isInfix strictness m_pats m_grhss = do
251269p_grhs :: GroupStyle -> GRHS GhcPs (LHsExpr GhcPs) -> R ()
252270p_grhs = p_grhs' exprPlacement p_hsExpr
253271
254−p_grhs'
255− :: Data body
256− => (body -> Placement) -- ^ How to get body placement
257− -> (body -> R ()) -- ^ How to print body
258− -> GroupStyle
259− -> GRHS GhcPs (Located body)
260− -> R ()
272+p_grhs' ::
273+ Data body =>
274+ -- | How to get body placement
275+ (body -> Placement) ->
276+ -- | How to print body
277+ (body -> R ()) ->
278+ GroupStyle ->
279+ GRHS GhcPs (Located body) ->
280+ R ()
261281p_grhs' placer render style (GRHS NoExt guards body) =
262282 case guards of
263283 [] -> p_body
@@ -276,8 +296,8 @@ p_grhs' placer render style (GRHS NoExt guards body) =
276296 Nothing -> placer (unLoc body)
277297 Just spn ->
278298 if isOneLineSpan (mkSrcSpan spn (srcSpanStart (getLoc body)))
279− then placer (unLoc body)
280− else Normal
299+ then placer (unLoc body)
300+ else Normal
281301 endOfGuards =
282302 case NE.nonEmpty guards of
283303 Nothing -> Nothing
@@ -335,12 +355,15 @@ p_hsCmdTop = \case
335355p_stmt :: Stmt GhcPs (LHsExpr GhcPs) -> R ()
336356p_stmt = p_stmt' exprPlacement p_hsExpr
337357
338−p_stmt'
339− :: Data body
340− => (body -> Placement) -- ^ Placer
341− -> (body -> R ()) -- ^ Render
342− -> Stmt GhcPs (Located body) -- ^ Statement to render
343− -> R ()
358+p_stmt' ::
359+ Data body =>
360+ -- | Placer
361+ (body -> Placement) ->
362+ -- | Render
363+ (body -> R ()) ->
364+ -- | Statement to render
365+ Stmt GhcPs (Located body) ->
366+ R ()
344367p_stmt' placer render = \case
345368 LastStmt NoExt body _ _ -> located body render
346369 BindStmt NoExt l f _ _ -> do
@@ -417,9 +440,10 @@ gatherStmtBlock XParStmtBlock {} = notImplemented "XParStmtBlock"
417440p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
418441p_hsLocalBinds = \case
419442 HsValBinds NoExt (ValBinds NoExt bag lsigs) -> do
420− let ssStart = either
421− (srcSpanStart . getLoc)
422− (srcSpanStart . getLoc)
443+ let ssStart =
444+ either
445+ (srcSpanStart . getLoc)
446+ (srcSpanStart . getLoc)
423447 items =
424448 (Left <$> bagToList bag) ++ (Right <$> lsigs)
425449 p_item (Left x) = located x p_valDecl
@@ -427,15 +451,16 @@ p_hsLocalBinds = \case
427451 -- Assigns 'False' to the last element, 'True' to the rest.
428452 markInit :: [a] -> [(Bool, a)]
429453 markInit [] = []
430− markInit (x:[]) = [(False, x)]
431− markInit (x:xs) = (True, x) : markInit xs
454+ markInit (x : []) = [(False, x)]
455+ markInit (x : xs) = (True, x) : markInit xs
432456 -- NOTE When in a single-line layout, there is a chance that the inner
433457 -- elements will also contain semicolons and they will confuse the
434458 -- parser. so we request braces around every element except the last.
435459 br <- layoutToBraces <$> getLayout
436− sitcc $ sepSemi
437− (\(m, i) -> (if m then br else id) $ p_item i)
438− (markInit $ sortOn ssStart items)
460+ sitcc $
461+ sepSemi
462+ (\(m, i) -> (if m then br else id) $ p_item i)
463+ (markInit $ sortOn ssStart items)
439464 HsValBinds NoExt _ -> notImplemented "HsValBinds"
440465 HsIPBinds NoExt (IPBinds NoExt xs) ->
441466 -- Second argument of IPBind is always Left before type-checking.
@@ -451,9 +476,9 @@ p_hsLocalBinds = \case
451476 EmptyLocalBinds NoExt -> return ()
452477 XHsLocalBindsLR _ -> notImplemented "XHsLocalBindsLR"
453478
454−p_hsRecField
455− :: HsRecField' RdrName (LHsExpr GhcPs)
456− -> R ()
479+p_hsRecField ::
480+ HsRecField' RdrName (LHsExpr GhcPs) ->
481+ R ()
457482p_hsRecField = \HsRecField {..} -> do
458483 p_rdrName hsRecFieldLbl
459484 unless hsRecPun $ do
@@ -505,8 +530,8 @@ p_hsExpr' s = \case
505530 -- and then use 'p_withoutHanging' for the descendants.
506531 let p_withoutHanging (HsApp NoExt f' x') = do
507532 case f' of
508− L _ (HsApp _ _ _) -> located f' p_withoutHanging
509− _ -> located f' (p_hsExpr' s)
533+ L _ (HsApp _ _ _) -> located f' p_withoutHanging
534+ _ -> located f' (p_hsExpr' s)
510535 breakpoint
511536 inci $ located x' p_hsExpr
512537 p_withoutHanging e = p_hsExpr e
@@ -514,15 +539,16 @@ p_hsExpr' s = \case
514539 -- a single line.
515540 placement =
516541 if isOneLineSpan (getLoc f)
517− then exprPlacement (unLoc x)
518− else Normal
542+ then exprPlacement (unLoc x)
543+ else Normal
519544 -- We only sit when the last expression is not hanging.
520545 -- This is to allow:
521546 -- f = foo bar do
522547 -- baz
523− sit' = if placement == Normal
524− then sitcc
525− else id
548+ sit' =
549+ if placement == Normal
550+ then sitcc
551+ else id
526552 sit' $ do
527553 useBraces $ located f p_withoutHanging
528554 placeHanging placement $
@@ -559,10 +585,12 @@ p_hsExpr' s = \case
559585 Boxed -> parens
560586 Unboxed -> parensHash
561587 if isSection
562− then switchLayout [] . parens' s $
563− sep comma (located' p_hsTupArg) args
564− else switchLayout (getLoc <$> args) . parens' s . sitcc $
565− sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
588+ then
589+ switchLayout [] . parens' s $
590+ sep comma (located' p_hsTupArg) args
591+ else
592+ switchLayout (getLoc <$> args) . parens' s . sitcc $
593+ sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
566594 ExplicitSum NoExt tag arity e ->
567595 p_unboxedSum N tag arity (located e p_hsExpr)
568596 HsCase NoExt e mgroup ->
@@ -580,16 +608,20 @@ p_hsExpr' s = \case
580608 txt header
581609 breakpoint
582610 ub <- layoutToBraces <$> getLayout
583− inci $ sepSemi
584− (located' (ub . p_stmt' exprPlacement (p_hsExpr' S)))
585− (unLoc es)
611+ inci $
612+ sepSemi
613+ (located' (ub . p_stmt' exprPlacement (p_hsExpr' S)))
614+ (unLoc es)
586615 compBody = brackets N $ located es $ \xs -> do
587− let p_parBody = sep
588− (breakpoint >> txt "| ")
589− p_seqBody
590− p_seqBody = sitcc . sep
591− (comma >> breakpoint)
592− (located' (sitcc . p_stmt))
616+ let p_parBody =
617+ sep
618+ (breakpoint >> txt "| ")
619+ p_seqBody
620+ p_seqBody =
621+ sitcc
622+ . sep
623+ (comma >> breakpoint)
624+ (located' (sitcc . p_stmt))
593625 stmts = init xs
594626 yield = last xs
595627 lists = foldr (liftAppend . gatherStmt) [] stmts
@@ -603,7 +635,7 @@ p_hsExpr' s = \case
603635 MDoExpr -> doBody "mdo"
604636 ListComp -> compBody
605637 MonadComp -> notImplemented "MonadComp"
606− ArrowExpr -> notImplemented "ArrowExpr"
638+ ArrowExpr -> notImplemented "ArrowExpr"
607639 GhciStmtCtxt -> notImplemented "GhciStmtCtxt"
608640 PatGuard _ -> notImplemented "PatGuard"
609641 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
@@ -615,11 +647,12 @@ p_hsExpr' s = \case
615647 located rcon_con_name atom
616648 breakpoint
617649 let HsRecFields {..} = rcon_flds
618− updName f = f {
619− hsRecFieldLbl = case unLoc $ hsRecFieldLbl f of
620− FieldOcc _ n -> n
621− XFieldOcc _ -> notImplemented "XFieldOcc"
622− }
650+ updName f =
651+ f
652+ { hsRecFieldLbl = case unLoc $ hsRecFieldLbl f of
653+ FieldOcc _ n -> n
654+ XFieldOcc _ -> notImplemented "XFieldOcc"
655+ }
623656 fields = located' (p_hsRecField . updName) <$> rec_flds
624657 dotdot =
625658 case rec_dotdot of
@@ -630,12 +663,13 @@ p_hsExpr' s = \case
630663 RecordUpd {..} -> do
631664 located rupd_expr p_hsExpr
632665 breakpoint
633− let updName f = f {
634− hsRecFieldLbl = case unLoc $ hsRecFieldLbl f of
635− Ambiguous _ n -> n
636− Unambiguous _ n -> n
637− XAmbiguousFieldOcc _ -> notImplemented "XAmbiguousFieldOcc"
638− }
666+ let updName f =
667+ f
668+ { hsRecFieldLbl = case unLoc $ hsRecFieldLbl f of
669+ Ambiguous _ n -> n
670+ Unambiguous _ n -> n
671+ XAmbiguousFieldOcc _ -> notImplemented "XAmbiguousFieldOcc"
672+ }
639673 inci . braces N . sitcc $
640674 sep
641675 (comma >> breakpoint)
@@ -697,7 +731,7 @@ p_hsExpr' s = \case
697731 txt "->"
698732 placeHanging (cmdTopPlacement (unLoc e)) $
699733 located e p_hsCmdTop
700− HsStatic _ e -> do
734+ HsStatic _ e -> do
701735 txt "static"
702736 breakpoint
703737 inci (located e p_hsExpr)
@@ -736,7 +770,7 @@ p_patSynBind PSB {..} = do
736770 txt "<-"
737771 breakpoint
738772 located psb_def p_pat
739− ImplicitBidirectional -> do
773+ ImplicitBidirectional -> do
740774 txt "="
741775 breakpoint
742776 located psb_def p_pat
@@ -753,7 +787,7 @@ p_patSynBind PSB {..} = do
753787 PrefixCon xs -> do
754788 space
755789 p_rdrName psb_id
756− inci $ do
790+ inci $ do
757791 switchLayout (getLoc <$> xs) $ do
758792 unless (null xs) breakpoint
759793 sitcc (sep breakpoint p_rdrName xs)
@@ -779,13 +813,17 @@ p_patSynBind PSB {..} = do
779813 inci rhs
780814p_patSynBind (XPatSynBind NoExt) = notImplemented "XPatSynBind"
781815
782−p_case
783− :: Data body
784− => (body -> Placement) -- ^ Placer
785− -> (body -> R ()) -- ^ Render
786− -> LHsExpr GhcPs -- ^ Expression
787− -> (MatchGroup GhcPs (Located body)) -- ^ Match group
788− -> R ()
816+p_case ::
817+ Data body =>
818+ -- | Placer
819+ (body -> Placement) ->
820+ -- | Render
821+ (body -> R ()) ->
822+ -- | Expression
823+ LHsExpr GhcPs ->
824+ -- | Match group
825+ (MatchGroup GhcPs (Located body)) ->
826+ R ()
789827p_case placer render e mgroup = do
790828 txt "case"
791829 space
@@ -795,14 +833,19 @@ p_case placer render e mgroup = do
795833 breakpoint
796834 inci (p_matchGroup' placer render Case mgroup)
797835
798−p_if
799− :: Data body
800− => (body -> Placement) -- ^ Placer
801− -> (body -> R ()) -- ^ Render
802− -> LHsExpr GhcPs -- ^ If
803− -> Located body -- ^ Then
804− -> Located body -- ^ Else
805− -> R ()
836+p_if ::
837+ Data body =>
838+ -- | Placer
839+ (body -> Placement) ->
840+ -- | Render
841+ (body -> R ()) ->
842+ -- | If
843+ LHsExpr GhcPs ->
844+ -- | Then
845+ Located body ->
846+ -- | Else
847+ Located body ->
848+ R ()
806849p_if placer render if' then' else' = do
807850 txt "if"
808851 space
@@ -817,12 +860,13 @@ p_if placer render if' then' else' = do
817860 located else' $ \x ->
818861 placeHanging (placer x) (render x)
819862
820−p_let
821− :: Data body
822− => (body -> R ()) -- ^ Render
823− -> Located (HsLocalBindsLR GhcPs GhcPs)
824− -> Located body
825− -> R ()
863+p_let ::
864+ Data body =>
865+ -- | Render
866+ (body -> R ()) ->
867+ Located (HsLocalBindsLR GhcPs GhcPs) ->
868+ Located body ->
869+ R ()
826870p_let render localBinds e = sitcc $ do
827871 txt "let"
828872 space
@@ -919,7 +963,7 @@ p_unboxedSum s tag arity m = do
919963 let before = tag - 1
920964 after = arity - before - 1
921965 args = replicate before Nothing <> [Just m] <> replicate after Nothing
922− f (x,i) = do
966+ f (x, i) = do
923967 let isFirst = i == 0
924968 isLast = i == arity - 1
925969 case x :: Maybe (R ()) of
@@ -929,7 +973,7 @@ p_unboxedSum s tag arity m = do
929973 unless isFirst space
930974 m'
931975 unless isLast space
932− parensHash s $ sep (txt "|") f (zip args [0..])
976+ parensHash s $ sep (txt "|") f (zip args [0 ..])
933977
934978p_hsSplice :: HsSplice GhcPs -> R ()
935979p_hsSplice = \case
@@ -946,11 +990,14 @@ p_hsSplice = \case
946990 HsSpliced {} -> notImplemented "HsSpliced"
947991 XSplice {} -> notImplemented "XSplice"
948992
949−p_hsSpliceTH
950− :: Bool -- ^ Typed splice?
951− -> LHsExpr GhcPs -- ^ Splice expression
952− -> SpliceDecoration -- ^ Splice decoration
953− -> R ()
993+p_hsSpliceTH ::
994+ -- | Typed splice?
995+ Bool ->
996+ -- | Splice expression
997+ LHsExpr GhcPs ->
998+ -- | Splice decoration
999+ SpliceDecoration ->
1000+ R ()
9541001p_hsSpliceTH isTyped expr = \case
9551002 HasParens -> do
9561003 txt decoSymbol
@@ -968,7 +1015,7 @@ p_hsBracket = \case
9681015 ExpBr NoExt expr -> do
9691016 anns <- getEnclosingAnns
9701017 let name = case anns of
971− AnnOpenEQ:_ -> ""
1018+ AnnOpenEQ : _ -> ""
9721019 _ -> "e"
9731020 quote name (located expr p_hsExpr)
9741021 PatBr NoExt pat -> quote "p" (located pat p_pat)
@@ -983,10 +1030,11 @@ p_hsBracket = \case
9831030 -- turn makes it impossible to detect if there are parentheses around
9841031 -- it, etc. So we have to add parentheses manually assuming they are
9851032 -- necessary for all operators.
986− let isOperator = all
987− (\i -> isPunctuation i || isSymbol i)
988− (showOutputable (rdrNameOcc name))
989− && not (doesNotNeedExtraParens name)
1033+ let isOperator =
1034+ all
1035+ (\i -> isPunctuation i || isSymbol i)
1036+ (showOutputable (rdrNameOcc name))
1037+ && not (doesNotNeedExtraParens name)
9901038 wrapper = if isOperator then parens N else id
9911039 wrapper $ p_rdrName (noLoc name)
9921040 TExpBr NoExt expr -> do
@@ -1019,50 +1067,46 @@ p_stringLit src =
10191067 multiLine =
10201068 sitcc $ sep breakpoint (txt . Text.pack) (backslashes s)
10211069 in vlayout singleLine multiLine
1022− where
1023− -- Split a string on gaps (backslash delimited whitespaces)
1024− --
1025− -- > splitGaps "bar\\ \\fo\\&o" == ["bar", "fo\\&o"]
1026− splitGaps :: String -> [String]
1027− splitGaps "" = []
1028− splitGaps s =
1029− let -- A backslash and a whitespace starts a "gap"
1030− p (Just '\\', _, _) = True
1031− p (_, '\\', Just c) | ghcSpace c = False
1032− p _ = True
1033− in case span p (zipPrevNext s) of
1034− (l, r) ->
1035− let -- drop the initial '\', any amount of 'ghcSpace', and another '\'
1070+ where
1071+ -- Split a string on gaps (backslash delimited whitespaces)
1072+ --
1073+ -- > splitGaps "bar\\ \\fo\\&o" == ["bar", "fo\\&o"]
1074+ splitGaps :: String -> [String]
1075+ splitGaps "" = []
1076+ splitGaps s =
1077+ let -- A backslash and a whitespace starts a "gap"
1078+ p (Just '\\', _, _) = True
1079+ p (_, '\\', Just c) | ghcSpace c = False
1080+ p _ = True
1081+ in case span p (zipPrevNext s) of
1082+ (l, r) ->
1083+ let -- drop the initial '\', any amount of 'ghcSpace', and another '\'
10361084 r' = drop 1 . dropWhile ghcSpace . drop 1 $ map orig r
1037− in map orig l : splitGaps r'
1038−
1039− -- GHC's definition of whitespaces in strings
1040− -- See: https://gitlab.haskell.org/ghc/ghc/blob/86753475/compiler/parser/Lexer.x#L1653
1041− ghcSpace :: Char -> Bool
1042− ghcSpace c = c <= '\x7f' && is_space c
1043−
1044− -- Add backslashes to the inner side of the strings
1045− --
1046− -- > backslashes ["a", "b", "c"] == ["a\\", "\\b\\", "\\c"]
1047− backslashes :: [String] -> [String]
1048− backslashes (x:y:xs) = (x ++ "\\") : backslashes (('\\' : y) : xs)
1049− backslashes xs = xs
1050−
1051− -- Attaches previous and next items to each list element
1052− zipPrevNext :: [a] -> [(Maybe a, a, Maybe a)]
1053− zipPrevNext xs =
1054− let z = zip (zip (Nothing : map Just xs) xs)
1055− (map Just (tail xs) ++ repeat Nothing)
1056− in map (\((p, x), n) -> (p, x, n)) z
1057−
1058− orig (_, x, _) = x
1085+ in map orig l : splitGaps r'
1086+ -- GHC's definition of whitespaces in strings
1087+ -- See: https://gitlab.haskell.org/ghc/ghc/blob/86753475/compiler/parser/Lexer.x#L1653
1088+ ghcSpace :: Char -> Bool
1089+ ghcSpace c = c <= '\x7f' && is_space c
1090+ -- Add backslashes to the inner side of the strings
1091+ --
1092+ -- > backslashes ["a", "b", "c"] == ["a\\", "\\b\\", "\\c"]
1093+ backslashes :: [String] -> [String]
1094+ backslashes (x : y : xs) = (x ++ "\\") : backslashes (('\\' : y) : xs)
1095+ backslashes xs = xs
1096+ -- Attaches previous and next items to each list element
1097+ zipPrevNext :: [a] -> [(Maybe a, a, Maybe a)]
1098+ zipPrevNext xs =
1099+ let z =
1100+ zip (zip (Nothing : map Just xs) xs)
1101+ (map Just (tail xs) ++ repeat Nothing)
1102+ in map (\((p, x), n) -> (p, x, n)) z
1103+ orig (_, x, _) = x
10591104
10601105----------------------------------------------------------------------------
10611106-- Helpers
10621107
10631108-- | Return the wrapping function controlling the use of braces according to
10641109-- the current layout.
1065−
10661110layoutToBraces :: Layout -> R () -> R ()
10671111layoutToBraces = \case
10681112 SingleLine -> useBraces
@@ -1070,7 +1114,6 @@ layoutToBraces = \case
10701114
10711115-- | Append each element in both lists with semigroups. If one list is shorter
10721116-- than the other, return the rest of the longer list unchanged.
1073−
10741117liftAppend :: Semigroup a => [a] -> [a] -> [a]
10751118liftAppend [] [] = []
10761119liftAppend [] (y : ys) = y : ys
@@ -1085,7 +1128,6 @@ getGRHSSpan (XGRHS NoExt) = notImplemented "XGRHS"
10851128-- | Place a thing that may have a hanging form. This function handles how
10861129-- to separate it from preceding expressions and whether to bump indentation
10871130-- depending on what sort of expression we have.
1088−
10891131placeHanging :: Placement -> R () -> R ()
10901132placeHanging placement m =
10911133 case placement of
@@ -1098,16 +1140,14 @@ placeHanging placement m =
10981140
10991141-- | Check if given block contains single expression which has a hanging
11001142-- form.
1101−
1102−blockPlacement
1103− :: (body -> Placement)
1104− -> [LGRHS GhcPs (Located body)]
1105− -> Placement
1143+blockPlacement ::
1144+ (body -> Placement) ->
1145+ [LGRHS GhcPs (Located body)] ->
1146+ Placement
11061147blockPlacement placer [(L _ (GRHS NoExt _ (L _ x)))] = placer x
11071148blockPlacement _ _ = Normal
11081149
11091150-- | Check if given command has a hanging form.
1110−
11111151cmdPlacement :: HsCmd GhcPs -> Placement
11121152cmdPlacement = \case
11131153 HsCmdLam NoExt _ -> Hanging
@@ -1121,14 +1161,13 @@ cmdTopPlacement = \case
11211161 XCmdTop {} -> notImplemented "XCmdTop"
11221162
11231163-- | Check if given expression has a hanging form.
1124−
11251164exprPlacement :: HsExpr GhcPs -> Placement
11261165exprPlacement = \case
11271166 -- Only hang lambdas with single line parameter lists
11281167 HsLam NoExt mg -> case mg of
1129− MG _ (L _ [L _ (Match NoExt _ (x:xs) _)]) _
1130− | isOneLineSpan (combineSrcSpans' $ fmap getLoc (x :| xs))
1131− -> Hanging
1168+ MG _ (L _ [L _ (Match NoExt _ (x : xs) _)]) _
1169+ | isOneLineSpan (combineSrcSpans' $ fmap getLoc (x :| xs)) ->
1170+ Hanging
11321171 _ -> Normal
11331172 HsLamCase NoExt _ -> Hanging
11341173 HsCase NoExt _ _ -> Hanging
@@ -1145,8 +1184,8 @@ exprPlacement = \case
11451184 -- Indentation breaks if pattern is longer than one line and left
11461185 -- hanging. Consequently, only apply hanging when it is safe.
11471186 if isOneLineSpan s
1148− then Hanging
1149− else Normal
1187+ then Hanging
1188+ else Normal
11501189 _ -> Normal
11511190
11521191withGuards :: [LGRHS GhcPs (Located body)] -> Bool
@@ -1165,11 +1204,13 @@ getOpName = \case
11651204 HsVar NoExt (L _ a) -> Just a
11661205 _ -> Nothing
11671206
1168−p_exprOpTree
1169− :: Bool -- ^ Can use special handling of dollar?
1170− -> BracketStyle -- ^ Bracket style to use
1171− -> OpTree (LHsExpr GhcPs) (LHsExpr GhcPs)
1172− -> R ()
1207+p_exprOpTree ::
1208+ -- | Can use special handling of dollar?
1209+ Bool ->
1210+ -- | Bracket style to use
1211+ BracketStyle ->
1212+ OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) ->
1213+ R ()
11731214p_exprOpTree _ s (OpNode x) = located x (p_hsExpr' s)
11741215p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
11751216 -- NOTE If the beginning of the first argument and the second argument
@@ -1179,8 +1220,8 @@ p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
11791220 if isOneLineSpan
11801221 (mkSrcSpan (srcSpanStart (opTreeLoc x)) (srcSpanStart (opTreeLoc y)))
11811222 then case y of
1182− OpNode (L _ n) -> exprPlacement n
1183− _ -> Normal
1223+ OpNode (L _ n) -> exprPlacement n
1224+ _ -> Normal
11841225 else Normal
11851226 opWrapper = case unLoc op of
11861227 EWildPat NoExt -> backticks
@@ -1194,8 +1235,9 @@ p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
11941235 gotDollar = case getOpName (unLoc op) of
11951236 Just rname -> mkVarOcc "$" == (rdrNameOcc rname)
11961237 _ -> False
1197− switchLayout [opTreeLoc x] $
1198− ub $ p_exprOpTree (not gotDollar) s x
1238+ switchLayout [opTreeLoc x]
1239+ $ ub
1240+ $ p_exprOpTree (not gotDollar) s x
11991241 let p_op = located op (opWrapper . p_hsExpr)
12001242 p_y = switchLayout [opTreeLoc y] (p_exprOpTree True N y)
12011243 if isDollarSpecial && gotDollar && placement == Normal && isOneLineSpan (opTreeLoc x)
@@ -1204,17 +1246,15 @@ p_exprOpTree isDollarSpecial s (OpBranch x op y) = do
12041246 p_op
12051247 breakpoint
12061248 inci p_y
1207− else
1208− placeHanging placement $ do
1209− p_op
1210− space
1211− p_y
1249+ else placeHanging placement $ do
1250+ p_op
1251+ space
1252+ p_y
12121253
12131254-- | Get annotations for the enclosing element.
1214−
12151255getEnclosingAnns :: R [AnnKeywordId]
12161256getEnclosingAnns = do
12171257 e <- getEnclosingSpan (const True)
12181258 case e of
12191259 Nothing -> return []
1220− Just e' -> getAnns (RealSrcSpan e')
1260+ Just e' -> getAnns (RealSrcSpan e')