dhall-haskell/dhall-lsp-server/src/Dhall/LSP/Backend/Formatting.hs
Simon Jakobi dedd5e0ea6
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>
2019-11-14 14:43:35 +01:00

32 lines
1.1 KiB
Haskell

module Dhall.LSP.Backend.Formatting (formatExpr, formatExprWithHeader) where
import Dhall.Core (Expr)
import Dhall.Pretty (CharacterSet(..))
import Dhall.Parser (Header(..))
import qualified Dhall.Pretty
import Dhall.Src (Src)
import Data.Monoid ((<>))
import Data.Text (Text)
import qualified Data.Text.Prettyprint.Doc as Pretty
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
$ Dhall.Pretty.prettyCharacterSet charSet expr
-- | Pretty-print the given Dhall expression, prepending the given a "header"
-- (usually consisting of comments and whitespace).
formatExprWithHeader :: Pretty.Pretty b => CharacterSet -> Expr Src b -> Header -> Text
formatExprWithHeader charSet expr (Header header) = Pretty.renderStrict
(Dhall.Pretty.layout doc)
where
doc =
Pretty.pretty header
<> Dhall.Pretty.prettyCharacterSet charSet expr
<> "\n"