From 10c94e5f84606f965e299d7bfe80e45e4786d5a8 Mon Sep 17 00:00:00 2001 From: John Ky Date: Sat, 13 Nov 2021 22:03:07 +1100 Subject: [PATCH 1/2] 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 | 26 ++++++++++++++++--- .../src/Prettyprinter/Render/Text.hs | 2 -- .../src/Prettyprinter/Util/Compat/Text.hs | 11 ++++++++ .../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, 77 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..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..eb4d9e3a --- /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 (and +-- bytestring). +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..1c8b787a --- /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 (and +-- bytestring). +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..f55cc54d --- /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 (and +-- bytestring). +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..416fdd90 --- /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 (and +-- bytestring). +module Prettyprinter.Util.Compat.Text.Lazy.Builder + ( module Data.Text.Lazy.Builder + ) where + +import Data.Text.Lazy.Builder (Builder (), fromText, singleton, toLazyText) From bb29b1e6d866645e66675acd7cbb6a2e9fde70c7 Mon Sep 17 00:00:00 2001 From: John Ky Date: Sat, 29 Jan 2022 14:33:31 +1100 Subject: [PATCH 2/2] Remove surplus extensions --- prettyprinter/bench/LargeOutput.hs | 2 -- 1 file changed, 2 deletions(-) diff --git a/prettyprinter/bench/LargeOutput.hs b/prettyprinter/bench/LargeOutput.hs index b6630d25..fba47023 100644 --- a/prettyprinter/bench/LargeOutput.hs +++ b/prettyprinter/bench/LargeOutput.hs @@ -1,7 +1,5 @@ {-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE TypeSynonymInstances #-} {-# OPTIONS_GHC -fno-warn-orphans #-}