Improve layouting of RecordUpd, Fix minor issue for HsLet

pull/123/merge
Lennart Spitzner 2018-02-19 21:33:43 +01:00
parent c124336738
commit 19e31fdaf2
2 changed files with 42 additions and 18 deletions

View File

@ -367,9 +367,8 @@ runBrittany tabSize text = do
let let
config' = staticDefaultConfig config' = staticDefaultConfig
config = config' config = config'
{ _conf_layout = (_conf_layout config') { _lconfig_indentAmount = coerce { _conf_layout =
tabSize (_conf_layout config') { _lconfig_indentAmount = coerce tabSize }
}
, _conf_forward = forwardOptionsSyntaxExtsEnabled , _conf_forward = forwardOptionsSyntaxExtsEnabled
} }
parsePrintModule config text parsePrintModule config text

View File

@ -535,13 +535,15 @@ layoutExpr lexpr@(L _ expr) = do
ifIndentLeftElse :: a -> a -> a ifIndentLeftElse :: a -> a -> a
ifIndentLeftElse x y = ifIndentLeftElse x y =
if indentPolicy == IndentPolicyLeft then x else y if indentPolicy == IndentPolicyLeft then x else y
-- this `docSetIndentLevel` might seem out of place, but is here due to -- this `docSetBaseAndIndent` might seem out of place (especially the
-- ghc-exactprint's DP handling of "let" in particular. -- Indent part; setBase is necessary due to the use of docLines below),
-- but is here due to 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
-- to making brittany idempotent, even though the result is non-optimal -- to making brittany idempotent, even though the result is non-optimal
-- 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 docSetBaseAndIndent $ case mBindDocs of
Just [bindDoc] -> docAlt Just [bindDoc] -> docAlt
[ docSeq [ docSeq
[ appSep $ docLit $ Text.pack "let" [ appSep $ docLit $ Text.pack "let"
@ -733,6 +735,8 @@ layoutExpr lexpr@(L _ expr) = do
, docLit $ Text.pack "}" , docLit $ Text.pack "}"
] ]
RecordCon lname _ _ (HsRecFields fs@(_:_) Nothing) -> do RecordCon lname _ _ (HsRecFields fs@(_:_) Nothing) -> do
-- TODO: the layouter for RecordUpd is slightly more clever. Should
-- probably copy the approach from there.
let nameDoc = docWrapNode lname $ docLit $ lrdrNameToText lname let nameDoc = docWrapNode lname $ docLit $ lrdrNameToText lname
((fd1l, fd1n, fd1e):fdr) <- fs `forM` \fieldl@(L _ (HsRecField (L _ (FieldOcc lnameF _)) fExpr pun)) -> do ((fd1l, fd1n, fd1e):fdr) <- fs `forM` \fieldl@(L _ (HsRecField (L _ (FieldOcc lnameF _)) fExpr pun)) -> do
fExpDoc <- if pun fExpDoc <- if pun
@ -852,7 +856,7 @@ layoutExpr lexpr@(L _ expr) = do
Unambiguous n _ -> (lfield, lrdrNameToText n, rFExpDoc) Unambiguous n _ -> (lfield, lrdrNameToText n, rFExpDoc)
Ambiguous n _ -> (lfield, lrdrNameToText n, rFExpDoc) Ambiguous n _ -> (lfield, lrdrNameToText n, rFExpDoc)
docAltFilter docAltFilter
-- singleline -- container { fieldA = blub, fieldB = blub }
[ ( True [ ( True
, docSeq , docSeq
[ docNodeAnnKW lexpr Nothing $ appSep $ docForceSingleline rExprDoc [ docNodeAnnKW lexpr Nothing $ appSep $ docForceSingleline rExprDoc
@ -870,7 +874,10 @@ layoutExpr lexpr@(L _ expr) = do
, docLit $ Text.pack "}" , docLit $ Text.pack "}"
] ]
) )
-- wild-indentation block -- hanging single-line fields
-- container { fieldA = blub
-- , fieldB = blub
-- }
, ( indentPolicy /= IndentPolicyLeft , ( indentPolicy /= IndentPolicyLeft
, docSeq , docSeq
[ docNodeAnnKW lexpr Nothing $ appSep rExprDoc [ docNodeAnnKW lexpr Nothing $ appSep rExprDoc
@ -881,7 +888,7 @@ layoutExpr lexpr@(L _ expr) = do
, case rF1e of , case rF1e of
Just x -> docWrapNodeRest rF1f $ docSeq Just x -> docWrapNodeRest rF1f $ docSeq
[ appSep $ docLit $ Text.pack "=" [ appSep $ docLit $ Text.pack "="
, docForceSingleline $ x , docForceSingleline x
] ]
Nothing -> docEmpty Nothing -> docEmpty
] ]
@ -901,28 +908,45 @@ layoutExpr lexpr@(L _ expr) = do
in [line1] ++ lineR ++ [lineN] in [line1] ++ lineR ++ [lineN]
] ]
) )
-- strict indentation block -- non-hanging with expressions placed to the right of the names
-- container
-- { fieldA = blub
-- , fieldB = potentially
-- multiline
-- }
, ( True , ( True
, docSetParSpacing , docSetParSpacing
$ docAddBaseY BrIndentRegular $ docAddBaseY BrIndentRegular
$ docPar $ docPar
(docNodeAnnKW lexpr Nothing $ rExprDoc) (docNodeAnnKW lexpr Nothing $ rExprDoc)
(docNonBottomSpacing $ docLines $ let (docNonBottomSpacing $ docLines $ let
expressionWrapper = if indentPolicy == IndentPolicyLeft
then docForceParSpacing
else docSetBaseY
line1 = docCols ColRecUpdate line1 = docCols ColRecUpdate
[ appSep $ docLit $ Text.pack "{" [ appSep $ docLit $ Text.pack "{"
, docWrapNodePrior rF1f $ appSep $ docLit $ rF1n , docWrapNodePrior rF1f $ appSep $ docLit $ rF1n
, docWrapNodeRest rF1f $ case rF1e of , docWrapNodeRest rF1f $ case rF1e of
Just x -> docSeq [ appSep $ docLit $ Text.pack "=" Just x -> docAlt
, docAddBaseY BrIndentRegular $ x [ docSeq [ appSep $ docLit $ Text.pack "="
, expressionWrapper x
]
, docAddBaseY BrIndentRegular
$ docPar (docLit $ Text.pack "=") x
] ]
Nothing -> docEmpty Nothing -> docEmpty
] ]
lineR = rFr <&> \(lfield, fText, fDoc) -> docWrapNode lfield $ docCols ColRecUpdate lineR = rFr <&> \(lfield, fText, fDoc) -> docWrapNode lfield
$ docCols ColRecUpdate
[ docCommaSep [ docCommaSep
, appSep $ docLit $ fText , appSep $ docLit $ fText
, case fDoc of , case fDoc of
Just x -> docSeq [ appSep $ docLit $ Text.pack "=" Just x -> docAlt
, docAddBaseY BrIndentRegular x [ docSeq [ appSep $ docLit $ Text.pack "="
, expressionWrapper x
]
, docAddBaseY BrIndentRegular
$ docPar (docLit $ Text.pack "=") x
] ]
Nothing -> docEmpty Nothing -> docEmpty
] ]
@ -930,7 +954,8 @@ layoutExpr lexpr@(L _ expr) = do
[ docNodeAnnKW lexpr (Just AnnOpenC) docEmpty [ docNodeAnnKW lexpr (Just AnnOpenC) docEmpty
, docLit $ Text.pack "}" , docLit $ Text.pack "}"
] ]
in [line1] ++ lineR ++ [lineN]) in [line1] ++ lineR ++ [lineN]
)
) )
] ]
#if MIN_VERSION_ghc(8,2,0) /* ghc-8.2 */ #if MIN_VERSION_ghc(8,2,0) /* ghc-8.2 */