on master help sign in
Be more conservative when inserting braces
e5b99e8Utku Demir
data/examples/declaration/value/function/do/let-out.hs+1−1
@@ -9,7 +9,7 @@ main = do
9-- single line let-where
9-- single line let-where
10samples n f = do
10samples n f = do
11 gen <- newQCGen
11 gen <- newQCGen
12− let rands g = g1 : rands g2 where { (g1, g2) = split g }
12+ let rands g = g1 : rands g2 where (g1, g2) = split g
13 return $ rands gen
13 return $ rands gen
14
14
15trickyLet = do
15trickyLet = do
data/examples/declaration/value/function/let-nested-out.hsnew file+3
@@ -0,0 +1,3 @@
1+_ = _
2+ where
3+ _ = [_ | let _ = _]
data/examples/declaration/value/function/let-nested.hsnew file+3
@@ -0,0 +1,3 @@
1+_ = _
2+ where
3+ _ = [_ | let _ = _]
data/examples/declaration/value/function/let-single-line-out.hs+1−1
@@ -1,6 +1,6 @@
1foo :: a -> a
1foo :: a -> a
2foo x = let x = x in x
2foo x = let x = x in x
3−foo x = let x = z where { z = 2 } in x
3+foo x = let x = z where z = 2 in x
4foo x = let x = z where { z = 2 }; a = 3 in x
4foo x = let x = z where { z = 2 }; a = 3 in x
5foo x = let g :: Int -> Int; g = id in ()
5foo x = let g :: Int -> Int; g = id in ()
6
6
src/Ormolu/Printer/Meat/Declaration/Value.hs+13−3
@@ -412,7 +412,17 @@ p_hsLocalBinds = \case
412 (Left <$> bagToList bag) ++ (Right <$> lsigs)
412 (Left <$> bagToList bag) ++ (Right <$> lsigs)
413 p_item (Left x) = located x p_valDecl
413 p_item (Left x) = located x p_valDecl
414 p_item (Right x) = located x p_sigDecl
414 p_item (Right x) = located x p_sigDecl
415+ -- Assigns 'False' to the last element, 'True' to the rest.
416+ markInit :: [a] -> [(Bool, a)]
417+ markInit [] = []
418+ markInit (x:[]) = [(False, x)]
419+ markInit (x:xs) = (True, x) : markInit xs
420+ -- NOTE When in a single-line layout, there is a chance that the inner
421+ -- elements will also contain semicolons and they will confuse the
422+ -- parser. so we request braces around every element except the last.
423+ sitcc $ sepSemi
424+ (\(m, i) -> (if m then useBraces else id) $ p_item i)
415− sitcc $ sepSemi (useBraces . p_item) (sortOn ssStart items)
425+ (markInit $ sortOn ssStart items)
416 HsValBinds NoExt _ -> notImplemented "HsValBinds"
426 HsValBinds NoExt _ -> notImplemented "HsValBinds"
417 HsIPBinds NoExt (IPBinds NoExt xs) ->
427 HsIPBinds NoExt (IPBinds NoExt xs) ->
418 -- Second argument of IPBind is always Left before type-checking.
428 -- Second argument of IPBind is always Left before type-checking.
@@ -420,7 +430,7 @@ p_hsLocalBinds = \case
420 atom name
430 atom name
421 txt " ="
431 txt " ="
422 breakpoint
432 breakpoint
423− dontUseBraces $ inci $ located expr p_hsExpr
433+ useBraces $ inci $ located expr p_hsExpr
424 p_ipBind _ = notImplemented "XHsIPBinds"
434 p_ipBind _ = notImplemented "XHsIPBinds"
425 in sepSemi (located' p_ipBind) xs
435 in sepSemi (located' p_ipBind) xs
426 HsIPBinds NoExt _ -> notImplemented "HsIpBinds"
436 HsIPBinds NoExt _ -> notImplemented "HsIpBinds"
@@ -553,7 +563,7 @@ p_hsExpr = \case
553 inci . sitcc $ sep newline (located' (p_grhs RightArrow)) guards
563 inci . sitcc $ sep newline (located' (p_grhs RightArrow)) guards
554 HsLet NoExt localBinds e -> do
564 HsLet NoExt localBinds e -> do
555 txt "let "
565 txt "let "
556− sitcc (located localBinds p_hsLocalBinds)
566+ dontUseBraces $ sitcc (located localBinds p_hsLocalBinds)
557 vlayout space (newline >> space)
567 vlayout space (newline >> space)
558 txt "in "
568 txt "in "
559 sitcc (located e p_hsExpr)
569 sitcc (located e p_hsExpr)