Skip to content

Commit

Permalink
Add PPShow
Browse files Browse the repository at this point in the history
  • Loading branch information
sergv committed May 20, 2023
1 parent 87de3ce commit bf22491
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 0.1.2

- Add `PPGeneric` newtype to apply `ppGeneric` through `DerivingVia`
- Add `PPShow` newtype to apply `ppShow` through `DerivingVia`

# 0.1.1.1
- `ppVector` and `ppVectorWith` now accept generic vectors
Expand Down
29 changes: 28 additions & 1 deletion src/Prettyprinter/Show.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Prettyprinter.Show (ppShow) where
module Prettyprinter.Show
( ppShow
, PPShow(..)
) where

import Data.Text qualified as T
import Prettyprinter
Expand All @@ -19,6 +22,30 @@ import Prettyprinter.Combinators
import Prettyprinter.MetaDoc
import Text.Show.Pretty (parseValue, Value(..))

-- $setup
-- >>> :set -XDerivingVia
-- >>> import Data.IntMap (IntMap)
-- >>> import Data.Map.Strict (Map)
-- >>> import Data.Set (Set)

-- | Helper to use 'Show'-based prettyprinting with DerivingVia.
--
-- >>> :{
-- data TestWithDeriving a b = TestWithDeriving
-- { testSet :: Maybe (Set a)
-- , testB :: b
-- , testIntMap :: IntMap String
-- , testComplexMap :: Map (Maybe (Set Int)) (IntMap (Set String))
-- }
-- deriving (Show)
-- deriving Pretty via PPShow (TestWithDeriving a b)
-- :}
--
newtype PPShow a = PPShow { unPPShow :: a }

instance Show a => Pretty (PPShow a) where
pretty = ppShow . unPPShow

ppShow :: Show a => a -> Doc ann
ppShow x =
case parseValue y of
Expand Down

0 comments on commit bf22491

Please sign in to comment.