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 ¶
- func Pretty(width int, doc Doc) string
- type Doc
- func BracketBy(left Doc, right Doc, body Doc, indent uint) Doc
- func Concat(ds []Doc) Doc
- func Empty() Doc
- func Fill(sep Doc, parts []Doc) Doc
- func FoldDocs(f func(a Doc, b Doc) Doc, ds []Doc) Doc
- func Group(doc Doc) Doc
- func Intercalate(sep Doc, ds []Doc) Doc
- func Line() Doc
- func LineBreak() Doc
- func LineOrSpace() Doc
- func Nest(indent uint, doc Doc) Doc
- func Spaces(n uint) Doc
- func Text(str string) Doc
- func TextWithLength(str string, strLength int) Doc
- func TightBracketBy(left Doc, right Doc, body Doc, indent uint) Doc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Doc ¶
type Doc interface { String() string // contains filtered or unexported methods }
Doc represents set of layouts.
func BracketBy ¶
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 ¶
Concat concatinate multiple documents.
In practice this method builds documents from the right, so that the resulting concatenations are all right-associated.
func Fill ¶
Fill collapse a collection of documents into one document, delimited by a specified separator.
func FoldDocs ¶
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 ¶
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 ¶
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 ¶
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 TextWithLength ¶
TextWithLength represents string whose length is strLength. This is useful for string that contains invisible characters like ansi color.
func TightBracketBy ¶
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)