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
```
pull/97/head
Evan Rutledge Borden 2017-12-31 00:04:53 -05:00
parent 43abab2dd2
commit f1b49b082f
2 changed files with 32 additions and 27 deletions

View File

@ -510,6 +510,11 @@ func = (abc, def)
func = (lakjsdlajsdljasdlkjasldjasldjasldjalsdjlaskjd func = (lakjsdlajsdljasdlkjasldjasldjasldjalsdjlaskjd
, lakjsdlajsdljasdlkjasldjasldjasldjalsdjlaskjd) , lakjsdlajsdljasdlkjasldjasldjasldjalsdjlaskjd)
#test let in on single line
foo =
let longIdentifierForShortValue = 1
in longIdentifierForShortValue + longIdentifierForShortValue
############################################################################### ###############################################################################

View File

@ -531,6 +531,9 @@ layoutExpr lexpr@(L _ expr) = do
HsLet binds exp1 -> do HsLet binds exp1 -> do
expDoc1 <- docSharedWrapper layoutExpr exp1 expDoc1 <- docSharedWrapper layoutExpr exp1
mBindDocs <- layoutLocalBinds binds 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 -- this `docSetIndentLevel` might seem out of place, but is here due to
-- ghc-exactprint's DP handling of "let" in particular. -- ghc-exactprint's DP handling of "let" in particular.
-- Just pushing another indentation level is a straightforward approach -- 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 -- if "let" is moved horizontally as part of the transformation, as the
-- comments before the first let item are moved horizontally with it. -- comments before the first let item are moved horizontally with it.
docSetIndentLevel $ case mBindDocs of docSetIndentLevel $ case mBindDocs of
Just [bindDoc] -> docAltFilter Just [bindDoc] -> docAlt
[ ( True [ docSeq
, docSeq
[ appSep $ docLit $ Text.pack "let" [ appSep $ docLit $ Text.pack "let"
, appSep $ docForceSingleline $ return bindDoc , appSep $ docForceSingleline $ return bindDoc
, appSep $ docLit $ Text.pack "in" , appSep $ docLit $ Text.pack "in"
, docForceSingleline $ expDoc1 , docForceSingleline $ expDoc1
] ]
)
, ( indentPolicy /= IndentPolicyLeft
, docLines , docLines
[ docAlt
[ docSeq [ docSeq
[ appSep $ docLit $ Text.pack "let" [ appSep $ docLit $ Text.pack "let"
, docSetBaseAndIndent $ return bindDoc , whenIndentLeftOr docForceSingleline docSetBaseAndIndent
$ return bindDoc
] ]
, docSeq , docAddBaseY BrIndentRegular
[ appSep $ docLit $ Text.pack "in "
, docSetBaseY $ expDoc1
]
]
)
, ( True
, docLines
[ docAddBaseY BrIndentRegular
$ docPar $ docPar
(appSep $ docLit $ Text.pack "let") (appSep $ docLit $ Text.pack "let")
(docSetBaseAndIndent $ return bindDoc) (docSetBaseAndIndent $ return bindDoc)
]
, docAlt
[ docSeq
[ whenIndentLeftOr id appSep $ docLit $ Text.pack "in "
, whenIndentLeftOr docForceSingleline docSetBaseAndIndent expDoc1
]
, docAddBaseY BrIndentRegular , docAddBaseY BrIndentRegular
$ docPar $ docPar
(appSep $ docLit $ Text.pack "in") (appSep $ docLit $ Text.pack "in")
(docSetBaseY $ expDoc1) (docSetBaseY $ expDoc1)
] ]
) ]
] ]
Just bindDocs@(_:_) -> docAltFilter Just bindDocs@(_:_) -> docAltFilter
--either --either