Class SourcePrinter

java.lang.Object
com.github.javaparser.printer.SourcePrinter

public class SourcePrinter extends Object
A support class for code that outputs formatted source code.
  • Method Details

    • indent

      public SourcePrinter indent()
      Add the default indentation to the current indentation and push it on the indentation stack. Does not actually output anything.
    • indentWithAlignTo

      public SourcePrinter indentWithAlignTo(int column)
      Add to the current indentation until it is reaches "column" and push it on the indentation stack. Does not actually output anything.
    • unindent

      public SourcePrinter unindent()
      Pop the last indentation of the indentation stack. Does not actually output anything.
    • print

      public SourcePrinter print(String arg)
      Append the source string passed as argument to the buffer. If this is being appended at the beginning of a line, performs indentation first.

      The source line to be printed should not contain newline/carriage-return characters; use println(String) to automatically append a newline at the end of the source string. If the source line passed as argument contains newline/carriage-return characters would impredictably affect a correct computation of the current getCursor() position.

      Parameters:
      arg - source line to be printed (should not contain newline/carriage-return characters)
      Returns:
      this instance, for nesting calls to method as fluent interface
      See Also:
      println(String)
    • println

      public SourcePrinter println(String arg)
      Append the source string passed as argument to the buffer, then append a newline. If this is being appended at the beginning of a line, performs indentation first.

      The source line to be printed should not contain newline/carriage-return characters. If the source line passed as argument contains newline/carriage-return characters would impredictably affect a correct computation of the current getCursor() position.

      Parameters:
      arg - source line to be printed (should not contain newline/carriage-return characters)
      Returns:
      this instance, for nesting calls to method as fluent interface
    • println

      public SourcePrinter println()
      Append a newline to the buffer.
      Returns:
      this instance, for nesting calls to method as fluent interface
    • getCursor

      public Position getCursor()
      Return the current cursor position (line, column) in the source printer buffer.

      Please notice in order to guarantee a correct computation of the cursor position, this printer expect the contracts of the methods print(String) and println(String) has been respected through all method calls, meaning the source string passed as argument to those method calls did not contain newline/carriage-return characters.

      Returns:
      the current cursor position (line, column).
    • getSource

      @Deprecated public String getSource()
      Deprecated.
      use toString()
      Returns:
      the currently printed source code.
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      the currently printed source code.
    • normalizeEolInTextBlock

      public String normalizeEolInTextBlock(String content)
      Changes all EOL characters in "content" to the EOL character this SourcePrinter is using.
    • reindentWithAlignToCursor

      public void reindentWithAlignToCursor()
      Set the top-most indent to the column the cursor is currently in, can be undone with reindentToPreviousLevel(). Does not actually output anything.
    • reindentToPreviousLevel

      public void reindentToPreviousLevel()
      Set the top-most indent to the column the cursor was before the last reindentWithAlignToCursor() call. Does not actually output anything.
    • duplicateIndent

      public void duplicateIndent()
      Adds an indent to the top of the stack that is a copy of the current top indent. With this you announce "I'm going to indent the next line(s)" but not how far yet. Once you do know, you can pop this indent ("unindent") and indent to the right column. (Does not actually output anything.)