public abstract class Doc
extends java.lang.Object
The pretty printer takes documents formed using a small set of operators, and formats them as strings suitable for display in a fixed number of columns.
A pretty-printer document (or Doc) is an immutable object that represents a piece of
formatted text. A programmer can use the combinators EMPTY and text()
to construct documents, and can concatenate documents with append(). For example:
append(text("Good"), text("Morning"), text("World"))
pretty-prints as:
GoodMorningWorld
Line breaking is controlled using the BREAK and group() combinators.
Within a group() combinator, all breaks are printed using a common breaking
policy, described using a Doc.GroupKind. For example:
group(HORIZONTAL, append(text("Good"), BREAK, text("Morning"), BREAK, text("World"))
pretty-prints as
Good Morning Worldwhereas
group(VERTICAL, append(text("Good"), BREAK, text("Morning"), BREAK, text("World"))
pretty-prints as:
Good Morning World
A key reason for the pretty-printer's expressive power is the automatic group kind
(Doc.GroupKind.AUTO), which allows us to specify pretty-printing alternatives. Breaks in an
automatic group are all printed as spaces, if the result would not overflow a single line, or as
newlines, otherwise; the pretty-printer makes the appropriate choice at layout time depending on
the space available.
The combinators nest(), align(), and hang()
control indentation. For instance, the nest() combinator increases the indentation
on new-lines by a number of columns. For example:
nest(2, group(VERTICAL, append(text("Good"), BREAK, text("Morning"), BREAK, text("World")))
pretty-prints as:
Good Morning World
Many methods of this class come in two variants:
Doc.group(xyz), and
xyz.group().
Wadler's original pretty printer is implemented in Haskell [1]. The original Wadler version relies on lazy evaluation, which Java does not have, so this implementation is based on Lindig's strict version in Objective Caml [2].
References:
This library is thread-safe.
| Modifier and Type | Class and Description |
|---|---|
static class |
Doc.AnsiColor
ANSI color codes.
|
static class |
Doc.BlockBuilder
A builder for block-like constructs.
|
static class |
Doc.GroupKind
Describes how the breaks within a
group() should be printed. |
| Modifier and Type | Field and Description |
|---|---|
static Doc |
BREAK
The default break, which is printed either as a space in horizontal mode, or as a newline in
vertical mode.
|
static Doc |
COMMA |
static int |
DEFAULT_INDENT
Size of the default indentation step in characters.
|
static int |
DEFAULT_WIDTH
The default number of columns to format for if no width is specified.
|
static Doc |
EMPTY
The empty document, which prints as
"" (the empty string). |
static Doc |
LANGLE |
static Doc |
LBRACE |
static Doc |
LBRACKET |
static Doc |
LPAREN |
static Doc |
RANGLE |
static Doc |
RBRACE |
static Doc |
RBRACKET |
static Doc |
RPAREN |
static Doc |
SEMI |
static Doc |
SOFT_BREAK
A break that is printed either as the empty string in horizontal mode, or as a newline in
vertical mode.
|
| Constructor and Description |
|---|
Doc() |
| Modifier and Type | Method and Description |
|---|---|
Doc |
add(Doc that)
Appends document
that to the document, with no breaks between documents. |
Doc |
align()
Aligns breaks in the document at the current column.
|
static Doc |
align(Doc child)
Aligns breaks in document
child at the current column. |
Doc |
angles()
Wraps the document in angle brackets.
|
static Doc |
angles(Doc child)
Wraps document
child in angle brackets. |
static Doc |
append(Doc... docs)
Appends a list of documents, with no breaks between documents.
|
static Doc |
append(java.lang.Iterable<Doc> docs)
Appends a list of documents, with no breaks between documents.
|
static Doc |
binary(Doc left,
Doc operator,
Doc right)
Builds a binary with indentation of 4.
|
static Doc |
binary(int indent,
Doc left,
Doc operator,
Doc right)
Build a binary operator invocation as
|
static Doc.BlockBuilder |
blockBuilder(Doc header)
Returns a builder for a block with indentation of 2.
|
static Doc.BlockBuilder |
blockBuilder(Doc header,
int indent)
Returns a builder for a block which breaks vertically as
|
Doc |
braces()
Wraps the document in braces.
|
static Doc |
braces(Doc child)
Wraps document
child in braces. |
Doc |
brackets()
Wraps the document in brackets.
|
static Doc |
brackets(Doc child)
Wraps document
child in brackets. |
static Doc |
breakWith(java.lang.String representation)
Returns a break, which will be printed according to the policy of the closest enclosing group.
|
Doc |
color(Doc.AnsiColor c)
Colors the document using the specified ANSI color code.
|
static Doc |
color(Doc.AnsiColor c,
Doc child)
Colors document
child using the specified ANSI color code. |
Doc |
fgroup()
Encloses the document in an
Doc.GroupKind.FILL group. |
static Doc |
fgroup(Doc child)
Encloses document
child in an Doc.GroupKind.FILL group. |
protected abstract void |
fits(com.google.api.tools.framework.snippet.Doc.LayoutMode mode,
com.google.api.tools.framework.snippet.Doc.FitsState state)
Determines if this document fits in
state.width columns, up to the next newline. |
protected abstract int |
format(java.lang.StringBuilder builder,
java.util.Deque<com.google.api.tools.framework.snippet.Doc.Agendum> agenda,
int width,
int indentation,
int consumed,
com.google.api.tools.framework.snippet.Doc.LayoutMode mode)
Pretty-print a document into a
StringBuilder. |
Doc |
group()
Encloses the document in an
Doc.GroupKind.AUTO group. |
Doc |
group(Doc.GroupKind kind)
Encloses the document in an group of a given
kind. |
static Doc |
group(Doc.GroupKind kind,
Doc child)
Encloses document
child in a group. |
static Doc |
group(Doc child)
Encloses document
child in an Doc.GroupKind.AUTO group. |
Doc |
hang()
Performs a hanging indentation of the document with an indent of
DEFAULT_INDENT
spaces. |
static Doc |
hang(Doc child)
Performs a hanging indentation of document
child with an indent of
DEFAULT_INDENT spaces. |
Doc |
hang(int indent)
Performs a hanging indentation of the document with an indent of
indent spaces. |
static Doc |
hang(int indent,
Doc child)
Performs a hanging indentation of document
child with an indent of indent
spaces. |
Doc |
hgroup()
Encloses the document in an
Doc.GroupKind.HORIZONTAL group. |
static Doc |
hgroup(Doc child)
Encloses document
child in an Doc.GroupKind.HORIZONTAL group. |
Doc |
indentAt(int indent)
Sets the indentation level of the document to
indent columns. |
static Doc |
indentAt(int indent,
Doc child)
Sets the indentation level of document
child to indent columns. |
static Doc |
invocation(Doc function,
java.lang.Iterable<Doc> arguments)
Build a function invocation with indentation of 4 for arguments.
|
static Doc |
invocation(int indent,
Doc function,
java.lang.Iterable<Doc> arguments)
Build a function invocation as
|
abstract boolean |
isWhitespace()
Determines whether the document is empty except of whitespace.
|
static Doc |
join(Doc... docs)
Appends documents, placing a
BREAK between adjacent documents. |
static Doc |
join(java.lang.Iterable<Doc> docs)
Appends documents, placing a
BREAK between adjacent documents. |
static Doc |
joinWith(Doc separator,
Doc... docs)
Appends documents, placing the separator
separator between adjacent documents. |
static Doc |
joinWith(Doc separator,
java.lang.Iterable<Doc> docs)
Appends documents, placing the separator
separator between adjacent documents. |
Doc |
nest()
Increase the indentation level of the document by
DEFAULT_INDENT spaces. |
static Doc |
nest(Doc child)
Increase the indentation level of document
child by DEFAULT_INDENT spaces. |
Doc |
nest(int indent)
Increases the indentation level of the document by
indent columns. |
static Doc |
nest(int indent,
Doc child)
Increases the indentation level of document
child by indent columns. |
Doc |
parens()
Wraps the document in parentheses.
|
static Doc |
parens(Doc child)
Wraps document
child in parentheses. |
java.lang.String |
prettyPrint()
Pretty prints the document, formatted for
DEFAULT_WIDTH columns. |
java.lang.String |
prettyPrint(int width)
Pretty prints the document, formatted for
width columns. |
void |
prettyPrint(java.lang.StringBuilder builder)
Pretty prints the document into a
StringBuilder, formatted for DEFAULT_WIDTH
columns. |
void |
prettyPrint(java.lang.StringBuilder builder,
int width)
Pretty prints the document into a
StringBuilder, formatted for width columns. |
static java.util.List<Doc> |
punctuate(Doc separator,
java.util.List<Doc> docs)
Appends a separator to all documents except the last one in a list.
|
static Doc |
text(java.lang.String text)
Returns a document representing the literal text
text. |
static java.util.List<Doc> |
texts(java.lang.Iterable<java.lang.String> strings)
Applies the
text() combinator to a sequence of strings. |
static java.util.List<Doc> |
texts(java.lang.String... strings)
Applies the
text() combinator to a sequence of strings. |
java.lang.String |
toString() |
Doc |
vgroup()
Encloses the document in an
Doc.GroupKind.VERTICAL group. |
static Doc |
vgroup(Doc child)
Encloses document
child in an Doc.GroupKind.VERTICAL group. |
static Doc |
words(java.lang.String input) |
public static final int DEFAULT_INDENT
public static final int DEFAULT_WIDTH
public static final Doc EMPTY
"" (the empty string).public static final Doc COMMA
public static final Doc LBRACKET
public static final Doc RBRACKET
public static final Doc LPAREN
public static final Doc RPAREN
public static final Doc LBRACE
public static final Doc RBRACE
public static final Doc LANGLE
public static final Doc RANGLE
public static final Doc SEMI
public static final Doc BREAK
group(HORIZONTAL, BREAK) prints as " "
group(VERTICAL, BREAK) prints as "\n"
public static final Doc SOFT_BREAK
group(HORIZONTAL, SOFT_BREAK) prints as ""
group(VERTICAL, SOFT_BREAK) prints as "\n"
public static Doc text(java.lang.String text)
text. Assumes that there are no
newline characters in the representation. (If there are new-line characters the pretty-printer
will print them as-is, usually leading to poorly formatted output.) For example,
text("hello") prints as "hello".public static java.util.List<Doc> texts(java.lang.Iterable<java.lang.String> strings)
text() combinator to a sequence of strings.public static java.util.List<Doc> texts(java.lang.String... strings)
text() combinator to a sequence of strings.public static Doc words(java.lang.String input)
public static Doc breakWith(java.lang.String representation)
The representation argument describes how the break should be printed in horizontal
mode; the representation is ignored in vertical mode. Assumes that there are no newline
characters in the representation. For example:
group(HORIZONTAL, breakWith("-")) prints as "-"
group(VERTICAL, breakWith("-")) prints as "\n"
public Doc add(Doc that)
that to the document, with no breaks between documents. For example,
text("a").add(text("b c")) prints as "ab c".public static Doc append(Doc... docs)
public static Doc append(java.lang.Iterable<Doc> docs)
public static Doc joinWith(Doc separator, Doc... docs)
separator between adjacent documents.
For example,
joinWith(append(COMMA, BREAK), text("a"), text("b"), text("c")))
prints as "a, b, c" in horizontal mode, or as "a,\nb,\nc" in vertical mode.public static Doc joinWith(Doc separator, java.lang.Iterable<Doc> docs)
separator between adjacent documents.public static Doc join(Doc... docs)
BREAK between adjacent documents.public static Doc join(java.lang.Iterable<Doc> docs)
BREAK between adjacent documents.public static Doc group(Doc.GroupKind kind, Doc child)
child in a group. Breaks are formatted according to the policy of the
closest enclosing group.
For example, consider formatting the document
group(p, join(break(), text("xx"), text("yy"), text("zz"), text("ww")))
under different policies p for different column widths.
Under policy Doc.GroupKind.HORIZONTAL, all breaks in a group are printed as spaces,
regardless of whether that leads to an overflow:
"xx yy zz ww"
"xx yy zz ww"
Doc.GroupKind.VERTICAL, all breaks are printed as newlines:
"xx\nyy\nzz\nww"
"xx\nyy\nzz\nww"
Doc.GroupKind.AUTO, all breaks in the group are printed as spaces if the
result would not overflow a line, or as newlines, otherwise:
"xx\nyy\nzz\nww"
"xx yy zz ww"
Doc.GroupKind.FILL, each break is printed as a space if that would not lead to
overflow, or as a newline, otherwise. Unlike Doc.GroupKind.AUTO, a separate decision is
made for each break.
"xx\nyy\nzz\nww"
"xx yy\nzz ww"
"xx yy zz ww"
public static Doc group(Doc child)
child in an Doc.GroupKind.AUTO group.public static Doc vgroup(Doc child)
child in an Doc.GroupKind.VERTICAL group.public static Doc hgroup(Doc child)
child in an Doc.GroupKind.HORIZONTAL group.public static Doc fgroup(Doc child)
child in an Doc.GroupKind.FILL group.public Doc group()
Doc.GroupKind.AUTO group.public Doc vgroup()
Doc.GroupKind.VERTICAL group.public Doc hgroup()
Doc.GroupKind.HORIZONTAL group.public Doc fgroup()
Doc.GroupKind.FILL group.public Doc group(Doc.GroupKind kind)
kind.public static Doc nest(int indent, Doc child)
child by indent columns.
After each vertical break, a number of spaces equal to the current indentation level is added. For example:
nest(2, group(VERTICAL, join(BREAK, text("x"), text("y"), text("z"))))
prints as:
x y z
Note that indentation is only relevant for vertical breaks; for example
nest(2, group(HORIZONTAL, join(BREAK, text("x"), text("y"), text("z"))))
prints as:
x y zIndentation is cumulative, for example:
nest(2, nest(2, group(VERTICAL,
join(BREAK, text("x"), text("y"), text("z")))))
prints as:
x
y
z
public Doc nest(int indent)
indent columns.public static Doc nest(Doc child)
child by DEFAULT_INDENT spaces.public Doc nest()
DEFAULT_INDENT spaces.public static Doc align(Doc child)
child at the current column. For example:
append(text("alist = ["),
align(join(append(text(","), BREAK),
text("x"), text("y"), text("z"))),
text("]"))
prints in horizontal mode as:
alist = [x, y, z]and prints in vertical mode as:
alist = [x,
y,
z]
public Doc align()
public static Doc hang(int indent, Doc child)
child with an indent of indent
spaces. For example
append(text("alist = ["),
hang(join(append(text(","), BREAK),
text("x"), text("y"), text("z"))),
text("]"))
prints in vertical mode as:
alist = [x,
y,
z]
hang(indent, child) is a shorthand for align(nest(indent, child)).public static Doc hang(Doc child)
child with an indent of
DEFAULT_INDENT spaces.public Doc hang(int indent)
indent spaces.public Doc hang()
DEFAULT_INDENT
spaces.public static Doc indentAt(int indent, Doc child)
child to indent columns.public Doc indentAt(int indent)
indent columns.public static Doc brackets(Doc child)
child in brackets.
For example, brackets(text("x")) prints as "[x]".public Doc brackets()
public static Doc parens(Doc child)
child in parentheses.
For example, parens(text("x")) prints as "(x)".public Doc parens()
public static Doc braces(Doc child)
child in braces.
For example, braces(text("x")) prints as "{x}".public Doc braces()
public static Doc angles(Doc child)
child in angle brackets.
For example, angles(text("x")) prints as "<x>".public Doc angles()
public static Doc color(Doc.AnsiColor c, Doc child)
child using the specified ANSI color code.
TODO(user): nested colors do not work.public Doc color(Doc.AnsiColor c)
public static java.util.List<Doc> punctuate(Doc separator, java.util.List<Doc> docs)
public static Doc.BlockBuilder blockBuilder(Doc header, int indent)
header
line
line
...
[footer]
public static Doc.BlockBuilder blockBuilder(Doc header)
public static Doc invocation(int indent, Doc function, java.lang.Iterable<Doc> arguments)
function ( arg1, BREAK arg2, BREAK ... argn )i.e. a break is only allowed after the first argument and before the last argument.
public static Doc invocation(Doc function, java.lang.Iterable<Doc> arguments)
public static Doc binary(int indent, Doc left, Doc operator, Doc right)
left BREAK operator SPACE right
public static Doc binary(Doc left, Doc operator, Doc right)
public abstract boolean isWhitespace()
public void prettyPrint(java.lang.StringBuilder builder,
int width)
StringBuilder, formatted for width columns.public void prettyPrint(java.lang.StringBuilder builder)
StringBuilder, formatted for DEFAULT_WIDTH
columns.public java.lang.String prettyPrint(int width)
width columns. Returns the result as a
string.public java.lang.String prettyPrint()
DEFAULT_WIDTH columns. Returns the result as
a string.public java.lang.String toString()
toString in class java.lang.Objectprotected abstract void fits(com.google.api.tools.framework.snippet.Doc.LayoutMode mode,
com.google.api.tools.framework.snippet.Doc.FitsState state)
state.width columns, up to the next newline.
Updates state.width to the new number of columns, and sets state.done to true
if a newline is encountered or the line overflows.protected abstract int format(java.lang.StringBuilder builder,
java.util.Deque<com.google.api.tools.framework.snippet.Doc.Agendum> agenda,
int width,
int indentation,
int consumed,
com.google.api.tools.framework.snippet.Doc.LayoutMode mode)
StringBuilder. Updates the agenda, and returns an
updated number of consumed characters.agenda - the document layout worklist.width - number of columns in the target layout (e.g., 80)indentation - current number of columns of indentation.consumed - number of columns already used in the current line.mode - the current layout mode.