on master help sign in
Introduce the ‘inciIf’ combinator
commit6dfd6f088e062c2fd9f67b26fb44652cb9d953a0 authorMark Karpov <markkarpov92@gmail.com> dateMon 8 Jun 2020, 11:54 (6 years ago) parent903d8d9
9 files changed+39−44
src/Ormolu/Printer/Combinators.hs+11−3 src/Ormolu/Printer/Meat/Common.hs+5−5 src/Ormolu/Printer/Meat/Declaration/Class.hs+1−1 src/Ormolu/Printer/Meat/Declaration/Data.hs+2−6 src/Ormolu/Printer/Meat/Declaration/Instance.hs+1−3 src/Ormolu/Printer/Meat/Declaration/Signature.hs+1−2 src/Ormolu/Printer/Meat/Declaration/Type.hs+1−1 src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs+3−3 src/Ormolu/Printer/Meat/Declaration/Value.hs+14−20
src/Ormolu/Printer/Combinators.hs+11−3
@@ -21,6 +21,7 @@ module Ormolu.Printer.Combinators
2121 space,
2222 newline,
2323 inci,
24+ inciIf,
2425 located,
2526 located',
2627 switchLayout,
@@ -73,6 +74,15 @@ import SrcLoc
7374----------------------------------------------------------------------------
7475-- Basic
7576
77+-- | Indent the inner expression if the first argument is 'True'.
78+inciIf ::
79+ -- | Whether to indent
80+ Bool ->
81+ -- | The expression to indent
82+ R () ->
83+ R ()
84+inciIf b m = if b then inci m else m
85+
7686-- | Enter a 'Located' entity. This combinator handles outputting comments
7787-- and sets layout (single-line vs multi-line) for the inner computation.
7888-- Roughly, the rule for using 'located' is that every time there is a
@@ -270,9 +280,7 @@ brackets_ needBreaks open close style m = sitcc (vlayout singleLine multiLine)
270280 then newline >> inci m
271281 else space >> sitcc m
272282 newline
273− case style of
274− N -> txt close
275− S -> inci (txt close)
283+ inciIf (style == S) (txt close)
276284
277285----------------------------------------------------------------------------
278286-- Literals
src/Ormolu/Printer/Meat/Common.hs+5−5
@@ -114,14 +114,14 @@ p_qualName mname occName = do
114114p_infixDefHelper ::
115115 -- | Whether to format in infix style
116116 Bool ->
117− -- | Indentation-bumping wrapper
118− (R () -> R ()) ->
117+ -- | Whether to bump indentation for arguments
118+ Bool ->
119119 -- | How to print the operator\/name
120120 R () ->
121121 -- | How to print the arguments
122122 [R ()] ->
123123 R ()
124−p_infixDefHelper isInfix inci' name args =
124+p_infixDefHelper isInfix indentArgs name args =
125125 case (isInfix, args) of
126126 (True, p0 : p1 : ps) -> do
127127 let parens' =
@@ -135,14 +135,14 @@ p_infixDefHelper isInfix inci' name args =
135135 name
136136 space
137137 p1
138− unless (null ps) . inci' $ do
138+ unless (null ps) . inciIf indentArgs $ do
139139 breakpoint
140140 sitcc (sep breakpoint sitcc ps)
141141 (_, ps) -> do
142142 name
143143 unless (null ps) $ do
144144 breakpoint
145− inci' $ sitcc (sep breakpoint sitcc args)
145+ inciIf indentArgs $ sitcc (sep breakpoint sitcc args)
146146
147147-- | Print a Haddock.
148148p_hsDocString ::
src/Ormolu/Printer/Meat/Declaration/Class.hs+1−1
@@ -58,7 +58,7 @@ p_classDecl ctx name HsQTvs {..} fixity fdeps csigs cdefs cats catdefs cdocs = d
5858 switchLayout signatureSpans $
5959 p_infixDefHelper
6060 (isInfix fixity)
61− inci
61+ True
6262 (p_rdrName name)
6363 (located' p_hsTyVarBndr <$> hsq_explicit)
6464 inci (p_classFundeps fdeps)
src/Ormolu/Printer/Meat/Declaration/Data.hs+2−6
@@ -41,7 +41,7 @@ p_dataDecl style name tpats fixity HsDataDefn {..} = do
4141 inci $
4242 p_infixDefHelper
4343 (isInfix fixity)
44− inci
44+ True
4545 (p_rdrName name)
4646 (located' p_hsType <$> tpats)
4747 case dd_kindSig of
@@ -157,11 +157,7 @@ p_conDecl singleConstRec = \case
157157 RecCon l -> do
158158 p_rdrName con_name
159159 breakpoint
160− let inci' =
161− if singleConstRec
162− then id
163− else inci
164− inci' (located l p_conDeclFields)
160+ inciIf (not singleConstRec) (located l p_conDeclFields)
165161 InfixCon x y -> do
166162 located x p_hsType
167163 breakpoint
src/Ormolu/Printer/Meat/Declaration/Instance.hs+1−3
@@ -32,9 +32,7 @@ p_standaloneDerivDecl DerivDecl {..} = do
3232 txt "instance"
3333 breakpoint
3434 match_overlap_mode deriv_overlap_mode breakpoint
35− if toIndent
36− then inci typesAfterInstance
37− else typesAfterInstance
35+ inciIf toIndent typesAfterInstance
3836 txt "deriving"
3937 space
4038 case deriv_strategy of
src/Ormolu/Printer/Meat/Declaration/Signature.hs+1−2
@@ -14,7 +14,6 @@ where
1414import BasicTypes
1515import BooleanFormula
1616import Control.Monad
17−import Data.Bool (bool)
1817import GHC
1918import Ormolu.Printer.Combinators
2019import Ormolu.Printer.Meat.Common
@@ -48,7 +47,7 @@ p_typeSig indentTail (n : ns) hswc = do
4847 p_rdrName n
4948 if null ns
5049 then p_typeAscription hswc
51− else bool id inci indentTail $ do
50+ else inciIf indentTail $ do
5251 commaDel
5352 sep commaDel p_rdrName ns
5453 p_typeAscription hswc
src/Ormolu/Printer/Meat/Declaration/Type.hs+1−1
@@ -28,7 +28,7 @@ p_synDecl name fixity HsQTvs {..} t = do
2828 switchLayout (getLoc name : map getLoc hsq_explicit) $
2929 p_infixDefHelper
3030 (case fixity of Infix -> True; _ -> False)
31− inci
31+ True
3232 (p_rdrName name)
3333 (map (located' p_hsTyVarBndr) hsq_explicit)
3434 space
src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs+3−3
@@ -31,7 +31,7 @@ p_famDecl style FamilyDecl {fdTyVars = HsQTvs {..}, ..} = do
3131 switchLayout (getLoc fdLName : (getLoc <$> hsq_explicit)) $
3232 p_infixDefHelper
3333 (isInfix fdFixity)
34− inci
34+ True
3535 (p_rdrName fdLName)
3636 (located' p_hsTyVarBndr <$> hsq_explicit)
3737 let resultSig = p_familyResultSigL fdResultSig
@@ -92,12 +92,12 @@ p_tyFamInstEqn HsIB {hsib_body = FamEqn {..}} = do
9292 Just bndrs -> do
9393 p_forallBndrs ForallInvis p_hsTyVarBndr bndrs
9494 breakpoint
95− (if null feqn_bndrs then id else inci) $ do
95+ inciIf (not $ null feqn_bndrs) $ do
9696 let famLhsSpn = getLoc feqn_tycon : fmap (getLoc . typeArgToType) feqn_pats
9797 switchLayout famLhsSpn $
9898 p_infixDefHelper
9999 (isInfix feqn_fixity)
100− inci
100+ True
101101 (p_rdrName feqn_tycon)
102102 (located' p_hsType . typeArgToType <$> feqn_pats)
103103 space
src/Ormolu/Printer/Meat/Declaration/Value.hs+14−20
@@ -179,25 +179,22 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
179179 NoSrcStrict -> return ()
180180 SrcStrict -> txt "!"
181181 SrcLazy -> txt "~"
182− inci' <- case NE.nonEmpty m_pats of
183− Nothing -> id <$ case style of
182+ indentBody <- case NE.nonEmpty m_pats of
183+ Nothing -> False <$ case style of
184184 Function name -> p_rdrName name
185185 _ -> return ()
186186 Just ne_pats -> do
187187 let combinedSpans =
188188 combineSrcSpans' $
189189 getLoc <$> ne_pats
190− inci' =
191− if isOneLineSpan combinedSpans
192− then id
193− else inci
190+ indentBody = not (isOneLineSpan combinedSpans)
194191 switchLayout [combinedSpans] $ do
195192 let stdCase = sep breakpoint (located' p_pat) m_pats
196193 case style of
197194 Function name ->
198195 p_infixDefHelper
199196 isInfix
200− inci'
197+ indentBody
201198 (p_rdrName name)
202199 (located' p_pat <$> m_pats)
203200 PatternBind -> stdCase
@@ -212,7 +209,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
212209 when needsSpace space
213210 sitcc stdCase
214211 LambdaCase -> stdCase
215− return inci'
212+ return indentBody
216213 let -- Calculate position of end of patterns. This is useful when we decide
217214 -- about putting certain constructions in hanging positions.
218215 endOfPats = case NE.nonEmpty m_pats of
@@ -224,7 +221,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
224221 Case -> True
225222 LambdaCase -> True
226223 _ -> False
227− let hasGuards = withGuards grhssGRHSs
224+ hasGuards = withGuards grhssGRHSs
228225 grhssSpan =
229226 combineSrcSpans' $
230227 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
@@ -253,7 +250,7 @@ p_match' placer render style isInfix strictness m_pats GRHSs {..} = do
253250 txt "where"
254251 unless whereIsEmpty breakpoint
255252 inci $ located grhssLocalBinds p_hsLocalBinds
256− inci' $ do
253+ inciIf indentBody $ do
257254 unless (length grhssGRHSs > 1) $
258255 case style of
259256 Function _ | hasGuards -> return ()
@@ -589,23 +586,20 @@ p_hsExpr' s = \case
589586 -- do-block or case expression it is not a good idea. It seems
590587 -- to be safe to always bump indentation when the function
591588 -- expression is parenthesised.
592− indent =
589+ doIndent =
593590 case func of
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
591+ L _ (HsPar NoExtField _) -> True
592+ L _ (HsAppType NoExtField _ _) -> True
593+ L _ (HsMultiIf NoExtField _) -> True
594+ L spn _ -> isOneLineSpan spn
601595 ub <- getLayout <&> \case
602596 SingleLine -> useBraces
603597 MultiLine -> id
604598 ub $ do
605599 located func (p_hsExpr' s)
606600 breakpoint
607− indent $ sep breakpoint (located' p_hsExpr) initp
608− indent $ do
601+ inciIf doIndent $ sep breakpoint (located' p_hsExpr) initp
602+ inciIf doIndent $ do
609603 unless (null initp) breakpoint
610604 located lastp p_hsExpr
611605 Hanging -> do