Skip to content

Commit

Permalink
Don’t strip trailing newlines at the end of the document
Browse files Browse the repository at this point in the history
  • Loading branch information
quchen committed Oct 19, 2019
1 parent 0fd83e7 commit c1bc750
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
4 changes: 1 addition & 3 deletions prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1490,9 +1490,7 @@ removeTrailingWhitespace = go (RecordedWhitespace [] 0)
-- release only the necessary ones.
go (RecordedWhitespace withheldLines withheldSpaces) = \sds -> case sds of
SFail -> SFail
SEmpty -> if null withheldLines
then SEmpty
else SLine 0 SEmpty -- do not remove the last newline, Unix-style
SEmpty -> foldr (\_i sds' -> SLine 0 sds') SEmpty withheldLines
SChar c rest
| c == ' ' -> go (RecordedWhitespace withheldLines (withheldSpaces+1)) rest
| otherwise -> commitSpaces
Expand Down
8 changes: 4 additions & 4 deletions prettyprinter/test/Testsuite/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ tests = testGroup "Tests"
, testCase "Keep single trailing newline"
removeTrailingWhitespaceKeepTrailingNewline
, testCase "Reduce to single trailing newline"
removeTrailingWhitespaceReduceToSingleTrailingNewline
removeTrailingWhitespaceInTrailingNewlines
]
]
]
Expand Down Expand Up @@ -262,9 +262,9 @@ removeTrailingWhitespaceKeepLonelyTrailingNewline
sdoc = SChar 'x' (SLine 0 SEmpty)
in assertEqual "" sdoc (removeTrailingWhitespace sdoc)

removeTrailingWhitespaceReduceToSingleTrailingNewline :: Assertion
removeTrailingWhitespaceReduceToSingleTrailingNewline
removeTrailingWhitespaceInTrailingNewlines :: Assertion
removeTrailingWhitespaceInTrailingNewlines
= let sdoc :: SimpleDocStream ()
sdoc = SChar 'x' (SLine 2 (SLine 2 SEmpty))
sdoc' = SChar 'x' (SLine 0 SEmpty)
sdoc' = SChar 'x' (SLine 0 (SLine 0 SEmpty))
in assertEqual "" sdoc' (removeTrailingWhitespace sdoc)

0 comments on commit c1bc750

Please sign in to comment.