From 3f414fbf27b4f4f2978093c87c50194925a80bc3 Mon Sep 17 00:00:00 2001 From: Bodigrim Date: Sun, 17 Apr 2022 00:54:56 +0100 Subject: [PATCH] Migrate to text-builder-linear --- cabal.project | 4 ++++ prettyprinter/prettyprinter.cabal | 3 ++- .../src/Prettyprinter/Render/Text.hs | 19 ++++++++++++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cabal.project b/cabal.project index 2c103018..582a3256 100644 --- a/cabal.project +++ b/cabal.project @@ -8,3 +8,7 @@ packages: prettyprinter , prettyprinter-compat-annotated-wl-pprint tests: true benchmarks: true + +source-repository-package + type: git + location: https://github.com/Bodigrim/linear-builder diff --git a/prettyprinter/prettyprinter.cabal b/prettyprinter/prettyprinter.cabal index b0b3502c..feee96f6 100644 --- a/prettyprinter/prettyprinter.cabal +++ b/prettyprinter/prettyprinter.cabal @@ -68,7 +68,8 @@ library , ScopedTypeVariables build-depends: - base >= 4.5 && < 5 + base >= 4.5 && < 5, + text-builder-linear if flag(text) build-depends: text >= 1.2 diff --git a/prettyprinter/src/Prettyprinter/Render/Text.hs b/prettyprinter/src/Prettyprinter/Render/Text.hs index 4fcaf8fa..bb6b92e5 100644 --- a/prettyprinter/src/Prettyprinter/Render/Text.hs +++ b/prettyprinter/src/Prettyprinter/Render/Text.hs @@ -1,4 +1,7 @@ {-# LANGUAGE CPP #-} +{-# LANGUAGE LinearTypes #-} +{-# LANGUAGE UnicodeSyntax #-} +{-# LANGUAGE BangPatterns #-} #include "version-compatibility-macros.h" @@ -36,6 +39,8 @@ import Data.Semigroup import Control.Applicative #endif +import Data.Text.Builder.Linear.Buffer + -- $setup -- -- (Definitions for the doctests) @@ -70,9 +75,17 @@ renderLazy = TLB.toLazyText . go -- | @('renderStrict' sdoc)@ takes the output @sdoc@ from a rendering function -- and transforms it to strict text. renderStrict :: SimpleDocStream ann -> Text -renderStrict = TL.toStrict . renderLazy - - +renderStrict sdc = runBuffer (\b -> (go b sdc)) + where + go :: Buffer ⊸ SimpleDocStream ann -> Buffer + go !b !sbc = case sbc of + SFail -> undefined b + SEmpty -> b + SChar c rest -> go (b |>. c) rest + SText _l t rest -> go (b |> t) rest + SLine i rest -> go ((b |>. '\n') |>… i) rest + SAnnPush _ann rest -> go b rest + SAnnPop rest -> go b rest -- | @('renderIO' h sdoc)@ writes @sdoc@ to the file @h@. --