on master help sign in
Adjust if formatting

This includes a refactoring which changes the default strategy for indentation level bumping. I think it's an improvement overall.

commit61f49600f8904aedf4b5ad7a86d3e0284cd0607d authormrkkrp <mark.karpov@tweag.io> dateWed 22 May 2019, 16:43 (7 years ago) parent2e17609
13 files changed+63−64
data/examples/declaration/value/function/case-multi-line-out.hs+2−2 data/examples/declaration/value/function/if-multi-line-out.hs+12−2 data/examples/declaration/value/function/if-multi-line.hs+8 data/examples/declaration/value/function/lambda-multi-line-out.hs+2−2 src/Ormolu/Printer/Combinators.hs+17−20 src/Ormolu/Printer/Internal.hs+1−21 src/Ormolu/Printer/Meat/Declaration/Data.hs+4−4 src/Ormolu/Printer/Meat/Declaration/Signature.hs+1−1 src/Ormolu/Printer/Meat/Declaration/Type.hs+1−1 src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs+2−2 src/Ormolu/Printer/Meat/Declaration/Value.hs+10−6 src/Ormolu/Printer/Meat/ImportExport.hs+1−1 src/Ormolu/Printer/Meat/Type.hs+2−2
data/examples/declaration/value/function/case-multi-line-out.hs+2−2
@@ -9,8 +9,8 @@ bar x =
99 case x of
1010 5 ->
1111 if x > 5
12− then 10
13− else 12
12+ then 10
13+ else 12
1414 _ -> 12
1515
1616baz :: Int -> Int
data/examples/declaration/value/function/if-multi-line-out.hs+12−2
@@ -1,5 +1,15 @@
11foo :: Int -> Int
22foo x =
33 if x > 5
4− then 10
5− else 12
4+ then 10
5+ else 12
6+
7+bar :: Int -> Int
8+bar x =
9+ if x > 5
10+ then
11+ foo x
12+ + 100
13+ else
14+ foo x
15+ + 200
data/examples/declaration/value/function/if-multi-line.hs+8
@@ -1,3 +1,11 @@
11foo :: Int -> Int
22foo x = if x > 5 then 10
33 else 12
4+
5+bar :: Int -> Int
6+bar x =
7+ if x > 5
8+ then foo x
9+ + 100
10+ else foo x
11+ + 200
data/examples/declaration/value/function/lambda-multi-line-out.hs+2−2
@@ -7,5 +7,5 @@ bar :: Int -> Int -> Int
77bar x =
88 \y ->
99 if x > y
10− then 10
11− else 12
10+ then 10
11+ else 12
src/Ormolu/Printer/Combinators.hs+17−20
@@ -16,7 +16,6 @@ module Ormolu.Printer.Combinators
1616 , atom
1717 , newline
1818 , inci
19− , relaxComments
2019 , located
2120 , locatedVia
2221 , located'
@@ -30,6 +29,7 @@ module Ormolu.Printer.Combinators
3029 , spaceSep
3130 , newlineSep
3231 -- ** Wrapping
32+ , sitcc
3333 , line
3434 , backticks
3535 , braces
@@ -43,7 +43,6 @@ module Ormolu.Printer.Combinators
4343 )
4444where
4545
46−import Data.Bool (bool)
4746import Data.Data (Data)
4847import Data.List (intersperse)
4948import Data.Text (Text)
@@ -94,24 +93,22 @@ locatedVia
9493 -> (a -> R ()) -- ^ How to render inner value
9594 -> R ()
9695locatedVia ml loc f = do
97− relaxed <- relaxedComments
98− bool sitcc id relaxed $ do
99− let withRealLocated (L l a) g =
100− case l of
101− UnhelpfulSpan _ -> return ()
102− RealSrcSpan l' -> g (L l' a)
103− withRealLocated loc spitPrecedingComments
104− let setEnclosingSpan =
105− case getSpan loc of
106− UnhelpfulSpan _ -> id
107− RealSrcSpan orf ->
108− if isModule (unL loc)
109− then id
110− else withEnclosingSpan orf
111− setEnclosingSpan $ case ml of
112− Nothing -> f (unL loc)
113− Just l' -> switchLayout l' (f (unL loc))
114− withRealLocated loc spitFollowingComments
96+ let withRealLocated (L l a) g =
97+ case l of
98+ UnhelpfulSpan _ -> return ()
99+ RealSrcSpan l' -> g (L l' a)
100+ withRealLocated loc spitPrecedingComments
101+ let setEnclosingSpan =
102+ case getSpan loc of
103+ UnhelpfulSpan _ -> id
104+ RealSrcSpan orf ->
105+ if isModule (unL loc)
106+ then id
107+ else withEnclosingSpan orf
108+ setEnclosingSpan $ case ml of
109+ Nothing -> f (unL loc)
110+ Just l' -> switchLayout l' (f (unL loc))
111+ withRealLocated loc spitFollowingComments
115112
116113-- | A version of 'located' with arguments flipped.
117114
src/Ormolu/Printer/Internal.hs+1−21
@@ -18,11 +18,9 @@ module Ormolu.Printer.Internal
1818 , ensureIndent
1919 , inci
2020 , sitcc
21− , relaxComments
2221 , Layout (..)
2322 , enterLayout
2423 , vlayout
25− , relaxedComments
2624 -- * Special helpers for comment placement
2725 , trimSpanStream
2826 , nextEltSpan
@@ -206,7 +204,7 @@ inci m' = do
206204-- | Set indentation level for the inner computation equal to current
207205-- column. This makes sure that the entire inner block is uniformly
208206-- \"shifted\" to the right. Only works (and makes sense) when enclosing
209−-- layout is multilined.
207+-- layout is multi-line.
210208
211209sitcc :: R () -> R ()
212210sitcc m' = do
@@ -220,19 +218,6 @@ sitcc m' = do
220218 vlayout m' (R (local modRC m))
221219 traceR "sitcc_ended" Nothing
222220
223−-- | Relax alignment rules for comments inside this block. This is usually
224−-- done to avoid bumping indentation level too aggressively. Important for
225−-- beautiful rendering of e.g. type signatures.
226−
227−relaxComments :: R () -> R ()
228−relaxComments (R m) = do
229− traceR "relax_start" Nothing
230− let modRC rc = rc
231− { rcRelaxedComments = True
232− }
233− R (local modRC m)
234− traceR "relax_end" Nothing
235−
236221-- | Set 'Layout' for internal computation.
237222
238223enterLayout :: Layout -> R () -> R ()
@@ -261,11 +246,6 @@ vlayout sline mline = do
261246 SingleLine -> sline
262247 MultiLine -> mline
263248
264−-- | Check whether we're in a region with relaxed comments placement.
265−
266−relaxedComments :: R Bool
267−relaxedComments = R (asks rcRelaxedComments)
268−
269249----------------------------------------------------------------------------
270250-- Special helpers for comment placement
271251
src/Ormolu/Printer/Meat/Declaration/Data.hs+4−4
@@ -39,19 +39,19 @@ p_dataDecl name tvars HsDataDefn {..} = do
3939 Just k -> do
4040 space
4141 txt ":: "
42− relaxComments (located k p_hsType)
42+ located k p_hsType
4343 let gadt = isJust dd_kindSig || any (isGadt . unL) dd_cons
4444 if gadt
4545 then do
4646 txt " where"
4747 newline
48− inci $ newlineSep (located' p_conDecl) dd_cons
48+ inci $ newlineSep (sitcc . located' p_conDecl) dd_cons
4949 else switchLayout (combineSrcSpans' (getSpan name :| (getSpan <$> dd_cons))) $ do
5050 breakpoint
5151 inci $ do
5252 txt "= "
5353 let sep = vlayout (txt " | ") (txt "| ")
54− velt $ withSep sep (located' p_conDecl) dd_cons
54+ velt $ withSep sep (sitcc . located' p_conDecl) dd_cons
5555 newline
5656 inci . located dd_derivs $ \xs ->
5757 forM_ xs (line . located' p_hsDerivingClause)
@@ -62,7 +62,7 @@ p_conDecl = \case
6262 [ spaceSep (located' p_rdrName) con_names
6363 , inci $ do
6464 txt ":: "
65− relaxComments (locatedVia Nothing (hsib_body con_type) p_hsType)
65+ locatedVia Nothing (hsib_body con_type) p_hsType
6666 ]
6767 ConDeclH98 {..} -> do
6868 case hsq_explicit <$> con_qvars of
src/Ormolu/Printer/Meat/Declaration/Signature.hs+1−1
@@ -33,4 +33,4 @@ p_typeSig names HsWC {..} = do
3333 breakpoint
3434 inci $ do
3535 txt ":: "
36− relaxComments $ located (hsib_body hswc_body) p_hsType
36+ located (hsib_body hswc_body) p_hsType
src/Ormolu/Printer/Meat/Declaration/Type.hs+1−1
@@ -28,4 +28,4 @@ p_synDecl name tvars t = line $ do
2828 unless (null hsq_explicit) space
2929 spaceSep (located' p_hsTyVarBndr) hsq_explicit
3030 breakpoint
31− inci (txt "= " >> relaxComments (located t p_hsType))
31+ inci (txt "= " >> located t p_hsType)
src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs+2−2
@@ -50,10 +50,10 @@ p_familyResultSigL injAnn l =
5050 NoSig -> Nothing
5151 KindSig k -> Just $ do
5252 if injAnn then txt "= " else txt ":: "
53− relaxComments (located k p_hsType)
53+ located k p_hsType
5454 TyVarSig bndr -> Just $ do
5555 if injAnn then txt "= " else txt ":: "
56− relaxComments (located bndr p_hsTyVarBndr)
56+ located bndr p_hsTyVarBndr
5757
5858p_injectivityAnn :: InjectivityAnn GhcPs -> R ()
5959p_injectivityAnn (InjectivityAnn a bs) = do
src/Ormolu/Printer/Meat/Declaration/Value.hs+10−6
@@ -193,18 +193,22 @@ p_hsExpr = \case
193193 txt "if "
194194 located if' p_hsExpr
195195 breakpoint
196− inci (txt "then ")
197− located then' p_hsExpr
196+ txt "then"
197+ located then' $ \x -> do
198+ breakpoint
199+ inci (p_hsExpr x)
198200 breakpoint
199− inci (txt "else ")
200− located else' p_hsExpr
201+ txt "else"
202+ located else' $ \x -> do
203+ breakpoint
204+ inci (p_hsExpr x)
201205 HsMultiIf {} -> notImplemented "MulitiIf"
202206 HsLet localBinds e -> do
203207 txt "let "
204− inci (located localBinds p_hsLocalBinds)
208+ sitcc (located localBinds p_hsLocalBinds)
205209 breakpoint
206210 txt "in "
207− located e p_hsExpr
211+ sitcc (located e p_hsExpr)
208212 HsDo {} -> notImplemented "HsDo"
209213 ExplicitList _ _ xs -> do
210214 brackets $ velt (withSep comma (located' p_hsExpr) xs)
src/Ormolu/Printer/Meat/ImportExport.hs+1−1
@@ -18,7 +18,7 @@ import Ormolu.Printer.Meat.Common
1818
1919p_hsmodExports :: [LIE GhcPs] -> R ()
2020p_hsmodExports xs = do
21− parens . velt $ withSep comma (located' p_lie) xs
21+ parens . velt $ withSep comma (sitcc . located' p_lie) xs
2222
2323p_hsmodImport :: ImportDecl GhcPs -> R ()
2424p_hsmodImport ImportDecl {..} = line $ do
src/Ormolu/Printer/Meat/Type.hs+2−2
@@ -129,7 +129,7 @@ p_hsAppType = \case
129129
130130p_conDeclFields :: [LConDeclField GhcPs] -> R ()
131131p_conDeclFields =
132− braces . velt . withSep comma (located' p_conDeclField)
132+ braces . velt . withSep comma (sitcc . located' p_conDeclField)
133133
134134p_conDeclField :: ConDeclField GhcPs -> R ()
135135p_conDeclField ConDeclField {..} = velt'
@@ -139,5 +139,5 @@ p_conDeclField ConDeclField {..} = velt'
139139 cd_fld_names
140140 , inci $ do
141141 txt ":: "
142− relaxComments (locatedVia Nothing cd_fld_type p_hsType)
142+ locatedVia Nothing cd_fld_type p_hsType
143143 ]