Skip to content

Commit

Permalink
Migrate to text-builder-linear
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodigrim committed Apr 17, 2022
1 parent 2ae35fb commit dda7ed6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion prettyprinter/prettyprinter.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions prettyprinter/src/Prettyprinter/Render/Text.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE UnicodeSyntax #-}
{-# LANGUAGE BangPatterns #-}

#include "version-compatibility-macros.h"

Expand Down Expand Up @@ -36,6 +39,8 @@ import Data.Semigroup
import Control.Applicative
#endif

import Data.Text.Builder.Linear.Buffer

-- $setup
--
-- (Definitions for the doctests)
Expand Down Expand Up @@ -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@.
--
Expand Down

0 comments on commit dda7ed6

Please sign in to comment.