public final class Formatter extends Object
This formatter uses the Eclipse parser to generate an AST. Because the Eclipse AST loses information about the non-tokens in the input (including newlines, comments, etc.), and even some tokens (e.g., optional commas or semicolons), this formatter lexes the input again and follows along in the resulting list of tokens. Its lexer splits all multi-character operators (like ">>") into multiple single-character operators. Each non-token is assigned to a token---non-tokens following a token on the same line go with that token; those following go with the next token--- and there is a final EOF token to hold final comments.
The formatter walks the AST to generate a Greg Nelson/Dereck Oppen-style list of formatting
Op
s [1--2] that then generate a structured Doc
. Each AST node type has a visitor
to emit a sequence of Op
s for the node.
Some data-structure operations are easier in the list of Op
s, while others become
easier in the Doc
. The Op
s are walked to attach the comments. As the Op
s
are generated, missing input tokens are inserted and incorrect output tokens are dropped,
ensuring that the output matches the input even in the face of formatter errors. Finally, the
formatter walks the Doc
to format it in the given width.
This formatter also produces data structures of which tokens and comments appear where on the input, and on the output, to help output a partial reformatting of a slightly edited input.
[1] Nelson, Greg, and John DeTreville. Personal communication.
[2] Oppen, Dereck C. "Prettyprinting". ACM Transactions on Programming Languages and Systems, Volume 2 Issue 4, Oct. 1980, pp. 465–483.
Constructor and Description |
---|
Formatter() |
Modifier and Type | Method and Description |
---|---|
static void |
format(JavaInput javaInput,
JavaOutput javaOutput,
int maxWidth,
List<FormatterDiagnostic> errors,
int indentationMultiplier)
Construct a
Formatter given Java compilation unit. |
void |
formatSource(CharSource input,
CharSink output)
Format the given input (a Java compilation unit) into the output stream.
|
String |
formatSource(String input)
Format an input string (a Java compilation unit) into an output string.
|
String |
formatSource(String input,
List<Range<Integer>> characterRanges)
Format an input string (a compilation), for only the specified character ranges.
|
ImmutableList<Replacement> |
getFormatReplacements(String input,
List<Range<Integer>> characterRanges)
Emit a list of
Replacement s to convert from input to output. |
public static void format(JavaInput javaInput, JavaOutput javaOutput, int maxWidth, List<FormatterDiagnostic> errors, int indentationMultiplier)
Formatter
given Java compilation unit. Parses the code; builds a
JavaInput
and the corresponding JavaOutput
.javaInput
- the input, a Java compilation unitjavaOutput
- the JavaOutput
maxWidth
- the maximum formatted widtherrors
- mutable list to receive errorsindentationMultiplier
- the multiplier for the unit of indent; the default is 1public void formatSource(CharSource input, CharSink output) throws FormatterException, IOException
FormatterException
- if the input cannot be parsedIOException
public String formatSource(String input) throws FormatterException
input
- the input stringFormatterException
- if the input string cannot be parsedpublic String formatSource(String input, List<Range<Integer>> characterRanges) throws FormatterException
input
- the input stringcharacterRanges
- the character ranges to be reformattedFormatterException
- if the input string cannot be parsedpublic ImmutableList<Replacement> getFormatReplacements(String input, List<Range<Integer>> characterRanges) throws FormatterException
Replacement
s to convert from input to output.input
- the input compilation unitcharacterRanges
- the character ranges to reformatReplacement
s, sorted from low index to high index, without
overlapsFormatterException
- if the input string cannot be parsedCopyright © 2015 Google Inc.. All rights reserved.