From 2e420c2ca1148b56848058dff10a5a4ef9fcb5a7 Mon Sep 17 00:00:00 2001 From: John Ky Date: Wed, 3 Nov 2021 17:53:32 +1100 Subject: [PATCH] Re-export some Data.Text modules for the purpose of downstream libraries being able to write code compatible with the fake text module --- prettyprinter/bench/LargeOutput.hs | 14 ++++++----- prettyprinter/prettyprinter.cabal | 4 +++ prettyprinter/src-text/Data/Text/Lazy.hs | 2 ++ .../src-text/Data/Text/Lazy/Builder.hs | 25 ++++++++++++++++--- .../src/Prettyprinter/Render/Text.hs | 2 -- .../src/Prettyprinter/Util/Compat/Text.hs | 10 ++++++++ .../src/Prettyprinter/Util/Compat/Text/IO.hs | 10 ++++++++ .../Prettyprinter/Util/Compat/Text/Lazy.hs | 10 ++++++++ .../Util/Compat/Text/Lazy/Builder.hs | 10 ++++++++ 9 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 prettyprinter/src/Prettyprinter/Util/Compat/Text.hs create mode 100644 prettyprinter/src/Prettyprinter/Util/Compat/Text/IO.hs create mode 100644 prettyprinter/src/Prettyprinter/Util/Compat/Text/Lazy.hs create mode 100644 prettyprinter/src/Prettyprinter/Util/Compat/Text/Lazy/Builder.hs diff --git a/prettyprinter/bench/LargeOutput.hs b/prettyprinter/bench/LargeOutput.hs index 9446cacc..66717771 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.Compat.Text (Text) +import qualified Prettyprinter.Compat.Text as T +import qualified Prettyprinter.Compat.Text.IO as T +import qualified Prettyprinter.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..4204194a 100644 --- a/prettyprinter/src-text/Data/Text/Lazy/Builder.hs +++ b/prettyprinter/src-text/Data/Text/Lazy/Builder.hs @@ -7,7 +7,24 @@ module Data.Text.Lazy.Builder where -type Builder = String -fromText = id -singleton = (:[]) -toLazyText = id +import Data.String (IsString (..)) + +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..87adbb6f --- /dev/null +++ b/prettyprinter/src/Prettyprinter/Util/Compat/Text.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 must have text as an optional dependency, include text (or +-- bytetring). +module Prettyprinter.Util.Compat.Text + ( module Data.Text + ) where + +import Data.Text 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..5796956c --- /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 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 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..143701a9 --- /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 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 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..20bf7cdf --- /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 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