mirror of
https://github.com/ilyakooo0/ormolu.git
synced 2024-10-26 19:17:55 +03:00
Change the way operators are formatted
This commit is contained in:
parent
5955c514ee
commit
e2a6305bd3
@ -31,8 +31,8 @@ foo
|
||||
w
|
||||
) -> \u -> -- Procs can have lambdas
|
||||
let v =
|
||||
u ^ -- Actually never used
|
||||
2
|
||||
u -- Actually never used
|
||||
^ 2
|
||||
in ( returnA -<
|
||||
-- Just do the calculation
|
||||
(x + y * z)
|
||||
@ -55,7 +55,7 @@ foo
|
||||
then ma -< ()
|
||||
else returnA -< ()
|
||||
returnA -<
|
||||
( i +
|
||||
x *
|
||||
y -- Just do the calculation
|
||||
( i
|
||||
+ x
|
||||
* y -- Just do the calculation
|
||||
)
|
||||
|
@ -9,15 +9,15 @@ foo1 f g h x =
|
||||
foo2 f g h x =
|
||||
proc (y, z) ->
|
||||
(| test
|
||||
( h f .
|
||||
h g -<
|
||||
y x .
|
||||
y z
|
||||
( h f
|
||||
. h g -<
|
||||
y x
|
||||
. y z
|
||||
)
|
||||
( h g .
|
||||
h f -<
|
||||
y z .
|
||||
y x
|
||||
( h g
|
||||
. h f -<
|
||||
y z
|
||||
. y x
|
||||
)
|
||||
|)
|
||||
|
||||
@ -30,6 +30,6 @@ bar1 f g h x =
|
||||
bar2 f g h x =
|
||||
proc (y, z) ->
|
||||
(h f . h g) -<
|
||||
(y x) . y z |||
|
||||
(h g . h f) -<
|
||||
(y x) . y z
|
||||
||| (h g . h f) -<
|
||||
y z . (y x)
|
||||
|
@ -5,8 +5,8 @@ foo f = proc a -> if a then f -< 0 else f -< 1
|
||||
bar f g = proc a ->
|
||||
if f a
|
||||
then
|
||||
f .
|
||||
g -<
|
||||
f
|
||||
. g -<
|
||||
a
|
||||
else
|
||||
g -<
|
||||
|
@ -4,9 +4,9 @@ foo f = proc a -> let b = a in f -< b
|
||||
|
||||
bar f g = proc a ->
|
||||
let h =
|
||||
f .
|
||||
g a
|
||||
f
|
||||
. g a
|
||||
j =
|
||||
g .
|
||||
h
|
||||
g
|
||||
. h
|
||||
in id -< (h, j)
|
||||
|
@ -26,8 +26,8 @@ withGuards x =
|
||||
case x of
|
||||
x
|
||||
| x > 10 ->
|
||||
foo +
|
||||
bar
|
||||
foo
|
||||
+ bar
|
||||
x | x > 5 -> 10
|
||||
_ -> 20
|
||||
|
||||
|
@ -23,17 +23,16 @@ baz = do
|
||||
let d = c + 2
|
||||
return d
|
||||
|
||||
quux =
|
||||
something $ do
|
||||
foo
|
||||
case x of
|
||||
1 -> 10
|
||||
2 -> 20
|
||||
bar
|
||||
if something
|
||||
then x
|
||||
else y
|
||||
baz
|
||||
quux = something $ do
|
||||
foo
|
||||
case x of
|
||||
1 -> 10
|
||||
2 -> 20
|
||||
bar
|
||||
if something
|
||||
then x
|
||||
else y
|
||||
baz
|
||||
|
||||
foo = do
|
||||
rec a <- b + 5
|
||||
@ -53,6 +52,29 @@ trickyLet = do
|
||||
let x = 5
|
||||
in bar x
|
||||
|
||||
f = unFoo . foo bar baz 3 $ do
|
||||
act
|
||||
ret
|
||||
|
||||
g = unFoo
|
||||
. foo
|
||||
bar
|
||||
baz
|
||||
3 $ do
|
||||
act
|
||||
ret
|
||||
|
||||
main =
|
||||
do
|
||||
stuff
|
||||
`finally` do
|
||||
recover
|
||||
|
||||
foo =
|
||||
do
|
||||
1
|
||||
+ 2
|
||||
|
||||
-- single line let-where
|
||||
samples n f = do
|
||||
gen <- newQCGen
|
||||
|
@ -52,6 +52,26 @@ trickyLet = do
|
||||
let x = 5
|
||||
in bar x
|
||||
|
||||
f = unFoo . foo bar baz 3 $ do
|
||||
act
|
||||
ret
|
||||
|
||||
g = unFoo . foo
|
||||
bar
|
||||
baz 3 $ do
|
||||
act
|
||||
ret
|
||||
|
||||
main =
|
||||
do stuff
|
||||
`finally` do
|
||||
recover
|
||||
|
||||
foo = do
|
||||
1
|
||||
+
|
||||
2
|
||||
|
||||
-- single line let-where
|
||||
samples n f = do
|
||||
gen <- newQCGen
|
||||
|
@ -6,11 +6,11 @@ foo x
|
||||
bar :: Int -> Int
|
||||
bar x
|
||||
| x == 5 =
|
||||
foo x +
|
||||
foo 10
|
||||
foo x
|
||||
+ foo 10
|
||||
| x == 6 =
|
||||
foo x +
|
||||
foo 20
|
||||
foo x
|
||||
+ foo 20
|
||||
| otherwise = foo 100
|
||||
|
||||
baz :: Int -> Int
|
||||
|
@ -8,8 +8,8 @@ bar :: Int -> Int
|
||||
bar x =
|
||||
if x > 5
|
||||
then
|
||||
foo x +
|
||||
100
|
||||
foo x
|
||||
+ 100
|
||||
else
|
||||
case x of
|
||||
1 -> 10
|
||||
|
@ -13,10 +13,10 @@ tricky0 =
|
||||
canUnify poly_given_ok wt gt || go False wt gt
|
||||
|
||||
tricky1 =
|
||||
flip all (zip ws gs) $
|
||||
\(wt, gt) -> canUnify poly_given_ok wt gt || go False wt gt
|
||||
flip all (zip ws gs)
|
||||
$ \(wt, gt) -> canUnify poly_given_ok wt gt || go False wt gt
|
||||
|
||||
tricky2 =
|
||||
flip all (zip ws gs) $
|
||||
\(wt, gt) ->
|
||||
flip all (zip ws gs)
|
||||
$ \(wt, gt) ->
|
||||
canUnify poly_given_ok wt gt || go False wt gt
|
||||
|
@ -8,8 +8,8 @@ bar :: Int -> Int
|
||||
bar x =
|
||||
let z = y
|
||||
y = x
|
||||
in z +
|
||||
100
|
||||
in z
|
||||
+ 100
|
||||
|
||||
inlineComment :: Int -> Int
|
||||
inlineComment =
|
||||
|
@ -9,12 +9,12 @@ barbaz x y z w =
|
||||
b <- y, -- Baz
|
||||
any even [a, b],
|
||||
c <-
|
||||
z *
|
||||
z ^
|
||||
2, -- Bar baz
|
||||
z
|
||||
* z
|
||||
^ 2, -- Bar baz
|
||||
d <-
|
||||
w +
|
||||
w, -- Baz bar
|
||||
w
|
||||
+ w, -- Baz bar
|
||||
all
|
||||
even
|
||||
[ a,
|
||||
|
41
data/examples/declaration/value/function/operators-out.hs
Normal file
41
data/examples/declaration/value/function/operators-out.hs
Normal file
@ -0,0 +1,41 @@
|
||||
foo = bar
|
||||
++ {- some comment -}
|
||||
case foo of
|
||||
a -> a
|
||||
|
||||
main =
|
||||
bar
|
||||
$ baz -- bar
|
||||
-- baz
|
||||
|
||||
f =
|
||||
Foo <$> bar
|
||||
<*> baz
|
||||
|
||||
update =
|
||||
do
|
||||
foobar
|
||||
`catch` \case
|
||||
a -> a
|
||||
|
||||
foo =
|
||||
do
|
||||
1
|
||||
+ 2
|
||||
|
||||
main =
|
||||
do
|
||||
stuff
|
||||
`finally` do
|
||||
recover
|
||||
|
||||
lenses =
|
||||
Just $ M.fromList
|
||||
$ "type"
|
||||
.= ("user.connection" :: Text)
|
||||
# "connection"
|
||||
.= uc
|
||||
# "user" .= case name of
|
||||
Just n -> Just $ object ["name" .= n]
|
||||
Nothing -> Nothing
|
||||
# []
|
36
data/examples/declaration/value/function/operators.hs
Normal file
36
data/examples/declaration/value/function/operators.hs
Normal file
@ -0,0 +1,36 @@
|
||||
foo = bar ++
|
||||
{- some comment -}
|
||||
case foo of
|
||||
a -> a
|
||||
|
||||
main =
|
||||
bar $ -- bar
|
||||
baz -- baz
|
||||
|
||||
f =
|
||||
Foo <$> bar
|
||||
<*> baz
|
||||
|
||||
update =
|
||||
do
|
||||
foobar
|
||||
`catch` \case
|
||||
a -> a
|
||||
|
||||
foo = do
|
||||
1
|
||||
+
|
||||
2
|
||||
|
||||
main =
|
||||
do stuff
|
||||
`finally` do
|
||||
recover
|
||||
|
||||
lenses = Just $ M.fromList
|
||||
$ "type" .= ("user.connection" :: Text)
|
||||
# "connection" .= uc
|
||||
# "user" .= case name of
|
||||
Just n -> Just $ object ["name" .= n]
|
||||
Nothing -> Nothing
|
||||
# []
|
@ -18,20 +18,22 @@ baz x y z w =
|
||||
x, -- Foo 2
|
||||
b <- -- Bar 1
|
||||
y, -- Bar 2
|
||||
a `mod`
|
||||
b == -- Value
|
||||
0
|
||||
a
|
||||
`mod` b -- Value
|
||||
== 0
|
||||
| c <- -- Baz 1
|
||||
z * -- Baz 2
|
||||
z -- Baz 3
|
||||
z
|
||||
* z -- Baz 2
|
||||
-- Baz 3
|
||||
| d <- w -- Other
|
||||
| e <- x * x -- Foo bar
|
||||
| f <- -- Foo baz 1
|
||||
y + y -- Foo baz 2
|
||||
| h <- z + z * w ^ 2 -- Bar foo
|
||||
| i <- -- Bar bar 1
|
||||
a + -- Bar bar 2
|
||||
b, -- Bar bar 3
|
||||
a
|
||||
+ b, -- Bar bar 2
|
||||
-- Bar bar 3
|
||||
j <- -- Bar baz 1
|
||||
a + b -- Bar baz 2
|
||||
]
|
||||
|
@ -5,8 +5,8 @@ singleLine = case () of
|
||||
$(y "something") -> ()
|
||||
|
||||
multiline = case () of
|
||||
$( x +
|
||||
y
|
||||
$( x
|
||||
+ y
|
||||
) -> ()
|
||||
$( y
|
||||
"something"
|
||||
|
@ -23,8 +23,8 @@ bar' xs ys =
|
||||
-- First comment
|
||||
then sortWith
|
||||
by
|
||||
( x +
|
||||
y -- Second comment
|
||||
( x
|
||||
+ y -- Second comment
|
||||
)
|
||||
]
|
||||
|
||||
@ -50,8 +50,8 @@ quux' xs ys =
|
||||
y <- ys,
|
||||
-- First comment
|
||||
then group by
|
||||
( x +
|
||||
y
|
||||
( x
|
||||
+ y
|
||||
)
|
||||
-- Second comment
|
||||
using groupWith -- Third comment
|
||||
|
@ -4,7 +4,7 @@ main = do
|
||||
[ migration1,
|
||||
migration1,
|
||||
migration3
|
||||
] -- When adding migrations here, don't forget to update
|
||||
]
|
||||
-- When adding migrations here, don't forget to update
|
||||
-- 'schemaVersion' in Galley.Data
|
||||
`finally`
|
||||
Log.close
|
||||
`finally` Log.close
|
||||
|
@ -23,7 +23,7 @@ in {
|
||||
ormolu = haskellPackages.ormolu;
|
||||
ormolu-shell = haskellPackages.shellFor {
|
||||
packages = ps: [ ps.ormolu ];
|
||||
buildInputs = [ pkgs.cabal-install ];
|
||||
buildInputs = [ haskellPackages.cabal-install haskellPackages.ghcid ];
|
||||
};
|
||||
hackage = pkgs.lib.mapAttrs ormolize haskellPackages;
|
||||
}
|
||||
|
@ -78,13 +78,22 @@ spitFollowingComment
|
||||
spitFollowingComment (L ref a) mlastSpn = do
|
||||
mnSpn <- nextEltSpan
|
||||
meSpn <- getEnclosingSpan ref
|
||||
newlineModified <- isNewlineModified
|
||||
i <- getIndent
|
||||
withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
|
||||
if theSameLine l ref && not (isModule a)
|
||||
then modNewline $ \m -> setIndent i $ do
|
||||
spit " "
|
||||
spitComment comment
|
||||
m
|
||||
if newlineModified
|
||||
then do
|
||||
-- This happens when we have several lines each with its own
|
||||
-- comment and they get merged by the formatter.
|
||||
m
|
||||
spitComment comment
|
||||
newline
|
||||
else do
|
||||
spit " "
|
||||
spitComment comment
|
||||
m
|
||||
else modNewline $ \m -> setIndent i $ do
|
||||
m
|
||||
when (needsNewlineBefore l mlastSpn) newline
|
||||
|
@ -16,6 +16,7 @@ module Ormolu.Printer.Internal
|
||||
, spit
|
||||
, newline
|
||||
, modNewline
|
||||
, isNewlineModified
|
||||
, ensureIndent
|
||||
, inci
|
||||
, sitcc
|
||||
@ -39,7 +40,7 @@ where
|
||||
import Control.Monad.Reader
|
||||
import Control.Monad.State.Strict
|
||||
import Data.Coerce
|
||||
import Data.Maybe (listToMaybe)
|
||||
import Data.Maybe (fromMaybe, isJust, listToMaybe)
|
||||
import Data.Text (Text)
|
||||
import Data.Text.Lazy.Builder
|
||||
import Debug.Trace
|
||||
@ -89,8 +90,8 @@ data SC = SC
|
||||
-- ^ Span stream
|
||||
, scCommentStream :: CommentStream
|
||||
-- ^ Comment stream
|
||||
, scNewline :: R ()
|
||||
-- ^ What to render as newline
|
||||
, scNewline :: Maybe (R ())
|
||||
-- ^ What to render as newline, or 'Nothing' ('newlineRaw' will be used)
|
||||
}
|
||||
|
||||
-- | 'Layout' options.
|
||||
@ -125,7 +126,7 @@ runR debug (R m) sstream cstream anns =
|
||||
, scBuilder = mempty
|
||||
, scSpanStream = sstream
|
||||
, scCommentStream = cstream
|
||||
, scNewline = newlineRaw
|
||||
, scNewline = Nothing
|
||||
}
|
||||
|
||||
----------------------------------------------------------------------------
|
||||
@ -149,9 +150,9 @@ newline :: R ()
|
||||
newline = do
|
||||
n <- R (gets scNewline)
|
||||
R . modify $ \sc -> sc
|
||||
{ scNewline = newlineRaw
|
||||
{ scNewline = Nothing
|
||||
}
|
||||
n
|
||||
fromMaybe newlineRaw n
|
||||
|
||||
-- | Low-level newline primitive. This one always just inserts a newline, no
|
||||
-- hooks can be attached.
|
||||
@ -179,9 +180,14 @@ modNewline :: (R () -> R ()) -> R ()
|
||||
modNewline f = R $ do
|
||||
old <- gets scNewline
|
||||
modify $ \sc -> sc
|
||||
{ scNewline = f old
|
||||
{ scNewline = Just $ f (fromMaybe newlineRaw old)
|
||||
}
|
||||
|
||||
-- | Check if newline is in modified state.
|
||||
|
||||
isNewlineModified :: R Bool
|
||||
isNewlineModified = isJust <$> R (gets scNewline)
|
||||
|
||||
-- | Ensure that indentation level is satisfied. Insert correct number of
|
||||
-- spaces if it isn't.
|
||||
|
||||
|
@ -472,21 +472,20 @@ p_hsExpr = \case
|
||||
located (hswc_body a) p_hsType
|
||||
OpApp NoExt x op y -> do
|
||||
located x p_hsExpr
|
||||
space
|
||||
let opWrapper = case unLoc op of
|
||||
EWildPat NoExt -> backticks
|
||||
_ -> id
|
||||
-- NOTE Sometimes operator may be displaced from the line by comments,
|
||||
-- it still should be more indented to remain valid code.
|
||||
inci $ located op (opWrapper . p_hsExpr)
|
||||
-- NOTE If the end of the first argument and the beginning of the second
|
||||
-- argument are on the same line, and the second argument has a hanging
|
||||
-- form, use hanging placement.
|
||||
let placement =
|
||||
-- NOTE If end of operator and start of second argument are on
|
||||
-- different lines, always use normal placement.
|
||||
if isOneLineSpan
|
||||
(mkSrcSpan (srcSpanEnd (getLoc op)) (srcSpanStart (getLoc y)))
|
||||
(mkSrcSpan (srcSpanEnd (getLoc x)) (srcSpanStart (getLoc y)))
|
||||
then exprPlacement (unLoc y)
|
||||
else Normal
|
||||
placeHanging placement $
|
||||
opWrapper = case unLoc op of
|
||||
EWildPat NoExt -> backticks
|
||||
_ -> id
|
||||
placeHanging placement $ do
|
||||
located op (opWrapper . p_hsExpr)
|
||||
space
|
||||
located y p_hsExpr
|
||||
NegApp NoExt e _ -> do
|
||||
txt "- "
|
||||
@ -931,9 +930,13 @@ exprPlacement = \case
|
||||
HsDo NoExt DoExpr _ -> Hanging
|
||||
HsDo NoExt MDoExpr _ -> Hanging
|
||||
RecordCon NoExt _ _ -> Hanging
|
||||
-- If the rightmost expression in an operator chain is hanging, make the
|
||||
-- whole block hanging; so that we can use the common @f = foo $ do@
|
||||
-- style.
|
||||
OpApp NoExt _ _ y -> exprPlacement (unLoc y)
|
||||
HsProc NoExt (L s _) _ ->
|
||||
-- Indentation breaks if pattern is longer than one line and left hanging.
|
||||
-- Consequently, only apply hanging when it is safe.
|
||||
-- Indentation breaks if pattern is longer than one line and left
|
||||
-- hanging. Consequently, only apply hanging when it is safe.
|
||||
if isOneLineSpan s
|
||||
then Hanging
|
||||
else Normal
|
||||
|
Loading…
Reference in New Issue
Block a user