on master help sign in
Improve handling of newlines

Now the printing code can correctly detect when hanging constructions should be placed in non-hanging fashion from the way they are placed in input source code.

commitd7d4592b8ea646468b50b9fbd4d7d69d9c4fab85 authormrkkrp <mark.karpov@tweag.io> dateSat 6 Jul 2019, 23:57 (7 years ago) parentc9f3320
8 files changed+88−68
data/examples/declaration/instance/single-parameter-out.hs+3−2 data/examples/declaration/value/function/arrow/proc-cases-out.hs+12−11 data/examples/declaration/value/function/arrow/proc-forms-out.hs+26−22 data/examples/declaration/value/function/arrow/proc-lambdas-out.hs+5−2 data/examples/declaration/value/function/case-multi-line-out.hs+15−13 data/examples/declaration/value/function/do-out.hs+10−9 data/examples/declaration/value/function/lambda-single-line-out.hs+2−1 src/Ormolu/Printer/Meat/Declaration/Value.hs+15−8
data/examples/declaration/instance/single-parameter-out.hs+3−2
@@ -6,5 +6,6 @@ instance Enum Int where
66
77 fromEnum x = x
88
9− toEnum = \x ->
10− x
9+ toEnum =
10+ \x ->
11+ x
data/examples/declaration/value/function/arrow/proc-cases-out.hs+12−11
@@ -2,14 +2,15 @@
22
33foo f = proc a -> case a of Left b -> f -< b
44
5−bar f g h j = proc a -> case a of
6− Left
7− ( (a, b)
8− , (c, d)
9− ) -> f (a <> c) -< b <> d
10− Right
11− (Left a) ->
12− h -< a
13− Right
14− (Right b) ->
15− j -< b
5+bar f g h j =
6+ proc a -> case a of
7+ Left
8+ ( (a, b)
9+ , (c, d)
10+ ) -> f (a <> c) -< b <> d
11+ Right
12+ (Left a) ->
13+ h -< a
14+ Right
15+ (Right b) ->
16+ j -< b
data/examples/declaration/value/function/arrow/proc-forms-out.hs+26−22
@@ -2,30 +2,34 @@
22
33foo0 f g x y = proc _ -> (|f (g -< (x, y))|)
44
5−foo1 f g h x = proc (y, z) ->
6− (|test (h f . (h g) -< (y x) . y z) ((h g) . h f -< y z . (y x))|)
5+foo1 f g h x =
6+ proc (y, z) ->
7+ (|test (h f . (h g) -< (y x) . y z) ((h g) . h f -< y z . (y x))|)
78
8−foo2 f g h x = proc (y, z) ->
9− (| test
10− ( h f .
11− h g -<
12− y x .
13− y z
14− )
15− ( h g .
16− h f -<
17− y z .
18− y x
19− )
20− |)
9+foo2 f g h x =
10+ proc (y, z) ->
11+ (| test
12+ ( h f .
13+ h g -<
14+ y x .
15+ y z
16+ )
17+ ( h g .
18+ h f -<
19+ y z .
20+ y x
21+ )
22+ |)
2123
2224bar0 f g x y = proc _ -> f -< x &&& g -< y
2325
24−bar1 f g h x = proc (y, z) ->
25− h f . (h g) -< (y x) . y z ||| (h g) . h f -< y z . (y x)
26+bar1 f g h x =
27+ proc (y, z) ->
28+ h f . (h g) -< (y x) . y z ||| (h g) . h f -< y z . (y x)
2629
27−bar2 f g h x = proc (y, z) ->
28− (h f . h g) -<
29− (y x) . y z |||
30− (h g . h f) -<
31− y z . (y x)
30+bar2 f g h x =
31+ proc (y, z) ->
32+ (h f . h g) -<
33+ (y x) . y z |||
34+ (h g . h f) -<
35+ y z . (y x)
data/examples/declaration/value/function/arrow/proc-lambdas-out.hs+5−2
@@ -2,5 +2,8 @@
22
33foo = proc a -> \f b -> a -< f b -- Foo
44
5−bar = proc x -> \f g h -> \() -> \(Left (x, y)) -> -- Tuple value
6− f (g (h x)) -< y
5+bar =
6+ proc x -> \f g h ->
7+ \() ->
8+ \(Left (x, y)) -> -- Tuple value
9+ f (g (h x)) -< y
data/examples/declaration/value/function/case-multi-line-out.hs+15−13
@@ -4,12 +4,13 @@ foo x = case x of
44 _ -> 12
55
66bar :: Int -> Int
7−bar x = case x of
8− 5 ->
9− if x > 5
10− then 10
11− else 12
12− _ -> 12
7+bar x =
8+ case x of
9+ 5 ->
10+ if x > 5
11+ then 10
12+ else 12
13+ _ -> 12
1314
1415baz :: Int -> Int
1516baz x = case x of
@@ -21,10 +22,11 @@ quux x = case x of
2122 x -> x
2223
2324withGuards :: Int -> Int
24−withGuards x = case x of
25− x
26− | x > 10 ->
27− foo +
28− bar
29− x | x > 5 -> 10
30− _ -> 20
25+withGuards x =
26+ case x of
27+ x
28+ | x > 10 ->
29+ foo +
30+ bar
31+ x | x > 5 -> 10
32+ _ -> 20
data/examples/declaration/value/function/do-out.hs+10−9
@@ -4,15 +4,16 @@ bar = do
44 foo
55 bar
66
7−baz = mdo
8− bar a
9− a <- foo
10− b <-
11− bar
12− 1
13− 2
14− 3
15− return (a + b)
7+baz =
8+ mdo
9+ bar a
10+ a <- foo
11+ b <-
12+ bar
13+ 1
14+ 2
15+ 3
16+ return (a + b)
1617
1718baz = do
1819 a <- foo
data/examples/declaration/value/function/lambda-single-line-out.hs+2−1
@@ -2,7 +2,8 @@ foo :: a -> a -> a
22foo x = \y -> x
33
44bar :: a -> a -> a
5−bar x = \y -> x
5+bar x =
6+ \y -> x
67
78baz :: a -> a -> a
89baz = \ ~x ~y -> x
src/Ormolu/Printer/Meat/Declaration/Value.hs+15−8
@@ -142,14 +142,13 @@ p_match' placer pretty style isInfix m_pats m_grhss = do
142142 return inci'
143143
144144 let
145− -- Calculate a 'SrcSpan' that captures the end of the name\/pattern part
146− -- of the match. This is used to tell if we need to have the body on a
147− -- newline.
148− endOfPatsSpan = srcLocSpan . srcSpanEnd <$> case NE.nonEmpty m_pats of
145+ -- Calculate position of end of patterns. This is useful when we decide
146+ -- about putting certain constructions in hanging positions.
147+ endOfPats = case NE.nonEmpty m_pats of
149148 Nothing -> case style of
150− Function name -> Just $ getLoc name
149+ Function name -> (Just . srcSpanEnd . getLoc) name
151150 _ -> Nothing
152− Just pats -> Just . getLoc $ NE.last pats
151+ Just pats -> (Just . srcSpanEnd . getLoc . NE.last) pats
153152 isCase = \case
154153 Case -> True
155154 LambdaCase -> True
@@ -166,8 +165,16 @@ p_match' placer pretty style isInfix m_pats m_grhss = do
166165 _ -> txt " ->"
167166 let grhssSpan = combineSrcSpans' $
168167 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
169− patGrhssSpan = maybe grhssSpan (combineSrcSpans grhssSpan) endOfPatsSpan
170− placement = blockPlacement placer grhssGRHSs
168+ patGrhssSpan = maybe grhssSpan
169+ (combineSrcSpans grhssSpan . srcLocSpan) endOfPats
170+ placement =
171+ case endOfPats of
172+ Nothing -> blockPlacement placer grhssGRHSs
173+ Just spn ->
174+ if isOneLineSpan
175+ (mkSrcSpan spn (srcSpanStart grhssSpan))
176+ then blockPlacement placer grhssGRHSs
177+ else Normal
171178 inciLocalBinds = case placement of
172179 Normal -> id
173180 Hanging -> inci