on master help sign in
Various adjustments

The main point of this commit is perhaps that certain constructions are rare and unnatural in multi-line form, so we could print them always in single-line form however putting parentheses still in multi-line fashion so the properly of idempotency is satisfied.

commit777524b6de237b25b60c44054569e16fdab3d762 authormrkkrp <mark.karpov@tweag.io> dateTue 4 Jun 2019, 22:38 (7 years ago) parent45e25a1
7 files changed+52−51
data/examples/declaration/value/function/arithmetic-sequences-out.hs+2−2 data/examples/declaration/value/function/multi-way-if-out.hs+4−5 data/examples/declaration/value/function/tuple-sections-out.hs+3−10 data/examples/declaration/value/function/unboxed-sums-out.hs+4−11 data/examples/declaration/value/function/unboxed-tuples-out.hs+2−1 src/Ormolu/Printer/Combinators.hs+4−3 src/Ormolu/Printer/Meat/Declaration/Value.hs+33−19
data/examples/declaration/value/function/arithmetic-sequences-out.hs+2−2
@@ -1,4 +1,4 @@
1−foo = [0 ..]
1+foo = [0..]
22
33bar x =
44 [ 0
@@ -11,4 +11,4 @@ baz x =
1111 .. x
1212 ]
1313
14−barbaz x = [0, 1 ..]
14+barbaz x = [0, 1..]
data/examples/declaration/value/function/multi-way-if-out.hs+4−5
@@ -2,8 +2,7 @@
22foo x = if | x == 5 -> 5
33
44bar x y =
5− if
6− | x > y -> x
7− | x < y ->
8− y
9− | otherwise -> x
5+ if | x > y -> x
6+ | x < y ->
7+ y
8+ | otherwise -> x
data/examples/declaration/value/function/tuple-sections-out.hs+3−10
@@ -1,15 +1,8 @@
11{-# LANGUAGE TupleSections #-}
2−foo = (, 2)
2+foo = (,2)
33
4−bar = (, 5, )
4+bar = (,5,)
55
66baz =
7− (
8− ,
9− , 5
10− , 6
11− , 7
12− ,
13− ,
14− ,
7+ ( ,,5,6,7,,,
158 )
data/examples/declaration/value/function/unboxed-sums-out.hs+4−11
@@ -1,15 +1,8 @@
11{-# LANGUAGE UnboxedSums #-}
2−foo = (# 1| #)
2+foo = (# 1 | #)
33
4−bar = (# | | 2| #)
4+bar = (# | | 2 | #)
55
66baz =
7− (#
8− |
9− |
10− | 10
11− |
12− |
13− |
14− |
15− | #)
7+ (# | | | 10 | | | | |
8+ #)
data/examples/declaration/value/function/unboxed-tuples-out.hs+2−1
@@ -4,4 +4,5 @@ foo = (# 1, 2, 3 #)
44bar =
55 (# 1
66 , 2
7− , 3 #)
7+ , 3
8+ #)
src/Ormolu/Printer/Combinators.hs+4−3
@@ -239,9 +239,9 @@ brackets m = sitcc $ do
239239
240240bracketsPar :: R () -> R ()
241241bracketsPar m = sitcc $ do
242− txt "[:"
242+ txt "[: "
243243 m
244− txt ":]"
244+ txt " :]"
245245
246246-- | Surround given entity by parentheses @(@ and @)@.
247247
@@ -257,7 +257,8 @@ parensHash :: R () -> R ()
257257parensHash m = sitcc $ do
258258 txt "(# "
259259 m
260− txt " #)"
260+ breakpoint
261+ txt "#)"
261262
262263-- | Surround given entity by optional space before and a newline after, iff
263264-- current layout is multiline.
src/Ormolu/Printer/Meat/Declaration/Value.hs+33−19
@@ -15,12 +15,12 @@ import Data.List (sortOn)
1515import GHC
1616import Ormolu.Printer.Combinators
1717import Ormolu.Printer.Meat.Common
18−import Ormolu.Printer.Meat.Type
1918import Ormolu.Printer.Meat.Declaration.Pat
2019import Ormolu.Printer.Meat.Declaration.Signature
20+import Ormolu.Printer.Meat.Type
2121import Ormolu.Utils
22−import SrcLoc (isOneLineSpan)
2322import Outputable (Outputable (..))
23+import SrcLoc (isOneLineSpan)
2424import qualified Data.List.NonEmpty as NE
2525
2626p_valDecl :: HsBindLR GhcPs GhcPs -> R ()
@@ -111,14 +111,14 @@ p_match style m_pats m_grhss = do
111111 case style of
112112 Lambda -> return ()
113113 _ -> breakpoint
114− newlineSep (located' (p_grhs Pattern)) grhssGRHSs
114+ newlineSep (located' (p_grhs Guard)) grhssGRHSs
115115 unless (GHC.isEmptyLocalBindsPR (unL grhssLocalBinds)) $ do
116116 newline
117117 line (txt "where")
118118 inci (located grhssLocalBinds p_hsLocalBinds)
119119
120120data GroupStyle
121− = Pattern
121+ = Guard
122122 | MultiIf
123123
124124p_grhs :: GroupStyle -> GRHS GhcPs (LHsExpr GhcPs) -> R ()
@@ -130,7 +130,7 @@ p_grhs style (GRHS NoExt guards body) =
130130 velt $ withSep comma (located' p_stmt) xs
131131 space
132132 txt $ case style of
133− Pattern -> "="
133+ Guard -> "="
134134 MultiIf -> "->"
135135 breakpoint
136136 inci p_body
@@ -234,17 +234,32 @@ p_hsExpr = \case
234234 breakpoint
235235 inci (located x p_hsExpr)
236236 ExplicitTuple NoExt args boxity -> do
237+ let isSection = any (isMissing . unL) args
238+ isMissing = \case
239+ Missing NoExt -> True
240+ _ -> False
237241 let parens' =
238242 case boxity of
239243 Boxed -> parens
240244 Unboxed -> parensHash
241− parens' $ velt (withSep comma (located' p_hsTupArg) args)
242− ExplicitSum NoExt tag arity x -> do
245+ parens' $ if isSection
246+ then sequence_ (withSep (txt ",") (located' p_hsTupArg) args)
247+ else velt (withSep comma (located' p_hsTupArg) args)
248+ ExplicitSum NoExt tag arity e -> do
243249 let before = tag - 1
244250 after = arity - before - 1
245− args = replicate before Nothing <> [Just x] <> replicate after Nothing
246− parensHash $
247− velt (withSep (txt "| ") (maybe (pure ()) (located' p_hsExpr)) args)
251+ args = replicate before Nothing <> [Just e] <> replicate after Nothing
252+ f (x,i) = do
253+ let isFirst = i == 0
254+ isLast = i == arity - 1
255+ case x of
256+ Nothing ->
257+ unless (isFirst || isLast) space
258+ Just l -> do
259+ unless isFirst space
260+ located l p_hsExpr
261+ unless isLast space
262+ parensHash $ sequence_ (withSep (txt "|") f (zip args [0..]))
248263 HsCase NoExt e mgroup -> do
249264 txt "case "
250265 located e p_hsExpr
@@ -265,10 +280,8 @@ p_hsExpr = \case
265280 breakpoint
266281 inci (p_hsExpr x)
267282 HsMultiIf NoExt guards -> do
268− txt "if"
269− inci $ forM_ guards $ \g -> do
270− breakpoint
271− located g (p_grhs MultiIf)
283+ txt "if "
284+ sitcc $ newlineSep (located' (p_grhs MultiIf)) guards
272285 HsLet NoExt localBinds e -> do
273286 txt "let "
274287 sitcc (located localBinds p_hsLocalBinds)
@@ -300,24 +313,25 @@ p_hsExpr = \case
300313 let HsWC {..} = affix
301314 HsIB {..} = hswc_body
302315 located hsib_body p_hsType
303− ArithSeq NoExt _ x ->
316+ ArithSeq NoExt _ x -> do
317+ let breakpoint' = vlayout (return ()) newline
304318 case x of
305319 From from -> brackets $ do
306320 located from p_hsExpr
307− breakpoint
321+ breakpoint'
308322 txt ".."
309323 FromThen from next -> brackets $ do
310324 velt (withSep comma (located' p_hsExpr) [from, next])
311− breakpoint
325+ breakpoint'
312326 txt ".."
313327 FromTo from to -> brackets $ do
314328 located from p_hsExpr
315− breakpoint
329+ breakpoint'
316330 txt ".. "
317331 located to p_hsExpr
318332 FromThenTo from next to -> brackets $ do
319333 velt (withSep comma (located' p_hsExpr) [from, next])
320− breakpoint
334+ breakpoint'
321335 txt ".. "
322336 located to p_hsExpr
323337 HsSCC NoExt _ name x -> do