Skip to content

Commit c1bc750

Browse files
committed
Don’t strip trailing newlines at the end of the document
1 parent 0fd83e7 commit c1bc750

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,9 +1490,7 @@ removeTrailingWhitespace = go (RecordedWhitespace [] 0)
14901490
-- release only the necessary ones.
14911491
go (RecordedWhitespace withheldLines withheldSpaces) = \sds -> case sds of
14921492
SFail -> SFail
1493-
SEmpty -> if null withheldLines
1494-
then SEmpty
1495-
else SLine 0 SEmpty -- do not remove the last newline, Unix-style
1493+
SEmpty -> foldr (\_i sds' -> SLine 0 sds') SEmpty withheldLines
14961494
SChar c rest
14971495
| c == ' ' -> go (RecordedWhitespace withheldLines (withheldSpaces+1)) rest
14981496
| otherwise -> commitSpaces

prettyprinter/test/Testsuite/Main.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ tests = testGroup "Tests"
7171
, testCase "Keep single trailing newline"
7272
removeTrailingWhitespaceKeepTrailingNewline
7373
, testCase "Reduce to single trailing newline"
74-
removeTrailingWhitespaceReduceToSingleTrailingNewline
74+
removeTrailingWhitespaceInTrailingNewlines
7575
]
7676
]
7777
]
@@ -262,9 +262,9 @@ removeTrailingWhitespaceKeepLonelyTrailingNewline
262262
sdoc = SChar 'x' (SLine 0 SEmpty)
263263
in assertEqual "" sdoc (removeTrailingWhitespace sdoc)
264264

265-
removeTrailingWhitespaceReduceToSingleTrailingNewline :: Assertion
266-
removeTrailingWhitespaceReduceToSingleTrailingNewline
265+
removeTrailingWhitespaceInTrailingNewlines :: Assertion
266+
removeTrailingWhitespaceInTrailingNewlines
267267
= let sdoc :: SimpleDocStream ()
268268
sdoc = SChar 'x' (SLine 2 (SLine 2 SEmpty))
269-
sdoc' = SChar 'x' (SLine 0 SEmpty)
269+
sdoc' = SChar 'x' (SLine 0 (SLine 0 SEmpty))
270270
in assertEqual "" sdoc' (removeTrailingWhitespace sdoc)

0 commit comments

Comments
 (0)