summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNorman Ramsey <Norman.Ramsey@tweag.io>2022-08-10 12:10:47 -0400
committerNorman Ramsey <Norman.Ramsey@tweag.io>2022-09-21 18:32:13 -0400
commit3e9e2c7ce37d5e1a898c4ee3274622fd26050826 (patch)
treed2b3c51b0b383de4c15f7175a0b18329929d5a63
parenta0edb89a32eda1746cd4eb05133758478ef2675e (diff)
downloadhaskell-wip/nr/indentable.tar.gz
add draft Indentable module (to replace Outputable)wip/nr/indentable
-rw-r--r--compiler/GHC/Utils/Indentable.hs54
-rw-r--r--compiler/ghc.cabal.in1
2 files changed, 55 insertions, 0 deletions
diff --git a/compiler/GHC/Utils/Indentable.hs b/compiler/GHC/Utils/Indentable.hs
new file mode 100644
index 0000000000..44f9e16096
--- /dev/null
+++ b/compiler/GHC/Utils/Indentable.hs
@@ -0,0 +1,54 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module GHC.Utils.Indentable
+ ( IndentedBuilder
+ , Indentation
+ , newline
+ , nestBy
+ , nest
+ , indentTo
+ , (<+>)
+ , Indentable(..)
+ )
+
+where
+
+import GHC.Prelude
+
+import Data.ByteString.Builder
+import Data.Monoid
+import Data.String
+
+type Indentation = Builder
+
+class Indentable a where
+ toIndentable :: a -> IndentedBuilder
+
+newtype IndentedBuilder = IB (Indentation -> Builder)
+
+newline :: IndentedBuilder
+newline = IB $ \indent -> "\n" <> indent
+
+instance Semigroup IndentedBuilder where
+ IB f <> IB f' = IB $ \indent -> f indent <> f' indent
+
+instance IsString IndentedBuilder where
+ fromString s = IB $ const (fromString s)
+
+instance Monoid IndentedBuilder where
+ mempty = IB $ const mempty
+
+nestBy :: Indentation -> IndentedBuilder -> IndentedBuilder
+nestBy moreIndent (IB f) = IB (\indent -> f (indent <> moreIndent))
+
+nest :: Int -> IndentedBuilder -> IndentedBuilder
+nest k = nestBy $ fromString $ take k spaces
+
+spaces :: String
+spaces = ' ' : spaces
+
+indentTo :: Indentation -> IndentedBuilder -> Builder
+indentTo indentation (IB f) = f indentation
+
+(<+>) :: IndentedBuilder -> IndentedBuilder -> IndentedBuilder
+s <+> s' = s <> " " <> s'
diff --git a/compiler/ghc.cabal.in b/compiler/ghc.cabal.in
index 35b619b8ce..4e269b5911 100644
--- a/compiler/ghc.cabal.in
+++ b/compiler/ghc.cabal.in
@@ -791,6 +791,7 @@ Library
GHC.Utils.FV
GHC.Utils.GlobalVars
GHC.Utils.IO.Unsafe
+ GHC.Utils.Indentable
GHC.Utils.Json
GHC.Utils.Lexeme
GHC.Utils.Logger