Skip to content

Commit dedd5e0

Browse files
Strip trailing whitespace (#1422)
This raises the lower bound on prettyprinter to 1.5.1 since `removeTrailingWhitespace` is buggy in earlier versions. This jailbreaks hnix, which isn't compatible with prettyprinter-1.5.1 yet. Fixes #183, #1400, #1525. Co-authored-by: Gabriel Gonzalez <Gabriel439@gmail.com>
1 parent 12ca71f commit dedd5e0

23 files changed

+121
-73
lines changed

cabal.project

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
packages: ./dhall ./dhall-bash ./dhall-json ./dhall-yaml ./dhall-lsp-server ./dhall-nix
2+
3+
-- While hnix doesn't allow prettyprinter > 1.3
4+
-- https://github.com/haskell-nix/hnix/issues/524
5+
allow-newer: hnix:prettyprinter

dhall-json/dhall-json.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Library
4444
exceptions >= 0.8.3 && < 0.11,
4545
filepath < 1.5 ,
4646
optparse-applicative >= 0.14.0.0 && < 0.16,
47-
prettyprinter >= 1.2.0.1 && < 1.4 ,
47+
prettyprinter >= 1.5.1 && < 1.6 ,
4848
scientific >= 0.3.0.0 && < 0.4 ,
4949
text >= 0.11.1.0 && < 1.3 ,
5050
unordered-containers < 0.3 ,

dhall-lsp-server/dhall-lsp-server.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ library
6161
, megaparsec >= 7.0.2 && < 7.1
6262
, mtl >= 2.2.2 && < 2.3
6363
, network-uri >= 2.6.1.0 && < 2.7
64-
, prettyprinter >= 1.2.1 && < 1.4
64+
, prettyprinter >= 1.5.1 && < 1.6
6565
, text >= 1.2.3.0 && < 1.3
6666
, transformers >= 0.5.5.0 && < 0.6
6767
, unordered-containers >= 0.2.9.0 && < 0.3

dhall-lsp-server/src/Dhall/LSP/Backend/Formatting.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Dhall.LSP.Backend.Formatting (formatExpr, formatExprWithHeader) where
22

33
import Dhall.Core (Expr)
4+
import Dhall.Pretty (CharacterSet(..))
45
import Dhall.Parser (Header(..))
5-
import Dhall.Pretty (CharacterSet, prettyCharacterSet)
66
import qualified Dhall.Pretty
77
import Dhall.Src (Src)
88

@@ -13,8 +13,10 @@ import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty
1313

1414
-- | Pretty-print the given Dhall expression.
1515
formatExpr :: Pretty.Pretty b => CharacterSet -> Expr Src b -> Text
16-
formatExpr charSet expr = Pretty.renderStrict . Dhall.Pretty.layout
17-
. Pretty.unAnnotate $ prettyCharacterSet charSet expr
16+
formatExpr charSet expr =
17+
Pretty.renderStrict
18+
. Dhall.Pretty.layout
19+
$ Dhall.Pretty.prettyCharacterSet charSet expr
1820

1921
-- | Pretty-print the given Dhall expression, prepending the given a "header"
2022
-- (usually consisting of comments and whitespace).
@@ -24,6 +26,6 @@ formatExprWithHeader charSet expr (Header header) = Pretty.renderStrict
2426
where
2527
doc =
2628
Pretty.pretty header
27-
<> Pretty.unAnnotate (prettyCharacterSet charSet expr)
29+
<> Dhall.Pretty.prettyCharacterSet charSet expr
2830
<> "\n"
2931

dhall-try/dhall-try.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ executable dhall-try
1818
, aeson-pretty >= 0.8.7 && < 0.9
1919
, dhall >= 1.19.0 && < 1.28
2020
, dhall-json >= 1.2.5 && < 1.6
21-
, prettyprinter >= 1.2.1 && < 1.3
21+
, prettyprinter >= 1.5.1 && < 1.6
2222
, text >= 1.2.3.0 && < 1.3
2323
, ghcjs-base >= 0.2.0.0 && < 0.3
2424
hs-source-dirs: src

dhall/dhall.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ Library
428428
network-uri >= 2.6 && < 2.7 ,
429429
optparse-applicative >= 0.14.0.0 && < 0.16,
430430
parsers >= 0.12.4 && < 0.13,
431-
prettyprinter >= 1.2.0.1 && < 1.4 ,
431+
prettyprinter >= 1.5.1 && < 1.6 ,
432432
prettyprinter-ansi-terminal >= 1.1.1 && < 1.2 ,
433433
profunctors >= 3.1.2 && < 5.6 ,
434434
repline >= 0.2.1.0 && < 0.3 ,

dhall/src/Dhall/Format.hs

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Dhall.Format
1414
import Control.Exception (Exception)
1515
import Data.Monoid ((<>))
1616
import Dhall.Pretty (CharacterSet(..), annToAnsiStyle)
17-
import Dhall.Util (Censor, Input(..))
17+
import Dhall.Util (Censor, Input(..), Header(..))
1818

1919
import qualified Data.Text.Prettyprint.Doc as Pretty
2020
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
@@ -50,54 +50,48 @@ data FormatMode
5050
format
5151
:: Format
5252
-> IO ()
53-
format (Format {..}) =
53+
format (Format {..}) = do
54+
let layoutHeaderAndExpr (Header header, expr) =
55+
Dhall.Pretty.layout
56+
( Pretty.pretty header
57+
<> Dhall.Pretty.prettyCharacterSet characterSet expr
58+
<> "\n")
59+
60+
let layoutInput input = do
61+
headerAndExpr <- Dhall.Util.getExpressionAndHeader censor input
62+
63+
return (layoutHeaderAndExpr headerAndExpr)
64+
5465
case formatMode of
55-
Modify {..} ->
66+
Modify {..} -> do
67+
docStream <- layoutInput inplace
68+
5669
case inplace of
5770
InputFile file -> do
58-
(Dhall.Util.Header header, expr) <-
59-
Dhall.Util.getExpressionAndHeader censor (InputFile file)
60-
61-
let doc = Pretty.pretty header
62-
<> Pretty.unAnnotate (Dhall.Pretty.prettyCharacterSet characterSet expr)
63-
<> "\n"
64-
6571
System.IO.withFile file System.IO.WriteMode (\handle -> do
66-
Pretty.Terminal.renderIO handle (Dhall.Pretty.layout doc))
67-
StandardInput -> do
68-
(Dhall.Util.Header header, expr) <-
69-
Dhall.Util.getExpressionAndHeader censor StandardInput
70-
71-
let doc = Pretty.pretty header
72-
<> Dhall.Pretty.prettyCharacterSet characterSet expr
73-
<> "\n"
72+
Pretty.Terminal.renderIO handle (Pretty.unAnnotateS docStream))
7473

74+
StandardInput -> do
7575
supportsANSI <- System.Console.ANSI.hSupportsANSI System.IO.stdout
7676

77-
if supportsANSI
78-
then
79-
Pretty.Terminal.renderIO
80-
System.IO.stdout
81-
(fmap annToAnsiStyle (Dhall.Pretty.layout doc))
82-
else
83-
Pretty.Terminal.renderIO
84-
System.IO.stdout
85-
(Dhall.Pretty.layout (Pretty.unAnnotate doc))
77+
Pretty.Terminal.renderIO
78+
System.IO.stdout
79+
(if supportsANSI
80+
then (fmap annToAnsiStyle docStream)
81+
else (Pretty.unAnnotateS docStream))
82+
8683
Check {..} -> do
8784
originalText <- case path of
8885
InputFile file -> Data.Text.IO.readFile file
8986
StandardInput -> Data.Text.IO.getContents
9087

91-
(Dhall.Util.Header header, expr) <- case path of
92-
InputFile _ -> Dhall.Util.getExpressionAndHeader censor path
93-
StandardInput -> Dhall.Util.getExpressionAndHeaderFromStdinText censor originalText
94-
95-
let doc = Pretty.pretty header
96-
<> Pretty.unAnnotate (Dhall.Pretty.prettyCharacterSet characterSet expr)
97-
<> "\n"
88+
docStream <- case path of
89+
InputFile _ -> layoutInput path
90+
StandardInput -> do
91+
headerAndExpr <- Dhall.Util.getExpressionAndHeaderFromStdinText censor originalText
92+
return (layoutHeaderAndExpr headerAndExpr)
9893

99-
let formattedText =
100-
Pretty.Text.renderStrict (Dhall.Pretty.layout doc)
94+
let formattedText = Pretty.Text.renderStrict docStream
10195

10296
if originalText == formattedText
10397
then return ()

dhall/src/Dhall/Freeze.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Dhall.Freeze
1717
import Data.Monoid ((<>))
1818
import Data.Text
1919
import Dhall.Parser (Src)
20-
import Dhall.Pretty (CharacterSet, annToAnsiStyle, prettyCharacterSet)
20+
import Dhall.Pretty (CharacterSet)
2121
import Dhall.Syntax (Expr(..), Import(..), ImportHashed(..), ImportType(..))
2222
import Dhall.Util (Censor, Input(..))
2323
import System.Console.ANSI (hSupportsANSI)
@@ -26,7 +26,6 @@ import qualified Control.Exception
2626
import qualified Control.Monad.Trans.State.Strict as State
2727
import qualified Data.Text.Prettyprint.Doc as Pretty
2828
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty
29-
import qualified Data.Text.IO
3029
import qualified Dhall.Core
3130
import qualified Dhall.Import
3231
import qualified Dhall.Optics
@@ -88,20 +87,22 @@ writeExpr :: Input -> (Text, Expr Src Import) -> CharacterSet -> IO ()
8887
writeExpr inplace (header, expr) characterSet = do
8988
let doc = Pretty.pretty header
9089
<> Dhall.Pretty.prettyCharacterSet characterSet expr
90+
<> "\n"
9191

92-
let unAnnotated = Dhall.Pretty.layout (Pretty.unAnnotate doc)
92+
let stream = Dhall.Pretty.layout doc
93+
94+
let unAnnotated = Pretty.unAnnotateS stream
9395

9496
case inplace of
9597
InputFile f ->
9698
System.IO.withFile f System.IO.WriteMode (\handle -> do
97-
Pretty.renderIO handle unAnnotated
98-
Data.Text.IO.hPutStrLn handle "" )
99+
Pretty.renderIO handle unAnnotated)
99100

100101
StandardInput -> do
101102
supportsANSI <- System.Console.ANSI.hSupportsANSI System.IO.stdout
102103
if supportsANSI
103104
then
104-
Pretty.renderIO System.IO.stdout (annToAnsiStyle <$> Dhall.Pretty.layout doc)
105+
Pretty.renderIO System.IO.stdout (Dhall.Pretty.annToAnsiStyle <$> stream)
105106
else
106107
Pretty.renderIO System.IO.stdout unAnnotated
107108

dhall/src/Dhall/Pretty/Internal.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ prettyCharacterSet characterSet expression =
10821082
where
10831083
long =
10841084
Pretty.align
1085-
( literal ("''" <> Pretty.hardline)
1085+
( literal "''" <> Pretty.hardline
10861086
<> Pretty.align
10871087
(foldMap prettyMultilineChunk a <> prettyMultilineText b)
10881088
<> literal "''"
@@ -1100,12 +1100,18 @@ prettyCharacterSet characterSet expression =
11001100
<> prettyExpression d
11011101
<> rbrace
11021102

1103-
prettyMultilineText text = literal (mconcat docs)
1103+
prettyMultilineText text = mconcat docs
11041104
where
11051105
lines_ = Text.splitOn "\n" (escapeSingleQuotedText text)
11061106

1107+
-- Annotate only non-empty lines so trailing whitespace can be
1108+
-- removed on empty ones.
1109+
prettyLine line =
1110+
(if Text.null line then id else literal)
1111+
(Pretty.pretty line)
1112+
11071113
docs =
1108-
Data.List.intersperse Pretty.hardline (fmap Pretty.pretty lines_)
1114+
Data.List.intersperse Pretty.hardline (map prettyLine lines_)
11091115

11101116
prettyChunk (c, d) =
11111117
prettyText c
@@ -1177,7 +1183,7 @@ prettyToStrictText = docToStrictText . Pretty.pretty
11771183
--
11781184
-- Tries hard to fit the document into 80 columns.
11791185
layout :: Pretty.Doc ann -> Pretty.SimpleDocStream ann
1180-
layout = Pretty.layoutSmart layoutOpts
1186+
layout = Pretty.removeTrailingWhitespace . Pretty.layoutSmart layoutOpts
11811187

11821188
-- | Default layout options
11831189
layoutOpts :: Pretty.LayoutOptions

dhall/tests/diff/function.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
→ …
33
→ …@- 1
44
+ 0
5-
5+
66
→ …

0 commit comments

Comments
 (0)