on master help sign in
Be more conservative when inserting braces
commite5b99e8e66a983c3aefa8d8a2acf0771f6157938 authorUtku Demir <me@utdemir.com> dateThu 22 Aug 2019, 16:57 (7 years ago) parentaf907ec
5 files changed+21−5
data/examples/declaration/value/function/do/let-out.hs+1−1 data/examples/declaration/value/function/let-nested-out.hs+3 data/examples/declaration/value/function/let-nested.hs+3 data/examples/declaration/value/function/let-single-line-out.hs+1−1 src/Ormolu/Printer/Meat/Declaration/Value.hs+13−3
data/examples/declaration/value/function/do/let-out.hs+1−1
@@ -9,7 +9,7 @@ main = do
99-- single line let-where
1010samples n f = do
1111 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
1313 return $ rands gen
1414
1515trickyLet = 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 @@
11foo :: a -> a
22foo 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
44foo x = let x = z where { z = 2 }; a = 3 in x
55foo x = let g :: Int -> Int; g = id in ()
66
src/Ormolu/Printer/Meat/Declaration/Value.hs+13−3
@@ -412,7 +412,17 @@ p_hsLocalBinds = \case
412412 (Left <$> bagToList bag) ++ (Right <$> lsigs)
413413 p_item (Left x) = located x p_valDecl
414414 p_item (Right x) = located x p_sigDecl
415− sitcc $ sepSemi (useBraces . p_item) (sortOn ssStart items)
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)
425+ (markInit $ sortOn ssStart items)
416426 HsValBinds NoExt _ -> notImplemented "HsValBinds"
417427 HsIPBinds NoExt (IPBinds NoExt xs) ->
418428 -- Second argument of IPBind is always Left before type-checking.
@@ -420,7 +430,7 @@ p_hsLocalBinds = \case
420430 atom name
421431 txt " ="
422432 breakpoint
423− dontUseBraces $ inci $ located expr p_hsExpr
433+ useBraces $ inci $ located expr p_hsExpr
424434 p_ipBind _ = notImplemented "XHsIPBinds"
425435 in sepSemi (located' p_ipBind) xs
426436 HsIPBinds NoExt _ -> notImplemented "HsIpBinds"
@@ -553,7 +563,7 @@ p_hsExpr = \case
553563 inci . sitcc $ sep newline (located' (p_grhs RightArrow)) guards
554564 HsLet NoExt localBinds e -> do
555565 txt "let "
556− sitcc (located localBinds p_hsLocalBinds)
566+ dontUseBraces $ sitcc (located localBinds p_hsLocalBinds)
557567 vlayout space (newline >> space)
558568 txt "in "
559569 sitcc (located e p_hsExpr)