Compare commits

...

3 Commits

9 changed files with 120 additions and 94 deletions

View File

@ -21,3 +21,10 @@ foo =
, 4 , 4
, 5 , 5
] ]
#test set-base-y for multiple line elements
foo =
[ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
, bbbbbbbbbbbbbbbbbb
$ cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
]

View File

@ -241,3 +241,9 @@ func = do
**~ cccccccccccccccccccccccccccccccccccccccccccccccccc **~ cccccccccccccccccccccccccccccccccccccccccccccccccc
) )
== 13 == 13
#test allow lambdacase parspacing after operator
foo = do
abc `forM_` \case
True -> 1
False -> 0

View File

@ -215,13 +215,13 @@ showPackageDetailedInfo pkginfo =
(altText null "[ Not available from server ]") (altText null "[ Not available from server ]")
(dispTopVersions 9 (preferredVersions pkginfo)) (dispTopVersions 9 (preferredVersions pkginfo))
, entry , entry
"Versions installed" "Versions installed"
installedVersions installedVersions
(altText (altText
null null
(if hasLib pkginfo then "[ Not installed ]" else "[ Unknown ]") (if hasLib pkginfo then "[ Not installed ]" else "[ Unknown ]")
) )
(dispTopVersions 4 (preferredVersions pkginfo)) (dispTopVersions 4 (preferredVersions pkginfo))
, entry "Homepage" homepage orNotSpecified text , entry "Homepage" homepage orNotSpecified text
, entry "Bug reports" bugReports orNotSpecified text , entry "Bug reports" bugReports orNotSpecified text
, entry "Description" description hideIfNull reflowParagraphs , entry "Description" description hideIfNull reflowParagraphs
@ -236,10 +236,10 @@ showPackageDetailedInfo pkginfo =
, entry "Documentation" haddockHtml showIfInstalled text , entry "Documentation" haddockHtml showIfInstalled text
, entry "Cached" haveTarball alwaysShow dispYesNo , entry "Cached" haveTarball alwaysShow dispYesNo
, if not (hasLib pkginfo) , if not (hasLib pkginfo)
then empty then empty
else else
text "Modules:" text "Modules:"
$+$ nest 4 (vcat (map disp . sort . modules $ pkginfo)) $+$ nest 4 (vcat (map disp . sort . modules $ pkginfo))
] ]
#test issue 7a #test issue 7a
@ -413,10 +413,10 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs = do
(ColBindingLine alignmentToken) (ColBindingLine alignmentToken)
[ docSeq (patPartInline ++ [guardPart]) [ docSeq (patPartInline ++ [guardPart])
, docSeq , docSeq
[ appSep $ return binderDoc [ appSep $ return binderDoc
, docForceSingleline $ return body , docForceSingleline $ return body
, wherePart , wherePart
] ]
] ]
| not hasComments | not hasComments
, [(guards, body, _bodyRaw)] <- [clauseDocs] , [(guards, body, _bodyRaw)] <- [clauseDocs]
@ -436,7 +436,7 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs = do
(ColBindingLine alignmentToken) (ColBindingLine alignmentToken)
[ docSeq (patPartInline ++ [guardPart]) [ docSeq (patPartInline ++ [guardPart])
, docSeq , docSeq
[appSep $ return binderDoc, docForceParSpacing $ return body] [appSep $ return binderDoc, docForceParSpacing $ return body]
] ]
] ]
++ wherePartMultiLine ++ wherePartMultiLine
@ -447,10 +447,10 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs = do
++ -- two-line solution + where in next line(s) ++ -- two-line solution + where in next line(s)
[ docLines [ docLines
$ [ docForceSingleline $ docSeq $ [ docForceSingleline $ docSeq
(patPartInline ++ [guardPart, return binderDoc]) (patPartInline ++ [guardPart, return binderDoc])
, docEnsureIndent BrIndentRegular , docEnsureIndent BrIndentRegular
$ docForceSingleline $ docForceSingleline
$ return body $ return body
] ]
++ wherePartMultiLine ++ wherePartMultiLine
| [(guards, body, _bodyRaw)] <- [clauseDocs] | [(guards, body, _bodyRaw)] <- [clauseDocs]
@ -1084,9 +1084,20 @@ func = do
-- but that's a type error. -- but that's a type error.
let jebnaZiegui = UtatUcaOrgmqf let jebnaZiegui = UtatUcaOrgmqf
$ eqazak $ eqazak
(NO.kaeyuo . NO.FUGOKovsxq) (NO.kaeyuo . NO.FUGOKovsxq)
(maybe (NO.kaeyuo NO.UvmsoItqOguTOLqtuld) (maybe (NO.kaeyuo NO.UvmsoItqOguTOLqtuld)
(NO.kaeyuo . NO.XvswJUBeroci) (NO.kaeyuo . NO.XvswJUBeroci)
) )
nogcVassuVvbFiew nogcVassuVvbFiew
kyxson = vzahxEooRecOriqdp apneZejuzTfuQkuJosqoa kyxson = vzahxEooRecOriqdp apneZejuzTfuQkuJosqoa
#test base level for operands
func = do
pure
$!! otherFunc
(SomeLargeRecord { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1
, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 2
, cccccccccccccccccccccccccccccccccc = 3
}
)
anotherArgument

View File

@ -1149,18 +1149,18 @@ showPackageDetailedInfo pkginfo =
$+$ something $+$ something
[ entry "Synopsis" synopsis hideIfNull reflowParagraphs [ entry "Synopsis" synopsis hideIfNull reflowParagraphs
, entry , entry
"Versions available" "Versions available"
sourceVersions sourceVersions
(altText null "[ Not available from server ]") (altText null "[ Not available from server ]")
(dispTopVersions 9 (preferredVersions pkginfo)) (dispTopVersions 9 (preferredVersions pkginfo))
, entry , entry
"Versions installed" "Versions installed"
installedVersions installedVersions
(altText (altText
null null
(if hasLib pkginfo then "[ Not installed ]" else "[ Unknown ]") (if hasLib pkginfo then "[ Not installed ]" else "[ Unknown ]")
) )
(dispTopVersions 4 (preferredVersions pkginfo)) (dispTopVersions 4 (preferredVersions pkginfo))
, entry "Homepage" homepage orNotSpecified text , entry "Homepage" homepage orNotSpecified text
, entry "Bug reports" bugReports orNotSpecified text , entry "Bug reports" bugReports orNotSpecified text
, entry "Description" description hideIfNull reflowParagraphs , entry "Description" description hideIfNull reflowParagraphs
@ -1175,10 +1175,10 @@ showPackageDetailedInfo pkginfo =
, entry "Documentation" haddockHtml showIfInstalled text , entry "Documentation" haddockHtml showIfInstalled text
, entry "Cached" haveTarball alwaysShow dispYesNo , entry "Cached" haveTarball alwaysShow dispYesNo
, if not (hasLib pkginfo) , if not (hasLib pkginfo)
then empty then empty
else else
text "Modules:" text "Modules:"
$+$ nest 4 (vcat (map disp . sort . modules $ pkginfo)) $+$ nest 4 (vcat (map disp . sort . modules $ pkginfo))
] ]
#test issue 7a #test issue 7a
@ -1339,10 +1339,10 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs = do
(ColBindingLine alignmentToken) (ColBindingLine alignmentToken)
[ docSeq (patPartInline ++ [guardPart]) [ docSeq (patPartInline ++ [guardPart])
, docSeq , docSeq
[ appSep $ return binderDoc [ appSep $ return binderDoc
, docForceSingleline $ return body , docForceSingleline $ return body
, wherePart , wherePart
] ]
] ]
| not hasComments | not hasComments
, [(guards, body, _bodyRaw)] <- [clauseDocs] , [(guards, body, _bodyRaw)] <- [clauseDocs]
@ -1362,7 +1362,7 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs = do
(ColBindingLine alignmentToken) (ColBindingLine alignmentToken)
[ docSeq (patPartInline ++ [guardPart]) [ docSeq (patPartInline ++ [guardPart])
, docSeq , docSeq
[appSep $ return binderDoc, docForceParSpacing $ return body] [appSep $ return binderDoc, docForceParSpacing $ return body]
] ]
] ]
++ wherePartMultiLine ++ wherePartMultiLine
@ -1373,10 +1373,10 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs = do
++ -- two-line solution + where in next line(s) ++ -- two-line solution + where in next line(s)
[ docLines [ docLines
$ [ docForceSingleline $ docSeq $ [ docForceSingleline $ docSeq
(patPartInline ++ [guardPart, return binderDoc]) (patPartInline ++ [guardPart, return binderDoc])
, docEnsureIndent BrIndentRegular , docEnsureIndent BrIndentRegular
$ docForceSingleline $ docForceSingleline
$ return body $ return body
] ]
++ wherePartMultiLine ++ wherePartMultiLine
| [(guards, body, _bodyRaw)] <- [clauseDocs] | [(guards, body, _bodyRaw)] <- [clauseDocs]

View File

@ -45,7 +45,7 @@ displayOpTree = \case
++ ")" ++ ")"
++ join [ (showOp op ++ "(" ++ displayOpTree t ++ ")") | (op, t) <- ops ] ++ join [ (showOp op ++ "(" ++ displayOpTree t ++ ")") | (op, t) <- ops ]
) )
OpLeaf (x, _) -> show x OpLeaf _ (x, _) -> show x
where where
showOp :: BriDocNumbered -> String showOp :: BriDocNumbered -> String
showOp = \case showOp = \case
@ -185,7 +185,7 @@ addAllParens topLevelParen = \case
remSuperfluousParens :: Int -> OpTree -> OpTree remSuperfluousParens :: Int -> OpTree -> OpTree
remSuperfluousParens outerFixity = \case remSuperfluousParens outerFixity = \case
x@OpLeaf{} -> x x@OpLeaf{} -> x
OpUnknown _ locO locC c@(OpLeaf doc) [] | isLit doc -> OpUnknown _ locO locC c@(OpLeaf _ doc) [] | isLit doc ->
OpUnknown NoParen locO locC c [] OpUnknown NoParen locO locC c []
OpUnknown _ locO locC c@(OpUnknown ParenWithSpace _ _ _ _) [] -> OpUnknown _ locO locC c@(OpUnknown ParenWithSpace _ _ _ _) [] ->
OpUnknown NoParen locO locC (remSuperfluousParens 11 c) [] OpUnknown NoParen locO locC (remSuperfluousParens 11 c) []

View File

@ -736,15 +736,15 @@ layoutExpr lexpr@(L _ expr) = do
, docForceSingleline eN , docForceSingleline eN
, closeDoc] , closeDoc]
addAlternative addAlternative
$ let start = docCols ColList [appSep $ openDoc, e1] $ let start = docCols ColList [appSep $ openDoc, docSetBaseY e1]
linesM = ems <&> \(p, ast, d) -> linesM = ems <&> \(p, ast, d) ->
docCols docCols
ColList ColList
[ docHandleComms p docCommaSep [ docHandleComms p docCommaSep
, docFlushCommsPost True ast $ d , docSetBaseY $ docFlushCommsPost True ast $ d
] ]
lineN = docCols ColList lineN = docCols ColList
[docHandleComms finalCommaPos $ docCommaSep, eN] [docHandleComms finalCommaPos $ docCommaSep, docSetBaseY eN]
in docSetBaseY in docSetBaseY
$ docLines $ docLines
$ [start] $ [start]

View File

@ -7,6 +7,7 @@ import GHC ( GenLocated(L) )
import GHC.Hs import GHC.Hs
import GHC.Types.Fixity ( Fixity(Fixity) ) import GHC.Types.Fixity ( Fixity(Fixity) )
import qualified GHC.Types.SrcLoc as GHC import qualified GHC.Types.SrcLoc as GHC
import qualified GHC.OldList as List
import Language.Haskell.Brittany.Internal.Components.BriDoc import Language.Haskell.Brittany.Internal.Components.BriDoc
import Language.Haskell.Brittany.Internal.Components.OpTree import Language.Haskell.Brittany.Internal.Components.OpTree
@ -17,6 +18,13 @@ import Language.Haskell.Brittany.Internal.Types
import Language.Haskell.Brittany.Internal.Utils import Language.Haskell.Brittany.Internal.Utils
allowAsOpPar :: LHsExpr GhcPs -> Bool
allowAsOpPar = \case
L _ HsLam{} -> True
L _ HsLamCase{} -> True
L _ HsDo{} -> True
_ -> False
gatherOpTreeE gatherOpTreeE
:: OpParenMode :: OpParenMode
-> Bool -> Bool
@ -39,7 +47,7 @@ gatherOpTreeE hasParen hasComms commWrap locOpen locClose opExprList = \case
locOpen locOpen
locClose locClose
( ( docHandleComms epAnn $ callLayouter layout_expr op1 ( ( docHandleComms epAnn $ callLayouter layout_expr op1
, OpLeaf inner , OpLeaf (allowAsOpPar r1) inner
) )
: opExprList : opExprList
) )
@ -72,25 +80,21 @@ gatherOpTreeE hasParen hasComms commWrap locOpen locClose opExprList = \case
-- if null opExprList -- if null opExprList
-- then pure (innerTree, innerHasComms) -- then pure (innerTree, innerHasComms)
-- else do -- else do
numberedRights <- numberedRights <- opExprList `forM` \(x, y) -> do
opExprList x' <- x
`forM` \(x, y) -> do pure (x', y)
x' <- x
pure (x', y)
pure pure
$ ( OpUnknown hasParen locOpen locClose innerTree numberedRights $ ( OpUnknown hasParen locOpen locClose innerTree numberedRights
, innerHasComms , innerHasComms
) )
final | hasParen == NoParen && null opExprList -> do final | hasParen == NoParen && null opExprList -> do
tree <- commWrap $ callLayouter layout_expr final tree <- commWrap $ callLayouter layout_expr final
pure (OpLeaf tree, hasComms) pure (OpLeaf (allowAsOpPar final) tree, hasComms)
final@(L _ inner) -> do final@(L _ inner) -> do
numberedLeft <- commWrap $ callLayouter layout_expr final numberedLeft <- commWrap $ callLayouter layout_expr final
numberedRights <- numberedRights <- opExprList `forM` \(x, y) -> do
opExprList x' <- x
`forM` \(x, y) -> do pure (x', y)
x' <- x
pure (x', y)
pure pure
$ ( OpUnknown $ ( OpUnknown
(case (hasParen, inner) of (case (hasParen, inner) of
@ -100,7 +104,7 @@ gatherOpTreeE hasParen hasComms commWrap locOpen locClose opExprList = \case
) )
locOpen locOpen
locClose locClose
(OpLeaf $ numberedLeft) (OpLeaf (allowAsOpPar final) numberedLeft)
numberedRights numberedRights
, hasComms , hasComms
) )
@ -126,17 +130,13 @@ gatherOpTreeT hasParen hasComms commWrap locOpen locClose opExprList = \case
commWrap commWrap
locOpen locOpen
locClose locClose
( (docLit $ printRdrNameWithAnns op1, OpLeaf inner) ((docLit $ printRdrNameWithAnns op1, OpLeaf False inner) : opExprList)
: opExprList
)
l1 l1
final@(L _ inner) -> do final@(L _ inner) -> do
numberedLeft <- commWrap $ callLayouter2 layout_type False final numberedLeft <- commWrap $ callLayouter2 layout_type False final
numberedRights <- numberedRights <- opExprList `forM` \(x, y) -> do
opExprList x' <- x
`forM` \(x, y) -> do pure (x', y)
x' <- x
pure (x', y)
pure pure
$ ( OpUnknown $ ( OpUnknown
(case (hasParen, inner) of (case (hasParen, inner) of
@ -146,7 +146,7 @@ gatherOpTreeT hasParen hasComms commWrap locOpen locClose opExprList = \case
) )
locOpen locOpen
locClose locClose
(OpLeaf $ numberedLeft) (OpLeaf False numberedLeft)
numberedRights numberedRights
, hasComms , hasComms
) )
@ -238,7 +238,9 @@ layoutOpTree allowSinglelinePar = \case
sharedOps <- simpleTransform docOps sharedOps <- simpleTransform docOps
let lastWrap = if getPrec fixity <= 1 let lastWrap = if getPrec fixity <= 1
then docForceParSpacing then docForceParSpacing
else docForceSingleline else case List.last docOps of
(_, OpLeaf True _) -> docForceParSpacing
_ -> docForceSingleline
coreAlternative hasParen coreAlternative hasParen
locO locO
locC locC
@ -247,7 +249,7 @@ layoutOpTree allowSinglelinePar = \case
sharedOps sharedOps
sharedOpsFlat sharedOpsFlat
lastWrap lastWrap
OpLeaf l -> pure l OpLeaf _ l -> pure l
where where
isPrec0 x = getPrec x == 0 isPrec0 x = getPrec x == 0
getPrec = \case getPrec = \case
@ -314,7 +316,7 @@ layoutOpTree allowSinglelinePar = \case
] ]
coreAlternative hasParen locO locC fixity treeL sharedOps sharedOpsFlat lastWrap coreAlternative hasParen locO locC fixity treeL sharedOps sharedOpsFlat lastWrap
= do = do
docL <- shareDoc $ layoutOpTree True treeL docL <- shareDoc $ layoutOpTree True treeL
indentPolicy <- askLayoutConf _lconfig_indentPolicy indentPolicy <- askLayoutConf _lconfig_indentPolicy
let zeroOps = null sharedOps let zeroOps = null sharedOps
spaceAfterPar = not zeroOps spaceAfterPar = not zeroOps
@ -338,14 +340,14 @@ layoutOpTree allowSinglelinePar = \case
ParenNoSpace -> wrapParenMl False innerHead innerLines ParenNoSpace -> wrapParenMl False innerHead innerLines
wrapParenMl space innerHead innerLines = docAlt wrapParenMl space innerHead innerLines = docAlt
[ docForceZeroAdd $ docSetBaseY $ docLines [ docForceZeroAdd $ docSetBaseY $ docLines
( [ docCols ( [ docCols
ColOpPrefix ColOpPrefix
[ (if spaceAfterPar || space then appSep else id) [ (if spaceAfterPar || space then appSep else id)
$ docLit $ docLit
$ Text.pack "(" $ Text.pack "("
, docHandleComms locO $ innerHead , docHandleComms locO $ innerHead
] ]
] ]
++ innerLines ++ innerLines
++ [docHandleComms locC $ docLit $ Text.pack ")"] ++ [docHandleComms locC $ docLit $ Text.pack ")"]
) )
@ -382,7 +384,7 @@ layoutOpTree allowSinglelinePar = \case
$ wrapParenIfSl hasParen $ wrapParenIfSl hasParen
$ docSetParSpacing $ docSetParSpacing
$ docSeq $ docSeq
( [docForceSingleline docL] ( [docForceSingleline docL]
++ case splitFirstLast sharedOpsFlat of ++ case splitFirstLast sharedOpsFlat of
FirstLastEmpty -> [] FirstLastEmpty -> []
FirstLastSingleton (od, ed) -> FirstLastSingleton (od, ed) ->
@ -423,7 +425,7 @@ layoutOpTree allowSinglelinePar = \case
let singlelineUnlessFree = case indentPolicy of let singlelineUnlessFree = case indentPolicy of
IndentPolicyLeft -> docForceSingleline IndentPolicyLeft -> docForceSingleline
IndentPolicyMultiple -> docForceSingleline IndentPolicyMultiple -> docForceSingleline
IndentPolicyFree -> id IndentPolicyFree -> docSetBaseY
let curIsPrec0 = case fixity of let curIsPrec0 = case fixity of
Nothing -> False Nothing -> False
Just (Fixity _ prec _) -> prec == 0 Just (Fixity _ prec _) -> prec == 0

View File

@ -75,7 +75,7 @@ splitArrowType ltype@(L _ typ) = case typ of
docPar open $ docLines $ list ++ [close] docPar open $ docLines $ list ++ [close]
] ]
arrowDoc <- docLitS "=>" arrowDoc <- docLitS "=>"
pure (OpLeaf outerHead, (arrowDoc, innerHead) : innerBody) pure (OpLeaf False outerHead, (arrowDoc, innerHead) : innerBody)
HsFunTy epAnn _ typ1 typ2 -> do HsFunTy epAnn _ typ1 typ2 -> do
(typ1Doc, (innerHead, innerBody)) <- do (typ1Doc, (innerHead, innerBody)) <- do
let let
@ -97,7 +97,7 @@ splitArrowType ltype@(L _ typ) = case typ of
typ2Tuple <- splitArrowType typ2 typ2Tuple <- splitArrowType typ2
pure (typ1Doc, typ2Tuple) pure (typ1Doc, typ2Tuple)
arrowDoc <- docLitS "->" arrowDoc <- docLitS "->"
pure $ (OpLeaf typ1Doc, (arrowDoc, innerHead) : innerBody) pure $ (OpLeaf False typ1Doc, (arrowDoc, innerHead) : innerBody)
HsParTy epAnn inner -> do HsParTy epAnn inner -> do
let AnnParen _ spanOpen spanClose = anns epAnn let AnnParen _ spanOpen spanClose = anns epAnn
(headPart, restParts) <- splitArrowType inner (headPart, restParts) <- splitArrowType inner
@ -115,7 +115,7 @@ splitArrowType ltype@(L _ typ) = case typ of
pure $ (OpUnknown NoParen Nothing Nothing innerHead innerRest, []) pure $ (OpUnknown NoParen Nothing Nothing innerHead innerRest, [])
_ -> do _ -> do
inner <- layoutType False ltype inner <- layoutType False ltype
pure (OpLeaf inner, []) pure (OpLeaf False inner, [])
splitOpType :: LHsType GhcPs -> ToBriDocM (OpTree, [(BriDocNumbered, OpTree)]) splitOpType :: LHsType GhcPs -> ToBriDocM (OpTree, [(BriDocNumbered, OpTree)])
splitOpType = \case splitOpType = \case
@ -123,10 +123,10 @@ splitOpType = \case
docL <- layoutType False l1 docL <- layoutType False l1
docOp <- docHandleComms pos $ docLit $ printRdrNameWithAnns op1 docOp <- docHandleComms pos $ docLit $ printRdrNameWithAnns op1
(innerHead, innerBody) <- splitOpType r1 (innerHead, innerBody) <- splitOpType r1
pure $ (OpLeaf docL, (docOp, innerHead) : innerBody) pure $ (OpLeaf False docL, (docOp, innerHead) : innerBody)
ltype -> do ltype -> do
inner <- layoutType False ltype inner <- layoutType False ltype
pure (OpLeaf inner, []) pure (OpLeaf False inner, [])
splitHsForallTypeFromBinders splitHsForallTypeFromBinders
@ -159,7 +159,7 @@ splitHsForallTypeFromBinders binders typ = do
) )
] ]
dotDoc <- docLitS "." dotDoc <- docLitS "."
pure (OpLeaf outerHead, (dotDoc, innerHead) : innerBody) pure (OpLeaf False outerHead, (dotDoc, innerHead) : innerBody)
layoutSplitArrowType layoutSplitArrowType
:: (OpTree, [(BriDocNumbered, OpTree)]) :: (OpTree, [(BriDocNumbered, OpTree)])

View File

@ -178,7 +178,7 @@ data OpTree
GHC.Fixity -- only Just after (successful!) lookup phase GHC.Fixity -- only Just after (successful!) lookup phase
OpTree OpTree
[(BriDocNumbered, OpTree)] [(BriDocNumbered, OpTree)]
| OpLeaf BriDocNumbered | OpLeaf Bool BriDocNumbered -- bool determines if parspacing is preferred
data SortItemsFlag = ShouldSortItems | KeepItemsUnsorted data SortItemsFlag = ShouldSortItems | KeepItemsUnsorted