prettier

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2021 License: MIT Imports: 2 Imported by: 1

README

prettier

Build Status

Overview

prettier is an implementation of Wadler's "A Prettier Printer".

Install

$ go get -u github.com/tanishiking/prettier

Usage

import (
    "fmt"

    p "github.com/tanishiking/prettier"
)

func main() {
    sep := p.Concat([]p.Doc{p.Text(","), p.LineOrSpace()})
    ds := []p.Doc{
        p.Text("foo"),
        p.Text("bar"),
        p.Text("baz"),
        p.Text("hello"),
        p.Text("world"),
    }
    doc := p.TightBracketBy(
        p.Text("["),
        p.Text("]"),
        p.Intercalate(sep, ds),
        uint(2),
    )

    fmt.Println(p.Pretty(40, doc))
    // [foo, bar, baz, hello, world]

    fmt.Println(p.Pretty(20, doc))
    // [
    //   foo, bar, baz,
    //   hello, world
    // ]

    fmt.Println(p.Pretty(10, doc))
    // [
    //   foo,
    //   bar,
    //   baz,
    //   hello,
    //   world
    // ]
}

License

MIT License

Documentation

Overview

Package prettier is an implementation of Wadler's classic "A Prettier Printer" see http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf

Example
package main

import (
	"fmt"

	p "github.com/tanishiking/prettier"
)

func main() {
	sep := p.Concat([]p.Doc{p.Text(","), p.LineOrSpace()})
	ds := []p.Doc{
		p.Text("foo"),
		p.Text("bar"),
		p.Text("baz"),
		p.Text("hello"),
		p.Text("world"),
	}
	doc := p.TightBracketBy(
		p.Text("["),
		p.Text("]"),
		p.Intercalate(sep, ds),
		uint(2),
	)

	fmt.Println(p.Pretty(20, doc))
}
Output:

[
  foo, bar, baz,
  hello, world
]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pretty

func Pretty(width int, doc Doc) string

Pretty renders the given Doc as a string, limiting line lengths to `width` or shorter when possible.

Note that this function does not guarantee there are no lines longer than `width` -- it just attempts to keep lines within this length when possible.

Types

type Doc

type Doc interface {
	String() string
	// contains filtered or unexported methods
}

Doc represents set of layouts.

func BracketBy

func BracketBy(left Doc, right Doc, body Doc, indent uint) Doc

BracketBy bookend specified Doc between the given Docs.

If the documents (when flattened) all fit on one line, then newlines will be collapsed, and spaces will be added, and the document will render on one line. If you do not want a space, see TightBracketBy

Otherwise, newlines will be used on either side of the document, and the requested level of indentation will be added.

func Concat

func Concat(ds []Doc) Doc

Concat concatinate multiple documents.

In practice this method builds documents from the right, so that the resulting concatenations are all right-associated.

func Empty

func Empty() Doc

Empty represents an empty document

func Fill

func Fill(sep Doc, parts []Doc) Doc

Fill collapse a collection of documents into one document, delimited by a specified separator.

func FoldDocs

func FoldDocs(f func(a Doc, b Doc) Doc, ds []Doc) Doc

FoldDocs combines documents, using the given associative function.

The function `f` must be associative. That is, the expression `f(x, f(y, z))` must be equivalent to `f(f(x, y), z)`.

In practice this method builds documents from the right, so that the resulting concatenations are all right-associated.

func Group

func Group(doc Doc) Doc

Group treats the specified doc as a group that can be compressed. The effect of this is to replace newlines with spaces, if there is enough room. Otherwise, the Doc will be rendered as-it is.

func Intercalate

func Intercalate(sep Doc, ds []Doc) Doc

Intercalate concatenate the given documents together, delimited by the given separator.

func Line

func Line() Doc

Line represents a single, literal newline. which is flattened to a space.

func LineBreak

func LineBreak() Doc

LineBreak represents a single, literal newline. which is flattened to a empty doc.

func LineOrSpace

func LineOrSpace() Doc

LineOrSpace represents a space (if there is enough room), or a newline otherwise.

func Nest

func Nest(indent uint, doc Doc) Doc

Nest represents a "remembered indentation level" for a document. Newlines in this document will be followed by at least this much indentation (nesting is cumulative).

func Spaces

func Spaces(n uint) Doc

Spaces returns multiple spaces.

func Text

func Text(str string) Doc

Text represents string The string must not be empty, and may not contain newlines.

func TextWithLength

func TextWithLength(str string, strLength int) Doc

TextWithLength represents string whose length is strLength. This is useful for string that contains invisible characters like ansi color.

func TightBracketBy

func TightBracketBy(left Doc, right Doc, body Doc, indent uint) Doc

TightBracketBy bookend specified Doc between the given Docs.

For more details, see BracketBy. The difference from BracketBy is that TightBracketBy will collapse the documents without a space (if flattened docs fit on one line)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL