on master help sign in
Adjust if formatting
61f4960mrkkrp
data/examples/declaration/value/function/case-multi-line-out.hs+2−2
@@ -9,8 +9,8 @@ bar x =
9 case x of
9 case x of
10 5 ->
10 5 ->
11 if x > 5
11 if x > 5
12− then 10
12+ then 10
13− else 12
13+ else 12
14 _ -> 12
14 _ -> 12
15
15
16baz :: Int -> Int
16baz :: Int -> Int
data/examples/declaration/value/function/if-multi-line-out.hs+12−2
@@ -1,5 +1,15 @@
1foo :: Int -> Int
1foo :: Int -> Int
2foo x =
2foo x =
3 if x > 5
3 if x > 5
4− then 10
4+ then 10
5− else 12
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 @@
1foo :: Int -> Int
1foo :: Int -> Int
2foo x = if x > 5 then 10
2foo x = if x > 5 then 10
3 else 12
3 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
7bar x =
7bar x =
8 \y ->
8 \y ->
9 if x > y
9 if x > y
10− then 10
10+ then 10
11− else 12
11+ else 12
src/Ormolu/Printer/Combinators.hs+17−20
@@ -16,7 +16,6 @@ module Ormolu.Printer.Combinators
16 , atom
16 , atom
17 , newline
17 , newline
18 , inci
18 , inci
19− , relaxComments
20 , located
19 , located
21 , locatedVia
20 , locatedVia
22 , located'
21 , located'
@@ -30,6 +29,7 @@ module Ormolu.Printer.Combinators
30 , spaceSep
29 , spaceSep
31 , newlineSep
30 , newlineSep
32 -- ** Wrapping
31 -- ** Wrapping
32+ , sitcc
33 , line
33 , line
34 , backticks
34 , backticks
35 , braces
35 , braces
@@ -43,7 +43,6 @@ module Ormolu.Printer.Combinators
43 )
43 )
44where
44where
45
45
46−import Data.Bool (bool)
47import Data.Data (Data)
46import Data.Data (Data)
48import Data.List (intersperse)
47import Data.List (intersperse)
49import Data.Text (Text)
48import Data.Text (Text)
@@ -94,24 +93,22 @@ locatedVia
94 -> (a -> R ()) -- ^ How to render inner value
93 -> (a -> R ()) -- ^ How to render inner value
95 -> R ()
94 -> R ()
96locatedVia ml loc f = do
95locatedVia ml loc f = do
97− relaxed <- relaxedComments
98− bool sitcc id relaxed $ do
99− let withRealLocated (L l a) g =
96+ let withRealLocated (L l a) g =
100− case l of
97+ case l of
101− UnhelpfulSpan _ -> return ()
98+ UnhelpfulSpan _ -> return ()
102− RealSrcSpan l' -> g (L l' a)
99+ RealSrcSpan l' -> g (L l' a)
103− withRealLocated loc spitPrecedingComments
100+ withRealLocated loc spitPrecedingComments
104− let setEnclosingSpan =
101+ let setEnclosingSpan =
105− case getSpan loc of
102+ case getSpan loc of
106− UnhelpfulSpan _ -> id
103+ UnhelpfulSpan _ -> id
107− RealSrcSpan orf ->
104+ RealSrcSpan orf ->
108− if isModule (unL loc)
105+ if isModule (unL loc)
109− then id
106+ then id
110− else withEnclosingSpan orf
107+ else withEnclosingSpan orf
111− setEnclosingSpan $ case ml of
108+ setEnclosingSpan $ case ml of
112− Nothing -> f (unL loc)
109+ Nothing -> f (unL loc)
113− Just l' -> switchLayout l' (f (unL loc))
110+ Just l' -> switchLayout l' (f (unL loc))
114− withRealLocated loc spitFollowingComments
111+ withRealLocated loc spitFollowingComments
115
112
116-- | A version of 'located' with arguments flipped.
113-- | A version of 'located' with arguments flipped.
117
114
src/Ormolu/Printer/Internal.hs+1−21
@@ -18,11 +18,9 @@ module Ormolu.Printer.Internal
18 , ensureIndent
18 , ensureIndent
19 , inci
19 , inci
20 , sitcc
20 , sitcc
21− , relaxComments
22 , Layout (..)
21 , Layout (..)
23 , enterLayout
22 , enterLayout
24 , vlayout
23 , vlayout
25− , relaxedComments
26 -- * Special helpers for comment placement
24 -- * Special helpers for comment placement
27 , trimSpanStream
25 , trimSpanStream
28 , nextEltSpan
26 , nextEltSpan
@@ -206,7 +204,7 @@ inci m' = do
206-- | Set indentation level for the inner computation equal to current
204-- | Set indentation level for the inner computation equal to current
207-- column. This makes sure that the entire inner block is uniformly
205-- column. This makes sure that the entire inner block is uniformly
208-- \"shifted\" to the right. Only works (and makes sense) when enclosing
206-- \"shifted\" to the right. Only works (and makes sense) when enclosing
209−-- layout is multilined.
207+-- layout is multi-line.
210
208
211sitcc :: R () -> R ()
209sitcc :: R () -> R ()
212sitcc m' = do
210sitcc m' = do
@@ -220,19 +218,6 @@ sitcc m' = do
220 vlayout m' (R (local modRC m))
218 vlayout m' (R (local modRC m))
221 traceR "sitcc_ended" Nothing
219 traceR "sitcc_ended" Nothing
222
220
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−
236-- | Set 'Layout' for internal computation.
221-- | Set 'Layout' for internal computation.
237
222
238enterLayout :: Layout -> R () -> R ()
223enterLayout :: Layout -> R () -> R ()
@@ -261,11 +246,6 @@ vlayout sline mline = do
261 SingleLine -> sline
246 SingleLine -> sline
262 MultiLine -> mline
247 MultiLine -> mline
263
248
264−-- | Check whether we're in a region with relaxed comments placement.
265−
266−relaxedComments :: R Bool
267−relaxedComments = R (asks rcRelaxedComments)
268−
269----------------------------------------------------------------------------
249----------------------------------------------------------------------------
270-- Special helpers for comment placement
250-- Special helpers for comment placement
271
251
src/Ormolu/Printer/Meat/Declaration/Data.hs+4−4
@@ -39,19 +39,19 @@ p_dataDecl name tvars HsDataDefn {..} = do
39 Just k -> do
39 Just k -> do
40 space
40 space
41 txt ":: "
41 txt ":: "
42− relaxComments (located k p_hsType)
42+ located k p_hsType
43 let gadt = isJust dd_kindSig || any (isGadt . unL) dd_cons
43 let gadt = isJust dd_kindSig || any (isGadt . unL) dd_cons
44 if gadt
44 if gadt
45 then do
45 then do
46 txt " where"
46 txt " where"
47 newline
47 newline
48− inci $ newlineSep (located' p_conDecl) dd_cons
48+ inci $ newlineSep (sitcc . located' p_conDecl) dd_cons
49 else switchLayout (combineSrcSpans' (getSpan name :| (getSpan <$> dd_cons))) $ do
49 else switchLayout (combineSrcSpans' (getSpan name :| (getSpan <$> dd_cons))) $ do
50 breakpoint
50 breakpoint
51 inci $ do
51 inci $ do
52 txt "= "
52 txt "= "
53 let sep = vlayout (txt " | ") (txt "| ")
53 let sep = vlayout (txt " | ") (txt "| ")
54− velt $ withSep sep (located' p_conDecl) dd_cons
54+ velt $ withSep sep (sitcc . located' p_conDecl) dd_cons
55 newline
55 newline
56 inci . located dd_derivs $ \xs ->
56 inci . located dd_derivs $ \xs ->
57 forM_ xs (line . located' p_hsDerivingClause)
57 forM_ xs (line . located' p_hsDerivingClause)
@@ -62,7 +62,7 @@ p_conDecl = \case
62 [ spaceSep (located' p_rdrName) con_names
62 [ spaceSep (located' p_rdrName) con_names
63 , inci $ do
63 , inci $ do
64 txt ":: "
64 txt ":: "
65− relaxComments (locatedVia Nothing (hsib_body con_type) p_hsType)
65+ locatedVia Nothing (hsib_body con_type) p_hsType
66 ]
66 ]
67 ConDeclH98 {..} -> do
67 ConDeclH98 {..} -> do
68 case hsq_explicit <$> con_qvars of
68 case hsq_explicit <$> con_qvars of
src/Ormolu/Printer/Meat/Declaration/Signature.hs+1−1
@@ -33,4 +33,4 @@ p_typeSig names HsWC {..} = do
33 breakpoint
33 breakpoint
34 inci $ do
34 inci $ do
35 txt ":: "
35 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
28 unless (null hsq_explicit) space
28 unless (null hsq_explicit) space
29 spaceSep (located' p_hsTyVarBndr) hsq_explicit
29 spaceSep (located' p_hsTyVarBndr) hsq_explicit
30 breakpoint
30 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 =
50 NoSig -> Nothing
50 NoSig -> Nothing
51 KindSig k -> Just $ do
51 KindSig k -> Just $ do
52 if injAnn then txt "= " else txt ":: "
52 if injAnn then txt "= " else txt ":: "
53− relaxComments (located k p_hsType)
53+ located k p_hsType
54 TyVarSig bndr -> Just $ do
54 TyVarSig bndr -> Just $ do
55 if injAnn then txt "= " else txt ":: "
55 if injAnn then txt "= " else txt ":: "
56− relaxComments (located bndr p_hsTyVarBndr)
56+ located bndr p_hsTyVarBndr
57
57
58p_injectivityAnn :: InjectivityAnn GhcPs -> R ()
58p_injectivityAnn :: InjectivityAnn GhcPs -> R ()
59p_injectivityAnn (InjectivityAnn a bs) = do
59p_injectivityAnn (InjectivityAnn a bs) = do
src/Ormolu/Printer/Meat/Declaration/Value.hs+10−6
@@ -193,18 +193,22 @@ p_hsExpr = \case
193 txt "if "
193 txt "if "
194 located if' p_hsExpr
194 located if' p_hsExpr
195 breakpoint
195 breakpoint
196− inci (txt "then ")
196+ txt "then"
197− located then' p_hsExpr
197+ located then' $ \x -> do
198+ breakpoint
199+ inci (p_hsExpr x)
198 breakpoint
200 breakpoint
199− inci (txt "else ")
201+ txt "else"
200− located else' p_hsExpr
202+ located else' $ \x -> do
203+ breakpoint
204+ inci (p_hsExpr x)
201 HsMultiIf {} -> notImplemented "MulitiIf"
205 HsMultiIf {} -> notImplemented "MulitiIf"
202 HsLet localBinds e -> do
206 HsLet localBinds e -> do
203 txt "let "
207 txt "let "
204− inci (located localBinds p_hsLocalBinds)
208+ sitcc (located localBinds p_hsLocalBinds)
205 breakpoint
209 breakpoint
206 txt "in "
210 txt "in "
207− located e p_hsExpr
211+ sitcc (located e p_hsExpr)
208 HsDo {} -> notImplemented "HsDo"
212 HsDo {} -> notImplemented "HsDo"
209 ExplicitList _ _ xs -> do
213 ExplicitList _ _ xs -> do
210 brackets $ velt (withSep comma (located' p_hsExpr) xs)
214 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
18
18
19p_hsmodExports :: [LIE GhcPs] -> R ()
19p_hsmodExports :: [LIE GhcPs] -> R ()
20p_hsmodExports xs = do
20p_hsmodExports xs = do
21− parens . velt $ withSep comma (located' p_lie) xs
21+ parens . velt $ withSep comma (sitcc . located' p_lie) xs
22
22
23p_hsmodImport :: ImportDecl GhcPs -> R ()
23p_hsmodImport :: ImportDecl GhcPs -> R ()
24p_hsmodImport ImportDecl {..} = line $ do
24p_hsmodImport ImportDecl {..} = line $ do
src/Ormolu/Printer/Meat/Type.hs+2−2
@@ -129,7 +129,7 @@ p_hsAppType = \case
129
129
130p_conDeclFields :: [LConDeclField GhcPs] -> R ()
130p_conDeclFields :: [LConDeclField GhcPs] -> R ()
131p_conDeclFields =
131p_conDeclFields =
132− braces . velt . withSep comma (located' p_conDeclField)
132+ braces . velt . withSep comma (sitcc . located' p_conDeclField)
133
133
134p_conDeclField :: ConDeclField GhcPs -> R ()
134p_conDeclField :: ConDeclField GhcPs -> R ()
135p_conDeclField ConDeclField {..} = velt'
135p_conDeclField ConDeclField {..} = velt'
@@ -139,5 +139,5 @@ p_conDeclField ConDeclField {..} = velt'
139 cd_fld_names
139 cd_fld_names
140 , inci $ do
140 , inci $ do
141 txt ":: "
141 txt ":: "
142− relaxComments (locatedVia Nothing cd_fld_type p_hsType)
142+ locatedVia Nothing cd_fld_type p_hsType
143 ]
143 ]