on master help sign in
Align opening/closing parentheses and other similar punctuation
commit87406cbd8f9af500c39cb6fdba5f41807ec5b62b authormrkkrp <mark.karpov@tweag.io> dateMon 16 Sep 2019, 15:01 (7 years ago) parenta4e97da
103 files changed+339−416
data/examples/declaration/annotation/annotation-out.hs+1−1 data/examples/declaration/class/default-signatures-out.hs+1−1 data/examples/declaration/class/dependency-super-classes-out.hs+1−1 data/examples/declaration/class/poly-kinded-classes-out.hs+1−1 data/examples/declaration/class/single-parameters-out.hs+1−1 data/examples/declaration/class/super-classes-out.hs+1−1 data/examples/declaration/class/type-operators1-out.hs+1−1 data/examples/declaration/class/type-operators2-out.hs+1−1 data/examples/declaration/data/deriving-strategies-out.hs+1−1 data/examples/declaration/data/gadt/record-out.hs+1−1 data/examples/declaration/data/multiline-names-out.hs+2−2 data/examples/declaration/data/record-out.hs+2−2 data/examples/declaration/default/default-out.hs+1−1 data/examples/declaration/instance/contexts-comments-out.hs+3−3 data/examples/declaration/instance/contexts-out.hs+2−2 data/examples/declaration/instance/data-family-instances-newtype-out.hs+1−1 data/examples/declaration/instance/data-family-instances-out.hs+2−2 data/examples/declaration/instance/type-family-instances-out.hs+2−2 data/examples/declaration/rewrite-rule/type-signature-out.hs+1−1 data/examples/declaration/signature/minimal/minimal-out.hs+1−1 data/examples/declaration/signature/specialize/specialize-instance-out.hs+1−1 data/examples/declaration/signature/type/arguments-out.hs+1−1 data/examples/declaration/signature/type/context-multi-line-out.hs+1−1 data/examples/declaration/signature/type/long-multiline-applications-out.hs+1−1 data/examples/declaration/splice/bracket-declaration-out.hs+1−1 data/examples/declaration/splice/typed-splice-out.hs+1−1 data/examples/declaration/splice/untyped-splice-out.hs+1−1 data/examples/declaration/value/function/arithmetic-sequences-out.hs+2−2 data/examples/declaration/value/function/arrow/expression-forms-out.hs+10−9 data/examples/declaration/value/function/arrow/proc-applications-out.hs+1−1 data/examples/declaration/value/function/arrow/proc-do-complex-out.hs+10−9 data/examples/declaration/value/function/arrow/proc-do-simple2-out.hs+1−1 data/examples/declaration/value/function/arrow/proc-forms1-out.hs+14−13 data/examples/declaration/value/function/arrow/proc-forms2-out.hs+1−1 data/examples/declaration/value/function/arrow/proc-lets-out.hs+2−1 data/examples/declaration/value/function/arrow/proc-parentheses-out.hs+4−4 data/examples/declaration/value/function/arrow/recursive-procs-out.hs+1−1 data/examples/declaration/value/function/complex-list-out.hs+2−2 data/examples/declaration/value/function/comprehension/transform-multi-line1-out.hs+2−2 data/examples/declaration/value/function/comprehension/transform-multi-line2-out.hs+3−3 data/examples/declaration/value/function/comprehension/transform-multi-line3-out.hs+2−2 data/examples/declaration/value/function/comprehension/transform-multi-line4-out.hs+3−3 data/examples/declaration/value/function/do/applications-and-parens-out.hs+9 data/examples/declaration/value/function/do/applications-and-parens.hs+6 data/examples/declaration/value/function/do/operator-and-parens-out.hs+7 data/examples/declaration/value/function/do/operator-and-parens.hs+7 data/examples/declaration/value/function/fancy-forall-0-out.hs+1−1 data/examples/declaration/value/function/fancy-forall-1-out.hs+1−1 data/examples/declaration/value/function/implicit-params-out.hs+1−1 data/examples/declaration/value/function/list-comprehensions-out.hs+2−2 data/examples/declaration/value/function/operator-sections-out.hs+2−2 data/examples/declaration/value/function/parallel-comprehensions-complex-out.hs+2−2 data/examples/declaration/value/function/pattern/record-patterns-out.hs+1−1 data/examples/declaration/value/function/pattern/splice-pattern-out.hs+2−2 data/examples/declaration/value/function/pattern/unboxed-sum-pattern-out.hs+5−3 data/examples/declaration/value/function/record-constructors-out.hs+4−4 data/examples/declaration/value/function/record-updaters-out.hs+1−1 data/examples/declaration/value/function/record-wildcards-out.hs+1−1 data/examples/declaration/value/function/static-pointers-out.hs+2−2 data/examples/declaration/value/function/tricky-parens-out.hs+1−1 data/examples/declaration/value/function/tuples-out.hs+2−2 data/examples/declaration/value/function/unboxed-sums-out.hs+3−2 data/examples/declaration/value/function/unboxed-tuples-out.hs+5−4 data/examples/declaration/value/other/comments-get-before-op-out.hs+1−1 data/examples/declaration/value/other/line-multi-line-out.hs+1−1 data/examples/declaration/value/pattern-synonyms/bidirectional-out.hs+1−1 data/examples/declaration/value/pattern-synonyms/unidirectional-out.hs+1−1 data/examples/declaration/warning/warning-multiline-out.hs+1−1 data/examples/import/explicit-imports-out.hs+2−2 data/examples/import/explicit-imports-with-comments-out.hs+2−2 data/examples/import/misc-out.hs+2−2 data/examples/import/nested-explicit-imports-out.hs+2−2 data/examples/module-header/double-dot-with-names-out.hs+2−2 data/examples/module-header/multiline-out.hs+2−2 data/examples/module-header/multiline-with-comments-out.hs+2−2 data/examples/module-header/multiline2-out.hs+2−2 data/examples/module-header/warning-pragma-list-multiline-out.hs+3−3 data/examples/module-header/warning-pragma-multiline-out.hs+2−2 data/examples/other/comment-inside-construct-out.hs+2−2 data/examples/other/comment-two-blocks-out.hs+1−1 data/examples/other/merging-comments-out.hs+1−1 data/printer/list0-multi.hs−4 data/printer/list0-single.hs−1 data/printer/list1-multi.hs−8 data/printer/list1-single.hs−1 data/printer/mhdr-multi.hs−7 data/printer/mhdr-single.hs−3 data/printer/ssig-multi.hs−5 data/printer/ssig-single.hs−1 ormolu.cabal+1−3 src/Ormolu/Exception.hs+1−2 src/Ormolu/Printer/Combinators.hs+45−46 src/Ormolu/Printer/Meat/Common.hs+14−2 src/Ormolu/Printer/Meat/Declaration/Data.hs+1−1 src/Ormolu/Printer/Meat/Declaration/Default.hs+1−1 src/Ormolu/Printer/Meat/Declaration/Rule.hs+1−1 src/Ormolu/Printer/Meat/Declaration/Signature.hs+1−1 src/Ormolu/Printer/Meat/Declaration/Value.hs+46−39 src/Ormolu/Printer/Meat/Declaration/Warning.hs+1−1 src/Ormolu/Printer/Meat/ImportExport.hs+14−10 src/Ormolu/Printer/Meat/Type.hs+13−13 tests/Ormolu/Printer/CombinatorsSpec.hs−115 tests/Ormolu/PrinterSpec.hs+13−1
data/examples/declaration/annotation/annotation-out.hs+1−1
@@ -4,7 +4,7 @@
44 module
55 ( 5
66 :: Int
7− )
7+ )
88 #-}
99
1010{-# ANN foo "hey" #-}
data/examples/declaration/class/default-signatures-out.hs+1−1
@@ -13,7 +13,7 @@ class Bar a where
1313 default bar
1414 :: ( Read a,
1515 Semigroup a
16− )
16+ )
1717 => a
1818 -> a
1919 -> a
data/examples/declaration/class/dependency-super-classes-out.hs+1−1
@@ -11,7 +11,7 @@ class (MonadReader r s, MonadWriter w m) => MonadState s m | m -> s where
1111class
1212 ( Stream s, -- Token streams
1313 MonadPlus m -- Potential for failure
14− )
14+ )
1515 => MonadParsec e s m
1616 | m -> e s where
1717
data/examples/declaration/class/poly-kinded-classes-out.hs+1−1
@@ -6,4 +6,4 @@ class
66 Bar
77 ( a -- Variable
88 :: * -- Star
9− )
9+ )
data/examples/declaration/class/single-parameters-out.hs+1−1
@@ -11,7 +11,7 @@ class Baz a where
1111 baz
1212 :: ( a,
1313 a -- ^ First argument
14− )
14+ )
1515 -> a -- ^ Second argument
1616 -> a -- ^ Return value
1717
data/examples/declaration/class/super-classes-out.hs+1−1
@@ -10,5 +10,5 @@ class
1010 ( Foo a, -- Foo?
1111 Bar a, -- Bar?
1212 Baz a -- Baz
13− )
13+ )
1414 => BarBar a
data/examples/declaration/class/type-operators1-out.hs+1−1
@@ -17,5 +17,5 @@ class
1717class
1818 ( f
1919 :. g
20− )
20+ )
2121 a
data/examples/declaration/class/type-operators2-out.hs+1−1
@@ -12,5 +12,5 @@ class (f `Product` g) a
1212class
1313 ( f
1414 `Sum` g
15− )
15+ )
1616 a
data/examples/declaration/data/deriving-strategies-out.hs+1−1
@@ -4,7 +4,7 @@ newtype Foo = Foo Int
44 deriving anyclass
55 ( ToJSON,
66 FromJSON
7− )
7+ )
88 deriving newtype (Num)
99 deriving (Monoid) via (Sum Int)
1010 deriving
data/examples/declaration/data/gadt/record-out.hs+1−1
@@ -8,5 +8,5 @@ data Foo where
88 barBar,
99 bazBaz
1010 :: Int
11− }
11+ }
1212 -> Foo
data/examples/declaration/data/multiline-names-out.hs+2−2
@@ -11,7 +11,7 @@ data (f :* g) a = f a :* g a
1111data
1212 ( f
1313 :+ g
14− )
14+ )
1515 a
1616 = L (f a)
1717 | R (g a)
@@ -23,7 +23,7 @@ data (f `Product` g) a = f a `Product` g a
2323data
2424 ( f
2525 `Sum` g
26− )
26+ )
2727 a
2828 = L' (f a)
2929 | R' (g a)
data/examples/declaration/data/record-out.hs+2−2
@@ -9,10 +9,10 @@ data Foo
99 :: NonEmpty
1010 ( Indentity
1111 Bool
12− ),
12+ ),
1313 -- ^ GagGog
1414 fooFoo,
1515 barBar
1616 :: Int -- ^ Huh!
17− }
17+ }
1818 deriving (Eq, Show)
data/examples/declaration/default/default-out.hs+1−1
@@ -4,4 +4,4 @@ default
44 ( Int,
55 Foo,
66 Bar
7− )
7+ )
data/examples/declaration/instance/contexts-comments-out.hs+3−3
@@ -4,14 +4,14 @@ instance
44 Read
55 ( c, -- Bar
66 d
7− )
8− )
7+ )
8+ )
99 => Read
1010 ( a, -- Baz
1111 b,
1212 ( c, -- Quux
1313 d
14− )
1514 )
15+ )
1616 where
1717 readsPrec = undefined
data/examples/declaration/instance/contexts-out.hs+2−2
@@ -3,7 +3,7 @@ instance Eq a => Eq [a] where (==) _ _ = False
33instance
44 ( Ord a,
55 Ord b
6− )
6+ )
77 => Ord (a, b)
88 where
99 compare _ _ = GT
@@ -13,6 +13,6 @@ instance
1313 => Show
1414 ( a,
1515 b
16− )
16+ )
1717 where
1818 showsPrec _ _ = showString ""
data/examples/declaration/instance/data-family-instances-newtype-out.hs+1−1
@@ -4,4 +4,4 @@
44newtype instance Foo [Double]
55 = DoubleListFoo
66 { unDoubleListFoo :: Double
7− }
7+ }
data/examples/declaration/instance/data-family-instances-out.hs+2−2
@@ -9,10 +9,10 @@ data instance
99 = IntListFoo
1010 ( Int,
1111 Int
12− )
12+ )
1313 ( Double,
1414 Double
15− )
15+ )
1616
1717data instance Bar Double a
1818 = DoubleBar
data/examples/declaration/instance/type-family-instances-out.hs+2−2
@@ -7,7 +7,7 @@ type instance
77 [Int] =
88 ( Int,
99 Int
10− )
10+ )
1111
1212type instance Bar Int [Int] Double = (Int, Double)
1313
@@ -18,4 +18,4 @@ type instance
1818 Double =
1919 ( Int,
2020 Double
21− )
21+ )
data/examples/declaration/rewrite-rule/type-signature-out.hs+1−1
@@ -11,7 +11,7 @@
1111 (a -> b -> b)
1212 -> b
1313 -> b
14− ).
14+ ).
1515 foldr k z (build g) =
1616 g k z
1717 #-}
data/examples/declaration/signature/minimal/minimal-out.hs+1−1
@@ -7,7 +7,7 @@ class Foo a where
77 | ( b, c, d
88 | e,
99 f
10− )
10+ )
1111 | g
1212 #-}
1313
data/examples/declaration/signature/specialize/specialize-instance-out.hs+1−1
@@ -12,7 +12,7 @@ instance (Num r, V.Vector v r, Factored m r) => Num (VT v m r) where
1212
1313 {-# SPECIALIZE instance
1414 ( Factored m Int => Num (VT U.Vector m Int)
15− )
15+ )
1616 #-}
1717
1818 VT x + VT y = VT $ V.zipWith (+) x y
data/examples/declaration/signature/type/arguments-out.hs+1−1
@@ -11,6 +11,6 @@ functionName
1111 -> AnotherLongDataTypeName
1212 -> b
1313 -> c
14− )
14+ )
1515 -> (c -> d)
1616 -> (a, b, c, d)
data/examples/declaration/signature/type/context-multi-line-out.hs+1−1
@@ -2,7 +2,7 @@ functionName
22 :: ( C1,
33 C2,
44 C3
5− )
5+ )
66 => a
77 -> b
88 -> c
data/examples/declaration/signature/type/long-multiline-applications-out.hs+1−1
@@ -10,6 +10,6 @@ functionName
1010 -> AnotherLongDataTypeName4
1111 -> b
1212 -> c
13− )
13+ )
1414 -> (c -> d)
1515 -> (a, b, c, d)
data/examples/declaration/splice/bracket-declaration-out.hs+1−1
@@ -23,4 +23,4 @@ $( singletons
2323 data T = T
2424 deriving (Eq, Ord, Enum, Bounded, Show)
2525 |]
26− )
26+ )
data/examples/declaration/splice/typed-splice-out.hs+1−1
@@ -2,6 +2,6 @@
22
33x =
44 $$( foo bar
5− )
5+ )
66
77x = $$foo
data/examples/declaration/splice/untyped-splice-out.hs+1−1
@@ -5,6 +5,6 @@ x = $(foo bar)
55x =
66 $( foo
77 bar
8− )
8+ )
99
1010x = $foo
data/examples/declaration/value/function/arithmetic-sequences-out.hs+2−2
@@ -5,13 +5,13 @@ foo' = [0 .. 5]
55bar x =
66 [ 0
77 .. x
8− ]
8+ ]
99
1010baz x =
1111 [ 1,
1212 3
1313 .. x
14− ]
14+ ]
1515
1616barbaz x = [0, 1 ..]
1717
data/examples/declaration/value/function/arrow/expression-forms-out.hs+10−9
@@ -1,13 +1,14 @@
11{-# LANGUAGE Arrows #-}
22
3−foo f g x y = (|test (f -< x) (g -< y)|)
3+foo f g x y = (| test (f -< x) (g -< y) |)
44
55bar f g x y =
6− (| test
7− ( f -<
8− x
9− )
10− ( g -<
11− y
12− )
13− |)
6+ (|
7+ test
8+ ( f -<
9+ x
10+ )
11+ ( g -<
12+ y
13+ )
14+ |)
data/examples/declaration/value/function/arrow/proc-applications-out.hs+1−1
@@ -13,6 +13,6 @@ bar f x =
1313 ( x, -- Foo
1414 w, -- Bar
1515 z -- Baz
16− )
16+ )
1717
1818baz x = proc a -> a -<< x
data/examples/declaration/value/function/arrow/proc-do-complex-out.hs+10−9
@@ -16,13 +16,13 @@ foo
1616 f -- Call into f
1717 ( a,
1818 c -- Tuple together arguments
19− )
19+ )
2020 ( b,
2121 d
22− ) -<
22+ ) -<
2323 ( b + 1, -- Funnel into arrow
2424 d * b
25− )
25+ )
2626 if x `mod` y == 0 -- Basic condition
2727 then
2828 case e of -- Only left case is relevant
@@ -33,10 +33,11 @@ foo
3333 let v =
3434 u -- Actually never used
3535 ^ 2
36− in ( returnA -<
37− -- Just do the calculation
38− (x + y * z)
39− )
36+
37+ in ( returnA -<
38+ -- Just do the calculation
39+ (x + y * z)
40+ )
4041 else
4142 do
4243 let u = x -- Let bindings bind expressions, not commands
@@ -47,8 +48,8 @@ foo
4748 n ->
4849 ( ( h . g -<
4950 y -- First actual use of y
50− )
5151 )
52+ )
5253 returnA -< ()
5354 -- Sometimes execute effects
5455 if i > 0
@@ -58,4 +59,4 @@ foo
5859 ( i
5960 + x
6061 * y -- Just do the calculation
61− )
62+ )
data/examples/declaration/value/function/arrow/proc-do-simple2-out.hs+1−1
@@ -15,6 +15,6 @@ bazbaz f g h = proc (a, b, c) -> do
1515 ( a,
1616 b,
1717 c
18− )
18+ )
1919 returnA -<
2020 (x, y, z)
data/examples/declaration/value/function/arrow/proc-forms1-out.hs+14−13
@@ -1,18 +1,19 @@
11{-# LANGUAGE Arrows #-}
22
3−foo0 f g x y = proc _ -> (|f (g -< (x, y))|)
3+foo0 f g x y = proc _ -> (| f (g -< (x, y)) |)
44
55foo1 f g h x =
66 proc (y, z) ->
7− (| test
8− ( h f
9− . h g -<
10− y x
11− . y z
12− )
13− ( h g
14− . h f -<
15− y z
16− . y x
17− )
18− |)
7+ (|
8+ test
9+ ( h f
10+ . h g -<
11+ y x
12+ . y z
13+ )
14+ ( h g
15+ . h f -<
16+ y z
17+ . y x
18+ )
19+ |)
data/examples/declaration/value/function/arrow/proc-forms2-out.hs+1−1
@@ -2,7 +2,7 @@
22
33bar0 f g h x =
44 proc (y, z) ->
5− (|test (h f . (h g) -< (y x) . y z) ((h g) . h f -< y z . (y x))|)
5+ (| test (h f . (h g) -< (y x) . y z) ((h g) . h f -< y z . (y x)) |)
66
77bar1 f g x y = proc _ -> f -< x &&& g -< y
88
data/examples/declaration/value/function/arrow/proc-lets-out.hs+2−1
@@ -9,4 +9,5 @@ bar f g = proc a ->
99 j =
1010 g
1111 . h
12− in id -< (h, j)
12+
13+ in id -< (h, j)
data/examples/declaration/value/function/arrow/proc-parentheses-out.hs+4−4
@@ -5,14 +5,14 @@ foo f = proc a -> (f -< a)
55bar f g = proc a ->
66 ( ( (f)
77 ( g
8− )
9− ) -<
8+ )
9+ ) -<
1010 ( ( ( ( ( ( g
1111 a
12− )
1312 )
1413 )
1514 )
1615 )
1716 )
18− )
17+ )
18+ )
data/examples/declaration/value/function/arrow/recursive-procs-out.hs+1−1
@@ -8,7 +8,7 @@ foo f g = proc (x, y) -> do
88 bar -<
99 ( a,
1010 b
11− )
11+ )
1212 rec p <-
1313 f
1414 p -<
data/examples/declaration/value/function/complex-list-out.hs+2−2
@@ -4,7 +4,7 @@ handleStuff =
44 something,
55 \ExceptionB ->
66 somethingElse
7− ]
7+ ]
88
99handleStuff =
1010 handle
@@ -12,4 +12,4 @@ handleStuff =
1212 bar,
1313 baz
1414 qux
15− ]
15+ ]
data/examples/declaration/value/function/comprehension/transform-multi-line1-out.hs+2−2
@@ -3,9 +3,9 @@
33foo' xs ys =
44 [ ( x,
55 y
6− )
6+ )
77 | x <- xs,
88 y <- ys,
99 -- First comment
1010 then reverse -- Second comment
11− ]
11+ ]
data/examples/declaration/value/function/comprehension/transform-multi-line2-out.hs+3−3
@@ -3,7 +3,7 @@
33bar' xs ys =
44 [ ( x,
55 y
6− )
6+ )
77 | x <- xs,
88 y <- ys,
99 -- First comment
@@ -11,5 +11,5 @@ bar' xs ys =
1111 by
1212 ( x
1313 + y -- Second comment
14− )
15− ]
14+ )
15+ ]
data/examples/declaration/value/function/comprehension/transform-multi-line3-out.hs+2−2
@@ -3,9 +3,9 @@
33baz' xs ys =
44 [ ( x,
55 y
6− )
6+ )
77 | x <- xs,
88 y <- ys,
99 -- First comment
1010 then group using permutations -- Second comment
11− ]
11+ ]
data/examples/declaration/value/function/comprehension/transform-multi-line4-out.hs+3−3
@@ -3,14 +3,14 @@
33quux' xs ys =
44 [ ( x,
55 y
6− )
6+ )
77 | x <- xs,
88 y <- ys,
99 -- First comment
1010 then group by
1111 ( x
1212 + y
13− )
13+ )
1414 -- Second comment
1515 using groupWith -- Third comment
16− ]
16+ ]
data/examples/declaration/value/function/do/applications-and-parens-out.hsnew file+9
@@ -0,0 +1,9 @@
1+warningFor var place = do
2+ guard $ isVariableName var
3+ guard . not $ isInArray var place || isGuarded place
4+ ( if includeGlobals || isLocal var
5+ then warningForLocals
6+ else warningForGlobals
7+ )
8+ var
9+ place
data/examples/declaration/value/function/do/applications-and-parens.hsnew file+6
@@ -0,0 +1,6 @@
1+warningFor var place = do
2+ guard $ isVariableName var
3+ guard . not $ isInArray var place || isGuarded place
4+ (if includeGlobals || isLocal var
5+ then warningForLocals
6+ else warningForGlobals) var place
data/examples/declaration/value/function/do/operator-and-parens-out.hsnew file+7
@@ -0,0 +1,7 @@
1+scientifically :: (Scientific -> a) -> Parser a
2+scientifically h = do
3+ something
4+ ( I.satisfy (\w -> w == 'e' || w == 'E')
5+ *> fmap (h . Sci.scientific signedCoeff . (e +)) (signed decimal)
6+ )
7+ <|> return (h $ Sci.scientific signedCoeff e)
data/examples/declaration/value/function/do/operator-and-parens.hsnew file+7
@@ -0,0 +1,7 @@
1+scientifically :: (Scientific -> a) -> Parser a
2+scientifically h = do
3+ something
4+ ( I.satisfy (\w -> w == 'e' || w == 'E')
5+ *> fmap (h . Sci.scientific signedCoeff . (e +)) (signed decimal)
6+ )
7+ <|> return (h $ Sci.scientific signedCoeff e)
data/examples/declaration/value/function/fancy-forall-0-out.hs+1−1
@@ -5,6 +5,6 @@ wrapError
55 HasCatch outertag outer m'
66 => HasCatch innertag inner (t m'),
77 HasCatch outertag outer m
8− )
8+ )
99 => (forall m'. HasCatch innertag inner m' => m' a)
1010 -> m a
data/examples/declaration/value/function/fancy-forall-1-out.hs+1−1
@@ -5,6 +5,6 @@ magnify
55 HasReader outertag outer m'
66 => HasReader innertag inner (t m'),
77 HasReader outertag outer m
8− )
8+ )
99 => (forall m'. HasReader innertag inner m' => m' a)
1010 -> m a
data/examples/declaration/value/function/implicit-params-out.hs+1−1
@@ -9,7 +9,7 @@ sort'
99 :: ( ?cmp
1010 :: a -> a -> Bool,
1111 ?foo :: Int
12− )
12+ )
1313 => [a]
1414 -> [a]
1515sort' = sort
data/examples/declaration/value/function/list-comprehensions-out.hs+2−2
@@ -20,5 +20,5 @@ barbaz x y z w =
2020 b,
2121 c,
2222 d
23− ]
24− ]
23+ ]
24+ ]
data/examples/declaration/value/function/operator-sections-out.hs+2−2
@@ -5,9 +5,9 @@ bar = (<> "hello")
55baz =
66 ( 1 * 2
77 +
8− )
8+ )
99 ( *
1010 3 ^ 5
11− )
11+ )
1212
1313quux = (,) <$> foo <$> bar
data/examples/declaration/value/function/parallel-comprehensions-complex-out.hs+2−2
@@ -9,7 +9,7 @@ baz x y z w =
99 h,
1010 i,
1111 j
12− )
12+ )
1313 | a <- -- Foo 1
1414 x, -- Foo 2
1515 b <- -- Bar 1
@@ -32,4 +32,4 @@ baz x y z w =
3232 -- Bar bar 3
3333 j <- -- Bar baz 1
3434 a + b -- Bar baz 2
35− ]
35+ ]
data/examples/declaration/value/function/pattern/record-patterns-out.hs+1−1
@@ -16,4 +16,4 @@ quux
1616 { boom = a,
1717 foom = b,
1818 ..
19− } = a + b
19+ } = a + b
data/examples/declaration/value/function/pattern/splice-pattern-out.hs+2−2
@@ -7,7 +7,7 @@ singleLine = case () of
77multiline = case () of
88 $( x
99 + y
10− ) -> ()
10+ ) -> ()
1111 $( y
1212 "something"
13− ) -> ()
13+ ) -> ()
data/examples/declaration/value/function/pattern/unboxed-sum-pattern-out.hs+5−3
@@ -18,7 +18,9 @@ z = True
1818
1919z_multiline = True
2020 where
21− (# | | _x
21+ (#
22+ | | _x
2223 #) =
23− (# | | True
24− #)
24+ (#
25+ | | True
26+ #)
data/examples/declaration/value/function/record-constructors-out.hs+4−4
@@ -3,7 +3,7 @@ foo = Foo {a = 3}
33bar = Bar
44 { abc = foo,
55 def = Foo {a = 10}
6− }
6+ }
77
88baz = Baz {}
99
@@ -14,14 +14,14 @@ aLongVariableName =
1414 { short = baz,
1515 aLongRecordFieldName = YetAnotherLongRecordName
1616 { yetAnotherLongRecordFieldName = "a long string"
17− },
17+ },
1818 aLongRecordFieldName2 = Just YetAnotherLongRecordName
1919 { yetAnotherLongRecordFieldName = "a long string",
2020 yetAnotherLongRecordFieldName =
2121 Just
2222 "a long string"
23− },
23+ },
2424 aLongRecordFieldName3 = do
2525 foo
2626 bar
27− }
27+ }
data/examples/declaration/value/function/record-updaters-out.hs+1−1
@@ -4,7 +4,7 @@ bar x =
44 x
55 { abc = foo,
66 def = Foo {a = 10}
7− }
7+ }
88
99baz x = x {}
1010
data/examples/declaration/value/function/record-wildcards-out.hs+1−1
@@ -8,6 +8,6 @@ bar x y z = Bar
88 y,
99 z,
1010 ..
11− }
11+ }
1212
1313baz = Baz {..}
data/examples/declaration/value/function/static-pointers-out.hs+2−2
@@ -9,11 +9,11 @@ bar =
99 [ 1,
1010 2,
1111 3
12− ]
12+ ]
1313
1414baz :: StaticPtr Bool
1515baz =
1616 static
1717 ( fun 1
1818 2
19− )
19+ )
data/examples/declaration/value/function/tricky-parens-out.hs+1−1
@@ -1,4 +1,4 @@
11handleStuff =
22 ( let foo = foo
33 in foo
4− )
4+ )
data/examples/declaration/value/function/tuples-out.hs+2−2
@@ -4,11 +4,11 @@ bar =
44 ( 1,
55 2,
66 3
7− )
7+ )
88
99handleStuff =
1010 ( let foo = foo
1111 in foo,
1212 let bar = bar
1313 in bar
14− )
14+ )
data/examples/declaration/value/function/unboxed-sums-out.hs+3−2
@@ -5,5 +5,6 @@ foo = (# 1 | #)
55bar = (# | | 2 | #)
66
77baz =
8− (# | | | 10 | | | | |
9− #)
8+ (#
9+ | | | 10 | | | | |
10+ #)
data/examples/declaration/value/function/unboxed-tuples-out.hs+5−4
@@ -3,7 +3,8 @@
33foo = (# 1, 2, 3 #)
44
55bar =
6− (# 1,
7− 2,
8− 3
9− #)
6+ (#
7+ 1,
8+ 2,
9+ 3
10+ #)
data/examples/declaration/value/other/comments-get-before-op-out.hs+1−1
@@ -6,5 +6,5 @@ main = do
66 migration3
77 -- When adding migrations here, don't forget to update
88 -- 'schemaVersion' in Galley.Data
9− ]
9+ ]
1010 `finally` Log.close
data/examples/declaration/value/other/line-multi-line-out.hs+1−1
@@ -3,4 +3,4 @@ x =
33 [ 1,
44 2,
55 somethingSomething 3
6− ]
6+ ]
data/examples/declaration/value/pattern-synonyms/bidirectional-out.hs+1−1
@@ -8,7 +8,7 @@ pattern Arrow {t1, t2} = App "->" [t1, t2]
88pattern Arrow
99 { t1,
1010 t2
11− }
11+ }
1212 = App "->" [t1, t2]
1313
1414pattern Int
data/examples/declaration/value/pattern-synonyms/unidirectional-out.hs+1−1
@@ -14,7 +14,7 @@ pattern FirstTwo {x, y}
1414pattern FirstTwo'
1515 { x,
1616 y
17− }
17+ }
1818 <- x : (y : xs)
1919
2020pattern Simple <- "Simple"
data/examples/declaration/warning/warning-multiline-out.hs+1−1
@@ -3,7 +3,7 @@
33 foo
44 [ "These are bad functions",
55 "Really bad!"
6− ]
6+ ]
77 #-}
88test :: IO ()
99test = pure ()
data/examples/import/explicit-imports-out.hs+2−2
@@ -8,5 +8,5 @@ import qualified MegaModule as M
88 MonadBaseControl,
99 join,
1010 liftIO,
11− void
12− )
11+ void,
12+ )
data/examples/import/explicit-imports-with-comments-out.hs+2−2
@@ -2,5 +2,5 @@ import qualified MegaModule as M
22 ( -- (1)
33 (<<<), -- (2)
44 (>>>),
5− Either -- (3)
6− )
5+ Either, -- (3)
6+ )
data/examples/import/misc-out.hs+2−2
@@ -5,7 +5,7 @@ import A hiding
55 foobarbazqux,
66 foobarbazqux,
77 foobarbazqux,
8− foobarbazqux
9− )
8+ foobarbazqux,
9+ )
1010import {-# SOURCE #-} safe qualified Module as M hiding (a, b, c, d, e, f)
1111import Name hiding ()
data/examples/import/nested-explicit-imports-out.hs+2−2
@@ -6,5 +6,5 @@ import qualified MegaModule as M
66 ( (>>),
77 (>>=),
88 return
9− )
10− )
9+ ),
10+ )
data/examples/module-header/double-dot-with-names-out.hs+2−2
@@ -3,8 +3,8 @@
33module ExportSyntax
44 ( A (.., NoA),
55 Q (F, ..),
6− G (T, .., U)
7− )
6+ G (T, .., U),
7+ )
88where
99
1010data A = A | B
data/examples/module-header/multiline-out.hs+2−2
@@ -1,6 +1,6 @@
11module Foo
22 ( foo,
33 bar,
4− baz
5− )
4+ baz,
5+ )
66where
data/examples/module-header/multiline-with-comments-out.hs+2−2
@@ -14,8 +14,8 @@ module My.Module
1414 bar2, -- a multiline comment
1515 -- the second line
1616 bar3,
17− module Foo.Bar.Baz
18− )
17+ module Foo.Bar.Baz,
18+ )
1919where
2020
2121-- Wow
data/examples/module-header/multiline2-out.hs+2−2
@@ -1,6 +1,6 @@
11module Foo
22 ( foo,
33 bar,
4− baz
5− )
4+ baz,
5+ )
66where
data/examples/module-header/warning-pragma-list-multiline-out.hs+3−3
@@ -2,10 +2,10 @@ module Test
22 {-# DEPRECATED
33 [ "This module is deprecated.",
44 "Please use OtherModule instead."
5− ]
5+ ]
66 #-}
77 ( foo,
88 bar,
9− baz
10− )
9+ baz,
10+ )
1111where
data/examples/module-header/warning-pragma-multiline-out.hs+2−2
@@ -2,8 +2,8 @@ module Test
22 {-# DEPRECATED "This module is unstable" #-}
33 ( foo,
44 bar,
5− baz
6− )
5+ baz,
6+ )
77where
88
99import Blah
data/examples/other/comment-inside-construct-out.hs+2−2
@@ -3,6 +3,6 @@ xs =
33 [ inner list first item,
44 inner list second item
55 -- inner list last item commented
6− ],
6+ ],
77 outer list item
8− ]
8+ ]
data/examples/other/comment-two-blocks-out.hs+1−1
@@ -5,4 +5,4 @@ newNames =
55 -- Foo
66
77 -- Bar
8− ]
8+ ]
data/examples/other/merging-comments-out.hs+1−1
@@ -5,7 +5,7 @@ foo xs = baz
55 [ lookup langKey gets, -- 1
66 lookup langKey cookies, -- 2
77 lookupText langKey session -- 3
8− ]
8+ ]
99 ++ xs -- 4
1010
1111 -- Blah
data/printer/list0-multi.hsdeleted−4
@@ -1,4 +0,0 @@
1−[ foo,
2− bar,
3− baz
4− ]
data/printer/list0-single.hsdeleted−1
@@ -1 +0,0 @@
1−[foo, bar, baz]
data/printer/list1-multi.hsdeleted−8
@@ -1,8 +0,0 @@
1−[ foo,
2− bar,
3− foo
4− :: ( Int
5− -> Int
6− )
7− -> Bool
8− ]
data/printer/list1-single.hsdeleted−1
@@ -1 +0,0 @@
1−[foo, bar, foo :: (Int -> Int) -> Bool]
data/printer/mhdr-multi.hsdeleted−7
@@ -1,7 +0,0 @@
1−module MyModule
2− ( R,
3− runR,
4− txt,
5− blah
6− )
7−where
data/printer/mhdr-single.hsdeleted−3
@@ -1,3 +0,0 @@
1−module MyModule
2− (R, runR, txt, blah)
3−where
data/printer/ssig-multi.hsdeleted−5
@@ -1,5 +0,0 @@
1−foo
2− :: ( Int
3− -> Int
4− )
5− -> Bool
data/printer/ssig-single.hsdeleted−1
@@ -1 +0,0 @@
1−foo :: (Int -> Int) -> Bool
ormolu.cabal+1−3
@@ -51,7 +51,6 @@ data-files: data/examples/declaration/annotation/*.hs
5151 , data/examples/import/*.hs
5252 , data/examples/module-header/*.hs
5353 , data/examples/other/*.hs
54− , data/printer/*.hs
5554
5655source-repository head
5756 type: git
@@ -136,8 +135,7 @@ test-suite tests
136135 , text >= 0.2 && < 1.3
137136 build-tools: hspec-discover >= 2.0 && < 3.0
138137 other-modules:
139− Ormolu.Printer.CombinatorsSpec
140− , Ormolu.Parser.PragmaSpec
138+ Ormolu.Parser.PragmaSpec
141139 , Ormolu.PrinterSpec
142140
143141 if flag(dev)
src/Ormolu/Exception.hs+1−2
@@ -9,10 +9,10 @@ module Ormolu.Exception
99where
1010
1111import Control.Exception
12+import Data.Text (Text)
1213import Ormolu.Utils (showOutputable)
1314import System.Exit (ExitCode (..), exitWith)
1415import System.IO
15−import Data.Text (Text)
1616import qualified GHC
1717import qualified Outputable as GHC
1818
@@ -54,7 +54,6 @@ instance Exception OrmoluException where
5454 [ "before: " ++ show left , "after: " ++ show right ]
5555 ++ "Please, consider reporting the bug.\n"
5656
57−
5857-- | Inside this wrapper 'OrmoluException' will be caught and displayed
5958-- nicely using 'displayException'.
6059
src/Ormolu/Printer/Combinators.hs+45−46
@@ -32,12 +32,12 @@ module Ormolu.Printer.Combinators
3232 , useBraces
3333 , dontUseBraces
3434 -- ** Wrapping
35+ , BracketStyle (..)
3536 , sitcc
3637 , backticks
3738 , banana
3839 , braces
3940 , brackets
40− , bracketsPar
4141 , parens
4242 , parensHash
4343 , pragmaBraces
@@ -180,6 +180,12 @@ sepSemi f xs = vlayout singleLine multiLine
180180----------------------------------------------------------------------------
181181-- Wrapping
182182
183+-- | 'BracketStyle' controlling how closing bracket is rendered.
184+
185+data BracketStyle
186+ = N -- ^ Normal
187+ | S -- ^ Shifted one level
188+
183189-- | Surround given entity by backticks.
184190
185191backticks :: R () -> R ()
@@ -191,61 +197,37 @@ backticks m = do
191197-- | Surround given entity by banana brackets (i.e., from arrow notation.)
192198
193199banana :: R () -> R ()
194−banana m = sitcc $ do
195− txt "(|"
196− ospaces m
197− txt "|)"
200+banana = brackets_ True "(|" "|)" N
198201
199202-- | Surround given entity by curly braces @{@ and @}@.
200203
201−braces :: R () -> R ()
202−braces m = sitcc $ do
203− txt "{"
204− ospaces m
205− txt "}"
204+braces :: BracketStyle -> R () -> R ()
205+braces = brackets_ False "{" "}"
206206
207207-- | Surround given entity by square brackets @[@ and @]@.
208208
209−brackets :: R () -> R ()
210−brackets m = sitcc $ do
211− txt "["
212− ospaces m
213− txt "]"
214−
215−-- | Surround given entity by parallel array brackets @[:@ and @:]@.
216−
217−bracketsPar :: R () -> R ()
218−bracketsPar m = sitcc $ do
219− txt "[: "
220− m
221− vlayout (return ()) space
222− txt " :]"
209+brackets :: BracketStyle -> R () -> R ()
210+brackets = brackets_ False "[" "]"
223211
224212-- | Surround given entity by parentheses @(@ and @)@.
225213
226−parens :: R () -> R ()
227−parens m = sitcc $ do
228− txt "("
229− ospaces m
230− txt ")"
214+parens :: BracketStyle -> R () -> R ()
215+parens = brackets_ False "(" ")"
231216
232217-- | Surround given entity by @(# @ and @ #)@.
233218
234−parensHash :: R () -> R ()
235−parensHash m = sitcc $ do
236− txt "(# "
237− m
238− vlayout space (newline >> txt " ")
239− txt "#)"
219+parensHash :: BracketStyle -> R () -> R ()
220+parensHash = brackets_ True "(#" "#)"
240221
241222-- | Braces as used for pragmas: @{-#@ and @#-}@.
242223
243224pragmaBraces :: R () -> R ()
244225pragmaBraces m = sitcc $ do
245− txt "{-# "
226+ txt "{-#"
227+ space
246228 m
247− vlayout space (newline >> txt " ")
248− txt "#-}"
229+ breakpoint
230+ inci (txt "#-}")
249231
250232-- | Surround the body with a pragma name and 'pragmaBraces'.
251233
@@ -258,15 +240,32 @@ pragma pragmaText body = pragmaBraces $ do
258240 breakpoint
259241 body
260242
261−-- | Surround given entity by optional space before and a newline after, iff
262−-- current layout is multiline.
243+-- | A helper for defining wrappers like 'parens' and 'braces'.
263244
264−ospaces :: R () -> R ()
265−ospaces m = vlayout m $ do
266− space
267− sitcc m
268− newline
269− txt " "
245+brackets_
246+ :: Bool -- ^ Insert breakpoints around brackets
247+ -> Text -- ^ Opening bracket
248+ -> Text -- ^ Closing bracket
249+ -> BracketStyle -- ^ Bracket style
250+ -> R () -- ^ Inner expression
251+ -> R ()
252+brackets_ needBreaks open close style m = sitcc (vlayout singleLine multiLine)
253+ where
254+ singleLine = do
255+ txt open
256+ when needBreaks space
257+ m
258+ when needBreaks space
259+ txt close
260+ multiLine = do
261+ txt open
262+ if needBreaks
263+ then newline >> inci m
264+ else space >> sitcc m
265+ newline
266+ case style of
267+ N -> txt close
268+ S -> inci (txt close)
270269
271270----------------------------------------------------------------------------
272271-- Literals
src/Ormolu/Printer/Meat/Common.hs+14−2
@@ -11,6 +11,7 @@ module Ormolu.Printer.Meat.Common
1111 , doesNotNeedExtraParens
1212 , p_qualName
1313 , p_infixDefHelper
14+ , p_trailingCommaFor
1415 )
1516where
1617
@@ -57,7 +58,7 @@ p_rdrName l@(L spn _) = located l $ \x -> do
5758 else id
5859 parensWrapper =
5960 if AnnOpenP `elem` ids
60− then parens
61+ then parens N
6162 else id
6263 singleQuoteWrapper =
6364 if AnnSimpleQuote `elem` ids
@@ -123,7 +124,7 @@ p_infixDefHelper isInfix inci' name args =
123124 let parens' =
124125 if null ps
125126 then id
126− else parens
127+ else parens N
127128 parens' $ do
128129 p0
129130 breakpoint
@@ -139,3 +140,14 @@ p_infixDefHelper isInfix inci' name args =
139140 unless (null ps) $ do
140141 breakpoint
141142 inci' $ sitcc (sep breakpoint sitcc args)
143+
144+-- | Insert a trailing comma when the given collection is not empty and
145+-- we're in multi-line layout.
146+
147+p_trailingCommaFor
148+ :: [a] -- ^ Collection to print trailing comma for
149+ -> R ()
150+p_trailingCommaFor xs =
151+ vlayout
152+ (return ())
153+ (unless (null xs) comma)
src/Ormolu/Printer/Meat/Declaration/Data.hs+1−1
@@ -157,7 +157,7 @@ p_hsDerivingClause HsDerivingClause {..} = do
157157 txt "deriving"
158158 let derivingWhat = located deriv_clause_tys $ \case
159159 [] -> txt "()"
160− xs -> parens . sitcc $ sep
160+ xs -> parens N . sitcc $ sep
161161 (comma >> breakpoint)
162162 (sitcc . located' p_hsType . hsib_body)
163163 xs
src/Ormolu/Printer/Meat/Declaration/Default.hs+1−1
@@ -16,6 +16,6 @@ p_defaultDecl = \case
1616 DefaultDecl NoExt ts -> do
1717 txt "default"
1818 breakpoint
19− inci . parens . sitcc $
19+ inci . parens N . sitcc $
2020 sep (comma >> breakpoint) (sitcc . located' p_hsType) ts
2121 XDefaultDecl {} -> notImplemented "XDefaultDecl"
src/Ormolu/Printer/Meat/Declaration/Rule.hs+1−1
@@ -55,7 +55,7 @@ p_ruleBndrs bndrs =
5555p_ruleBndr :: RuleBndr GhcPs -> R ()
5656p_ruleBndr = \case
5757 RuleBndr NoExt x -> p_rdrName x
58− RuleBndrSig NoExt x hswc -> parens $ do
58+ RuleBndrSig NoExt x hswc -> parens N $ do
5959 p_rdrName x
6060 p_typeAscription hswc
6161 XRuleBndr NoExt -> notImplemented "XRuleBndr"
src/Ormolu/Printer/Meat/Declaration/Signature.hs+1−1
@@ -174,7 +174,7 @@ p_booleanFormula = \case
174174 (breakpoint >> txt "| ")
175175 (located' p_booleanFormula)
176176 xs
177− Parens l -> located l (parens . p_booleanFormula)
177+ Parens l -> located l (parens N . p_booleanFormula)
178178
179179p_completeSig
180180 :: Located [Located RdrName] -- ^ Constructors\/patterns
src/Ormolu/Printer/Meat/Declaration/Value.hs+46−39
@@ -310,7 +310,7 @@ p_hsCmd = \case
310310 -- does. Open an issue and ping @yumiova if this ever occurs in output.
311311 notImplemented "HsCmdApp"
312312 HsCmdLam NoExt mgroup -> p_matchGroup' cmdPlacement p_hsCmd Lambda mgroup
313− HsCmdPar NoExt c -> parens (located c p_hsCmd)
313+ HsCmdPar NoExt c -> parens N (located c p_hsCmd)
314314 HsCmdCase NoExt e mgroup -> do
315315 txt "case"
316316 space
@@ -338,6 +338,7 @@ p_hsCmd = \case
338338 space
339339 sitcc (located localBinds p_hsLocalBinds)
340340 breakpoint
341+ vlayout space (newline >> txt " ")
341342 txt "in"
342343 space
343344 sitcc (located c p_hsCmd)
@@ -486,7 +487,10 @@ p_hsTupArg = \case
486487 XTupArg {} -> notImplemented "XTupArg"
487488
488489p_hsExpr :: HsExpr GhcPs -> R ()
489−p_hsExpr = \case
490+p_hsExpr = p_hsExpr' N
491+
492+p_hsExpr' :: BracketStyle -> HsExpr GhcPs -> R ()
493+p_hsExpr' s = \case
490494 HsVar NoExt name -> p_rdrName name
491495 HsUnboundVar NoExt _ -> notImplemented "HsUnboundVar"
492496 HsConLikeOut NoExt _ -> notImplemented "HsConLikeOut"
@@ -504,8 +508,8 @@ p_hsExpr = \case
504508 HsOverLit NoExt v -> atom (ol_val v)
505509 HsLit NoExt lit ->
506510 case lit of
507− HsString (SourceText s) _ -> p_stringLit s
508− HsStringPrim (SourceText s) _ -> p_stringLit s
511+ HsString (SourceText stxt) _ -> p_stringLit stxt
512+ HsStringPrim (SourceText stxt) _ -> p_stringLit stxt
509513 r -> atom r
510514 HsLam NoExt mgroup ->
511515 p_matchGroup Lambda mgroup
@@ -519,8 +523,8 @@ p_hsExpr = \case
519523 -- and then use 'p_withoutHanging' for the descendants.
520524 let p_withoutHanging (HsApp NoExt f' x') = do
521525 case f' of
522− (L _ (HsApp _ _ _)) -> located f' p_withoutHanging
523− _ -> located f' p_hsExpr
526+ L _ (HsApp _ _ _) -> located f' p_withoutHanging
527+ _ -> located f' (p_hsExpr' s)
524528 breakpoint
525529 inci $ located x' p_hsExpr
526530 p_withoutHanging e = p_hsExpr e
@@ -552,12 +556,12 @@ p_hsExpr = \case
552556 getOpName = \case
553557 HsVar NoExt (L _ a) -> Just a
554558 _ -> Nothing
555− p_exprOpTree (reassociateOpTree getOpName opTree)
559+ p_exprOpTree s (reassociateOpTree getOpName opTree)
556560 NegApp NoExt e _ -> do
557561 txt "-"
558562 space
559563 located e p_hsExpr
560− HsPar NoExt e -> parens (located e p_hsExpr)
564+ HsPar NoExt e -> parens s (located e p_hsExpr)
561565 SectionL NoExt x op -> do
562566 located x p_hsExpr
563567 breakpoint
@@ -576,12 +580,12 @@ p_hsExpr = \case
576580 Boxed -> parens
577581 Unboxed -> parensHash
578582 if isSection
579− then switchLayout [] . parens' $
583+ then switchLayout [] . parens' s $
580584 sep comma (located' p_hsTupArg) args
581− else switchLayout (getLoc <$> args) . parens' . sitcc $
585+ else switchLayout (getLoc <$> args) . parens' s . sitcc $
582586 sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args
583587 ExplicitSum NoExt tag arity e ->
584− p_unboxedSum tag arity (located e p_hsExpr)
588+ p_unboxedSum N tag arity (located e p_hsExpr)
585589 HsCase NoExt e mgroup -> do
586590 txt "case"
587591 space
@@ -620,8 +624,8 @@ p_hsExpr = \case
620624 txt header
621625 breakpoint
622626 ub <- vlayout (return useBraces) (return id)
623− inci $ sepSemi (located' (ub . p_stmt)) (unLoc es)
624− compBody = brackets $ located es $ \xs -> do
627+ inci $ sepSemi (located' (ub . p_stmt' (p_hsExpr' S))) (unLoc es)
628+ compBody = brackets N $ located es $ \xs -> do
625629 let p_parBody = sep
626630 (breakpoint >> txt "| ")
627631 p_seqBody
@@ -647,7 +651,8 @@ p_hsExpr = \case
647651 ParStmtCtxt _ -> notImplemented "ParStmtCtxt"
648652 TransStmtCtxt _ -> notImplemented "TransStmtCtxt"
649653 ExplicitList _ _ xs ->
650− brackets . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs
654+ brackets s . sitcc $
655+ sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs
651656 RecordCon {..} -> do
652657 located rcon_con_name atom
653658 breakpoint
@@ -662,7 +667,8 @@ p_hsExpr = \case
662667 case rec_dotdot of
663668 Just {} -> [txt ".."]
664669 Nothing -> []
665− inci . braces . sitcc $ sep (comma >> breakpoint) sitcc (fields <> dotdot)
670+ inci . braces N . sitcc $
671+ sep (comma >> breakpoint) sitcc (fields <> dotdot)
666672 RecordUpd {..} -> do
667673 located rupd_expr p_hsExpr
668674 breakpoint
@@ -672,7 +678,7 @@ p_hsExpr = \case
672678 Unambiguous _ n -> n
673679 XAmbiguousFieldOcc _ -> notImplemented "XAmbiguousFieldOcc"
674680 }
675− inci . braces . sitcc $
681+ inci . braces N . sitcc $
676682 sep
677683 (comma >> breakpoint)
678684 (sitcc . located' (p_hsRecField . updName))
@@ -688,21 +694,21 @@ p_hsExpr = \case
688694 located hsib_body p_hsType
689695 ArithSeq NoExt _ x -> do
690696 case x of
691− From from -> brackets . sitcc $ do
697+ From from -> brackets s . sitcc $ do
692698 located from p_hsExpr
693699 breakpoint
694700 txt ".."
695− FromThen from next -> brackets . sitcc $ do
701+ FromThen from next -> brackets s . sitcc $ do
696702 sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
697703 breakpoint
698704 txt ".."
699− FromTo from to -> brackets . sitcc $ do
705+ FromTo from to -> brackets s . sitcc $ do
700706 located from p_hsExpr
701707 breakpoint
702708 txt ".."
703709 space
704710 located to p_hsExpr
705− FromThenTo from next to -> brackets . sitcc $ do
711+ FromThenTo from next to -> brackets s . sitcc $ do
706712 sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next]
707713 breakpoint
708714 txt ".."
@@ -799,7 +805,7 @@ p_patSynBind PSB {..} = do
799805 inci $ do
800806 switchLayout (getLoc . recordPatSynPatVar <$> xs) $ do
801807 unless (null xs) breakpoint
802− braces . sitcc $
808+ braces N . sitcc $
803809 sep (comma >> breakpoint) (p_rdrName . recordPatSynPatVar) xs
804810 breakpoint
805811 rhs
@@ -829,20 +835,20 @@ p_pat = \case
829835 txt "@"
830836 located pat p_pat
831837 ParPat NoExt pat ->
832− located pat (parens . p_pat)
838+ located pat (parens S . p_pat)
833839 BangPat NoExt pat -> do
834840 txt "!"
835841 located pat p_pat
836842 ListPat NoExt pats -> do
837− brackets . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats
843+ brackets S . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats
838844 TuplePat NoExt pats boxing -> do
839845 let f =
840846 case boxing of
841− Boxed -> parens
842− Unboxed -> parensHash
847+ Boxed -> parens S
848+ Unboxed -> parensHash S
843849 f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats
844850 SumPat NoExt pat tag arity ->
845− p_unboxedSum tag arity (located pat p_pat)
851+ p_unboxedSum S tag arity (located pat p_pat)
846852 ConPatIn pat details ->
847853 case details of
848854 PrefixCon xs -> sitcc $ do
@@ -856,9 +862,10 @@ p_pat = \case
856862 let f = \case
857863 Nothing -> txt ".."
858864 Just x -> located x p_pat_hsRecField
859− inci . braces . sitcc . sep (comma >> breakpoint) f $ case dotdot of
860− Nothing -> Just <$> fields
861− Just n -> (Just <$> take n fields) ++ [Nothing]
865+ inci . braces N . sitcc . sep (comma >> breakpoint) f $
866+ case dotdot of
867+ Nothing -> Just <$> fields
868+ Just n -> (Just <$> take n fields) ++ [Nothing]
862869 InfixCon x y -> do
863870 located x p_pat
864871 space
@@ -898,8 +905,8 @@ p_pat_hsRecField HsRecField {..} = do
898905 breakpoint
899906 inci (located hsRecFieldArg p_pat)
900907
901−p_unboxedSum :: ConTag -> Arity -> R () -> R ()
902−p_unboxedSum tag arity m = do
908+p_unboxedSum :: BracketStyle -> ConTag -> Arity -> R () -> R ()
909+p_unboxedSum s tag arity m = do
903910 let before = tag - 1
904911 after = arity - before - 1
905912 args = replicate before Nothing <> [Just m] <> replicate after Nothing
@@ -913,7 +920,7 @@ p_unboxedSum tag arity m = do
913920 unless isFirst space
914921 m'
915922 unless isLast space
916− parensHash $ sep (txt "|") f (zip args [0..])
923+ parensHash s $ sep (txt "|") f (zip args [0..])
917924
918925p_hsSplice :: HsSplice GhcPs -> R ()
919926p_hsSplice = \case
@@ -938,7 +945,7 @@ p_hsSpliceTH
938945p_hsSpliceTH isTyped expr = \case
939946 HasParens -> do
940947 txt decoSymbol
941− parens (located expr (sitcc . p_hsExpr))
948+ parens N (located expr (sitcc . p_hsExpr))
942949 HasDollar -> do
943950 txt decoSymbol
944951 located expr (sitcc . p_hsExpr)
@@ -971,7 +978,7 @@ p_hsBracket = \case
971978 (\i -> isPunctuation i || isSymbol i)
972979 (showOutputable (rdrNameOcc name))
973980 && not (doesNotNeedExtraParens name)
974− wrapper = if isOperator then parens else id
981+ wrapper = if isOperator then parens N else id
975982 wrapper $ p_rdrName (noLoc name)
976983 TExpBr NoExt expr -> do
977984 txt "[||"
@@ -1136,9 +1143,9 @@ exprOpTree :: LHsExpr GhcPs -> OpTree (LHsExpr GhcPs) (LHsExpr GhcPs)
11361143exprOpTree (L _ (OpApp NoExt x op y)) = OpBranch (exprOpTree x) op (exprOpTree y)
11371144exprOpTree n = OpNode n
11381145
1139−p_exprOpTree :: OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) -> R ()
1140−p_exprOpTree (OpNode x) = located x p_hsExpr
1141−p_exprOpTree (OpBranch x op y) = do
1146+p_exprOpTree :: BracketStyle -> OpTree (LHsExpr GhcPs) (LHsExpr GhcPs) -> R ()
1147+p_exprOpTree s (OpNode x) = located x (p_hsExpr' s)
1148+p_exprOpTree s (OpBranch x op y) = do
11421149 -- NOTE If the beginning of the first argument and the second argument
11431150 -- are on the same line, and the second argument has a hanging form, use
11441151 -- hanging placement.
@@ -1158,12 +1165,12 @@ p_exprOpTree (OpBranch x op y) = do
11581165 Hanging -> useBraces
11591166 Normal -> dontUseBraces)
11601167 switchLayout [opTreeLoc x] $
1161− ub $ p_exprOpTree x
1168+ ub $ p_exprOpTree s x
11621169 placeHanging placement $ do
11631170 located op (opWrapper . p_hsExpr)
11641171 space
11651172 switchLayout [opTreeLoc y] $
1166− p_exprOpTree y
1173+ p_exprOpTree N y
11671174
11681175-- | Get annotations for the enclosing element.
11691176
src/Ormolu/Printer/Meat/Declaration/Warning.hs+1−1
@@ -48,4 +48,4 @@ warningText = \case
4848p_lits :: [Located StringLiteral] -> R ()
4949p_lits = \case
5050 [l] -> atom l
51− ls -> brackets . sitcc $ sep (comma >> breakpoint) atom ls
51+ ls -> brackets N . sitcc $ sep (comma >> breakpoint) atom ls
src/Ormolu/Printer/Meat/ImportExport.hs+14−10
@@ -23,7 +23,9 @@ p_hsmodExports [] = do
2323 breakpoint'
2424 txt ")"
2525p_hsmodExports xs =
26− parens . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_lie) xs
26+ parens N . sitcc $ do
27+ sep (comma >> breakpoint) (sitcc . located' p_lie) xs
28+ p_trailingCommaFor xs
2729
2830p_hsmodImport :: ImportDecl GhcPs -> R ()
2931p_hsmodImport ImportDecl {..} = do
@@ -55,10 +57,11 @@ p_hsmodImport ImportDecl {..} = do
5557 when hiding (txt "hiding")
5658 case ideclHiding of
5759 Nothing -> return ()
58− Just (_, (L _ a)) -> do
60+ Just (_, (L _ xs)) -> do
5961 breakpoint
60− parens . sitcc $
61− sep (comma >> breakpoint) (sitcc . located' p_lie) a
62+ parens N . sitcc $ do
63+ sep (comma >> breakpoint) (sitcc . located' p_lie) xs
64+ p_trailingCommaFor xs
6265 newline
6366p_hsmodImport (XImportDecl NoExt) = notImplemented "XImportDecl"
6467
@@ -77,12 +80,13 @@ p_lie = \case
7780 inci $ do
7881 let names :: [R ()]
7982 names = located' p_ieWrappedName <$> xs
80− parens . sitcc . sep (comma >> breakpoint) sitcc $
81− case w of
82− NoIEWildcard -> names
83− IEWildcard n ->
84− let (before, after) = splitAt n names
85− in before ++ [txt ".."] ++ after
83+ parens N . sitcc $
84+ sep (comma >> breakpoint) sitcc $
85+ case w of
86+ NoIEWildcard -> names
87+ IEWildcard n ->
88+ let (before, after) = splitAt n names
89+ in before ++ [txt ".."] ++ after
8690 IEModuleContents NoExt l1 -> located l1 p_hsmodName
8791 -- XXX I have no idea what these things are for.
8892 IEGroup NoExt _ _ -> return ()
src/Ormolu/Printer/Meat/Type.hs+13−13
@@ -60,18 +60,18 @@ p_hsType = \case
6060 case y' of
6161 HsFunTy{} -> p_hsType y'
6262 _ -> located y p_hsType
63− HsListTy NoExt t -> located t (brackets . p_hsType)
63+ HsListTy NoExt t -> located t (brackets N . p_hsType)
6464 HsTupleTy NoExt tsort xs ->
6565 let parens' =
6666 case tsort of
67− HsUnboxedTuple -> parensHash
68− HsBoxedTuple -> parens
69− HsConstraintTuple -> parens
70− HsBoxedOrConstraintTuple -> parens
67+ HsUnboxedTuple -> parensHash N
68+ HsBoxedTuple -> parens N
69+ HsConstraintTuple -> parens N
70+ HsBoxedOrConstraintTuple -> parens N
7171 in parens' . sitcc $
7272 sep (comma >> breakpoint) (sitcc . located' p_hsType) xs
7373 HsSumTy NoExt xs ->
74− parensHash . sitcc $
74+ parensHash N . sitcc $
7575 sep (txt "|" >> breakpoint) (sitcc . located' p_hsType) xs
7676 HsOpTy NoExt x op y -> sitcc $ do
7777 let opTree = OpBranch (tyOpTree x) op (tyOpTree y)
@@ -83,7 +83,7 @@ p_hsType = \case
8383 -- always necessary when we have kind signatures in place.
8484 p_hsType t
8585 HsParTy NoExt t ->
86− parens (located t p_hsType)
86+ parens N (located t p_hsType)
8787 HsIParamTy NoExt n t -> sitcc $ do
8888 located n atom
8989 breakpoint
@@ -94,7 +94,7 @@ p_hsType = \case
9494 HsStarTy NoExt _ -> txt "*"
9595 HsKindSig NoExt t k ->
9696 -- NOTE Also see the comment for 'HsParTy'.
97− parens . sitcc $ do
97+ parens N . sitcc $ do
9898 located t p_hsType
9999 space -- FIXME
100100 inci $ do
@@ -119,7 +119,7 @@ p_hsType = \case
119119 case p of
120120 Promoted -> txt "'"
121121 NotPromoted -> return ()
122− brackets $ do
122+ brackets N $ do
123123 -- If both this list itself and the first element is promoted,
124124 -- we need to put a space in between or it fails to parse.
125125 case (p, xs) of
@@ -128,7 +128,7 @@ p_hsType = \case
128128 sitcc $ sep (comma >> breakpoint) (sitcc . located' p_hsType) xs
129129 HsExplicitTupleTy NoExt xs -> do
130130 txt "'"
131− parens $ do
131+ parens N $ do
132132 case xs of
133133 ((L _ t):_) | isPromoted t -> space
134134 _ -> return ()
@@ -150,14 +150,14 @@ p_hsContext :: HsContext GhcPs -> R ()
150150p_hsContext = \case
151151 [] -> txt "()"
152152 [x] -> located x p_hsType
153− xs -> parens . sitcc $
153+ xs -> parens N . sitcc $
154154 sep (comma >> breakpoint) (sitcc . located' p_hsType) xs
155155
156156p_hsTyVarBndr :: HsTyVarBndr GhcPs -> R ()
157157p_hsTyVarBndr = \case
158158 UserTyVar NoExt x ->
159159 p_rdrName x
160− KindedTyVar NoExt l k -> parens $ do
160+ KindedTyVar NoExt l k -> parens N $ do
161161 located l atom
162162 breakpoint
163163 inci $ do
@@ -167,7 +167,7 @@ p_hsTyVarBndr = \case
167167 XTyVarBndr NoExt -> notImplemented "XTyVarBndr"
168168
169169p_conDeclFields :: [LConDeclField GhcPs] -> R ()
170−p_conDeclFields xs = braces . sitcc $
170+p_conDeclFields xs = braces N . sitcc $
171171 sep (comma >> breakpoint) (sitcc . located' p_conDeclField) xs
172172
173173p_conDeclField :: ConDeclField GhcPs -> R ()
tests/Ormolu/Printer/CombinatorsSpec.hsdeleted−115
@@ -1,115 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−
3−module Ormolu.Printer.CombinatorsSpec (spec) where
4−
5−import Ormolu.Parser.Anns
6−import Ormolu.Printer.Combinators
7−import Ormolu.Printer.Internal
8−import Test.Hspec
9−import qualified Data.Text.IO as T
10−
11−-- NOTE Testing combinators in separation is easy, but it's not very useful
12−-- either because interesting things happen only when combinators are used
13−-- together. However, there are too many combinations to test, so we test
14−-- just a few things here and expect new tests to be added on demand when
15−-- particularly tricky or problematic use cases are discovered.
16−--
17−-- The printing logic is indirectly tested in the main bulk of reformatting
18−-- tests.
19−
20−spec :: Spec
21−spec = do
22− stdTest "simple signature" rSimpleSig "ssig"
23− stdTest "list" rList0 "list0"
24− stdTest "list with annotated name in it" rList1 "list1"
25− stdTest "module header" rModuleHeader "mhdr"
26−
27−-- | Do a standard test prodedure testing both single-line and multi-line
28−-- layouts.
29−
30−stdTest
31− :: String -- ^ Test name
32− -> R () -- ^ Computation to test
33− -> FilePath -- ^ Base file name where expected result is stored
34− -> Spec
35−stdTest name m path = describe name $ do
36− let spath = "data/printer/" ++ path ++ "-single.hs"
37− mpath = "data/printer/" ++ path ++ "-multi.hs"
38− it "single-line works" $
39− singleLine m `shouldRender` spath
40− it "multi-line works" $
41− multiLine m `shouldRender` mpath
42−
43−----------------------------------------------------------------------------
44−-- Test computations
45−
46−line :: R () -> R ()
47−line m = m >> newline
48−
49−rSimpleSig :: R ()
50−rSimpleSig = line rFn
51−
52−rList0 :: R ()
53−rList0 = line . brackets . sitcc $
54− sep (comma >> breakpoint) id
55− [ txt "foo"
56− , txt "bar"
57− , txt "baz"
58− ]
59−
60−rList1 :: R ()
61−rList1 = line . brackets . sitcc $
62− sep (comma >> breakpoint) id
63− [ txt "foo"
64− , txt "bar"
65− , rFn
66− ]
67−
68−rFn :: R ()
69−rFn = sitcc $ sep breakpoint sitcc
70− [ txt "foo"
71− , inci . sitcc $ sep breakpoint sitcc
72− [ do txt ":: "
73− parens . sitcc $ sep breakpoint sitcc
74− [ txt "Int"
75− , txt "-> " >> txt "Int"
76− ]
77− , txt "-> " >> txt "Bool"
78− ]
79− ]
80−
81−rModuleHeader :: R ()
82−rModuleHeader = do
83− line $ do
84− txt "module "
85− txt "MyModule"
86− line . inci . parens . sitcc . sep (comma >> breakpoint) sitcc $
87− [ txt "R"
88− , txt "runR"
89− , txt "txt"
90− , txt "blah"
91− ]
92− line (txt "where")
93−
94−----------------------------------------------------------------------------
95−-- Helpers
96−
97−-- | Run given computation in @'R' ()@ monad and compare result of rendering
98−-- with contents of a given file.
99−
100−shouldRender :: R () -> FilePath -> Expectation
101−shouldRender m path = do
102− let rendered = runR m mempty mempty emptyAnns
103− -- T.writeFile path rendered
104− expected <- T.readFile path
105− rendered `shouldBe` expected
106−
107−-- | Render using single-line layout.
108−
109−singleLine :: R () -> R ()
110−singleLine = enterLayout SingleLine
111−
112−-- | Render using multi-line layout.
113−
114−multiLine :: R () -> R ()
115−multiLine = enterLayout MultiLine
tests/Ormolu/PrinterSpec.hs+13−1
@@ -2,6 +2,7 @@
22
33module Ormolu.PrinterSpec (spec) where
44
5+import Control.Exception
56import Control.Monad
67import Control.Monad.IO.Class
78import Data.List (isSuffixOf)
@@ -22,7 +23,7 @@ spec = do
2223-- | Check a single given example.
2324
2425checkExample :: Path Rel File -> Spec
25−checkExample srcPath' = it (fromRelFile srcPath' ++ " works") $ do
26+checkExample srcPath' = it (fromRelFile srcPath' ++ " works") . withNiceExceptions $ do
2627 let srcPath = examplesDir </> srcPath'
2728 expectedOutputPath <- deriveOutput srcPath
2829 -- 1. Given input snippet of source code parse it and pretty print it.
@@ -80,3 +81,14 @@ shouldMatch idempotencyTest actual expected =
8081
8182examplesDir :: Path Rel Dir
8283examplesDir = $(mkRelDir "data/examples")
84+
85+-- | Inside this wrapper 'OrmoluException' will be caught and displayed
86+-- nicely using 'displayException'.
87+
88+withNiceExceptions
89+ :: Expectation -- ^ Action that may throw the exception
90+ -> Expectation
91+withNiceExceptions m = m `catch` h
92+ where
93+ h :: OrmoluException -> IO ()
94+ h = expectationFailure . displayException