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>
This commit is contained in:
Simon Jakobi 2019-11-14 14:43:35 +01:00 committed by GitHub
parent 12ca71f0c7
commit dedd5e0ea6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 121 additions and 73 deletions

View File

@ -1 +1,5 @@
packages: ./dhall ./dhall-bash ./dhall-json ./dhall-yaml ./dhall-lsp-server ./dhall-nix
-- While hnix doesn't allow prettyprinter > 1.3
-- https://github.com/haskell-nix/hnix/issues/524
allow-newer: hnix:prettyprinter

View File

@ -44,7 +44,7 @@ Library
exceptions >= 0.8.3 && < 0.11,
filepath < 1.5 ,
optparse-applicative >= 0.14.0.0 && < 0.16,
prettyprinter >= 1.2.0.1 && < 1.4 ,
prettyprinter >= 1.5.1 && < 1.6 ,
scientific >= 0.3.0.0 && < 0.4 ,
text >= 0.11.1.0 && < 1.3 ,
unordered-containers < 0.3 ,

View File

@ -61,7 +61,7 @@ library
, megaparsec >= 7.0.2 && < 7.1
, mtl >= 2.2.2 && < 2.3
, network-uri >= 2.6.1.0 && < 2.7
, prettyprinter >= 1.2.1 && < 1.4
, prettyprinter >= 1.5.1 && < 1.6
, text >= 1.2.3.0 && < 1.3
, transformers >= 0.5.5.0 && < 0.6
, unordered-containers >= 0.2.9.0 && < 0.3

View File

@ -1,8 +1,8 @@
module Dhall.LSP.Backend.Formatting (formatExpr, formatExprWithHeader) where
import Dhall.Core (Expr)
import Dhall.Pretty (CharacterSet(..))
import Dhall.Parser (Header(..))
import Dhall.Pretty (CharacterSet, prettyCharacterSet)
import qualified Dhall.Pretty
import Dhall.Src (Src)
@ -13,8 +13,10 @@ import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty
-- | Pretty-print the given Dhall expression.
formatExpr :: Pretty.Pretty b => CharacterSet -> Expr Src b -> Text
formatExpr charSet expr = Pretty.renderStrict . Dhall.Pretty.layout
. Pretty.unAnnotate $ prettyCharacterSet charSet expr
formatExpr charSet expr =
Pretty.renderStrict
. Dhall.Pretty.layout
$ Dhall.Pretty.prettyCharacterSet charSet expr
-- | Pretty-print the given Dhall expression, prepending the given a "header"
-- (usually consisting of comments and whitespace).
@ -24,6 +26,6 @@ formatExprWithHeader charSet expr (Header header) = Pretty.renderStrict
where
doc =
Pretty.pretty header
<> Pretty.unAnnotate (prettyCharacterSet charSet expr)
<> Dhall.Pretty.prettyCharacterSet charSet expr
<> "\n"

View File

@ -18,7 +18,7 @@ executable dhall-try
, aeson-pretty >= 0.8.7 && < 0.9
, dhall >= 1.19.0 && < 1.28
, dhall-json >= 1.2.5 && < 1.6
, prettyprinter >= 1.2.1 && < 1.3
, prettyprinter >= 1.5.1 && < 1.6
, text >= 1.2.3.0 && < 1.3
, ghcjs-base >= 0.2.0.0 && < 0.3
hs-source-dirs: src

View File

@ -428,7 +428,7 @@ Library
network-uri >= 2.6 && < 2.7 ,
optparse-applicative >= 0.14.0.0 && < 0.16,
parsers >= 0.12.4 && < 0.13,
prettyprinter >= 1.2.0.1 && < 1.4 ,
prettyprinter >= 1.5.1 && < 1.6 ,
prettyprinter-ansi-terminal >= 1.1.1 && < 1.2 ,
profunctors >= 3.1.2 && < 5.6 ,
repline >= 0.2.1.0 && < 0.3 ,

View File

@ -14,7 +14,7 @@ module Dhall.Format
import Control.Exception (Exception)
import Data.Monoid ((<>))
import Dhall.Pretty (CharacterSet(..), annToAnsiStyle)
import Dhall.Util (Censor, Input(..))
import Dhall.Util (Censor, Input(..), Header(..))
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
@ -50,54 +50,48 @@ data FormatMode
format
:: Format
-> IO ()
format (Format {..}) =
format (Format {..}) = do
let layoutHeaderAndExpr (Header header, expr) =
Dhall.Pretty.layout
( Pretty.pretty header
<> Dhall.Pretty.prettyCharacterSet characterSet expr
<> "\n")
let layoutInput input = do
headerAndExpr <- Dhall.Util.getExpressionAndHeader censor input
return (layoutHeaderAndExpr headerAndExpr)
case formatMode of
Modify {..} ->
Modify {..} -> do
docStream <- layoutInput inplace
case inplace of
InputFile file -> do
(Dhall.Util.Header header, expr) <-
Dhall.Util.getExpressionAndHeader censor (InputFile file)
let doc = Pretty.pretty header
<> Pretty.unAnnotate (Dhall.Pretty.prettyCharacterSet characterSet expr)
<> "\n"
System.IO.withFile file System.IO.WriteMode (\handle -> do
Pretty.Terminal.renderIO handle (Dhall.Pretty.layout doc))
Pretty.Terminal.renderIO handle (Pretty.unAnnotateS docStream))
StandardInput -> do
(Dhall.Util.Header header, expr) <-
Dhall.Util.getExpressionAndHeader censor StandardInput
let doc = Pretty.pretty header
<> Dhall.Pretty.prettyCharacterSet characterSet expr
<> "\n"
supportsANSI <- System.Console.ANSI.hSupportsANSI System.IO.stdout
if supportsANSI
then
Pretty.Terminal.renderIO
System.IO.stdout
(fmap annToAnsiStyle (Dhall.Pretty.layout doc))
else
Pretty.Terminal.renderIO
System.IO.stdout
(Dhall.Pretty.layout (Pretty.unAnnotate doc))
Pretty.Terminal.renderIO
System.IO.stdout
(if supportsANSI
then (fmap annToAnsiStyle docStream)
else (Pretty.unAnnotateS docStream))
Check {..} -> do
originalText <- case path of
InputFile file -> Data.Text.IO.readFile file
StandardInput -> Data.Text.IO.getContents
(Dhall.Util.Header header, expr) <- case path of
InputFile _ -> Dhall.Util.getExpressionAndHeader censor path
StandardInput -> Dhall.Util.getExpressionAndHeaderFromStdinText censor originalText
docStream <- case path of
InputFile _ -> layoutInput path
StandardInput -> do
headerAndExpr <- Dhall.Util.getExpressionAndHeaderFromStdinText censor originalText
return (layoutHeaderAndExpr headerAndExpr)
let doc = Pretty.pretty header
<> Pretty.unAnnotate (Dhall.Pretty.prettyCharacterSet characterSet expr)
<> "\n"
let formattedText =
Pretty.Text.renderStrict (Dhall.Pretty.layout doc)
let formattedText = Pretty.Text.renderStrict docStream
if originalText == formattedText
then return ()

View File

@ -17,7 +17,7 @@ module Dhall.Freeze
import Data.Monoid ((<>))
import Data.Text
import Dhall.Parser (Src)
import Dhall.Pretty (CharacterSet, annToAnsiStyle, prettyCharacterSet)
import Dhall.Pretty (CharacterSet)
import Dhall.Syntax (Expr(..), Import(..), ImportHashed(..), ImportType(..))
import Dhall.Util (Censor, Input(..))
import System.Console.ANSI (hSupportsANSI)
@ -26,7 +26,6 @@ import qualified Control.Exception
import qualified Control.Monad.Trans.State.Strict as State
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty
import qualified Data.Text.IO
import qualified Dhall.Core
import qualified Dhall.Import
import qualified Dhall.Optics
@ -88,20 +87,22 @@ writeExpr :: Input -> (Text, Expr Src Import) -> CharacterSet -> IO ()
writeExpr inplace (header, expr) characterSet = do
let doc = Pretty.pretty header
<> Dhall.Pretty.prettyCharacterSet characterSet expr
<> "\n"
let unAnnotated = Dhall.Pretty.layout (Pretty.unAnnotate doc)
let stream = Dhall.Pretty.layout doc
let unAnnotated = Pretty.unAnnotateS stream
case inplace of
InputFile f ->
System.IO.withFile f System.IO.WriteMode (\handle -> do
Pretty.renderIO handle unAnnotated
Data.Text.IO.hPutStrLn handle "" )
Pretty.renderIO handle unAnnotated)
StandardInput -> do
supportsANSI <- System.Console.ANSI.hSupportsANSI System.IO.stdout
if supportsANSI
then
Pretty.renderIO System.IO.stdout (annToAnsiStyle <$> Dhall.Pretty.layout doc)
Pretty.renderIO System.IO.stdout (Dhall.Pretty.annToAnsiStyle <$> stream)
else
Pretty.renderIO System.IO.stdout unAnnotated

View File

@ -1082,7 +1082,7 @@ prettyCharacterSet characterSet expression =
where
long =
Pretty.align
( literal ("''" <> Pretty.hardline)
( literal "''" <> Pretty.hardline
<> Pretty.align
(foldMap prettyMultilineChunk a <> prettyMultilineText b)
<> literal "''"
@ -1100,12 +1100,18 @@ prettyCharacterSet characterSet expression =
<> prettyExpression d
<> rbrace
prettyMultilineText text = literal (mconcat docs)
prettyMultilineText text = mconcat docs
where
lines_ = Text.splitOn "\n" (escapeSingleQuotedText text)
-- Annotate only non-empty lines so trailing whitespace can be
-- removed on empty ones.
prettyLine line =
(if Text.null line then id else literal)
(Pretty.pretty line)
docs =
Data.List.intersperse Pretty.hardline (fmap Pretty.pretty lines_)
Data.List.intersperse Pretty.hardline (map prettyLine lines_)
prettyChunk (c, d) =
prettyText c
@ -1177,7 +1183,7 @@ prettyToStrictText = docToStrictText . Pretty.pretty
--
-- Tries hard to fit the document into 80 columns.
layout :: Pretty.Doc ann -> Pretty.SimpleDocStream ann
layout = Pretty.layoutSmart layoutOpts
layout = Pretty.removeTrailingWhitespace . Pretty.layoutSmart layoutOpts
-- | Default layout options
layoutOpts :: Pretty.LayoutOptions

View File

@ -2,5 +2,5 @@
→ …
→ …@- 1
+ 0
→ …

View File

@ -23,7 +23,7 @@ let concatSep
status
)
Status.Empty
in merge { Empty = "", NonEmpty = λ(result : Text) → result } status
let example0 = assert : concatSep ", " [ "ABC", "DEF", "GHI" ] ≡ "ABC, DEF, GHI"

View File

@ -1,3 +1,3 @@
{- bla -}
True

View File

@ -1,11 +1,11 @@
{ inner =
''
one
two
three
''
}

View File

@ -1,5 +1,5 @@
let foo =
{- test -}
"hello"

View File

@ -0,0 +1,11 @@
let foo = 1
in λ(bar : Integer)
→ let exposePort =
λ(portSpec : { ext : Integer, int : Integer })
→ Integer/show portSpec.ext ++ ":" ++ Integer/show portSpec.int
in let exposeSamePort =
λ(port : Integer) → exposePort { ext = port, int = port }
in { blah = bar }

View File

@ -0,0 +1,11 @@
let foo = 1
in λ(bar : Integer)
→ let exposePort =
λ(portSpec : { ext : Integer, int : Integer })
→ Integer/show portSpec.ext ++ ":" ++ Integer/show portSpec.int
in let exposeSamePort =
λ(port : Integer) → exposePort { ext = port, int = port }
in { blah = bar }

View File

@ -5,5 +5,5 @@ in let {- aaaaaaaaaaaaaaaaaaa
: {- bbbbbbbbbbbbbbbbbbbbb
-} Natural
= {- ddddddddddddddddd -} 2
in x

View File

@ -0,0 +1 @@
{ x = " \n\nfoo \n" }

View File

@ -0,0 +1,7 @@
{ x =
''
foo
''
}

View File

@ -1,22 +1,22 @@
{ mkDerivation, ansi-wl-pprint, base, bytestring, containers
, criterion, deepseq, doctest, mtl, pgp-wordlist, QuickCheck
, random, stdenv, tasty, tasty-hunit, tasty-quickcheck, text
, transformers
{ mkDerivation, ansi-wl-pprint, base, base-compat, bytestring
, containers, criterion, deepseq, doctest, mtl, pgp-wordlist
, QuickCheck, random, stdenv, tasty, tasty-hunit, tasty-quickcheck
, text, transformers
}:
mkDerivation {
pname = "prettyprinter";
version = "1.2.1";
sha256 = "e7653e0ba87cc06553a50e4780dde81c5dd156196c0199511d03d972e5517fcf";
version = "1.5.1";
sha256 = "fb66b498cdd46aa7f36abdaf0b49e88444a3e6ed9d04bec8924ed6355f393794";
isLibrary = true;
isExecutable = true;
libraryHaskellDepends = [ base text ];
testHaskellDepends = [
base bytestring doctest pgp-wordlist tasty tasty-hunit
base bytestring doctest pgp-wordlist QuickCheck tasty tasty-hunit
tasty-quickcheck text
];
benchmarkHaskellDepends = [
ansi-wl-pprint base containers criterion deepseq mtl QuickCheck
random text transformers
ansi-wl-pprint base base-compat containers criterion deepseq mtl
QuickCheck random text transformers
];
homepage = "http://github.com/quchen/prettyprinter";
description = "A modern, easy to use, well-documented, extensible pretty-printer";

View File

@ -177,6 +177,11 @@ let
doBenchmarkExtension =
mass pkgsNew.haskell.lib.doBenchmark allDhallPackages;
doJailbreakExtension =
mass pkgsNew.haskell.lib.doJailbreak [
"hnix"
];
failOnAllWarningsExtension =
mass failOnAllWarnings [
"dhall"
@ -266,6 +271,7 @@ let
extension
doCheckExtension
doBenchmarkExtension
doJailbreakExtension
failOnAllWarningsExtension
failOnMissingHaddocksExtension
];

View File

@ -45,7 +45,7 @@ extra-deps:
- optparse-generic-1.3.0
- parsec-3.1.13.0
- parser-combinators-1.0.0
- prettyprinter-1.2.0.1
- prettyprinter-1.5.1@sha256:fca87c3e2611d3499a0341a59857e9b424a23f31646e4737d535a18582284f96,5375
- prettyprinter-ansi-terminal-1.1.1.2
- primitive-0.6.3.0
- process-1.6.2.0

View File

@ -23,7 +23,12 @@ extra-deps:
- ordered-containers-0.2.2@sha256:ebf2be3f592d9cf148ea6b8375f8af97148d44f82d8d04476899285e965afdbf,810
- lsp-test-0.6.1.0@sha256:df0fc403c03b6d036be13de3ff23d9951ae2506080135cd6862eded2c969a6da,3483
- aeson-yaml-1.0.4.0@sha256:72d91a4a2ade87b8a4bdf73937d2c62bd2c60053df1841f8bf1e6204387959b8,1975
- prettyprinter-1.5.1@sha256:fca87c3e2611d3499a0341a59857e9b424a23f31646e4737d535a18582284f96,5375
nix:
packages:
- ncurses
- zlib
# hnix doesn't allow prettyprinter >= 1.3
# https://github.com/haskell-nix/hnix/issues/524
allow-newer: true