Fix/Implement proper layouting of MultiWayIf

pull/35/head
Lennart Spitzner 2017-06-28 23:35:02 +02:00
parent 6d472315e3
commit cece70380c
3 changed files with 105 additions and 33 deletions

View File

@ -391,13 +391,22 @@ func x
| otherwise = "fit without putting the guards in new lines" | otherwise = "fit without putting the guards in new lines"
#test multiple-clauses-3 #test multiple-clauses-3
func x
| very long guard, another rather long guard that refers to x = nontrivial
expression
foo
bar
alsdkjlasdjlasj
| otherwise = 0
#test multiple-clauses-4
func x func x
| very long guard, another rather long guard that refers to x | very long guard, another rather long guard that refers to x
= nontrivialexpression foo bar alsdkjlasdjlasj = nontrivialexpression foo bar alsdkjlasdjlasj
| otherwise | otherwise
= 0 = 0
#test multiple-clauses-4 #test multiple-clauses-5
func x func x
| very loooooooooooooooooooooooooooooong guard | very loooooooooooooooooooooooooooooong guard
, another rather long guard that refers to x , another rather long guard that refers to x
@ -541,6 +550,29 @@ func =
] ]
###############################################################################
###############################################################################
###############################################################################
#group expression.multiwayif
###############################################################################
###############################################################################
###############################################################################
#test simple
{-# LANGUAGE MultiWayIf #-}
func = if
| cond1 -> loooooooooooooooooooooooooooooong expr1
| cond2 -> loooooooooooooooooooooooooooooong expr2
#test simplenested
{-# LANGUAGE MultiWayIf #-}
func = do
foo
bar $ if
| cond1 -> loooooooooooooooooooooooooooooong expr1
| cond2 -> loooooooooooooooooooooooooooooong expr2
############################################################################### ###############################################################################
############################################################################### ###############################################################################
############################################################################### ###############################################################################
@ -680,7 +712,7 @@ layoutWriteNewlineBlock
=> m () => m ()
#test multiwayif proper indentation #test multiwayif proper indentation
#pending "TODO" {-# LANGUAGE MultiWayIf #-}
readMergePersConfig path shouldCreate conf = do readMergePersConfig path shouldCreate conf = do
exists <- liftIO $ System.Directory.doesFileExist path exists <- liftIO $ System.Directory.doesFileExist path
if if
@ -690,15 +722,17 @@ readMergePersConfig path shouldCreate conf = do
Left e -> do Left e -> do
liftIO liftIO
$ putStrErrLn $ putStrErrLn
$ "error reading in brittany config from " ++ path ++ ":" $ "error reading in brittany config from "
++ path
++ ":"
liftIO $ putStrErrLn e liftIO $ putStrErrLn e
mzero mzero
Right x -> return x Right x -> return x
return $ fileConf Semigroup.<> conf return $ fileConf Semigroup.<> conf
| shouldCreate -> do | shouldCreate -> do
liftIO $ ByteString.writeFile path liftIO $ ByteString.writeFile path $ Data.Yaml.encode $ cMap
$ Data.Yaml.encode (Option . Just . runIdentity)
$ cMap (Option . Just . runIdentity) staticDefaultConfig staticDefaultConfig
return $ conf return $ conf
| otherwise -> do | otherwise -> do
return conf return conf
@ -854,7 +888,7 @@ func =
+ foooooooooooooooooooooooooooooooo foooooooooooooooooooooooooooooooo + foooooooooooooooooooooooooooooooo foooooooooooooooooooooooooooooooo
foooooooooooooooooooooooooooooooo foooooooooooooooooooooooooooooooo
#test opapp-specialcasing-2 #test opapp-specialcasing-3
func = fooooooooooooooooooooooooooooooooo + foooooooooooooooooooooooooooooooo func = fooooooooooooooooooooooooooooooooo + foooooooooooooooooooooooooooooooo
[ foooooooooooooooooooooooooooooooo [ foooooooooooooooooooooooooooooooo
, foooooooooooooooooooooooooooooooo , foooooooooooooooooooooooooooooooo

View File

@ -324,7 +324,7 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha
] ]
++ -- pattern and exactly one clause in single line, body in new line. ++ -- pattern and exactly one clause in single line, body in new line.
[ docLines [ docLines
$ [ docSeq (patPartInline ++ [appSep $ guardPart, return binderDoc]) $ [ docSeq (patPartInline ++ [guardPart, return binderDoc])
, docEnsureIndent BrIndentRegular , docEnsureIndent BrIndentRegular
$ docNonBottomSpacing $ docNonBottomSpacing
$ (docAddBaseY BrIndentRegular $ return body) $ (docAddBaseY BrIndentRegular $ return body)
@ -364,9 +364,10 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha
] ]
++ -- multiple clauses, each in a separate, single line ++ -- multiple clauses, each in a separate, single line
[ docLines [ docLines
$ [ patPartParWrap $ [ docAddBaseY BrIndentRegular
$ docEnsureIndent BrIndentRegular $ patPartParWrap
$ docLines $ docLines
$ map docSetBaseY
$ clauseDocs $ clauseDocs
<&> \(guardDocs, bodyDoc, _) -> do <&> \(guardDocs, bodyDoc, _) -> do
let guardPart = singleLineGuardsDoc guardDocs let guardPart = singleLineGuardsDoc guardDocs
@ -389,9 +390,45 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha
++ -- multiple clauses, each with the guard(s) in a single line, body ++ -- multiple clauses, each with the guard(s) in a single line, body
-- as a paragraph -- as a paragraph
[ docLines [ docLines
$ [ patPartParWrap $ [ docAddBaseY BrIndentRegular
$ docEnsureIndent BrIndentRegular $ patPartParWrap
$ docLines $ docLines
$ map docSetBaseY
$ clauseDocs
<&> \(guardDocs, bodyDoc, _) ->
docSeq
$ ( case guardDocs of
[] -> []
[g] ->
[ docForceSingleline
$ docSeq [appSep $ docLit $ Text.pack "|", return g]
]
gs ->
[ docForceSingleline
$ docSeq
$ [appSep $ docLit $ Text.pack "|"]
++ List.intersperse docCommaSep (return <$> gs)
]
)
++ [ docSeparator
, docCols
ColOpPrefix
[ appSep $ return binderDoc
, docAddBaseY BrIndentRegular
$ docForceParSpacing
$ return bodyDoc
]
]
]
++ wherePartMultiLine
]
++ -- multiple clauses, each with the guard(s) in a single line, body
-- in a new line as a paragraph
[ docLines
$ [ docAddBaseY BrIndentRegular
$ patPartParWrap
$ docLines
$ map docSetBaseY
$ clauseDocs $ clauseDocs
>>= \(guardDocs, bodyDoc, _) -> >>= \(guardDocs, bodyDoc, _) ->
( case guardDocs of ( case guardDocs of
@ -410,8 +447,8 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha
++ [ docCols ++ [ docCols
ColOpPrefix ColOpPrefix
[ appSep $ return binderDoc [ appSep $ return binderDoc
, docForceParSpacing , docAddBaseY BrIndentRegular
$ docAddBaseY BrIndentRegular $ docForceParSpacing
$ return bodyDoc $ return bodyDoc
] ]
] ]
@ -420,9 +457,10 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauseDocs mWhereDocs ha
] ]
++ -- conservative approach: everything starts on the left. ++ -- conservative approach: everything starts on the left.
[ docLines [ docLines
$ [ patPartParWrap $ [ docAddBaseY BrIndentRegular
$ docEnsureIndent BrIndentRegular $ patPartParWrap
$ docLines $ docLines
$ map docSetBaseY
$ clauseDocs $ clauseDocs
>>= \(guardDocs, bodyDoc, _) -> >>= \(guardDocs, bodyDoc, _) ->
( case guardDocs of ( case guardDocs of

View File

@ -475,7 +475,7 @@ layoutExpr lexpr@(L _ expr) = docWrapNode lexpr $ case expr of
clauseDocs <- cases `forM` layoutGrhs clauseDocs <- cases `forM` layoutGrhs
binderDoc <- docLit $ Text.pack "->" binderDoc <- docLit $ Text.pack "->"
hasComments <- hasAnyCommentsBelow lexpr hasComments <- hasAnyCommentsBelow lexpr
docAddBaseY BrIndentRegular $ docPar docSetParSpacing $ docAddBaseY BrIndentRegular $ docPar
(docLit $ Text.pack "if") (docLit $ Text.pack "if")
(layoutPatternBindFinal Nothing binderDoc Nothing clauseDocs Nothing hasComments) (layoutPatternBindFinal Nothing binderDoc Nothing clauseDocs Nothing hasComments)
HsLet binds exp1 -> do HsLet binds exp1 -> do