Package com.google.googlejavaformat
Class OpsBuilder
- java.lang.Object
-
- com.google.googlejavaformat.OpsBuilder
-
public final class OpsBuilder extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classOpsBuilder.BlankLineWantedA request to add or remove a blank line in the output.
-
Field Summary
Fields Modifier and Type Field Description (package private) intdepthThe number of unclosed open ops in the input stream.private Inputinputprivate intinputPositionprivate intlastPartialFormatBoundaryprivate java.util.List<Op>opsprivate Outputoutputprivate static Doc.SpaceSPACEprivate inttokenIprivate static Indent.ConstZERO
-
Constructor Summary
Constructors Constructor Description OpsBuilder(Input input, Output output)TheOpsBuilderconstructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intactualSize(int position, int length)java.lang.IntegeractualStartColumn(int position)voidadd(Op op)Add anOp, and record open/close ops for later validation of unclosed levels.voidaddAll(java.util.List<Op> ops)Add a list ofOps.voidblankLineWanted(OpsBuilder.BlankLineWanted wanted)Force or suppress a blank line here in the output.voidbreakOp()Emit aDoc.Break.voidbreakOp(Doc.FillMode fillMode, java.lang.String flat, Indent plusIndent)Emit a genericDoc.Break.voidbreakOp(Doc.FillMode fillMode, java.lang.String flat, Indent plusIndent, java.util.Optional<Output.BreakTag> optionalTag)Emit a genericDoc.Break.voidbreakOp(Indent plusIndent)Emit aDoc.Break.voidbreakOp(java.lang.String flat)voidbreakToFill()Emit a filledDoc.Break.voidbreakToFill(java.lang.String flat)com.google.common.collect.ImmutableList<Op>build()Build a list ofOps from theOpsBuilder.voidcheckClosed(int previous)Checks that all open ops in the op stream have matching close ops.voidclose()Close the current level, by emitting aCloseOp.intdepth()Returns the number of unclosed open ops in the input stream.FormatterDiagnosticdiagnostic(java.lang.String message)Create aFormatterDiagnosticat the current position.voiddrain()Output any remaining tokens from the input stream (e.g.voidforcedBreak()Emit a forcedDoc.Break.voidforcedBreak(Indent plusIndent)Emit a forcedDoc.Break.private static intgetI(Input.Token token)InputgetInput()Get theOpsBuilder'sInput.voidguessToken(java.lang.String token)Emit an optional token iff it exists on the input.private static booleanisForcedBreak(Op op)private static java.util.List<Op>makeComment(Input.Tok comment)voidmarkForPartialFormat()Make the boundary of a region that can be partially formatted.voidop(java.lang.String op)Emit a single- or multi-character op by breaking it into single-characterDoc.Tokens.voidopen(Indent plusIndent)Open a new level by emitting anOpenOp.java.util.Optional<java.lang.String>peekToken()Return the text of the nextInput.Token, or absent if there is none.java.util.Optional<java.lang.String>peekToken(int skip)Return the text of an upcomingInput.Token, or absent if there is none.com.google.common.collect.ImmutableList<Input.Tok>peekTokens(int startPosition, com.google.common.base.Predicate<Input.Tok> predicate)Returns theInput.Toks starting at the current source position, which are satisfied by the given predicate.voidspace()Emit aDoc.Space.voidsync(int inputPosition)Sync to position in the input.voidtoken(java.lang.String token, Doc.Token.RealOrImaginary realOrImaginary, Indent plusIndentCommentsBefore, java.util.Optional<Indent> breakAndIndentTrailingComment)java.lang.StringtoString()
-
-
-
Field Detail
-
input
private final Input input
-
ops
private final java.util.List<Op> ops
-
output
private final Output output
-
ZERO
private static final Indent.Const ZERO
-
tokenI
private int tokenI
-
inputPosition
private int inputPosition
-
depth
int depth
The number of unclosed open ops in the input stream.
-
lastPartialFormatBoundary
private int lastPartialFormatBoundary
-
SPACE
private static final Doc.Space SPACE
-
-
Method Detail
-
actualSize
public int actualSize(int position, int length)- Returns:
- the actual size of the AST node at position, including comments.
-
actualStartColumn
public java.lang.Integer actualStartColumn(int position)
- Returns:
- the start column of the token at
position, including leading comments.
-
add
public final void add(Op op)
Add anOp, and record open/close ops for later validation of unclosed levels.
-
depth
public int depth()
Returns the number of unclosed open ops in the input stream.
-
checkClosed
public void checkClosed(int previous)
Checks that all open ops in the op stream have matching close ops.- Throws:
FormattingError- if any ops were unclosed
-
diagnostic
public FormatterDiagnostic diagnostic(java.lang.String message)
Create aFormatterDiagnosticat the current position.
-
sync
public final void sync(int inputPosition)
Sync to position in the input. If we've skipped outputting any tokens that were present in the input tokens, output them here and optionally complain.- Parameters:
inputPosition- the0-based input position
-
drain
public final void drain()
Output any remaining tokens from the input stream (e.g. terminal whitespace).
-
open
public final void open(Indent plusIndent)
Open a new level by emitting anOpenOp.- Parameters:
plusIndent- the extra indent for the new level
-
close
public final void close()
Close the current level, by emitting aCloseOp.
-
peekToken
public final java.util.Optional<java.lang.String> peekToken()
Return the text of the nextInput.Token, or absent if there is none.
-
peekToken
public final java.util.Optional<java.lang.String> peekToken(int skip)
Return the text of an upcomingInput.Token, or absent if there is none.
-
peekTokens
public com.google.common.collect.ImmutableList<Input.Tok> peekTokens(int startPosition, com.google.common.base.Predicate<Input.Tok> predicate)
Returns theInput.Toks starting at the current source position, which are satisfied by the given predicate.
-
guessToken
public final void guessToken(java.lang.String token)
Emit an optional token iff it exists on the input. This is used to emit tokens whose existence has been lost in the AST.- Parameters:
token- the optional token
-
token
public final void token(java.lang.String token, Doc.Token.RealOrImaginary realOrImaginary, Indent plusIndentCommentsBefore, java.util.Optional<Indent> breakAndIndentTrailingComment)
-
op
public final void op(java.lang.String op)
Emit a single- or multi-character op by breaking it into single-characterDoc.Tokens.- Parameters:
op- the operator to emit
-
space
public final void space()
Emit aDoc.Space.
-
breakOp
public final void breakOp()
Emit aDoc.Break.
-
breakOp
public final void breakOp(Indent plusIndent)
Emit aDoc.Break.- Parameters:
plusIndent- extra indent if taken
-
breakToFill
public final void breakToFill()
Emit a filledDoc.Break.
-
forcedBreak
public final void forcedBreak()
Emit a forcedDoc.Break.
-
forcedBreak
public final void forcedBreak(Indent plusIndent)
Emit a forcedDoc.Break.- Parameters:
plusIndent- extra indent if taken
-
breakOp
public final void breakOp(java.lang.String flat)
- Parameters:
flat- theDoc.Breakwhen not broken
-
breakToFill
public final void breakToFill(java.lang.String flat)
- Parameters:
flat- theDoc.Breakwhen not broken
-
breakOp
public final void breakOp(Doc.FillMode fillMode, java.lang.String flat, Indent plusIndent)
Emit a genericDoc.Break.- Parameters:
fillMode- theDoc.FillModeflat- theDoc.Breakwhen not brokenplusIndent- extra indent if taken
-
breakOp
public final void breakOp(Doc.FillMode fillMode, java.lang.String flat, Indent plusIndent, java.util.Optional<Output.BreakTag> optionalTag)
Emit a genericDoc.Break.- Parameters:
fillMode- theDoc.FillModeflat- theDoc.Breakwhen not brokenplusIndent- extra indent if takenoptionalTag- an optional tag for remembering whether the break was taken
-
markForPartialFormat
public void markForPartialFormat()
Make the boundary of a region that can be partially formatted. The boundary will be included in the following region, e.g.: [[boundary0, boundary1), [boundary1, boundary2), ...].
-
blankLineWanted
public final void blankLineWanted(OpsBuilder.BlankLineWanted wanted)
Force or suppress a blank line here in the output.- Parameters:
wanted- whether to force (true) or suppressfalse) the blank line
-
getI
private static int getI(Input.Token token)
-
build
public final com.google.common.collect.ImmutableList<Op> build()
Build a list ofOps from theOpsBuilder.- Returns:
- the list of
Ops
-
isForcedBreak
private static boolean isForcedBreak(Op op)
-
toString
public final java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-