Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: A different approach to avoiding trailing whitespace #139

Merged
merged 6 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,15 @@ layoutWadlerLeijen
Empty -> best nl cc ds
Char c -> let !cc' = cc+1 in SChar c (best nl cc' ds)
Text l t -> let !cc' = cc+l in SText l t (best nl cc' ds)
Line -> SLine i (best i i ds)
Line -> let x = best i i ds
-- Don't produce indentation if there's no
-- following text on the same line.
-- This prevents trailing whitespace.
i' = case x of
SEmpty -> 0
SLine{} -> 0
_ -> i
in SLine i' x
FlatAlt x _ -> best nl cc (Cons i x ds)
Cat x y -> best nl cc (Cons i x (Cons i y ds))
Nest j x -> let !ij = i+j in best nl cc (Cons ij x ds)
Expand Down
10 changes: 10 additions & 0 deletions prettyprinter/test/Testsuite/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ tests = testGroup "Tests"
, testCase "Line within align" regressionUnboundedGroupedLineWithinAlign
]
]
, testCase "Indentation on otherwise empty lines results in trailing whitespace (#139)"
indentationShouldntCauseTrailingWhitespaceOnOtherwiseEmptyLines
]

fusionDoesNotChangeRendering :: FusionDepth -> Property
Expand Down Expand Up @@ -380,3 +382,11 @@ regressionUnboundedGroupedLineWithinAlign
sdoc = layoutPretty (LayoutOptions Unbounded) doc
expected = SChar 'x' (SLine 0 (SChar 'y' SEmpty))
in assertEqual "" expected sdoc

indentationShouldntCauseTrailingWhitespaceOnOtherwiseEmptyLines :: Assertion
indentationShouldntCauseTrailingWhitespaceOnOtherwiseEmptyLines
= let doc :: Doc ()
doc = indent 1 ("x" <> hardline <> hardline <> "y" <> hardline)
sdoc = layoutPretty (LayoutOptions Unbounded) doc
expected = SChar ' ' (SChar 'x' (SLine 0 (SLine 1 (SChar 'y' (SLine 0 SEmpty)))))
in assertEqual "" expected sdoc