Apply brittany to `layoutBriDocM`

pull/35/head
Lennart Spitzner 2017-05-17 21:38:50 +02:00
parent 6448ee780a
commit 56e53a9cbb
1 changed files with 257 additions and 228 deletions

View File

@ -171,8 +171,7 @@ layoutBriDocM = \case
indentF $ do indentF $ do
layoutWriteNewlineBlock layoutWriteNewlineBlock
layoutBriDocM indented layoutBriDocM indented
BDLines lines -> BDLines lines -> alignColsLines lines
alignColsLines lines
BDAlt [] -> error "empty BDAlt" BDAlt [] -> error "empty BDAlt"
BDAlt (alt:_) -> layoutBriDocM alt BDAlt (alt:_) -> layoutBriDocM alt
BDForceMultiline bd -> layoutBriDocM bd BDForceMultiline bd -> layoutBriDocM bd
@ -183,7 +182,11 @@ layoutBriDocM = \case
tlineCount = length tlines tlineCount = length tlines
anns :: ExactPrint.Anns <- mAsk anns :: ExactPrint.Anns <- mAsk
when shouldAddComment $ do when shouldAddComment $ do
layoutWriteAppend $ Text.pack $ "{-" ++ show (annKey, Map.lookup annKey anns) ++ "-}" layoutWriteAppend
$ Text.pack
$ "{-"
++ show (annKey, Map.lookup annKey anns)
++ "-}"
zip [1 ..] tlines `forM_` \(i, l) -> do zip [1 ..] tlines `forM_` \(i, l) -> do
layoutWriteAppend $ l layoutWriteAppend $ l
unless (i == tlineCount) layoutWriteNewlineBlock unless (i == tlineCount) layoutWriteNewlineBlock
@ -191,8 +194,7 @@ layoutBriDocM = \case
state <- mGet state <- mGet
let filterF k _ = not $ k `Set.member` subKeys let filterF k _ = not $ k `Set.member` subKeys
mSet $ state mSet $ state
{ _lstate_comments = Map.filterWithKey filterF { _lstate_comments = Map.filterWithKey filterF $ _lstate_comments state
$ _lstate_comments state
} }
BDAnnotationPrior annKey bd -> do BDAnnotationPrior annKey bd -> do
state <- mGet state <- mGet
@ -202,8 +204,10 @@ layoutBriDocM = \case
mAnn <- do mAnn <- do
let mAnn = ExactPrint.annPriorComments <$> Map.lookup annKey m let mAnn = ExactPrint.annPriorComments <$> Map.lookup annKey m
mSet $ state mSet $ state
{ _lstate_comments = { _lstate_comments = Map.adjust
Map.adjust (\ann -> ann { ExactPrint.annPriorComments = [] }) annKey m (\ann -> ann { ExactPrint.annPriorComments = [] })
annKey
m
} }
return mAnn return mAnn
case mAnn of case mAnn of
@ -211,9 +215,9 @@ layoutBriDocM = \case
Just [] -> when allowMTEL $ moveToExactAnn annKey Just [] -> when allowMTEL $ moveToExactAnn annKey
Just priors -> do Just priors -> do
-- layoutResetSepSpace -- layoutResetSepSpace
priors `forM_` \( ExactPrint.Types.Comment comment _ _ priors
, ExactPrint.Types.DP (y, x) `forM_` \(ExactPrint.Types.Comment comment _ _, ExactPrint.Types.DP (y, x)) ->
) -> do do
-- evil hack for CPP: -- evil hack for CPP:
case comment of case comment of
('#':_) -> layoutMoveToCommentPos y (-999) ('#':_) -> layoutMoveToCommentPos y (-999)
@ -233,8 +237,8 @@ layoutBriDocM = \case
let mAnn = ExactPrint.annsDP <$> Map.lookup annKey m let mAnn = ExactPrint.annsDP <$> Map.lookup annKey m
let mToSpan = case mAnn of let mToSpan = case mAnn of
Just anns | keyword == Nothing -> Just anns Just anns | keyword == Nothing -> Just anns
Just ((ExactPrint.Types.G kw1, _):annR) Just ((ExactPrint.Types.G kw1, _):annR) | keyword == Just kw1 -> Just
| keyword==Just kw1 -> Just annR annR
_ -> Nothing _ -> Nothing
case mToSpan of case mToSpan of
Just anns -> do Just anns -> do
@ -242,16 +246,17 @@ layoutBriDocM = \case
(ExactPrint.Types.AnnComment x, dp) -> Just (x, dp) (ExactPrint.Types.AnnComment x, dp) -> Just (x, dp)
_ -> Nothing _ -> Nothing
mSet $ state mSet $ state
{ _lstate_comments = { _lstate_comments = Map.adjust
Map.adjust (\ann -> ann { ExactPrint.annsDP = rest }) (\ann -> ann { ExactPrint.annsDP = rest })
annKey annKey
m m
} }
return $ [ comments | not $ null comments ] return $ [ comments | not $ null comments ]
_ -> return Nothing _ -> return Nothing
forM_ mAnn $ mapM_ $ \( ExactPrint.Types.Comment comment _ _ forM_ mAnn
, ExactPrint.Types.DP (y, x) $ mapM_
) -> do $ \(ExactPrint.Types.Comment comment _ _, ExactPrint.Types.DP (y, x)) ->
do
-- evil hack for CPP: -- evil hack for CPP:
case comment of case comment of
('#':_) -> layoutMoveToCommentPos y (-999) ('#':_) -> layoutMoveToCommentPos y (-999)
@ -268,8 +273,8 @@ layoutBriDocM = \case
let m = _lstate_comments state let m = _lstate_comments state
let mAnn = extractAllComments <$> Map.lookup annKey m let mAnn = extractAllComments <$> Map.lookup annKey m
mSet $ state mSet $ state
{ _lstate_comments = { _lstate_comments = Map.adjust
Map.adjust (\ann -> ann { ExactPrint.annFollowingComments = [] ( \ann -> ann { ExactPrint.annFollowingComments = []
, ExactPrint.annPriorComments = [] , ExactPrint.annPriorComments = []
, ExactPrint.annsDP = [] , ExactPrint.annsDP = []
} }
@ -278,9 +283,10 @@ layoutBriDocM = \case
m m
} }
return mAnn return mAnn
forM_ mAnn $ mapM_ $ \( ExactPrint.Types.Comment comment _ _ forM_ mAnn
, ExactPrint.Types.DP (y, x) $ mapM_
) -> do $ \(ExactPrint.Types.Comment comment _ _, ExactPrint.Types.DP (y, x)) ->
do
-- evil hack for CPP: -- evil hack for CPP:
case comment of case comment of
('#':_) -> layoutMoveToCommentPos y (-999) ('#':_) -> layoutMoveToCommentPos y (-999)
@ -324,18 +330,22 @@ layoutBriDocM = \case
alignColsLines bridocs = do -- colInfos `forM_` \colInfo -> do alignColsLines bridocs = do -- colInfos `forM_` \colInfo -> do
curX <- do curX <- do
state <- mGet state <- mGet
return $ either id (const 0) (_lstate_curYOrAddNewline state) return $ either id (const 0) (_lstate_curYOrAddNewline state) + fromMaybe
+ fromMaybe 0 (_lstate_addSepSpace state) 0
(_lstate_addSepSpace state)
colMax <- mAsk <&> _conf_layout .> _lconfig_cols .> confUnpack colMax <- mAsk <&> _conf_layout .> _lconfig_cols .> confUnpack
alignMax <- mAsk <&> _conf_layout .> _lconfig_alignmentLimit .> confUnpack alignMax <- mAsk <&> _conf_layout .> _lconfig_alignmentLimit .> confUnpack
alignBreak <- mAsk <&> _conf_layout .> _lconfig_alignmentBreakOnMultiline .> confUnpack alignBreak <-
mAsk <&> _conf_layout .> _lconfig_alignmentBreakOnMultiline .> confUnpack
case () of case () of
_ -> _ ->
sequence_ $ List.intersperse layoutWriteEnsureNewlineBlock sequence_
$ colInfos <&> processInfo processedMap $ List.intersperse layoutWriteEnsureNewlineBlock
$ colInfos
<&> processInfo processedMap
where where
(colInfos, finalState) = StateS.runState (mergeBriDocs bridocs) (colInfos, finalState) =
(ColBuildState IntMapS.empty 0) StateS.runState (mergeBriDocs bridocs) (ColBuildState IntMapS.empty 0)
-- maxZipper :: [Int] -> [Int] -> [Int] -- maxZipper :: [Int] -> [Int] -> [Int]
-- maxZipper [] ys = ys -- maxZipper [] ys = ys
-- maxZipper xs [] = xs -- maxZipper xs [] = xs
@ -344,12 +354,15 @@ layoutBriDocM = \case
colAggregation xs = maximum [ x | x <- xs, x < minimum xs + alignMax ] colAggregation xs = maximum [ x | x <- xs, x < minimum xs + alignMax ]
processedMap :: ColMap2 processedMap :: ColMap2
processedMap = fix $ \result -> processedMap =
_cbs_map finalState <&> \(lastFlag, colSpacingss) -> fix $ \result -> _cbs_map finalState <&> \(lastFlag, colSpacingss) ->
let colss = colSpacingss <&> \spss -> case reverse spss of let
colss = colSpacingss <&> \spss -> case reverse spss of
[] -> [] [] -> []
(xN:xR) -> reverse (xN:xR) ->
$ (if lastFlag then fLast else fInit) xN : fmap fInit xR reverse $ (if lastFlag then fLast else fInit) xN : fmap
fInit
xR
where where
fLast (ColumnSpacingLeaf len ) = len fLast (ColumnSpacingLeaf len ) = len
fLast (ColumnSpacingRef len _) = len fLast (ColumnSpacingRef len _) = len
@ -360,36 +373,43 @@ layoutBriDocM = \case
maxCols = {-Foldable.foldl1 maxZipper-} maxCols = {-Foldable.foldl1 maxZipper-}
fmap colAggregation $ transpose $ Foldable.toList colss fmap colAggregation $ transpose $ Foldable.toList colss
(_, posXs) = mapAccumL (\acc x -> (acc + x, acc)) curX maxCols (_, posXs) = mapAccumL (\acc x -> (acc + x, acc)) curX maxCols
counter count l = counter count l = if List.last posXs + List.last l <= colMax
if List.last posXs + List.last l <=colMax
then count + 1 then count + 1
else count else count
ratio = fromIntegral (foldl counter (0 :: Int) colss) ratio = fromIntegral (foldl counter (0 :: Int) colss)
/ fromIntegral (length colss) / fromIntegral (length colss)
in (ratio, maxCols, colss) in
(ratio, maxCols, colss)
mergeBriDocs :: [BriDoc] -> StateS.State ColBuildState [ColInfo] mergeBriDocs :: [BriDoc] -> StateS.State ColBuildState [ColInfo]
mergeBriDocs bds = mergeBriDocsW ColInfoStart bds mergeBriDocs bds = mergeBriDocsW ColInfoStart bds
mergeBriDocsW :: ColInfo -> [BriDoc] -> StateS.State ColBuildState [ColInfo] mergeBriDocsW
:: ColInfo -> [BriDoc] -> StateS.State ColBuildState [ColInfo]
mergeBriDocsW _ [] = return [] mergeBriDocsW _ [] = return []
mergeBriDocsW lastInfo (bd:bdr) = do mergeBriDocsW lastInfo (bd:bdr) = do
info <- mergeInfoBriDoc True lastInfo bd info <- mergeInfoBriDoc True lastInfo bd
infor <- mergeBriDocsW (if alignBreak && briDocIsMultiLine bd then ColInfoStart else info) bdr infor <- mergeBriDocsW
(if alignBreak && briDocIsMultiLine bd then ColInfoStart else info)
bdr
return $ info : infor return $ info : infor
mergeInfoBriDoc :: Bool mergeInfoBriDoc
:: Bool
-> ColInfo -> ColInfo
-> BriDoc -> BriDoc
-> StateS.StateT ColBuildState Identity ColInfo -> StateS.StateT ColBuildState Identity ColInfo
mergeInfoBriDoc lastFlag ColInfoStart = briDocToColInfo lastFlag mergeInfoBriDoc lastFlag ColInfoStart = briDocToColInfo lastFlag
mergeInfoBriDoc lastFlag ColInfoNo{} = briDocToColInfo lastFlag mergeInfoBriDoc lastFlag ColInfoNo{} = briDocToColInfo lastFlag
mergeInfoBriDoc lastFlag (ColInfo infoInd infoSig subLengthsInfos) = \case mergeInfoBriDoc lastFlag (ColInfo infoInd infoSig subLengthsInfos) =
\case
brdc@(BDCols colSig subDocs) brdc@(BDCols colSig subDocs)
| infoSig == colSig | infoSig == colSig && length subLengthsInfos == length subDocs
&& length subLengthsInfos == length subDocs -> do -> do
let isLastList = let
if lastFlag then (== length subDocs) <$> [1..] else repeat False isLastList = if lastFlag
then (==length subDocs) <$> [1 ..]
else repeat False
infos <- zip3 isLastList (snd <$> subLengthsInfos) subDocs infos <- zip3 isLastList (snd <$> subLengthsInfos) subDocs
`forM` \(lf, info, bd) -> mergeInfoBriDoc lf info bd `forM` \(lf, info, bd) -> mergeInfoBriDoc lf info bd
let curLengths = briDocLineLength <$> subDocs let curLengths = briDocLineLength <$> subDocs
@ -399,12 +419,14 @@ layoutBriDocM = \case
let m = _cbs_map s let m = _cbs_map s
let (Just (_, spaces)) = IntMapS.lookup infoInd m let (Just (_, spaces)) = IntMapS.lookup infoInd m
StateS.put s StateS.put s
{ _cbs_map = IntMapS.insert infoInd { _cbs_map = IntMapS.insert
infoInd
(lastFlag, spaces Seq.|> trueSpacings) (lastFlag, spaces Seq.|> trueSpacings)
m m
} }
return $ ColInfo infoInd colSig (zip curLengths infos) return $ ColInfo infoInd colSig (zip curLengths infos)
| otherwise -> briDocToColInfo lastFlag brdc | otherwise
-> briDocToColInfo lastFlag brdc
brdc -> return $ ColInfoNo brdc brdc -> return $ ColInfoNo brdc
briDocToColInfo :: Bool -> BriDoc -> StateS.State ColBuildState ColInfo briDocToColInfo :: Bool -> BriDoc -> StateS.State ColBuildState ColInfo
@ -423,8 +445,11 @@ layoutBriDocM = \case
(len, ColInfo i _ _) -> ColumnSpacingRef len i (len, ColInfo i _ _) -> ColumnSpacingRef len i
(len, _ ) -> ColumnSpacingLeaf len (len, _ ) -> ColumnSpacingLeaf len
withAlloc :: Bool withAlloc
-> (ColIndex -> StateS.State ColBuildState (ColumnBlocks ColumnSpacing, ColInfo)) :: Bool
-> ( ColIndex
-> StateS.State ColBuildState (ColumnBlocks ColumnSpacing, ColInfo)
)
-> StateS.State ColBuildState ColInfo -> StateS.State ColBuildState ColInfo
withAlloc lastFlag f = do withAlloc lastFlag f = do
cbs <- StateS.get cbs <- StateS.get
@ -441,10 +466,12 @@ layoutBriDocM = \case
ColInfoNo doc -> layoutBriDocM doc ColInfoNo doc -> layoutBriDocM doc
ColInfo ind _ list -> do ColInfo ind _ list -> do
colMax <- mAsk <&> _conf_layout .> _lconfig_cols .> confUnpack colMax <- mAsk <&> _conf_layout .> _lconfig_cols .> confUnpack
alignMode <- mAsk <&> _conf_layout .> _lconfig_columnAlignMode .> confUnpack alignMode <-
mAsk <&> _conf_layout .> _lconfig_columnAlignMode .> confUnpack
curX <- do curX <- do
state <- mGet state <- mGet
return $ either id (const 0) (_lstate_curYOrAddNewline state) return
$ either id (const 0) (_lstate_curYOrAddNewline state)
+ fromMaybe 0 (_lstate_addSepSpace state) + fromMaybe 0 (_lstate_addSepSpace state)
-- tellDebugMess $ show curX -- tellDebugMess $ show curX
let Just (ratio, maxCols, _colss) = IntMapS.lookup ind m let Just (ratio, maxCols, _colss) = IntMapS.lookup ind m
@ -457,14 +484,15 @@ layoutBriDocM = \case
-- sizes in such a way that it works _if_ we have sizes (*factor) -- sizes in such a way that it works _if_ we have sizes (*factor)
-- in each column. but in that line, in the last column, we will be -- in each column. but in that line, in the last column, we will be
-- forced to occupy the full vertical space, not reduced by any factor. -- forced to occupy the full vertical space, not reduced by any factor.
let fixedPosXs = case alignMode of let
fixedPosXs = case alignMode of
ColumnAlignModeAnimouslyScale i | maxX > colMax -> fixed <&> (+curX) ColumnAlignModeAnimouslyScale i | maxX > colMax -> fixed <&> (+curX)
where where
factor :: Float = factor :: Float =
-- 0.0001 as an offering to the floating point gods. -- 0.0001 as an offering to the floating point gods.
min 1.0001 ( fromIntegral (i + colMax - curX) min
/ fromIntegral (maxX - curX) 1.0001
) (fromIntegral (i + colMax - curX) / fromIntegral (maxX - curX))
offsets = (subtract curX) <$> posXs offsets = (subtract curX) <$> posXs
fixed = offsets <&> fromIntegral .> (*factor) .> truncate fixed = offsets <&> fromIntegral .> (*factor) .> truncate
_ -> posXs _ -> posXs
@ -486,6 +514,7 @@ layoutBriDocM = \case
ColumnAlignModeAnimouslyScale{} -> animousAct ColumnAlignModeAnimouslyScale{} -> animousAct
ColumnAlignModeAnimously -> animousAct ColumnAlignModeAnimously -> animousAct
ColumnAlignModeAlways -> alignAct ColumnAlignModeAlways -> alignAct
processInfoIgnore :: ColInfo -> m () processInfoIgnore :: ColInfo -> m ()
processInfoIgnore = \case processInfoIgnore = \case
ColInfoStart -> error "should not happen (TM)" ColInfoStart -> error "should not happen (TM)"