Skip to content

Commit

Permalink
Remove unnecessary indent for list comprehensions in do blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonchinn178 committed Jun 3, 2024
1 parent 657e695 commit 7f6ac41
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 82 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

* Remove unnecessary indentation from list comprehensions

## Ormolu 0.7.5.0

* Switched to `ghc-lib-parser-9.10`, with the following new syntactic features/behaviors:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ quux' xs ys =
[ ( x,
y
)
| x <- xs,
y <- ys,
then group by
-- First comment
( x
+ y
)
using
-- Second comment
groupWith -- Third comment
| x <- xs,
y <- ys,
then group by
-- First comment
( x
+ y
)
using
-- Second comment
groupWith -- Third comment
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ foo' xs ys =
[ ( x,
y
)
| x <- xs,
y <- ys,
then
-- First comment
reverse -- Second comment
| x <- xs,
y <- ys,
then
-- First comment
reverse -- Second comment
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ bar' xs ys =
[ ( x,
y
)
| x <- xs,
y <- ys,
then
-- First comment
sortWith
by
( x
+ y -- Second comment
)
| x <- xs,
y <- ys,
then
-- First comment
sortWith
by
( x
+ y -- Second comment
)
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ baz' xs ys =
[ ( x,
y
)
| x <- xs,
y <- ys,
then group using
-- First comment
permutations -- Second comment
| x <- xs,
y <- ys,
then group using
-- First comment
permutations -- Second comment
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ quux' xs ys =
[ ( x,
y
)
| x <- xs,
y <- ys,
then group by
-- First comment
( x
+ y
)
using
-- Second comment
groupWith -- Third comment
| x <- xs,
y <- ys,
then group by
-- First comment
( x
+ y
)
using
-- Second comment
groupWith -- Third comment
]
39 changes: 22 additions & 17 deletions data/examples/declaration/value/function/list-comprehensions-out.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@ bar x y = [(a, b) | a <- x, even a, b <- y, a != b]

barbaz x y z w =
[ (a, b, c, d) -- Foo
| a <-
x, -- Bar
b <- y, -- Baz
any even [a, b],
c <-
z
* z ^ 2, -- Bar baz
d <-
w
+ w, -- Baz bar
all
even
[ a,
b,
c,
d
]
| a <-
x, -- Bar
b <- y, -- Baz
any even [a, b],
c <-
z
* z ^ 2, -- Bar baz
d <-
w
+ w, -- Baz bar
all
even
[ a,
b,
c,
d
]
]

a = do
d <-
[ x + 1
| x <- b
]

[ c
| c <- d
]
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ barbaz x y z w = [
]

a = do
d <-
[ x + 1
| x <- b
]

[ c
| c <- d ]
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ baz x y z w =
i,
j
)
| a <- -- Foo 1
x, -- Foo 2
b <- -- Bar 1
y, -- Bar 2
| a <- -- Foo 1
x, -- Foo 2
b <- -- Bar 1
y, -- Bar 2
a
`mod` b -- Value
== 0
| c <- -- Baz 1
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
`mod` b -- Value
== 0
| c <- -- Baz 1
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
+ b, -- Bar bar 2
-- Bar bar 3
j <- -- Bar baz 1
a + b -- Bar baz 2
+ b, -- Bar bar 2
-- Bar bar 3
j <- -- Bar baz 1
a + b -- Bar baz 2
]
14 changes: 9 additions & 5 deletions src/Ormolu/Printer/Meat/Declaration/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ p_stmt' s placer render = \case
| isOneLineSpan (mkSrcSpan (srcSpanEnd loc) (srcSpanStart l)) = placer (unLoc f)
| otherwise = Normal
switchLayout [loc, l] $
placeHanging placement (located f (render s))
placeHanging placement (located f (render N))
ApplicativeStmt {} -> notImplemented "ApplicativeStmt" -- generated by renamer
BodyStmt _ body _ _ -> located body (render s)
LetStmt _ binds -> do
Expand Down Expand Up @@ -851,6 +851,9 @@ p_hsExpr' isApp s = \case
space
located hswc_body p_hsType

-- | Print a list comprehension.
--
-- BracketStyle should be N except in a do-block, which must be S or else it's a parse error.
p_listComp :: BracketStyle -> GenLocated SrcSpanAnnL [ExprLStmt GhcPs] -> R ()
p_listComp s es = sitcc (vlayout singleLine multiLine)
where
Expand All @@ -860,17 +863,18 @@ p_listComp s es = sitcc (vlayout singleLine multiLine)
txt "]"
multiLine = do
txt "[" >> space
sitcc body
newline
inciIf (s == S) (txt "]")
(if s == S then sitcc else id) $ do
body
newline
txt "]"

body = located es p_body
p_body xs = do
let (stmts, yield) =
case xs of
[] -> error $ "list comprehension unexpectedly had no expressions"
_ -> (init xs, last xs)
located yield p_stmt
sitcc $ located yield p_stmt
breakpoint
txt "|"
space
Expand Down

0 comments on commit 7f6ac41

Please sign in to comment.