From 702ce77376dddbbb09e5b18de9be28634180db94 Mon Sep 17 00:00:00 2001 From: Simon Jakobi Date: Wed, 20 Nov 2019 17:43:36 +0100 Subject: [PATCH 1/6] diag v0 --- prettyprinter/prettyprinter.cabal | 1 + .../src/Data/Text/Prettyprint/Doc/Internal.hs | 4 ++ .../Text/Prettyprint/Doc/Internal.hs-boot | 20 +++++++ .../Prettyprint/Doc/Internal/Diagnostic.hs | 52 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot create mode 100644 prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs diff --git a/prettyprinter/prettyprinter.cabal b/prettyprinter/prettyprinter.cabal index 4f83971d..ffba4310 100644 --- a/prettyprinter/prettyprinter.cabal +++ b/prettyprinter/prettyprinter.cabal @@ -27,6 +27,7 @@ library exposed-modules: Data.Text.Prettyprint.Doc , Data.Text.Prettyprint.Doc.Internal + , Data.Text.Prettyprint.Doc.Internal.Diagnostic , Data.Text.Prettyprint.Doc.Internal.Type , Data.Text.Prettyprint.Doc.Render.String , Data.Text.Prettyprint.Doc.Render.Text diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 9c966c25..d1331704 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -17,6 +17,9 @@ -- "Data.Text.Prettyprint.Doc.Internal.Type". module Data.Text.Prettyprint.Doc.Internal ( module Data.Text.Prettyprint.Doc.Internal + + -- * Debugging + , diag ) where @@ -56,6 +59,7 @@ import Data.Monoid hiding ((<>)) import Data.Functor.Identity #endif +import Data.Text.Prettyprint.Doc.Internal.Diagnostic (diag) import Data.Text.Prettyprint.Doc.Render.Util.Panic diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot new file mode 100644 index 00000000..22d1633c --- /dev/null +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot @@ -0,0 +1,20 @@ +module Data.Text.Prettyprint.Doc.Internal where + +import Data.Text (Text) + +data Doc ann = + Fail + | Empty + | Char !Char + | Text !Int !Text + | Line + | FlatAlt (Doc ann) (Doc ann) + | Cat (Doc ann) (Doc ann) + | Nest !Int (Doc ann) + | Union (Doc ann) (Doc ann) + | Column (Int -> Doc ann) + | WithPageWidth (PageWidth -> Doc ann) + | Nesting (Int -> Doc ann) + | Annotated ann (Doc ann) + +data PageWidth diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs new file mode 100644 index 00000000..a93eca02 --- /dev/null +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs @@ -0,0 +1,52 @@ +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE StandaloneDeriving #-} +{-# LANGUAGE UndecidableInstances #-} +module Data.Text.Prettyprint.Doc.Internal.Diagnostic + ( diag + , diagS + ) where + +import Data.Text (Text) +import {-# SOURCE #-} Data.Text.Prettyprint.Doc.Internal (Doc, PageWidth) + +import {-# SOURCE #-} qualified Data.Text.Prettyprint.Doc.Internal as Doc + +-- | A copy of the 'Doc' type, used to provide a derived 'Show' instance. +data Diag ann = + Fail + | Empty + | Char !Char + | Text !Int !Text + | Line + | FlatAlt (Diag ann) (Diag ann) + | Cat (Diag ann) (Diag ann) + | Nest !Int (Diag ann) + | Union (Diag ann) (Diag ann) + | Column (Int -> Diag ann) + | WithPageWidth (PageWidth -> Diag ann) + | Nesting (Int -> Diag ann) + | Annotated ann (Diag ann) + +deriving instance (Show ann, Show (Int -> Diag ann), Show (PageWidth -> Diag ann)) => Show (Diag ann) + +docToDiag :: Doc ann -> Diag ann +docToDiag doc = case doc of + Doc.Fail -> Fail + Doc.Empty -> Empty + Doc.Char c -> Char c + Doc.Text l t -> Text l t + Doc.Line -> Line + Doc.FlatAlt a b -> FlatAlt (docToDiag a) (docToDiag b) + Doc.Cat a b -> Cat (docToDiag a) (docToDiag b) + Doc.Nest i d -> Nest i (docToDiag d) + Doc.Union a b -> Union (docToDiag a) (docToDiag b) + Doc.Column f -> Column (docToDiag . f) + Doc.WithPageWidth f -> WithPageWidth (docToDiag . f) + Doc.Nesting f -> Nesting (docToDiag . f) + Doc.Annotated ann d -> Annotated ann (docToDiag d) + +diagS :: (Show ann, Show (Int -> Diag ann), Show (PageWidth -> Diag ann)) => Doc ann -> String +diagS = show . docToDiag + +diag :: (Show ann, Show (Int -> Diag ann), Show (PageWidth -> Diag ann)) => Doc ann -> IO () +diag = putStrLn . diagS From 01fa39dd9fa4483c6405e3a8942b4bbe69dca825 Mon Sep 17 00:00:00 2001 From: Simon Jakobi Date: Wed, 20 Nov 2019 19:35:45 +0100 Subject: [PATCH 2/6] v0.1 --- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 70 +++++++++++++++++-- .../Text/Prettyprint/Doc/Internal.hs-boot | 19 +---- .../Prettyprint/Doc/Internal/Diagnostic.hs | 48 +++---------- 3 files changed, 77 insertions(+), 60 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index d1331704..31014b01 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -17,9 +17,6 @@ -- "Data.Text.Prettyprint.Doc.Internal.Type". module Data.Text.Prettyprint.Doc.Internal ( module Data.Text.Prettyprint.Doc.Internal - - -- * Debugging - , diag ) where @@ -59,7 +56,8 @@ import Data.Monoid hiding ((<>)) import Data.Functor.Identity #endif -import Data.Text.Prettyprint.Doc.Internal.Diagnostic (diag) +import Data.Text.Prettyprint.Doc.Internal.Diagnostic (Diag) +import qualified Data.Text.Prettyprint.Doc.Internal.Diagnostic as Diag import Data.Text.Prettyprint.Doc.Render.Util.Panic @@ -1628,6 +1626,9 @@ data PageWidth deriving (Eq, Ord, Show, Typeable) +defaultPageWidth :: PageWidth +defaultPageWidth = AvailablePerLine 80 1 + -- $ Test to avoid surprising behaviour -- >>> Unbounded > AvailablePerLine maxBound 1 -- True @@ -1643,7 +1644,7 @@ newtype LayoutOptions = LayoutOptions { layoutPageWidth :: PageWidth } -- >>> defaultLayoutOptions -- LayoutOptions {layoutPageWidth = AvailablePerLine 80 1.0} defaultLayoutOptions :: LayoutOptions -defaultLayoutOptions = LayoutOptions { layoutPageWidth = AvailablePerLine 80 1 } +defaultLayoutOptions = LayoutOptions { layoutPageWidth = defaultPageWidth } -- | This is the default layout algorithm, and it is used by 'show', 'putDoc' -- and 'hPutDoc'. @@ -1886,6 +1887,65 @@ renderShowS = \sds -> case sds of SAnnPush _ x -> renderShowS x SAnnPop x -> renderShowS x +-- * Debugging +-- +-- Use the @pretty-simple@ package to get a nicer layout for 'show'n +-- 'Diag's +-- +-- >>> Text.Pretty.Simple.pPrintNoColor . diag $ align (vcat ["foo", "bar"]) +-- Column +-- [ +-- ( 10 +-- , Nesting +-- [ +-- ( 10 +-- , Cat ( Text 3 "foo" ) +-- ( Cat ( FlatAlt Line Empty ) ( Text 3 "bar" ) ) +-- ) +-- ] +-- ) +-- ] + +-- | Convert a 'Doc' to its diagnostic representation. +-- +-- The functions in the 'Column', 'WithPageWidth' and 'Nesting' constructors are +-- sampled with some default values. +-- +-- Use `diag'` to control the function inputs yourself. +-- +-- >>> diag $ align (vcat ["foo", "bar"]) +-- Column [(10,Nesting [(10,Cat (Text 3 "foo") (Cat (FlatAlt Line Empty) (Text 3 "bar")))])] +diag :: Doc ann -> Diag ann +diag = diag' [10] [defaultPageWidth] [10] + +diag' + :: [Int] + -- ^ Cursor positions for the 'Column' constructor + -> [PageWidth] + -- ^ For 'WithPageWidth' + -> [Int] + -- ^ Nesting levels for 'Nesting' + -> Doc ann + -> Diag ann +diag' columns pageWidths nestings = go + where + go doc = case doc of + Fail -> Diag.Fail + Empty -> Diag.Empty + Char c -> Diag.Char c + Text l t -> Diag.Text l t + Line -> Diag.Line + FlatAlt a b -> Diag.FlatAlt (go a) (go b) + Cat a b -> Diag.Cat (go a) (go b) + Nest i d -> Diag.Nest i (go d) + Union a b -> Diag.Union (go a) (go b) + Column f -> Diag.Column (apply f columns) + WithPageWidth f -> Diag.WithPageWidth (apply f pageWidths) + Nesting f -> Diag.Nesting (apply f nestings) + Annotated ann d -> Diag.Annotated ann (go d) + + apply :: (a -> Doc ann) -> [a] -> [(a, Diag ann)] + apply f = map (\x -> (x, go (f x))) -- $setup diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot index 22d1633c..5ab82870 100644 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot @@ -1,20 +1,5 @@ module Data.Text.Prettyprint.Doc.Internal where -import Data.Text (Text) - -data Doc ann = - Fail - | Empty - | Char !Char - | Text !Int !Text - | Line - | FlatAlt (Doc ann) (Doc ann) - | Cat (Doc ann) (Doc ann) - | Nest !Int (Doc ann) - | Union (Doc ann) (Doc ann) - | Column (Int -> Doc ann) - | WithPageWidth (PageWidth -> Doc ann) - | Nesting (Int -> Doc ann) - | Annotated ann (Doc ann) - data PageWidth + +instance Show PageWidth diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs index a93eca02..a229d5ab 100644 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs @@ -1,17 +1,9 @@ -{-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE StandaloneDeriving #-} -{-# LANGUAGE UndecidableInstances #-} -module Data.Text.Prettyprint.Doc.Internal.Diagnostic - ( diag - , diagS - ) where +module Data.Text.Prettyprint.Doc.Internal.Diagnostic where import Data.Text (Text) -import {-# SOURCE #-} Data.Text.Prettyprint.Doc.Internal (Doc, PageWidth) +import {-# SOURCE #-} Data.Text.Prettyprint.Doc.Internal (PageWidth) -import {-# SOURCE #-} qualified Data.Text.Prettyprint.Doc.Internal as Doc - --- | A copy of the 'Doc' type, used to provide a derived 'Show' instance. +-- | A variant of 'Doc' for debugging. data Diag ann = Fail | Empty @@ -22,31 +14,11 @@ data Diag ann = | Cat (Diag ann) (Diag ann) | Nest !Int (Diag ann) | Union (Diag ann) (Diag ann) - | Column (Int -> Diag ann) - | WithPageWidth (PageWidth -> Diag ann) - | Nesting (Int -> Diag ann) + | Column [(Int, Diag ann)] + -- ^ 'Doc': @(Int -> Diag ann)@ + | WithPageWidth [(PageWidth, Diag ann)] + -- ^ 'Doc': @(PageWidth -> Diag ann)@ + | Nesting [(Int, Diag ann)] + -- ^ 'Doc': @(Int -> Diag ann)@ | Annotated ann (Diag ann) - -deriving instance (Show ann, Show (Int -> Diag ann), Show (PageWidth -> Diag ann)) => Show (Diag ann) - -docToDiag :: Doc ann -> Diag ann -docToDiag doc = case doc of - Doc.Fail -> Fail - Doc.Empty -> Empty - Doc.Char c -> Char c - Doc.Text l t -> Text l t - Doc.Line -> Line - Doc.FlatAlt a b -> FlatAlt (docToDiag a) (docToDiag b) - Doc.Cat a b -> Cat (docToDiag a) (docToDiag b) - Doc.Nest i d -> Nest i (docToDiag d) - Doc.Union a b -> Union (docToDiag a) (docToDiag b) - Doc.Column f -> Column (docToDiag . f) - Doc.WithPageWidth f -> WithPageWidth (docToDiag . f) - Doc.Nesting f -> Nesting (docToDiag . f) - Doc.Annotated ann d -> Annotated ann (docToDiag d) - -diagS :: (Show ann, Show (Int -> Diag ann), Show (PageWidth -> Diag ann)) => Doc ann -> String -diagS = show . docToDiag - -diag :: (Show ann, Show (Int -> Diag ann), Show (PageWidth -> Diag ann)) => Doc ann -> IO () -diag = putStrLn . diagS + deriving Show From 57c90af4e730c6f560df09cfffa320ebbb487bd6 Mon Sep 17 00:00:00 2001 From: Simon Jakobi Date: Sun, 12 Jan 2020 21:01:46 +0100 Subject: [PATCH 3/6] Fix haddocks --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index 31014b01..fca62286 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -15,9 +15,7 @@ -- For a stable API, use the non-internal modules. For the special case of -- writing adaptors to this library’s @'Doc'@ type, see -- "Data.Text.Prettyprint.Doc.Internal.Type". -module Data.Text.Prettyprint.Doc.Internal ( - module Data.Text.Prettyprint.Doc.Internal -) where +module Data.Text.Prettyprint.Doc.Internal where @@ -1889,8 +1887,10 @@ renderShowS = \sds -> case sds of -- * Debugging -- +-- $standalone-text +-- -- Use the @pretty-simple@ package to get a nicer layout for 'show'n --- 'Diag's +-- 'Diag's: -- -- >>> Text.Pretty.Simple.pPrintNoColor . diag $ align (vcat ["foo", "bar"]) -- Column From 00d977902fba40c0bd0c1e6de1d0d1de45ccc002 Mon Sep 17 00:00:00 2001 From: Simon Jakobi Date: Tue, 14 Jan 2020 19:46:02 +0100 Subject: [PATCH 4/6] Move things to Internal.Debug --- prettyprinter/prettyprinter.cabal | 2 +- .../src/Data/Text/Prettyprint/Doc/Internal.hs | 64 ------------- .../Text/Prettyprint/Doc/Internal.hs-boot | 5 - .../Text/Prettyprint/Doc/Internal/Debug.hs | 96 +++++++++++++++++++ .../Prettyprint/Doc/Internal/Diagnostic.hs | 24 ----- 5 files changed, 97 insertions(+), 94 deletions(-) delete mode 100644 prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot create mode 100644 prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs delete mode 100644 prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs diff --git a/prettyprinter/prettyprinter.cabal b/prettyprinter/prettyprinter.cabal index ffba4310..609c8441 100644 --- a/prettyprinter/prettyprinter.cabal +++ b/prettyprinter/prettyprinter.cabal @@ -27,7 +27,7 @@ library exposed-modules: Data.Text.Prettyprint.Doc , Data.Text.Prettyprint.Doc.Internal - , Data.Text.Prettyprint.Doc.Internal.Diagnostic + , Data.Text.Prettyprint.Doc.Internal.Debug , Data.Text.Prettyprint.Doc.Internal.Type , Data.Text.Prettyprint.Doc.Render.String , Data.Text.Prettyprint.Doc.Render.Text diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs index fca62286..f3910f1f 100755 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs @@ -54,8 +54,6 @@ import Data.Monoid hiding ((<>)) import Data.Functor.Identity #endif -import Data.Text.Prettyprint.Doc.Internal.Diagnostic (Diag) -import qualified Data.Text.Prettyprint.Doc.Internal.Diagnostic as Diag import Data.Text.Prettyprint.Doc.Render.Util.Panic @@ -1885,68 +1883,6 @@ renderShowS = \sds -> case sds of SAnnPush _ x -> renderShowS x SAnnPop x -> renderShowS x --- * Debugging --- --- $standalone-text --- --- Use the @pretty-simple@ package to get a nicer layout for 'show'n --- 'Diag's: --- --- >>> Text.Pretty.Simple.pPrintNoColor . diag $ align (vcat ["foo", "bar"]) --- Column --- [ --- ( 10 --- , Nesting --- [ --- ( 10 --- , Cat ( Text 3 "foo" ) --- ( Cat ( FlatAlt Line Empty ) ( Text 3 "bar" ) ) --- ) --- ] --- ) --- ] - --- | Convert a 'Doc' to its diagnostic representation. --- --- The functions in the 'Column', 'WithPageWidth' and 'Nesting' constructors are --- sampled with some default values. --- --- Use `diag'` to control the function inputs yourself. --- --- >>> diag $ align (vcat ["foo", "bar"]) --- Column [(10,Nesting [(10,Cat (Text 3 "foo") (Cat (FlatAlt Line Empty) (Text 3 "bar")))])] -diag :: Doc ann -> Diag ann -diag = diag' [10] [defaultPageWidth] [10] - -diag' - :: [Int] - -- ^ Cursor positions for the 'Column' constructor - -> [PageWidth] - -- ^ For 'WithPageWidth' - -> [Int] - -- ^ Nesting levels for 'Nesting' - -> Doc ann - -> Diag ann -diag' columns pageWidths nestings = go - where - go doc = case doc of - Fail -> Diag.Fail - Empty -> Diag.Empty - Char c -> Diag.Char c - Text l t -> Diag.Text l t - Line -> Diag.Line - FlatAlt a b -> Diag.FlatAlt (go a) (go b) - Cat a b -> Diag.Cat (go a) (go b) - Nest i d -> Diag.Nest i (go d) - Union a b -> Diag.Union (go a) (go b) - Column f -> Diag.Column (apply f columns) - WithPageWidth f -> Diag.WithPageWidth (apply f pageWidths) - Nesting f -> Diag.Nesting (apply f nestings) - Annotated ann d -> Diag.Annotated ann (go d) - - apply :: (a -> Doc ann) -> [a] -> [(a, Diag ann)] - apply f = map (\x -> (x, go (f x))) - -- $setup -- diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot deleted file mode 100644 index 5ab82870..00000000 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs-boot +++ /dev/null @@ -1,5 +0,0 @@ -module Data.Text.Prettyprint.Doc.Internal where - -data PageWidth - -instance Show PageWidth diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs new file mode 100644 index 00000000..7c726cd2 --- /dev/null +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs @@ -0,0 +1,96 @@ +-- | __Warning: internal module!__ This means that the API may change +-- arbitrarily between versions without notice. Depending on this module may +-- lead to unexpected breakages, so proceed with caution! + +module Data.Text.Prettyprint.Doc.Internal.Debug where + +import Data.Text (Text) +import Data.Text.Prettyprint.Doc.Internal (PageWidth, Doc) +import qualified Data.Text.Prettyprint.Doc.Internal as Doc + +-- | A variant of 'Doc' for debugging. +-- +-- Unlike in the 'Doc' type, the 'Column', 'WithPageWidth' and 'Nesting' +-- constructors don't contain functions but are \"sampled\" to allow +-- simple inspection with 'show'. +data Diag ann = + Fail + | Empty + | Char !Char + | Text !Int !Text + | Line + | FlatAlt (Diag ann) (Diag ann) + | Cat (Diag ann) (Diag ann) + | Nest !Int (Diag ann) + | Union (Diag ann) (Diag ann) + | Column [(Int, Diag ann)] + -- ^ 'Doc': @(Int -> Diag ann)@ + | WithPageWidth [(PageWidth, Diag ann)] + -- ^ 'Doc': @(PageWidth -> Diag ann)@ + | Nesting [(Int, Diag ann)] + -- ^ 'Doc': @(Int -> Diag ann)@ + | Annotated ann (Diag ann) + deriving Show + +-- * Debugging +-- +-- $standalone-text +-- +-- Use the @pretty-simple@ package to get a nicer layout for 'show'n +-- 'Diag's: +-- +-- >>> Text.Pretty.Simple.pPrintNoColor . diag $ align (vcat ["foo", "bar"]) +-- Column +-- [ +-- ( 10 +-- , Nesting +-- [ +-- ( 10 +-- , Cat ( Text 3 "foo" ) +-- ( Cat ( FlatAlt Line Empty ) ( Text 3 "bar" ) ) +-- ) +-- ] +-- ) +-- ] + +-- | Convert a 'Doc' to its diagnostic representation. +-- +-- The functions in the 'Column', 'WithPageWidth' and 'Nesting' constructors are +-- sampled with some default values. +-- +-- Use `diag'` to control the function inputs yourself. +-- +-- >>> diag $ align (vcat ["foo", "bar"]) +-- Column [(10,Nesting [(10,Cat (Text 3 "foo") (Cat (FlatAlt Line Empty) (Text 3 "bar")))])] +diag :: Doc ann -> Diag ann +diag = diag' [10] [Doc.defaultPageWidth] [10] + +diag' + :: [Int] + -- ^ Cursor positions for the 'Column' constructor + -> [PageWidth] + -- ^ For 'WithPageWidth' + -> [Int] + -- ^ Nesting levels for 'Nesting' + -> Doc ann + -> Diag ann +diag' columns pageWidths nestings = go + where + go doc = case doc of + Doc.Fail -> Fail + Doc.Empty -> Empty + Doc.Char c -> Char c + Doc.Text l t -> Text l t + Doc.Line -> Line + Doc.FlatAlt a b -> FlatAlt (go a) (go b) + Doc.Cat a b -> Cat (go a) (go b) + Doc.Nest i d -> Nest i (go d) + Doc.Union a b -> Union (go a) (go b) + Doc.Column f -> Column (apply f columns) + Doc.WithPageWidth f -> WithPageWidth (apply f pageWidths) + Doc.Nesting f -> Nesting (apply f nestings) + Doc.Annotated ann d -> Annotated ann (go d) + + apply :: (a -> Doc ann) -> [a] -> [(a, Diag ann)] + apply f = map (\x -> (x, go (f x))) + diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs deleted file mode 100644 index a229d5ab..00000000 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Diagnostic.hs +++ /dev/null @@ -1,24 +0,0 @@ -module Data.Text.Prettyprint.Doc.Internal.Diagnostic where - -import Data.Text (Text) -import {-# SOURCE #-} Data.Text.Prettyprint.Doc.Internal (PageWidth) - --- | A variant of 'Doc' for debugging. -data Diag ann = - Fail - | Empty - | Char !Char - | Text !Int !Text - | Line - | FlatAlt (Diag ann) (Diag ann) - | Cat (Diag ann) (Diag ann) - | Nest !Int (Diag ann) - | Union (Diag ann) (Diag ann) - | Column [(Int, Diag ann)] - -- ^ 'Doc': @(Int -> Diag ann)@ - | WithPageWidth [(PageWidth, Diag ann)] - -- ^ 'Doc': @(PageWidth -> Diag ann)@ - | Nesting [(Int, Diag ann)] - -- ^ 'Doc': @(Int -> Diag ann)@ - | Annotated ann (Diag ann) - deriving Show From 836031bcec33f6a0b681065e0f20c2a73f91b7da Mon Sep 17 00:00:00 2001 From: Simon Jakobi Date: Tue, 14 Jan 2020 19:46:40 +0100 Subject: [PATCH 5/6] Haddock bit --- prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs index 7c726cd2..98a14ed8 100644 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs @@ -1,6 +1,8 @@ -- | __Warning: internal module!__ This means that the API may change -- arbitrarily between versions without notice. Depending on this module may -- lead to unexpected breakages, so proceed with caution! +-- +-- This module provides debugging helpers for inspecting 'Doc's. module Data.Text.Prettyprint.Doc.Internal.Debug where From 7ad4522ac077c28d8c5eb0efe7a65e2f8065951e Mon Sep 17 00:00:00 2001 From: Simon Jakobi Date: Sun, 19 Jan 2020 22:26:34 +0100 Subject: [PATCH 6/6] Move around haddocks --- .../Text/Prettyprint/Doc/Internal/Debug.hs | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs index 98a14ed8..c6f0bc79 100644 --- a/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs +++ b/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal/Debug.hs @@ -3,6 +3,24 @@ -- lead to unexpected breakages, so proceed with caution! -- -- This module provides debugging helpers for inspecting 'Doc's. +-- +-- Use the @pretty-simple@ package to get a nicer layout for 'show'n +-- 'Diag's: +-- +-- >>> Text.Pretty.Simple.pPrintNoColor . diag $ align (vcat ["foo", "bar"]) +-- Column +-- [ +-- ( 10 +-- , Nesting +-- [ +-- ( 10 +-- , Cat ( Text 3 "foo" ) +-- ( Cat ( FlatAlt Line Empty ) ( Text 3 "bar" ) ) +-- ) +-- ] +-- ) +-- ] + module Data.Text.Prettyprint.Doc.Internal.Debug where @@ -34,27 +52,6 @@ data Diag ann = | Annotated ann (Diag ann) deriving Show --- * Debugging --- --- $standalone-text --- --- Use the @pretty-simple@ package to get a nicer layout for 'show'n --- 'Diag's: --- --- >>> Text.Pretty.Simple.pPrintNoColor . diag $ align (vcat ["foo", "bar"]) --- Column --- [ --- ( 10 --- , Nesting --- [ --- ( 10 --- , Cat ( Text 3 "foo" ) --- ( Cat ( FlatAlt Line Empty ) ( Text 3 "bar" ) ) --- ) --- ] --- ) --- ] - -- | Convert a 'Doc' to its diagnostic representation. -- -- The functions in the 'Column', 'WithPageWidth' and 'Nesting' constructors are