Class OpsBuilder

java.lang.Object
com.palantir.javaformat.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)
      The OpsBuilder constructor.
      Parameters:
      input - the Input, used for retrieve information from the AST
  • 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 void addAll(List<Op> ops)
      Add a list of Ops.
    • getInput

      public 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 com.palantir.javaformat.java.FormatterDiagnostic diagnostic(String message)
      Create a FormatterDiagnostic at the current position.
    • sync

      public 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 void drain()
      Output any remaining tokens from the input stream (e.g. terminal whitespace).
    • open

      public void open(Indent plusIndent)
      Open a new level by emitting an OpenOp.
      Parameters:
      plusIndent - the extra indent for the new level
    • open

      public void open(String debugName, Indent plusIndent)
      Open a new level by emitting an OpenOp.
      Parameters:
      debugName - a representative name for this lambda
      plusIndent - the extra indent for the new level
    • open

      public void open(Indent plusIndent, BreakBehaviour breakBehaviour, LastLevelBreakability breakabilityIfLastLevel)
      Open a new level by emitting an OpenOp.
      Parameters:
      plusIndent - the extra indent for the new level
      breakBehaviour - how to decide whether to break this level or not
      breakabilityIfLastLevel - if last level, when to break this rather than parent
    • open

      public void open(OpenOp openOp)
    • close

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

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

      public boolean mostRecentTokenFollowedByNewline()
      Return whether the last token emitted is followed by a newline.
    • peekToken

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

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

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

      public void space()
    • breakOp

      public void breakOp()
      Emit a Break.
    • breakOp

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

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

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

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

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

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

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

      public void breakOp(FillMode fillMode, String flat, Indent plusIndent, Optional<BreakTag> optionalTag)
      Emit a generic Break.
      Parameters:
      fillMode - the FillMode
      flat - the Break when not broken
      plusIndent - extra indent if taken
      optionalTag - an optional tag for remembering whether the break was taken
    • breakOp

      public void breakOp(Break breakOp)
      Emit a self-built Break.
    • 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 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 OpsBuilder.OpsOutput build()
      Build a list of Ops from the OpsBuilder.
    • toString

      public String toString()
      Overrides:
      toString in class Object