on master help sign in
Change the way operators are formatted
commite2a6305bd3d5c2ee64f541b778a5fb97075a868c authorUtku Demir <me@utdemir.com> dateTue 6 Aug 2019, 23:29 (7 years ago) parent5955c51
22 files changed+230−91
data/examples/declaration/value/function/arrow/proc-do-complex-out.hs+5−5 data/examples/declaration/value/function/arrow/proc-forms-out.hs+10−10 data/examples/declaration/value/function/arrow/proc-ifs-out.hs+2−2 data/examples/declaration/value/function/arrow/proc-lets-out.hs+4−4 data/examples/declaration/value/function/case-multi-line-out.hs+2−2 data/examples/declaration/value/function/do-out.hs+33−11 data/examples/declaration/value/function/do.hs+20 data/examples/declaration/value/function/guards-out.hs+4−4 data/examples/declaration/value/function/if-multi-line-out.hs+2−2 data/examples/declaration/value/function/lambda-multi-line-out.hs+4−4 data/examples/declaration/value/function/let-multi-line-out.hs+2−2 data/examples/declaration/value/function/list-comprehensions-out.hs+5−5 data/examples/declaration/value/function/operators-out.hs+41 data/examples/declaration/value/function/operators.hs+36 data/examples/declaration/value/function/parallel-comprehensions-out.hs+9−7 data/examples/declaration/value/function/pattern/splice-pattern-out.hs+2−2 data/examples/declaration/value/function/transform-comprehensions-out.hs+4−4 data/examples/declaration/value/other/comments-get-before-op-out.hs+3−3 default.nix+1−1 src/Ormolu/Printer/Comments.hs+12−3 src/Ormolu/Printer/Internal.hs+13−7 src/Ormolu/Printer/Meat/Declaration/Value.hs+16−13
data/examples/declaration/value/function/arrow/proc-do-complex-out.hs+5−5
@@ -31,8 +31,8 @@ foo
3131 w
3232 ) -> \u -> -- Procs can have lambdas
3333 let v =
34− u ^ -- Actually never used
35− 2
34+ u -- Actually never used
35+ ^ 2
3636 in ( returnA -<
3737 -- Just do the calculation
3838 (x + y * z)
@@ -55,7 +55,7 @@ foo
5555 then ma -< ()
5656 else returnA -< ()
5757 returnA -<
58− ( i +
59− x *
60− y -- Just do the calculation
58+ ( i
59+ + x
60+ * y -- Just do the calculation
6161 )
data/examples/declaration/value/function/arrow/proc-forms-out.hs+10−10
@@ -9,15 +9,15 @@ foo1 f g h x =
99foo2 f g h x =
1010 proc (y, z) ->
1111 (| test
12− ( h f .
13− h g -<
14− y x .
15− y z
12+ ( h f
13+ . h g -<
14+ y x
15+ . y z
1616 )
17− ( h g .
18− h f -<
19− y z .
20− y x
17+ ( h g
18+ . h f -<
19+ y z
20+ . y x
2121 )
2222 |)
2323
@@ -30,6 +30,6 @@ bar1 f g h x =
3030bar2 f g h x =
3131 proc (y, z) ->
3232 (h f . h g) -<
33− (y x) . y z |||
34− (h g . h f) -<
33+ (y x) . y z
34+ ||| (h g . h f) -<
3535 y z . (y x)
data/examples/declaration/value/function/arrow/proc-ifs-out.hs+2−2
@@ -5,8 +5,8 @@ foo f = proc a -> if a then f -< 0 else f -< 1
55bar f g = proc a ->
66 if f a
77 then
8− f .
9− g -<
8+ f
9+ . g -<
1010 a
1111 else
1212 g -<
data/examples/declaration/value/function/arrow/proc-lets-out.hs+4−4
@@ -4,9 +4,9 @@ foo f = proc a -> let b = a in f -< b
44
55bar f g = proc a ->
66 let h =
7− f .
8− g a
7+ f
8+ . g a
99 j =
10− g .
11− h
10+ g
11+ . h
1212 in id -< (h, j)
data/examples/declaration/value/function/case-multi-line-out.hs+2−2
@@ -26,8 +26,8 @@ withGuards x =
2626 case x of
2727 x
2828 | x > 10 ->
29− foo +
30− bar
29+ foo
30+ + bar
3131 x | x > 5 -> 10
3232 _ -> 20
3333
data/examples/declaration/value/function/do-out.hs+33−11
@@ -23,17 +23,16 @@ baz = do
2323 let d = c + 2
2424 return d
2525
26−quux =
27− something $ do
28− foo
29− case x of
30− 1 -> 10
31− 2 -> 20
32− bar
33− if something
34− then x
35− else y
36− baz
26+quux = something $ do
27+ foo
28+ case x of
29+ 1 -> 10
30+ 2 -> 20
31+ bar
32+ if something
33+ then x
34+ else y
35+ baz
3736
3837foo = do
3938 rec a <- b + 5
@@ -53,6 +52,29 @@ trickyLet = do
5352 let x = 5
5453 in bar x
5554
55+f = unFoo . foo bar baz 3 $ do
56+ act
57+ ret
58+
59+g = unFoo
60+ . foo
61+ bar
62+ baz
63+ 3 $ do
64+ act
65+ ret
66+
67+main =
68+ do
69+ stuff
70+ `finally` do
71+ recover
72+
73+foo =
74+ do
75+ 1
76+ + 2
77+
5678-- single line let-where
5779samples n f = do
5880 gen <- newQCGen
data/examples/declaration/value/function/do.hs+20
@@ -52,6 +52,26 @@ trickyLet = do
5252 let x = 5
5353 in bar x
5454
55+f = unFoo . foo bar baz 3 $ do
56+ act
57+ ret
58+
59+g = unFoo . foo
60+ bar
61+ baz 3 $ do
62+ act
63+ ret
64+
65+main =
66+ do stuff
67+ `finally` do
68+ recover
69+
70+foo = do
71+ 1
72+ +
73+ 2
74+
5575-- single line let-where
5676samples n f = do
5777 gen <- newQCGen
data/examples/declaration/value/function/guards-out.hs+4−4
@@ -6,11 +6,11 @@ foo x
66bar :: Int -> Int
77bar x
88 | x == 5 =
9− foo x +
10− foo 10
9+ foo x
10+ + foo 10
1111 | x == 6 =
12− foo x +
13− foo 20
12+ foo x
13+ + foo 20
1414 | otherwise = foo 100
1515
1616baz :: Int -> Int
data/examples/declaration/value/function/if-multi-line-out.hs+2−2
@@ -8,8 +8,8 @@ bar :: Int -> Int
88bar x =
99 if x > 5
1010 then
11− foo x +
12− 100
11+ foo x
12+ + 100
1313 else
1414 case x of
1515 1 -> 10
data/examples/declaration/value/function/lambda-multi-line-out.hs+4−4
@@ -13,10 +13,10 @@ tricky0 =
1313 canUnify poly_given_ok wt gt || go False wt gt
1414
1515tricky1 =
16− flip all (zip ws gs) $
17− \(wt, gt) -> canUnify poly_given_ok wt gt || go False wt gt
16+ flip all (zip ws gs)
17+ $ \(wt, gt) -> canUnify poly_given_ok wt gt || go False wt gt
1818
1919tricky2 =
20− flip all (zip ws gs) $
21− \(wt, gt) ->
20+ flip all (zip ws gs)
21+ $ \(wt, gt) ->
2222 canUnify poly_given_ok wt gt || go False wt gt
data/examples/declaration/value/function/let-multi-line-out.hs+2−2
@@ -8,8 +8,8 @@ bar :: Int -> Int
88bar x =
99 let z = y
1010 y = x
11− in z +
12− 100
11+ in z
12+ + 100
1313
1414inlineComment :: Int -> Int
1515inlineComment =
data/examples/declaration/value/function/list-comprehensions-out.hs+5−5
@@ -9,12 +9,12 @@ barbaz x y z w =
99 b <- y, -- Baz
1010 any even [a, b],
1111 c <-
12− z *
13− z ^
14− 2, -- Bar baz
12+ z
13+ * z
14+ ^ 2, -- Bar baz
1515 d <-
16− w +
17− w, -- Baz bar
16+ w
17+ + w, -- Baz bar
1818 all
1919 even
2020 [ a,
data/examples/declaration/value/function/operators-out.hsnew file+41
@@ -0,0 +1,41 @@
1+foo = bar
2+ ++ {- some comment -}
3+ case foo of
4+ a -> a
5+
6+main =
7+ bar
8+ $ baz -- bar
9+ -- baz
10+
11+f =
12+ Foo <$> bar
13+ <*> baz
14+
15+update =
16+ do
17+ foobar
18+ `catch` \case
19+ a -> a
20+
21+foo =
22+ do
23+ 1
24+ + 2
25+
26+main =
27+ do
28+ stuff
29+ `finally` do
30+ recover
31+
32+lenses =
33+ Just $ M.fromList
34+ $ "type"
35+ .= ("user.connection" :: Text)
36+ # "connection"
37+ .= uc
38+ # "user" .= case name of
39+ Just n -> Just $ object ["name" .= n]
40+ Nothing -> Nothing
41+ # []
data/examples/declaration/value/function/operators.hsnew file+36
@@ -0,0 +1,36 @@
1+foo = bar ++
2+ {- some comment -}
3+ case foo of
4+ a -> a
5+
6+main =
7+ bar $ -- bar
8+ baz -- baz
9+
10+f =
11+ Foo <$> bar
12+ <*> baz
13+
14+update =
15+ do
16+ foobar
17+ `catch` \case
18+ a -> a
19+
20+foo = do
21+ 1
22+ +
23+ 2
24+
25+main =
26+ do stuff
27+ `finally` do
28+ recover
29+
30+lenses = Just $ M.fromList
31+ $ "type" .= ("user.connection" :: Text)
32+ # "connection" .= uc
33+ # "user" .= case name of
34+ Just n -> Just $ object ["name" .= n]
35+ Nothing -> Nothing
36+ # []
data/examples/declaration/value/function/parallel-comprehensions-out.hs+9−7
@@ -18,20 +18,22 @@ baz x y z w =
1818 x, -- Foo 2
1919 b <- -- Bar 1
2020 y, -- Bar 2
21− a `mod`
22− b == -- Value
23− 0
21+ a
22+ `mod` b -- Value
23+ == 0
2424 | c <- -- Baz 1
25− z * -- Baz 2
26− z -- Baz 3
25+ z
26+ * z -- Baz 2
27+ -- Baz 3
2728 | d <- w -- Other
2829 | e <- x * x -- Foo bar
2930 | f <- -- Foo baz 1
3031 y + y -- Foo baz 2
3132 | h <- z + z * w ^ 2 -- Bar foo
3233 | i <- -- Bar bar 1
33− a + -- Bar bar 2
34− b, -- Bar bar 3
34+ a
35+ + b, -- Bar bar 2
36+ -- Bar bar 3
3537 j <- -- Bar baz 1
3638 a + b -- Bar baz 2
3739 ]
data/examples/declaration/value/function/pattern/splice-pattern-out.hs+2−2
@@ -5,8 +5,8 @@ singleLine = case () of
55 $(y "something") -> ()
66
77multiline = case () of
8− $( x +
9− y
8+ $( x
9+ + y
1010 ) -> ()
1111 $( y
1212 "something"
data/examples/declaration/value/function/transform-comprehensions-out.hs+4−4
@@ -23,8 +23,8 @@ bar' xs ys =
2323 -- First comment
2424 then sortWith
2525 by
26− ( x +
27− y -- Second comment
26+ ( x
27+ + y -- Second comment
2828 )
2929 ]
3030
@@ -50,8 +50,8 @@ quux' xs ys =
5050 y <- ys,
5151 -- First comment
5252 then group by
53− ( x +
54− y
53+ ( x
54+ + y
5555 )
5656 -- Second comment
5757 using groupWith -- Third comment
data/examples/declaration/value/other/comments-get-before-op-out.hs+3−3
@@ -4,7 +4,7 @@ main = do
44 [ migration1,
55 migration1,
66 migration3
7− ] -- When adding migrations here, don't forget to update
7+ ]
8+ -- When adding migrations here, don't forget to update
89 -- 'schemaVersion' in Galley.Data
9− `finally`
10− Log.close
10+ `finally` Log.close
default.nix+1−1
@@ -23,7 +23,7 @@ in {
2323 ormolu = haskellPackages.ormolu;
2424 ormolu-shell = haskellPackages.shellFor {
2525 packages = ps: [ ps.ormolu ];
26− buildInputs = [ pkgs.cabal-install ];
26+ buildInputs = [ haskellPackages.cabal-install haskellPackages.ghcid ];
2727 };
2828 hackage = pkgs.lib.mapAttrs ormolize haskellPackages;
2929}
src/Ormolu/Printer/Comments.hs+12−3
@@ -78,13 +78,22 @@ spitFollowingComment
7878spitFollowingComment (L ref a) mlastSpn = do
7979 mnSpn <- nextEltSpan
8080 meSpn <- getEnclosingSpan ref
81+ newlineModified <- isNewlineModified
8182 i <- getIndent
8283 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
8384 if theSameLine l ref && not (isModule a)
8485 then modNewline $ \m -> setIndent i $ do
85− spit " "
86− spitComment comment
87− m
86+ if newlineModified
87+ then do
88+ -- This happens when we have several lines each with its own
89+ -- comment and they get merged by the formatter.
90+ m
91+ spitComment comment
92+ newline
93+ else do
94+ spit " "
95+ spitComment comment
96+ m
8897 else modNewline $ \m -> setIndent i $ do
8998 m
9099 when (needsNewlineBefore l mlastSpn) newline
src/Ormolu/Printer/Internal.hs+13−7
@@ -16,6 +16,7 @@ module Ormolu.Printer.Internal
1616 , spit
1717 , newline
1818 , modNewline
19+ , isNewlineModified
1920 , ensureIndent
2021 , inci
2122 , sitcc
@@ -39,7 +40,7 @@ where
3940import Control.Monad.Reader
4041import Control.Monad.State.Strict
4142import Data.Coerce
42−import Data.Maybe (listToMaybe)
43+import Data.Maybe (fromMaybe, isJust, listToMaybe)
4344import Data.Text (Text)
4445import Data.Text.Lazy.Builder
4546import Debug.Trace
@@ -89,8 +90,8 @@ data SC = SC
8990 -- ^ Span stream
9091 , scCommentStream :: CommentStream
9192 -- ^ Comment stream
92− , scNewline :: R ()
93− -- ^ What to render as newline
93+ , scNewline :: Maybe (R ())
94+ -- ^ What to render as newline, or 'Nothing' ('newlineRaw' will be used)
9495 }
9596
9697-- | 'Layout' options.
@@ -125,7 +126,7 @@ runR debug (R m) sstream cstream anns =
125126 , scBuilder = mempty
126127 , scSpanStream = sstream
127128 , scCommentStream = cstream
128− , scNewline = newlineRaw
129+ , scNewline = Nothing
129130 }
130131
131132----------------------------------------------------------------------------
@@ -149,9 +150,9 @@ newline :: R ()
149150newline = do
150151 n <- R (gets scNewline)
151152 R . modify $ \sc -> sc
152− { scNewline = newlineRaw
153+ { scNewline = Nothing
153154 }
154− n
155+ fromMaybe newlineRaw n
155156
156157-- | Low-level newline primitive. This one always just inserts a newline, no
157158-- hooks can be attached.
@@ -179,9 +180,14 @@ modNewline :: (R () -> R ()) -> R ()
179180modNewline f = R $ do
180181 old <- gets scNewline
181182 modify $ \sc -> sc
182− { scNewline = f old
183+ { scNewline = Just $ f (fromMaybe newlineRaw old)
183184 }
184185
186+-- | Check if newline is in modified state.
187+
188+isNewlineModified :: R Bool
189+isNewlineModified = isJust <$> R (gets scNewline)
190+
185191-- | Ensure that indentation level is satisfied. Insert correct number of
186192-- spaces if it isn't.
187193
src/Ormolu/Printer/Meat/Declaration/Value.hs+16−13
@@ -472,21 +472,20 @@ p_hsExpr = \case
472472 located (hswc_body a) p_hsType
473473 OpApp NoExt x op y -> do
474474 located x p_hsExpr
475− space
476− let opWrapper = case unLoc op of
477− EWildPat NoExt -> backticks
478− _ -> id
479− -- NOTE Sometimes operator may be displaced from the line by comments,
480− -- it still should be more indented to remain valid code.
481− inci $ located op (opWrapper . p_hsExpr)
475+ -- NOTE If the end of the first argument and the beginning of the second
476+ -- argument are on the same line, and the second argument has a hanging
477+ -- form, use hanging placement.
482478 let placement =
483− -- NOTE If end of operator and start of second argument are on
484− -- different lines, always use normal placement.
485479 if isOneLineSpan
486− (mkSrcSpan (srcSpanEnd (getLoc op)) (srcSpanStart (getLoc y)))
480+ (mkSrcSpan (srcSpanEnd (getLoc x)) (srcSpanStart (getLoc y)))
487481 then exprPlacement (unLoc y)
488482 else Normal
489− placeHanging placement $
483+ opWrapper = case unLoc op of
484+ EWildPat NoExt -> backticks
485+ _ -> id
486+ placeHanging placement $ do
487+ located op (opWrapper . p_hsExpr)
488+ space
490489 located y p_hsExpr
491490 NegApp NoExt e _ -> do
492491 txt "- "
@@ -931,9 +930,13 @@ exprPlacement = \case
931930 HsDo NoExt DoExpr _ -> Hanging
932931 HsDo NoExt MDoExpr _ -> Hanging
933932 RecordCon NoExt _ _ -> Hanging
933+ -- If the rightmost expression in an operator chain is hanging, make the
934+ -- whole block hanging; so that we can use the common @f = foo $ do@
935+ -- style.
936+ OpApp NoExt _ _ y -> exprPlacement (unLoc y)
934937 HsProc NoExt (L s _) _ ->
935− -- Indentation breaks if pattern is longer than one line and left hanging.
936− -- Consequently, only apply hanging when it is safe.
938+ -- Indentation breaks if pattern is longer than one line and left
939+ -- hanging. Consequently, only apply hanging when it is safe.
937940 if isOneLineSpan s
938941 then Hanging
939942 else Normal