From 28a9ba136bce06b9d08e295735c32cd2f5132692 Mon Sep 17 00:00:00 2001 From: Alexander Esgen Date: Mon, 19 Jun 2023 22:19:20 +0200 Subject: [PATCH] Preserve necessary braces for final function arguments --- CHANGELOG.md | 5 +++++ data/examples/other/necessary-brackets-out.hs | 3 +++ data/examples/other/necessary-brackets.hs | 3 +++ src/Ormolu/Printer/Meat/Declaration/Value.hs | 15 ++++++++++++--- 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 data/examples/other/necessary-brackets-out.hs create mode 100644 data/examples/other/necessary-brackets.hs diff --git a/CHANGELOG.md b/CHANGELOG.md index 82202a3b..ac1e77e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## Unreleased + +* Preserve necessary braces for final function arguments. [Issue + 1044](https://github.com/tweag/ormolu/issues/1044). + ## Ormolu 0.7.1.0 * Include `base` fixity information when formatting a Haskell file that's diff --git a/data/examples/other/necessary-brackets-out.hs b/data/examples/other/necessary-brackets-out.hs new file mode 100644 index 00000000..0c5b439e --- /dev/null +++ b/data/examples/other/necessary-brackets-out.hs @@ -0,0 +1,3 @@ +insertEmDash = Text.concat . map \case { x | x == "--" -> "—"; x -> x } . Text.groupBy ((==) `on` Char.isSpace) + +foo f a b c = f do { a } + f do { b } + f do c diff --git a/data/examples/other/necessary-brackets.hs b/data/examples/other/necessary-brackets.hs new file mode 100644 index 00000000..519701f6 --- /dev/null +++ b/data/examples/other/necessary-brackets.hs @@ -0,0 +1,3 @@ +insertEmDash = Text.concat . map \case{ x | x == "--" -> "—"; x -> x } . Text.groupBy ((==) `on` Char.isSpace) + +foo f a b c = f do {a} + f do {b} + f do {c} diff --git a/src/Ormolu/Printer/Meat/Declaration/Value.hs b/src/Ormolu/Printer/Meat/Declaration/Value.hs index b377e07e..a0507b84 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Value.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Value.hs @@ -489,9 +489,18 @@ p_stmts :: p_stmts isApp placer render es = do breakpoint ub <- layoutToBraces <$> getLayout + let p_stmtExt (relPos, stmt) = + ub' $ withSpacing (p_stmt' placer render) stmt + where + -- We need to set brace usage information for all but the last + -- statement (e.g.in the case of nested do blocks). + ub' = case relPos of + FirstPos -> ub + MiddlePos -> ub + LastPos -> id + SinglePos -> id inciApplicand isApp . located es $ - sepSemi - (ub . withSpacing (p_stmt' placer render)) + sepSemi p_stmtExt . attachRelativePos gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]] gatherStmt (L _ (ParStmt _ block _ _)) = @@ -654,7 +663,7 @@ p_hsExpr' isApp s = \case located func (p_hsExpr' Applicand s) breakpoint sep breakpoint (located' p_hsExpr) initp - placeHanging placement . dontUseBraces $ + placeHanging placement $ located lastp p_hsExpr HsAppType _ e _ a -> do located e p_hsExpr