portray-prettyprinter-0.1.0: A portray backend using the prettyprinter package.
Safe HaskellNone
LanguageHaskell2010

Data.Portray.Prettyprinter

Description

Provides rendering of Portrayal to Doc.

There are two intended uses of this module: firstly, to use prettyprinter's layout and rendering algorithms to render Portray instances, Diffs, or other Portrayals; and secondly, to derive Pretty instances based on existing Portray instances. I find the former more ergonomic, but in established codebases that want to benefit from deriving, the latter may be more achievable.

The first usage is for codebases with pervasive use of Portray, and involves using e.g. pp and ppd in GHCi, or showPortrayal or showDiff in application code. With this usage, anything you want to pretty-print needs a Portray instance, and the typeclass Pretty is not involved in any way. With this approach, pretty-printable types and the types they include should derive only Portray, and pretty-printing should be done with the aforementioned utility functions:

data MyRecord = MyRecord { anInt :: Int, anotherRecord :: MyOtherRecord }
  deriving Generic
  deriving Portray via Wrapped Generic MyRecord

example = showPortrayal (MyRecord 2 ...)

The second usage is to use portray's generic deriving to provide derived Pretty instances, in a codebase that uses Pretty as the preferred typeclass for pretty-printable values. With this usage, things you want to pretty-print need Pretty instances, and Portray is needed for the transitive closure of types included in types you want to derive Pretty instances for. This may result in many types needing both instances of both Pretty (for direct pretty-printing) and Portray (for deriving Portray on downstream types) instances. Note that with this approach, types that derive their Pretty instances via Portray will ignore any custom Pretty instances of nested types, since they recurse to nested Portray instances instead.

To derive an instance for a pretty-printable type, the type itself should look like the following:

data MyRecord = MyRecord { anInt :: Int, anotherRecord :: MyOtherRecord }
  deriving Generic
  deriving Portray via Wrapped Generic MyRecord
  deriving Pretty via WrappedPortray MyRecord

example = renderStrict $ pretty (MyRecord 2 ...)

And any types transitively included in it should look like the following:

data MyOtherRecord = MyOtherRecord
  deriving Generic
  deriving Portray via Wrapped Generic MyRecord

This module also exports the underlying rendering functionality in a variety of forms for more esoteric uses.

Synopsis

Pretty-Printing

showPortrayal :: Portray a => a -> Text Source #

Pretty-print a value using its Portray instance.

pp :: Portray a => a -> IO () Source #

Pretty-print a value to stdout using its Portray instance.

Diffing

showDiff :: Diff a => a -> a -> Text Source #

Pretty-print a diff between two values using a Diff instance.

ppd :: Diff a => a -> a -> IO () Source #

Pretty-print a diff between two values to stdout using a Diff instance.

DerivingVia wrapper

newtype WrappedPortray a Source #

A newtype providing a Pretty instance via Portray, for DerivingVia.

Sadly we can't use Wrapped since it would be an orphan instance. Oh well. We'll just define a unique WrappedPortray newtype in each pretty-printer-integration package.

Constructors

WrappedPortray 

Fields

Instances

Instances details
Eq a => Eq (WrappedPortray a) Source # 
Instance details

Defined in Data.Portray.Prettyprinter

Ord a => Ord (WrappedPortray a) Source # 
Instance details

Defined in Data.Portray.Prettyprinter

Show a => Show (WrappedPortray a) Source # 
Instance details

Defined in Data.Portray.Prettyprinter

Portray a => Pretty (WrappedPortray a) Source #

Provide an instance for Pretty by way of Portray.

Instance details

Defined in Data.Portray.Prettyprinter

Methods

pretty :: WrappedPortray a -> Doc ann #

prettyList :: [WrappedPortray a] -> Doc ann #

Rendering Functions

With Associativity

type DocAssocPrec ann = Assoc -> Rational -> Doc ann Source #

A Doc that varies according to associativity and precedence context.

toDocAssocPrec :: Portrayal -> DocAssocPrec ann Source #

Render a Portrayal to a Doc with support for operator associativity.

With Precedence

portrayalToDocPrec :: Portrayal -> Rational -> Doc ann Source #

Render a Portrayal to a Doc with only operator precedence.

Convenience Functions

portrayalToDoc :: Portrayal -> Doc ann Source #

Convert a Portrayal to a Doc.

prettyShowPortrayal :: Portrayal -> Text Source #

Convenience function for rendering a Portrayal to a String.