diff --git a/prettyprinter/bench/LargeOutput.hs b/prettyprinter/bench/LargeOutput.hs index 9446cacc..b6630d25 100644 --- a/prettyprinter/bench/LargeOutput.hs +++ b/prettyprinter/bench/LargeOutput.hs @@ -1,5 +1,7 @@ -{-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeSynonymInstances #-} {-# OPTIONS_GHC -fno-warn-orphans #-} @@ -13,10 +15,10 @@ import Control.Monad.Compat import Data.Char import Data.Map (Map) import qualified Data.Map as M -import Data.Text (Text) -import qualified Data.Text as T -import qualified Data.Text.IO as T -import qualified Data.Text.Lazy as TL +import Prettyprinter.Util.Compat.Text (Text) +import qualified Prettyprinter.Util.Compat.Text as T +import qualified Prettyprinter.Util.Compat.Text.IO as T +import qualified Prettyprinter.Util.Compat.Text.Lazy as TL import Prettyprinter import Prettyprinter.Render.Text import GHC.Generics diff --git a/prettyprinter/prettyprinter.cabal b/prettyprinter/prettyprinter.cabal index 694a260b..f3de4142 100644 --- a/prettyprinter/prettyprinter.cabal +++ b/prettyprinter/prettyprinter.cabal @@ -36,6 +36,10 @@ library , Prettyprinter.Render.Util.SimpleDocTree , Prettyprinter.Render.Util.StackMachine , Prettyprinter.Util + , Prettyprinter.Util.Compat.Text + , Prettyprinter.Util.Compat.Text.IO + , Prettyprinter.Util.Compat.Text.Lazy + , Prettyprinter.Util.Compat.Text.Lazy.Builder , Prettyprinter.Symbols.Unicode , Prettyprinter.Symbols.Ascii diff --git a/prettyprinter/src-text/Data/Text/Lazy.hs b/prettyprinter/src-text/Data/Text/Lazy.hs index f66d8974..8a9d6e88 100644 --- a/prettyprinter/src-text/Data/Text/Lazy.hs +++ b/prettyprinter/src-text/Data/Text/Lazy.hs @@ -13,3 +13,5 @@ type Text = T.Text length = T.length lines = T.lines toStrict = id +pack = T.pack +unpack = T.unpack diff --git a/prettyprinter/src-text/Data/Text/Lazy/Builder.hs b/prettyprinter/src-text/Data/Text/Lazy/Builder.hs index 690533c9..2a47c778 100644 --- a/prettyprinter/src-text/Data/Text/Lazy/Builder.hs +++ b/prettyprinter/src-text/Data/Text/Lazy/Builder.hs @@ -7,7 +7,25 @@ module Data.Text.Lazy.Builder where -type Builder = String -fromText = id -singleton = (:[]) -toLazyText = id +import Data.String (IsString (..)) +import Data.Semigroup + +newtype Builder = Builder (String -> String) + +instance IsString Builder where + fromString s = Builder (s ++) + +instance Semigroup Builder where + Builder a <> Builder b = Builder (a . b) + +instance Monoid Builder where + mempty = Builder id + +fromText :: String -> Builder +fromText t = Builder (t ++) + +singleton :: Char -> Builder +singleton c = Builder ([c] ++) + +toLazyText :: Builder -> String +toLazyText (Builder b) = b "" diff --git a/prettyprinter/src/Prettyprinter/Render/Text.hs b/prettyprinter/src/Prettyprinter/Render/Text.hs index 4fcaf8fa..e1ea66bb 100644 --- a/prettyprinter/src/Prettyprinter/Render/Text.hs +++ b/prettyprinter/src/Prettyprinter/Render/Text.hs @@ -4,10 +4,8 @@ -- | Render an unannotated 'SimpleDocStream' as plain 'Text'. module Prettyprinter.Render.Text ( -#ifdef MIN_VERSION_text -- * Conversion to plain 'Text' renderLazy, renderStrict, -#endif -- * Render to a 'Handle' renderIO, diff --git a/prettyprinter/src/Prettyprinter/Util/Compat/Text.hs b/prettyprinter/src/Prettyprinter/Util/Compat/Text.hs new file mode 100644 index 00000000..1e1c398c --- /dev/null +++ b/prettyprinter/src/Prettyprinter/Util/Compat/Text.hs @@ -0,0 +1,11 @@ +-- | This module is for use by packages that need to be able to use the prettyprinter package +-- without incurring a dependency on the text package. +-- +-- Legitimate examples of packages that must have text as an optional dependency, include text (or +-- bytetring). +module Prettyprinter.Util.Compat.Text + ( module Data.Text + ) where + +import Data.Text (Text, cons, dropWhileEnd, head, intercalate, length, lines, map, null, pack, replicate, + singleton, snoc, stripEnd, unlines, unpack, words, uncons, splitOn) diff --git a/prettyprinter/src/Prettyprinter/Util/Compat/Text/IO.hs b/prettyprinter/src/Prettyprinter/Util/Compat/Text/IO.hs new file mode 100644 index 00000000..76d3fa05 --- /dev/null +++ b/prettyprinter/src/Prettyprinter/Util/Compat/Text/IO.hs @@ -0,0 +1,10 @@ +-- | This module is for use by packages that need to be able to use the prettyprinter package +-- without incurring a dependency on the text package. +-- +-- Legitimate examples of packages that must have text as an optional dependency, include text (or +-- bytetring). +module Prettyprinter.Util.Compat.Text.IO + ( module Data.Text.IO + ) where + +import Data.Text.IO (hPutStr, putStrLn) diff --git a/prettyprinter/src/Prettyprinter/Util/Compat/Text/Lazy.hs b/prettyprinter/src/Prettyprinter/Util/Compat/Text/Lazy.hs new file mode 100644 index 00000000..b77a63f5 --- /dev/null +++ b/prettyprinter/src/Prettyprinter/Util/Compat/Text/Lazy.hs @@ -0,0 +1,10 @@ +-- | This module is for use by packages that need to be able to use the prettyprinter package +-- without incurring a dependency on the text package. +-- +-- Legitimate examples of packages that must have text as an optional dependency, include text (or +-- bytetring). +module Prettyprinter.Util.Compat.Text.Lazy + ( module Data.Text.Lazy + ) where + +import Data.Text.Lazy (Text, length, lines, toStrict, pack, unpack) diff --git a/prettyprinter/src/Prettyprinter/Util/Compat/Text/Lazy/Builder.hs b/prettyprinter/src/Prettyprinter/Util/Compat/Text/Lazy/Builder.hs new file mode 100644 index 00000000..448b2567 --- /dev/null +++ b/prettyprinter/src/Prettyprinter/Util/Compat/Text/Lazy/Builder.hs @@ -0,0 +1,10 @@ +-- | This module is for use by packages that need to be able to use the prettyprinter package +-- without incurring a dependency on the text package. +-- +-- Legitimate examples of packages that must have text as an optional dependency, include text (or +-- bytetring). +module Prettyprinter.Util.Compat.Text.Lazy.Builder + ( module Data.Text.Lazy.Builder + ) where + +import Data.Text.Lazy.Builder (Builder (), fromText, singleton, toLazyText)