Skip to content

Commit

Permalink
Separate commitSpaces and commitLines
Browse files Browse the repository at this point in the history
  • Loading branch information
sjakobi authored and quchen committed Nov 5, 2019
1 parent 01d05aa commit ce0f71f
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1460,16 +1460,19 @@ data SimpleDocStream ann =
removeTrailingWhitespace :: SimpleDocStream ann -> SimpleDocStream ann
removeTrailingWhitespace = go (RecordedWhitespace [] 0)
where
commitSpaces
commitWhitespace
:: [Int] -- Withheld lines
-> Int -- Withheld spaces
-> SimpleDocStream ann
-> SimpleDocStream ann
commitSpaces [] 0 = id
commitSpaces [] 1 = SChar ' '
commitSpaces [] n = SText n (T.replicate n " ")
commitSpaces [i] n = SLine i . commitSpaces [] n
commitSpaces (i:is) n = commitSpaces (map (const 0) is) 0 . SLine i . commitSpaces [] n
commitWhitespace is0 n0 = commitLines is0 . commitSpaces n0
where
commitLines [] = id
commitLines (i:is) = (\sds -> foldr (\_ -> SLine 0) sds is) . SLine i

commitSpaces 0 = id
commitSpaces 1 = SChar ' '
commitSpaces n = SText n (T.replicate n " ")

go :: WhitespaceStrippingState -> SimpleDocStream ann -> SimpleDocStream ann
-- We do not strip whitespace inside annotated documents, since it might
Expand All @@ -1493,7 +1496,7 @@ removeTrailingWhitespace = go (RecordedWhitespace [] 0)
SEmpty -> foldr (\_i sds' -> SLine 0 sds') SEmpty withheldLines
SChar c rest
| c == ' ' -> go (RecordedWhitespace withheldLines (withheldSpaces+1)) rest
| otherwise -> commitSpaces
| otherwise -> commitWhitespace
withheldLines
withheldSpaces
(SChar c (go (RecordedWhitespace [] 0) rest))
Expand All @@ -1504,14 +1507,14 @@ removeTrailingWhitespace = go (RecordedWhitespace [] 0)
isOnlySpace = strippedLength == 0
in if isOnlySpace
then go (RecordedWhitespace withheldLines (withheldSpaces + textLength)) rest
else commitSpaces
else commitWhitespace
withheldLines
withheldSpaces
(SText strippedLength
stripped
(go (RecordedWhitespace [] trailingLength) rest))
SLine i rest -> go (RecordedWhitespace (i:withheldLines) 0) rest
SAnnPush ann rest -> commitSpaces
SAnnPush ann rest -> commitWhitespace
withheldLines
withheldSpaces
(SAnnPush ann (go (AnnotationLevel 1) rest))
Expand Down

0 comments on commit ce0f71f

Please sign in to comment.