Refactor and Add missing docSharedWrapper

pull/83/head
Lennart Spitzner 2017-12-21 15:44:58 +01:00
parent f651d02898
commit 33f23a65ec
3 changed files with 41 additions and 40 deletions

View File

@ -71,12 +71,15 @@ layoutIE lie@(L _ ie) = docWrapNode lie $ case ie of
-- left to the caller since that is context sensitive
layoutAnnAndSepLLIEs :: (Located [LIE RdrName]) -> ToBriDocM [ToBriDocM BriDocNumbered]
layoutAnnAndSepLLIEs llies@(L _ lies) = do
let makeIENode ie = docSeq [docCommaSep, ie]
layoutAnnAndSepLLIEs' ies = case ies of
[] -> []
[ie] -> [docWrapNode llies $ ie]
(ie:ies') -> ie:map makeIENode (List.init ies')
++ [makeIENode $ docWrapNode llies $ List.last ies']
let
makeIENode ie = docSeq [docCommaSep, ie]
layoutAnnAndSepLLIEs' ies = case splitFirstLast ies of
FirstLastEmpty -> []
FirstLastSingleton ie -> [docWrapNodeRest llies $ ie]
FirstLast ie1 ieMs ieN ->
[ie1]
++ map makeIENode ieMs
++ [makeIENode $ docWrapNodeRest llies $ ieN]
layoutAnnAndSepLLIEs' <$> mapM (docSharedWrapper layoutIE) lies
-- Builds a complete layout for the given located

View File

@ -46,6 +46,9 @@ layoutImport :: ToBriDoc ImportDecl
layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
ImportDecl _ (L _ modName) pkg src safe q False as mllies -> do
importCol <- mAsk <&> _conf_layout .> _lconfig_importColumn .> confUnpack
-- NB we don't need to worry about sharing in the below code
-- (docSharedWrapper etc.) because we do not use any docAlt nodes; all
-- "decisions" are made statically.
let
modNameT = Text.pack $ moduleNameString modName
pkgNameT = Text.pack . prepPkg . sl_st <$> pkg
@ -77,36 +80,30 @@ layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
hidDoc =
if hiding then appSep $ docLit $ Text.pack "hiding" else docEmpty
importHead = docSeq [importQualifiers, modNameD]
bindingsH = docParenLSep
bindingsT = [docSeq [docSeparator, docParenR]]
bindingsD = case mllies of
Nothing -> docSeq [docEmpty]
Just (_, llies) -> do
ieDs <- layoutAnnAndSepLLIEs llies
hasComments <- hasAnyCommentsBelow llies
case ieDs of
docWrapNodeRest llies $ case ieDs of
-- ..[hiding].( )
[] -> do
if hasComments
then
docWrapNodeRest llies $ docPar (docSeq [hidDoc, bindingsH, docWrapNode llies docEmpty]) $ docLines
bindingsT
else
docWrapNodeRest llies $ docSeq $ hidDoc : bindingsH : bindingsT
[] -> if hasComments
then docPar
(docSeq [hidDoc, docParenLSep, docWrapNode llies docEmpty])
docParenR
else docSeq [hidDoc, docParenLSep, docSeparator, docParenR]
-- ..[hiding].( b )
[ieD] -> do
if hasComments
then
docWrapNodeRest llies $ docPar (docSeq [hidDoc, bindingsH, ieD ]) $ docLines $
bindingsT
else
docWrapNodeRest llies $ docSeq $ hidDoc : bindingsH : ieD : bindingsT
[ieD] -> if hasComments
then docPar (docSeq [hidDoc, docParenLSep, ieD]) docParenR
else docSeq [hidDoc, docParenLSep, ieD, docSeparator, docParenR]
-- ..[hiding].( b
-- , b'
-- )
(ieD:ieDs') -> do
docWrapNodeRest llies $ docPar (docSeq [hidDoc, docSetBaseY $ docSeq [bindingsH, ieD]])
$ docLines $ ieDs' ++ bindingsT
(ieD:ieDs') ->
docPar (docSeq [hidDoc, docSetBaseY $ docSeq [docParenLSep, ieD]])
$ docLines
$ ieDs'
++ [docParenR]
bindingLine =
docEnsureIndent (BrIndentSpecial (importCol - bindingCost)) bindingsD
case asT of

View File

@ -30,11 +30,12 @@ layoutModule lmod@(L _ mod') = do
HsModule Nothing _ imports _ _ _ -> docLines $ map layoutImport imports
HsModule (Just n) les imports _ _ _ -> do
let tn = Text.pack $ moduleNameString $ unLoc n
(hasComments, es) <- case les of
(hasComments, exportsDoc) <- case les of
Nothing -> return (False, docEmpty)
Just llies -> do
hasComments <- hasAnyCommentsBelow llies
return (hasComments, layoutLLIEs llies)
exportsDoc <- docSharedWrapper layoutLLIEs llies
return (hasComments, exportsDoc)
docLines
$ docSeq
[ docWrapNode lmod $ docEmpty
@ -44,7 +45,7 @@ layoutModule lmod@(L _ mod') = do
( [ docSeq
[ appSep $ docLit $ Text.pack "module"
, appSep $ docLit tn
, appSep $ docForceSingleline es
, appSep $ docForceSingleline exportsDoc
, docLit $ Text.pack "where"
]
| not hasComments
@ -54,7 +55,7 @@ layoutModule lmod@(L _ mod') = do
( docSeq
[appSep $ docLit $ Text.pack "module", docLit tn]
)
(docForceMultiline es)
(docForceMultiline exportsDoc)
, docLit $ Text.pack "where"
]
]