Top Description Fields Constructors Methods
com.github.javaparser.printer

public Class SourcePrinter

extends Object
Class Inheritance
Imports
com.github.javaparser.Position, com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType, com.github.javaparser.utils.Utils, java.util.Deque, .LinkedList

A support class for code that outputs formatted source code.

Field Summary

Modifier and TypeField and Description
private final StringBuilder
private Position
private final String
private final String
private boolean
private final Deque<String>
private final PrettyPrinterConfiguration.IndentType
private String
private final Deque<String>
private final int

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv

Method Summary

Modifier and TypeMethod and Description
private void
private String
public void
duplicateIndent()

Adds an indent to the top of the stack that is a copy of the current top indent.

public Position

Returns:

the current cursor position (line, column).
getCursor
()

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

public String

Returns:

the currently printed source code.
getSource
()

Deprecated use toString()
public SourcePrinter
indent()

Add the default indentation to the current indentation and push it on the indentation stack.

public SourcePrinter
indentWithAlignTo(int column)

Add to the current indentation until it is reaches "column" and push it on the indentation stack.

public String
normalizeEolInTextBlock(String content)

Changes all EOL characters in "content" to the EOL character this SourcePrinter is using.

public SourcePrinter

Returns:

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

Append the source string passed as argument to the buffer, then append a newline.

public SourcePrinter

Returns:

this instance, for nesting calls to method as fluent interface
println
()

Append a newline to the buffer.

public void
reindentToPreviousLevel()

Set the top-most indent to the column the cursor was before the last reindentWithAlignToCursor() call.

public void
reindentWithAlignToCursor()

Set the top-most indent to the column the cursor is currently in, can be undone with reindentToPreviousLevel().

public String

Returns:

the currently printed source code.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public SourcePrinter
unindent()

Pop the last indentation of the indentation stack.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

bufback to summary
private final StringBuilder buf
cursorback to summary
private Position cursor
endOfLineCharacterback to summary
private final String endOfLineCharacter
indentationback to summary
private final String indentation
indentedback to summary
private boolean indented
indentsback to summary
private final Deque<String> indents
indentTypeback to summary
private final PrettyPrinterConfiguration.IndentType indentType
lastPrintedIndentback to summary
private String lastPrintedIndent
reindentedIndentsback to summary
private final Deque<String> reindentedIndents
tabWidthback to summary
private final int tabWidth

Constructor Detail

SourcePrinterback to summary
pack-priv SourcePrinter()
SourcePrinterback to summary
pack-priv SourcePrinter(final PrettyPrinterConfiguration configuration)

Method Detail

appendback to summary
private void append(String arg)
calculateIndentWithAlignToback to summary
private String calculateIndentWithAlignTo(int column)
duplicateIndentback to summary
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.)

getCursorback to summary
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:Position

the current cursor position (line, column).

getSourceback to summary
public String getSource()

Deprecated

use toString()

Returns:String

the currently printed source code.

Annotations
@Deprecated
indentback to summary
public SourcePrinter indent()

Add the default indentation to the current indentation and push it on the indentation stack. Does not actually output anything.

indentWithAlignToback to summary
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.

normalizeEolInTextBlockback to summary
public String normalizeEolInTextBlock(String content)

Changes all EOL characters in "content" to the EOL character this SourcePrinter is using.

printlnback to summary
public SourcePrinter println(final 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:String

source line to be printed (should not contain newline/carriage-return characters)

Returns:SourcePrinter

this instance, for nesting calls to method as fluent interface

printlnback to summary
public SourcePrinter println()

Append a newline to the buffer.

Returns:SourcePrinter

this instance, for nesting calls to method as fluent interface

reindentToPreviousLevelback to summary
public void reindentToPreviousLevel()

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

reindentWithAlignToCursorback to summary
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.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

the currently printed source code.

Annotations
@Override
unindentback to summary
public SourcePrinter unindent()

Pop the last indentation of the indentation stack. Does not actually output anything.