Class OpsBuilder

java.lang.Object
com.google.googlejavaformat.OpsBuilder

public final class OpsBuilder
extends Object
An OpsBuilder creates a list of Ops, which is turned into a Doc by DocBuilder.
  • Constructor Details

    • OpsBuilder

      public OpsBuilder​(Input input, Output output)
      The OpsBuilder constructor.
      Parameters:
      input - the Input, used for retrieve information from the AST
      output - the Output, used here only to record blank-line information
  • Method Details

    • actualSize

      public int actualSize​(int position, int length)
      Returns:
      the actual size of the AST node at position, including comments.
    • actualStartColumn

      public Integer actualStartColumn​(int position)
      Returns:
      the start column of the token at position, including leading comments.
    • addAll

      public final void addAll​(List<Op> ops)
      Add a list of Ops.
    • getInput

      public final Input getInput()
      Get the OpsBuilder's Input.
    • 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​(String message)
      Create a FormatterDiagnostic 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 - the 0-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 an OpenOp.
      Parameters:
      plusIndent - the extra indent for the new level
    • close

      public final void close()
      Close the current level, by emitting a CloseOp.
    • peekToken

      public final Optional<String> peekToken()
      Return the text of the next Input.Token, or absent if there is none.
    • peekToken

      public final Optional<String> peekToken​(int skip)
      Return the text of an upcoming Input.Token, or absent if there is none.
    • guessToken

      public final void guessToken​(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​(String token, Doc.Token.RealOrImaginary realOrImaginary, Indent plusIndentCommentsBefore, Optional<Indent> breakAndIndentTrailingComment)
    • op

      public final void op​(String op)
      Emit a single- or multi-character op by breaking it into single-character Doc.Tokens.
      Parameters:
      op - the operator to emit
    • space

      public final void space()
      Emit a Doc.Space.
    • breakOp

      public final void breakOp()
      Emit a Doc.Break.
    • breakOp

      public final void breakOp​(Indent plusIndent)
      Emit a Doc.Break.
      Parameters:
      plusIndent - extra indent if taken
    • breakToFill

      public final void breakToFill()
      Emit a filled Doc.Break.
    • forcedBreak

      public final void forcedBreak()
      Emit a forced Doc.Break.
    • forcedBreak

      public final void forcedBreak​(Indent plusIndent)
      Emit a forced Doc.Break.
      Parameters:
      plusIndent - extra indent if taken
    • breakOp

      public final void breakOp​(String flat)
      Emit a Doc.Break, with a specified flat value (e.g., " ").
      Parameters:
      flat - the Doc.Break when not broken
    • breakToFill

      public final void breakToFill​(String flat)
      Emit a Doc.Break, with a specified flat value (e.g., " ").
      Parameters:
      flat - the Doc.Break when not broken
    • breakOp

      public final void breakOp​(Doc.FillMode fillMode, String flat, Indent plusIndent)
      Emit a generic Doc.Break.
      Parameters:
      fillMode - the Doc.FillMode
      flat - the Doc.Break when not broken
      plusIndent - extra indent if taken
    • breakOp

      public final void breakOp​(Doc.FillMode fillMode, String flat, Indent plusIndent, Optional<Output.BreakTag> optionalTag)
      Emit a generic Doc.Break.
      Parameters:
      fillMode - the Doc.FillMode
      flat - the Doc.Break when not broken
      plusIndent - extra indent if taken
      optionalTag - 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 suppress false) the blank line
    • build

      public final ImmutableList<Op> build()
      Build a list of Ops from the OpsBuilder.
      Returns:
      the list of Ops
    • toString

      public final String toString()
      Overrides:
      toString in class Object