prettyprint-avh4-0.1.1.0: API for prettyprinting custom syntax trees (extracted from elm-format)
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.PrettyPrint.Avh4.Block

Synopsis

Line

data Line Source #

A Line is ALWAYS just one single line of text, and can always be combined horizontally with other Lines.

  • Space is a single horizontal space,
  • Blank is a line with no content.
  • Text brings any text into the data structure. (Uses Builder for the possibility of optimal performance)
  • Row joins multiple elements onto one line.

Instances

Instances details
Semigroup Line Source # 
Instance details

Defined in Text.PrettyPrint.Avh4.Block

Methods

(<>) :: Line -> Line -> Line #

sconcat :: NonEmpty Line -> Line #

stimes :: Integral b => b -> Line -> Line #

Show Line Source # 
Instance details

Defined in Text.PrettyPrint.Avh4.Block

Methods

showsPrec :: Int -> Line -> ShowS #

show :: Line -> String #

showList :: [Line] -> ShowS #

space :: Line Source #

A Line containing a single space. You **must** use this to create space characters if the spaces will ever be at the start or end of a line that is joined in the context of indentation changes.

string7 :: String -> Line Source #

Creates a Line from the given String. You must guarantee that all characters in the String are valid 7-bit ASCII characters, and that the string does not start or end with spaces (use space instead).

char7 :: Char -> Line Source #

Creates a Line from the given Char. You must guarantee that the given character is a valid 7-bit ASCII character, and is not a space character (use space instead).

stringUtf8 :: String -> Line Source #

If you know the String only contains ASCII characters, then use string7 instead for better performance.

lineFromBuilder :: Builder -> Line Source #

You must guarantee that the content of the Builder does not contain newlines and does not start with whitespace.

Block

data Block Source #

Block contains Lines (at least one; it can't be empty).

Block either:

  • can appear in the middle of a line (Stack someLine [], thus can be joined without problems), or
  • has to appear on its own (Stack someLine moreLines OR MustBreak someLine).

Types of Blocks:

  • SingleLine is a single line, and the indentation level for the line.
  • MustBreak is a single line (and its indentation level)) that cannot have anything joined to its right side. Notably, it is used for `--` comments.
  • Stack contains two or more lines, and the indentation level for each.

Sometimes (see prefix) the first line of Stack gets different treatment than the other lines.

Instances

Instances details
Show Block Source # 
Instance details

Defined in Text.PrettyPrint.Avh4.Block

Methods

showsPrec :: Int -> Block -> ShowS #

show :: Block -> String #

showList :: [Block] -> ShowS #

convert to

render :: Block -> Builder Source #

Converts a Block into a Builder.

You can then write to a file with writeFile, or convert to Text with Data.Text.Encoding.decodeUtf8 . toLazyByteString

create

blankLine :: Block Source #

A blank line (taking up one vertical space), with no text content.

line :: Line -> Block Source #

Promote a Line into a Block.

mustBreak :: Line -> Block Source #

Promote a Line into a Block that will always have a newline at the end of it, meaning that this Line will never have another Line joined to its right side.

combine

stack :: NonEmpty Block -> Block Source #

A vertical stack of Blocks. The left edges of all the Blocks will be aligned.

indent :: Block -> Block Source #

Makes a new Block with the contents of the input Block indented by one additional level.

prefix :: Word -> Line -> Block -> Block Source #

Adds the prefix to the first line, and pads the other lines with spaces of the given length. You are responsible for making sure that the given length is the actual length of the content of the given Line.

NOTE: An exceptional case that we haven't really designed for is if the first line of the input Block is indented.

EXAMPLE:

abcde
xyz
----->
myPrefix abcde
        xyz

addSuffix :: Line -> Block -> Block Source #

Adds the given suffix to then end of the last line of the Block.

rowOrStack :: Maybe Line -> NonEmpty Block -> Block Source #

This is the same as rowOrStackForce False.

rowOrStackForce :: Bool -> Maybe Line -> NonEmpty Block -> Block Source #

If all given Blocks are single-line and the Bool is False, then makes a new single-line Block, with the Maybe Line interspersed. Otherwise, makes a vertical stack of the given Blocks.

rowOrIndentForce :: Bool -> Maybe Line -> NonEmpty Block -> Block Source #

This is the same as rowOrStackForce, but all non-first lines in the resulting block are indented one additional level.

spaceSeparatedOrStack :: NonEmpty Block -> Block Source #

A convenience alias for `rowOrStack (Just space)`.

spaceSeparatedOrStackForce :: Bool -> NonEmpty Block -> Block Source #

A convenience alias for `rowOrStackForce (Just space)`.

spaceSeparatedOrIndent :: NonEmpty Block -> Block Source #

A convenience alias for `rowOrIndentForce (Just space)`.

spaceSeparatedOrIndentForce :: Bool -> NonEmpty Block -> Block Source #

A convenience alias for `rowOrIndentForce (Just space)`.

deprecated

stackForce :: Block -> Block -> Block Source #

Deprecated: Use stack instead.

A binary version of stack.

andThen :: [Block] -> Block -> Block Source #

Deprecated: Use stack instead.

An alternate style for stack.

a & andThen [b, c] is the same as stack [a, b, c]

joinMustBreak :: Block -> Block -> Block Source #

Deprecated: The normal rowOr* functions should handle this automatically now. Use `rowOrStack (Just space)` instead.