Safe Haskell | None |
---|---|
Language | Haskell2010 |
Printing combinators. The definitions here are presented in such an order so you can just go through the Haddocks and by the end of the file you should have a pretty good idea how to program rendering logic.
Synopsis
- data R a
- runR :: R () -> SpanStream -> CommentStream -> Anns -> Bool -> Text
- getAnns :: SrcSpan -> R [AnnKeywordId]
- getEnclosingSpan :: (RealSrcSpan -> Bool) -> R (Maybe RealSrcSpan)
- txt :: Text -> R ()
- atom :: Outputable a => a -> R ()
- space :: R ()
- newline :: R ()
- inci :: R () -> R ()
- inciIf :: Bool -> R () -> R ()
- located :: Located a -> (a -> R ()) -> R ()
- located' :: (a -> R ()) -> Located a -> R ()
- switchLayout :: [SrcSpan] -> R () -> R ()
- data Layout
- vlayout :: R a -> R a -> R a
- getLayout :: R Layout
- breakpoint :: R ()
- breakpoint' :: R ()
- sep :: R () -> (a -> R ()) -> [a] -> R ()
- sepSemi :: (a -> R ()) -> [a] -> R ()
- canUseBraces :: R Bool
- useBraces :: R () -> R ()
- dontUseBraces :: R () -> R ()
- data BracketStyle
- sitcc :: R () -> R ()
- backticks :: R () -> R ()
- banana :: R () -> R ()
- braces :: BracketStyle -> R () -> R ()
- recordDotBraces :: R () -> R ()
- brackets :: BracketStyle -> R () -> R ()
- parens :: BracketStyle -> R () -> R ()
- parensHash :: BracketStyle -> R () -> R ()
- pragmaBraces :: R () -> R ()
- pragma :: Text -> R () -> R ()
- comma :: R ()
- commaDel :: R ()
- equals :: R ()
- data SpanMark
- spanMarkSpan :: SpanMark -> RealSrcSpan
- data HaddockStyle
- setSpanMark :: SpanMark -> R ()
- getSpanMark :: R (Maybe SpanMark)
The R
monad
The R
monad hosts combinators that allow us to describe how to render
AST.
:: R () | Monad to run |
-> SpanStream | Span stream |
-> CommentStream | Comment stream |
-> Anns | Annotations |
-> Bool | Use Record Dot Syntax |
-> Text | Resulting rendition |
Run an R
monad.
getAnns :: SrcSpan -> R [AnnKeywordId] Source #
For a given span return AnnKeywordId
s associated with it.
:: (RealSrcSpan -> Bool) | Predicate to use |
-> R (Maybe RealSrcSpan) |
Get the first enclosing RealSrcSpan
that satisfies given predicate.
Combinators
Basic
Output a fixed Text
fragment. The argument may not contain any line
breaks. txt
is used to output all sorts of “fixed” bits of syntax like
keywords and pipes |
in functional dependencies.
To separate various bits of syntax with white space use space
instead
of
. To output txt
" "Outputable
Haskell entities like numbers use
atom
.
atom :: Outputable a => a -> R () Source #
Output Outputable
fragment of AST. This can be used to output numeric
literals and similar. Everything that doesn't have inner structure but
does have an Outputable
instance.
This primitive does not necessarily output a space. It just ensures that the next thing that will be printed on the same line will be separated by a single space from the previous output. Using this combinator twice results in at most one space.
In practice this design prevents trailing white space and makes it hard to output more than one delimiting space in a row, which is what we usually want.
Output a newline. First time newline
is used after some non-newline
output it gets inserted immediately. Second use of newline
does not
output anything but makes sure that the next non-white space output will
be prefixed by a newline. Using newline
more than twice in a row has no
effect. Also, using newline
at the very beginning has no effect, this
is to avoid leading whitespace.
Similarly to space
, this design prevents trailing newlines and makes it
hard to output more than one blank newline in a row.
Increase indentation level by one indentation step for the inner
computation. inci
should be used when a part of code must be more
indented relative to the parts outside of inci
in order for the output
to be valid Haskell. When layout is single-line there is no obvious
effect, but with multi-line layout correct indentation levels matter.
Indent the inner expression if the first argument is True
.
A version of located
with arguments flipped.
Set layout according to combination of given SrcSpan
s for a given.
Use this only when you need to set layout based on e.g. combined span of
several elements when there is no corresponding Located
wrapper
provided by GHC AST. It is relatively rare that this one is needed.
Given empty list this function will set layout to single line.
Layout
options.
SingleLine | Put everything on single line |
MultiLine | Use multiple lines |
Do one or another thing depending on current Layout
.
breakpoint :: R () Source #
Insert a space if enclosing layout is single-line, or newline if it's multiline.
breakpoint = vlayout space newline
breakpoint' :: R () Source #
Similar to breakpoint
but outputs nothing in case of single-line
layout.
breakpoint' = vlayout (return ()) newline
Formatting lists
Render a collection of elements inserting a separator between them.
Render a collection of elements layout-sensitively using given printer,
inserting semicolons if necessary and respecting useBraces
and
dontUseBraces
combinators.
useBraces $ sepSemi txt ["foo", "bar"] == vlayout (txt "{ foo; bar }") (txt "foo\nbar")
dontUseBraces $ sepSemi txt ["foo", "bar"] == vlayout (txt "foo; bar") (txt "foo\nbar")
dontUseBraces :: R () -> R () Source #
Make the inner computation omit braces around single-line layouts.
Wrapping
data BracketStyle Source #
BracketStyle
controlling how closing bracket is rendered.
Instances
Eq BracketStyle Source # | |
Defined in Ormolu.Printer.Combinators (==) :: BracketStyle -> BracketStyle -> Bool # (/=) :: BracketStyle -> BracketStyle -> Bool # | |
Show BracketStyle Source # | |
Defined in Ormolu.Printer.Combinators showsPrec :: Int -> BracketStyle -> ShowS # show :: BracketStyle -> String # showList :: [BracketStyle] -> ShowS # |
sitcc :: R () -> R () Source #
Set indentation level for the inner computation equal to current column. This makes sure that the entire inner block is uniformly "shifted" to the right.
banana :: R () -> R () Source #
Surround given entity by banana brackets (i.e., from arrow notation.)
recordDotBraces :: R () -> R () Source #
Surround record update fields which use RecordDot plugin entity by
curly braces {
and }
.
Since: 0.1.3.1
parensHash :: BracketStyle -> R () -> R () Source #
Surround given entity by (#
and #)
.
pragmaBraces :: R () -> R () Source #
Braces as used for pragmas: {-#
and #-}
.
Surround the body with a pragma name and pragmaBraces
.
Literals
Stateful markers
An auxiliary marker for keeping track of last output element.
HaddockSpan HaddockStyle RealSrcSpan | Haddock comment |
CommentSpan RealSrcSpan | Non-haddock comment |
StatementSpan RealSrcSpan | A statement in a do-block and such span |
spanMarkSpan :: SpanMark -> RealSrcSpan Source #
Project RealSrcSpan
from SpanMark
.