Skip to content

polytypic/prettier-printer

Repository files navigation

A pretty printing library for text documents that can be rendered to a desired maximum width. Basic features:

As an example, the evaluation output in this live CodeSandbox example is formatted using this library.

npm version Bower version Build Status Code Coverage

To be done.

In the meanwhile, read Philip Wadler's paper A prettier printer.

Typically one imports the library as:

import * as PP from 'prettier-printer'

The examples also utilize Ramda, bound as R.

PP.render renders the document to a string trying to keep the width of the document within the specified maximum. A width of 0 means that there is no maximum. See also PP.renderWith.

For example:

PP.render(
  10,
  PP.indent('-- ', PP.group(PP.intersperse(PP.line, ['Hello,', 'world!'])))
)
// -- Hello,
// -- world!

PP.renderWith renders the document with the given actions text and line. You can use this function to output the document without creating an intermediate string of the whole document. See also PP.render.

Any string that doesn't contain '\n' or '\r' characters is considered as an atomic document. For example, '' is an empty document and ' ' is a space.

PP.line renders as a new line unless undone by PP.group in which case PP.line renders as a space.

For example:

PP.render(20, ['Hello,', PP.line, 'world!'])
// Hello,
// world!
PP.render(20, PP.group(['Hello,', PP.line, 'world!']))
// Hello, world!

PP.lineBreak renders as a new line unless undone by PP.group in which case PP.lineBreak renders as empty.

For example:

PP.render(20, ['Lol', PP.lineBreak, 'Bal'])
// Lol
// Bal
PP.render(20, PP.group(['Lol', PP.lineBreak, 'Bal']))
// LolBal

PP.softLine renders as a space if the output fits and otherwise as a new line.

For example:

PP.render(
  20,
  PP.intersperse(
    PP.softLine,
    R.split(
      /\s+/,
      'Here is a paragraph of text that we will format to a desired width.'
    )
  )
)
// Here is a paragraph
// of text that we will
// format to a desired
// width.

PP.softBreak renders as empty if the output fits and otherwise as a new line.

For example:

PP.render(10, PP.intersperse(PP.softBreak, R.split(/\b/, 'this.method(rocks)')))
// this.
// method(
// rocks)

An array of documents is considered as a concatenation of documents. For example, [] is an empty document and ['foo', 'bar'] is equivalent to 'foobar'.

PP.append reverse concatenates the documents.

For example:

PP.render(0, PP.append('bar', 'foo'))
// foobar

PP.prepend concatenates the documents.

For example:

PP.render(0, PP.prepend('foo', 'bar'))
// foobar

PP.intersperse puts the given separator document between each document in the given list of documents.

For example:

PP.intersperse(',', ['a', 'b', 'c'])
// ['a', ',', 'b', ',', 'c']

PP.punctuate concatenates the given separator after each document in the given list of documents except the last.

For example:

PP.punctuate(',', ['a', 'b', 'c'])
// [ [ 'a', ',' ], [ 'b', ',' ], 'c' ]

PP.lazy creates a lazy document. The given thunk is only invoked as needed to compute the document.

PP.enclose encloses the given document between the given pair of documents.

For example:

PP.render(0, PP.enclose(PP.parens, 'foo'))
// (foo)

PP.choice(wideDoc, narrowDoc) renders as the given wideDoc on a line if it fits within the maximum width and otherwise as the narrowDoc. PP.lines and PP.lineBreaks within the wideDoc are undone like with PP.group.

For example:

PP.render(5, PP.choice('wide', 'narrow'))
// 'wide'
PP.render(3, PP.choice('wide', 'narrow'))
// 'narrow'

Note that usually the idea is that the narrow version can indeed be rendered more narrowly.

For example:

const hyphen = PP.choice('', ['-', PP.lineBreak])

PP.render(5, PP.intersperse(hyphen, ['hy', 'phen', 'at', 'ed']))
// hy-
// phen-
// ated

PP.group allows PP.lines and PP.lineBreaks within the given document to be undone if the result fits within the maximum width. PP.group(doc) is equivalent to PP.choice(doc, doc).

PP.nest increases the nesting after next new line by the given string or by the given number of spaces.

For example:

PP.render(6, PP.nest(2, PP.group(PP.intersperse(PP.line, ['foo', 'bar']))))
// foo
//   bar

PP.column allows a document to depend on the column at which the document starts.

PP.nesting allows a document to depend on the nesting after the next new line.

PP.align creates a document such that the nesting of the document is aligned to the current column.

For example:

PP.render(10, PP.group(['foo(', PP.align(['bar,', PP.line, 'baz']), ')']))
// foo(bar,
//     baz)

PP.hang creates a document such that the document is nested by the given string or number of spaces starting from the current column.

For example:

PP.render(10, PP.group(['foo(', PP.hang(2, ['bar,', PP.line, 'baz']), ')']))
// foo(bar,
//       baz)

PP.indent creates a document such that the document is indented by the given prefix or number of spaces starting from the current column.

PP.render(
  20,
  PP.nest(
    2,
    PP.group([
      'A comment:',
      PP.line,
      PP.line,
      PP.indent(
        '-- ',
        PP.intersperse(
          PP.softLine,
          R.split(/\s+/, 'This is the comment that you are looking for.')
        )
      )
    ])
  )
)
// A comment:
// 
//   -- This is the
//   -- comment that
//   -- you are looking
//   -- for.