java.lang.Object
com.google.googlejavaformat.OpsBuilder
public final class OpsBuilder extends Object
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
OpsBuilder.BlankLineWanted
A request to add or remove a blank line in the output. -
Constructor Summary
Constructors Constructor Description OpsBuilder(Input input, Output output)
TheOpsBuilder
constructor. -
Method Summary
Modifier and Type Method Description int
actualSize(int position, int length)
Integer
actualStartColumn(int position)
void
addAll(List<Op> ops)
Add a list ofOp
s.void
blankLineWanted(OpsBuilder.BlankLineWanted wanted)
Force or suppress a blank line here in the output.void
breakOp()
Emit aDoc.Break
.void
breakOp(Doc.FillMode fillMode, String flat, Indent plusIndent)
Emit a genericDoc.Break
.void
breakOp(Doc.FillMode fillMode, String flat, Indent plusIndent, Optional<Output.BreakTag> optionalTag)
Emit a genericDoc.Break
.void
breakOp(Indent plusIndent)
Emit aDoc.Break
.void
breakOp(String flat)
void
breakToFill()
Emit a filledDoc.Break
.void
breakToFill(String flat)
ImmutableList<Op>
build()
Build a list ofOp
s from theOpsBuilder
.void
checkClosed(int previous)
Checks that all open ops in the op stream have matching close ops.void
close()
Close the current level, by emitting aCloseOp
.int
depth()
Returns the number of unclosed open ops in the input stream.FormatterDiagnostic
diagnostic(String message)
Create aFormatterDiagnostic
at the current position.void
drain()
Output any remaining tokens from the input stream (e.g.void
forcedBreak()
Emit a forcedDoc.Break
.void
forcedBreak(Indent plusIndent)
Emit a forcedDoc.Break
.Input
getInput()
Get theOpsBuilder
'sInput
.void
guessToken(String token)
Emit an optional token iff it exists on the input.void
markForPartialFormat()
Make the boundary of a region that can be partially formatted.void
op(String op)
Emit a single- or multi-character op by breaking it into single-characterDoc.Token
s.void
open(Indent plusIndent)
Open a new level by emitting anOpenOp
.Optional<String>
peekToken()
Return the text of the nextInput.Token
, or absent if there is none.Optional<String>
peekToken(int skip)
Return the text of an upcomingInput.Token
, or absent if there is none.void
space()
Emit aDoc.Space
.void
sync(int inputPosition)
Sync to position in the input.void
token(String token, Doc.Token.RealOrImaginary realOrImaginary, Indent plusIndentCommentsBefore, Optional<Indent> breakAndIndentTrailingComment)
String
toString()
-
Constructor Details
-
OpsBuilder
TheOpsBuilder
constructor.
-
-
Method Details
-
actualSize
public int actualSize(int position, int length)- Returns:
- the actual size of the AST node at position, including comments.
-
actualStartColumn
- Returns:
- the start column of the token at
position
, including leading comments.
-
addAll
Add a list ofOp
s. -
getInput
Get theOpsBuilder
'sInput
. -
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
Create aFormatterDiagnostic
at 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
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
Return the text of the nextInput.Token
, or absent if there is none. -
peekToken
Return the text of an upcomingInput.Token
, or absent if there is none. -
guessToken
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(String token, Doc.Token.RealOrImaginary realOrImaginary, Indent plusIndentCommentsBefore, Optional<Indent> breakAndIndentTrailingComment) -
op
Emit a single- or multi-character op by breaking it into single-characterDoc.Token
s.- Parameters:
op
- the operator to emit
-
space
public final void space()Emit aDoc.Space
. -
breakOp
public final void breakOp()Emit aDoc.Break
. -
breakOp
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
Emit a forcedDoc.Break
.- Parameters:
plusIndent
- extra indent if taken
-
breakOp
- Parameters:
flat
- theDoc.Break
when not broken
-
breakToFill
- Parameters:
flat
- theDoc.Break
when not broken
-
breakOp
Emit a genericDoc.Break
.- Parameters:
fillMode
- theDoc.FillMode
flat
- theDoc.Break
when not brokenplusIndent
- extra indent if taken
-
breakOp
public final void breakOp(Doc.FillMode fillMode, String flat, Indent plusIndent, Optional<Output.BreakTag> optionalTag)Emit a genericDoc.Break
.- Parameters:
fillMode
- theDoc.FillMode
flat
- theDoc.Break
when 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
Force or suppress a blank line here in the output.- Parameters:
wanted
- whether to force (true
) or suppressfalse
) the blank line
-
build
Build a list ofOp
s from theOpsBuilder
.- Returns:
- the list of
Op
s
-
toString
-