Created
August 9, 2022 21:01
-
-
Save nrnrnr/1440511a4f3785ded41a27d2ca90b004 to your computer and use it in GitHub Desktop.
Simple substitute for Outputable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module GHC.Utils.Indentable | |
( IndentedBuilder | |
, Indentation | |
, newline | |
, indentBy | |
, indent | |
, (<+>) | |
) | |
where | |
import GHC.Prelude | |
import Data.ByteString.Builder | |
import Data.Monoid | |
import Data.Semigroup | |
import Data.String | |
type Indentation = Builder | |
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 | |
indentBy :: Indentation -> IndentedBuilder -> IndentedBuilder | |
indentBy moreIndent (IB f) = IB (\indent -> f (indent <> moreIndent)) | |
indent :: Indentation -> IndentedBuilder -> Builder | |
indent indentation (IB f) = f indentation | |
(<+>) :: IndentedBuilder -> IndentedBuilder -> IndentedBuilder | |
s <+> s' = s <> " " <> s' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment