From f1b49b082fec2e4ca1c2dc4de7773546daf13763 Mon Sep 17 00:00:00 2001 From: Evan Rutledge Borden Date: Sun, 31 Dec 2017 00:04:53 -0500 Subject: [PATCH 1/3] Format let and in on a single line if they fit The following is wasteful of vertical space: ``` _ = let longIdentifierForShortValue = 1 in longIdentifierForShortValue + longIdentifierForShortValue ``` We should format it on two lines if possible. ``` _ = let longIdentifierForShortValue = 1 in longIdentifierForShortValue + longIdentifierForShortValue ``` This commit also allows for a mix of variations: ``` _ = let longIdentifierForShortValue = 1 in longIdentifierForShortValue + longIdentifierForShortValue _ = let longIdentifierForShortValue = 1 in longIdentifierForShortValue + longIdentifierForShortValue ``` --- src-literatetests/tests-context-free.blt | 5 ++ .../Brittany/Internal/Layouters/Expr.hs | 54 +++++++++---------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src-literatetests/tests-context-free.blt b/src-literatetests/tests-context-free.blt index 0918e60..e8303cd 100644 --- a/src-literatetests/tests-context-free.blt +++ b/src-literatetests/tests-context-free.blt @@ -510,6 +510,11 @@ func = (abc, def) func = (lakjsdlajsdljasdlkjasldjasldjasldjalsdjlaskjd , lakjsdlajsdljasdlkjasldjasldjasldjalsdjlaskjd) +#test let in on single line +foo = + let longIdentifierForShortValue = 1 + in longIdentifierForShortValue + longIdentifierForShortValue + ############################################################################### diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs b/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs index 1462c56..6e6929f 100644 --- a/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs +++ b/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs @@ -531,6 +531,9 @@ layoutExpr lexpr@(L _ expr) = do HsLet binds exp1 -> do expDoc1 <- docSharedWrapper layoutExpr exp1 mBindDocs <- layoutLocalBinds binds + let + whenIndentLeftOr x y = + if indentPolicy == IndentPolicyLeft then x else y -- this `docSetIndentLevel` might seem out of place, but is here due to -- ghc-exactprint's DP handling of "let" in particular. -- Just pushing another indentation level is a straightforward approach @@ -538,39 +541,36 @@ layoutExpr lexpr@(L _ expr) = do -- if "let" is moved horizontally as part of the transformation, as the -- comments before the first let item are moved horizontally with it. docSetIndentLevel $ case mBindDocs of - Just [bindDoc] -> docAltFilter - [ ( True - , docSeq + Just [bindDoc] -> docAlt + [ docSeq [ appSep $ docLit $ Text.pack "let" , appSep $ docForceSingleline $ return bindDoc , appSep $ docLit $ Text.pack "in" , docForceSingleline $ expDoc1 ] - ) - , ( indentPolicy /= IndentPolicyLeft - , docLines - [ docSeq - [ appSep $ docLit $ Text.pack "let" - , docSetBaseAndIndent $ return bindDoc - ] - , docSeq - [ appSep $ docLit $ Text.pack "in " - , docSetBaseY $ expDoc1 - ] + , docLines + [ docAlt + [ docSeq + [ appSep $ docLit $ Text.pack "let" + , whenIndentLeftOr docForceSingleline docSetBaseAndIndent + $ return bindDoc + ] + , docAddBaseY BrIndentRegular + $ docPar + (appSep $ docLit $ Text.pack "let") + (docSetBaseAndIndent $ return bindDoc) + ] + , docAlt + [ docSeq + [ whenIndentLeftOr id appSep $ docLit $ Text.pack "in " + , whenIndentLeftOr docForceSingleline docSetBaseAndIndent expDoc1 + ] + , docAddBaseY BrIndentRegular + $ docPar + (appSep $ docLit $ Text.pack "in") + (docSetBaseY $ expDoc1) + ] ] - ) - , ( True - , docLines - [ docAddBaseY BrIndentRegular - $ docPar - (appSep $ docLit $ Text.pack "let") - (docSetBaseAndIndent $ return bindDoc) - , docAddBaseY BrIndentRegular - $ docPar - (appSep $ docLit $ Text.pack "in") - (docSetBaseY $ expDoc1) - ] - ) ] Just bindDocs@(_:_) -> docAltFilter --either From cab12975851076e568c839471ce46830a3948dfc Mon Sep 17 00:00:00 2001 From: Evan Rutledge Borden Date: Sun, 31 Dec 2017 00:11:10 -0500 Subject: [PATCH 2/3] Change function name to if/else --- src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs b/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs index 6e6929f..fcf69b4 100644 --- a/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs +++ b/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs @@ -532,7 +532,7 @@ layoutExpr lexpr@(L _ expr) = do expDoc1 <- docSharedWrapper layoutExpr exp1 mBindDocs <- layoutLocalBinds binds let - whenIndentLeftOr x y = + ifIndentLeftElse x y = if indentPolicy == IndentPolicyLeft then x else y -- this `docSetIndentLevel` might seem out of place, but is here due to -- ghc-exactprint's DP handling of "let" in particular. @@ -552,7 +552,7 @@ layoutExpr lexpr@(L _ expr) = do [ docAlt [ docSeq [ appSep $ docLit $ Text.pack "let" - , whenIndentLeftOr docForceSingleline docSetBaseAndIndent + , ifIndentLeftElse docForceSingleline docSetBaseAndIndent $ return bindDoc ] , docAddBaseY BrIndentRegular @@ -562,8 +562,8 @@ layoutExpr lexpr@(L _ expr) = do ] , docAlt [ docSeq - [ whenIndentLeftOr id appSep $ docLit $ Text.pack "in " - , whenIndentLeftOr docForceSingleline docSetBaseAndIndent expDoc1 + [ ifIndentLeftElse id appSep $ docLit $ Text.pack "in " + , ifIndentLeftElse docForceSingleline docSetBaseAndIndent expDoc1 ] , docAddBaseY BrIndentRegular $ docPar From 399e2f4f43a4fad1ddf68da6f065d0d16ea08991 Mon Sep 17 00:00:00 2001 From: Lennart Spitzner Date: Sat, 13 Jan 2018 18:41:51 +0100 Subject: [PATCH 3/3] Minor cleanups --- src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs b/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs index fcf69b4..8d90148 100644 --- a/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs +++ b/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs @@ -532,6 +532,7 @@ layoutExpr lexpr@(L _ expr) = do expDoc1 <- docSharedWrapper layoutExpr exp1 mBindDocs <- layoutLocalBinds binds let + ifIndentLeftElse :: a -> a -> a ifIndentLeftElse x y = if indentPolicy == IndentPolicyLeft then x else y -- this `docSetIndentLevel` might seem out of place, but is here due to @@ -557,17 +558,17 @@ layoutExpr lexpr@(L _ expr) = do ] , docAddBaseY BrIndentRegular $ docPar - (appSep $ docLit $ Text.pack "let") + (docLit $ Text.pack "let") (docSetBaseAndIndent $ return bindDoc) ] , docAlt [ docSeq - [ ifIndentLeftElse id appSep $ docLit $ Text.pack "in " + [ appSep $ docLit $ Text.pack $ ifIndentLeftElse "in" "in " , ifIndentLeftElse docForceSingleline docSetBaseAndIndent expDoc1 ] , docAddBaseY BrIndentRegular $ docPar - (appSep $ docLit $ Text.pack "in") + (docLit $ Text.pack "in") (docSetBaseY $ expDoc1) ] ]