项目作者: polytypic

项目描述 :
A pretty printing library
高级语言: JavaScript
项目地址: git://github.com/polytypic/prettier-printer.git
创建时间: 2017-11-12T12:04:20Z
项目社区:https://github.com/polytypic/prettier-printer

开源协议:MIT License

下载


" class="reference-link"> Prettier Printer · GitHub stars npm

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

Contents

Tutorial

To be done.

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

Reference

Typically one imports the library as:

  1. import * as PP from 'prettier-printer'

The examples also utilize Ramda, bound as R.

Rendering documents

PP.render(maxCols, doc) ~> string v1.0.0" class="reference-link"> PP.render(maxCols, doc) ~> string v1.0.0

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:

  1. PP.render(
  2. 10,
  3. PP.indent('-- ', PP.group(PP.intersperse(PP.line, ['Hello,', 'world!'])))
  4. )
  5. // -- Hello,
  6. // -- world!

PP.renderWith({text: (state, string) => state, line: state => state}, state, maxCols, doc) ~> state v1.0.0" class="reference-link"> PP.renderWith({text: (state, string) => state, line: state => state}, state, maxCols, doc) ~> state v1.0.0

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.

Document constants

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 ~> doc v1.0.0" class="reference-link"> PP.line ~> doc v1.0.0

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

For example:

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

PP.lineBreak ~> doc v1.0.0" class="reference-link"> PP.lineBreak ~> doc v1.0.0

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

For example:

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

PP.softLine ~> doc v1.0.0" class="reference-link"> PP.softLine ~> doc v1.0.0

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

For example:

  1. PP.render(
  2. 20,
  3. PP.intersperse(
  4. PP.softLine,
  5. R.split(
  6. /\s+/,
  7. 'Here is a paragraph of text that we will format to a desired width.'
  8. )
  9. )
  10. )
  11. // Here is a paragraph
  12. // of text that we will
  13. // format to a desired
  14. // width.

PP.softBreak ~> doc v1.0.0" class="reference-link"> PP.softBreak ~> doc v1.0.0

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

For example:

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

Concatenating documents

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(rhsDoc, lhsDoc) ~> doc v1.0.0" class="reference-link"> PP.append(rhsDoc, lhsDoc) ~> doc v1.0.0

PP.append reverse concatenates the documents.

For example:

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

PP.prepend(lhsDoc, rhsDoc) ~> doc v1.0.0" class="reference-link"> PP.prepend(lhsDoc, rhsDoc) ~> doc v1.0.0

PP.prepend concatenates the documents.

For example:

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

Lists of documents

PP.intersperse(doc, [...docs]) ~> [...docs] v1.0.0" class="reference-link"> PP.intersperse(doc, [...docs]) ~> [...docs] v1.0.0

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

For example:

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

PP.punctuate(sepDoc, [...docs]) ~> [...docs] v1.0.0" class="reference-link"> PP.punctuate(sepDoc, [...docs]) ~> [...docs] v1.0.0

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

For example:

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

Lazy documents

PP.lazy(() => doc) ~> doc v1.0.0" class="reference-link"> PP.lazy(() => doc) ~> doc v1.0.0

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

Enclosing documents

PP.enclose([lhsDoc, rhsDoc], doc) ~> doc v1.0.0" class="reference-link"> PP.enclose([lhsDoc, rhsDoc], doc) ~> doc v1.0.0

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

For example:

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

Document pair constants

PP.angles ~> ['<', '>'] v1.0.0" class="reference-link"> PP.angles ~> ['<', '>'] v1.0.0
PP.braces ~> ['{', '}'] v1.0.0" class="reference-link"> PP.braces ~> ['{', '}'] v1.0.0
PP.brackets ~> [‘[‘, ‘]’] v1.0.0" class="reference-link"> PP.brackets ~> [‘[‘, ‘]’] v1.0.0
PP.dquotes ~> ['"', '"'] v1.0.0" class="reference-link"> PP.dquotes ~> ['"', '"'] v1.0.0
PP.lineBreaks ~> [PP.lineBreak, PP.lineBreak] v1.1.0" class="reference-link"> PP.lineBreaks ~> [PP.lineBreak, PP.lineBreak] v1.1.0
PP.lines ~> [PP.line, PP.line] v1.1.0" class="reference-link"> PP.lines ~> [PP.line, PP.line] v1.1.0
PP.parens ~> ['(', ')'] v1.0.0" class="reference-link"> PP.parens ~> ['(', ')'] v1.0.0
PP.spaces ~> [' ', ' '] v1.0.0" class="reference-link"> PP.spaces ~> [' ', ' '] v1.0.0
PP.squotes ~> ["'", "'"] v1.0.0" class="reference-link"> PP.squotes ~> ["'", "'"] v1.0.0

Alternative documents

PP.choice(wideDoc, narrowDoc) ~> doc v1.0.0" class="reference-link"> PP.choice(wideDoc, narrowDoc) ~> doc v1.0.0

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:

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

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

For example:

  1. const hyphen = PP.choice('', ['-', PP.lineBreak])
  2. PP.render(5, PP.intersperse(hyphen, ['hy', 'phen', 'at', 'ed']))
  3. // hy-
  4. // phen-
  5. // ated

PP.group(doc) ~> doc v1.0.0" class="reference-link"> PP.group(doc) ~> doc v1.0.0

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).

Nested documents

PP.nest(string | number, doc) ~> doc v1.0.0" class="reference-link"> PP.nest(string | number, doc) ~> doc v1.0.0

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

For example:

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

Layout dependent documents

PP.column(column => doc) ~> doc v1.0.0" class="reference-link"> PP.column(column => doc) ~> doc v1.0.0

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

PP.nesting(nesting => doc) ~> doc v1.0.0" class="reference-link"> PP.nesting(nesting => doc) ~> doc v1.0.0

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

Aligned documents

PP.align(doc) ~> doc v1.0.0" class="reference-link"> PP.align(doc) ~> doc v1.0.0

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

For example:

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

PP.hang(string | number, doc) ~> doc v1.0.0" class="reference-link"> PP.hang(string | number, doc) ~> doc v1.0.0

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:

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

PP.indent(string | number, doc) ~> doc v1.0.0" class="reference-link"> PP.indent(string | number, doc) ~> doc v1.0.0

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

  1. PP.render(
  2. 20,
  3. PP.nest(
  4. 2,
  5. PP.group([
  6. 'A comment:',
  7. PP.line,
  8. PP.line,
  9. PP.indent(
  10. '-- ',
  11. PP.intersperse(
  12. PP.softLine,
  13. R.split(/\s+/, 'This is the comment that you are looking for.')
  14. )
  15. )
  16. ])
  17. )
  18. )
  19. // A comment:
  20. //
  21. // -- This is the
  22. // -- comment that
  23. // -- you are looking
  24. // -- for.
  • Philip Wadler’s paper A prettier
    printer

    describes the basic ideas and implementation.
  • Text.PrettyPrint.Leijen
    is Daan Leijen’s implementation with some extensions.
  • Other prettier printer implementations by the author of this library:
  • text.pretty-printing
    another JS implementation based on Wadler’s paper. Marked as
    “[Unmaintained]”.
  • Prettier uses a similar pretty printing library
    underneath.