Class SourcePrinter
- java.lang.Object
-
- com.github.javaparser.printer.SourcePrinter
-
public class SourcePrinter extends java.lang.Object
A support class for code that outputs formatted source code.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringBuilder
buf
private Position
cursor
private java.lang.String
endOfLineCharacter
private java.lang.String
indentation
private boolean
indented
private java.util.Deque<java.lang.String>
indents
private PrettyPrinterConfiguration.IndentType
indentType
private java.lang.String
lastPrintedIndent
private java.util.Deque<java.lang.String>
reindentedIndents
private int
tabWidth
-
Constructor Summary
Constructors Constructor Description SourcePrinter()
SourcePrinter(PrettyPrinterConfiguration configuration)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private void
append(java.lang.String arg)
private java.lang.String
calculateIndentWithAlignTo(int column)
void
duplicateIndent()
Adds an indent to the top of the stack that is a copy of the current top indent.Position
getCursor()
Return the current cursor position (line, column) in the source printer buffer.java.lang.String
getSource()
Deprecated.use toString()SourcePrinter
indent()
Add the default indentation to the current indentation and push it on the indentation stack.SourcePrinter
indentWithAlignTo(int column)
Add to the current indentation until it is reaches "column" and push it on the indentation stack.java.lang.String
normalizeEolInTextBlock(java.lang.String content)
Changes all EOL characters in "content" to the EOL character this SourcePrinter is using.SourcePrinter
print(java.lang.String arg)
Append the source string passed as argument to the buffer.SourcePrinter
println()
Append a newline to the buffer.SourcePrinter
println(java.lang.String arg)
Append the source string passed as argument to the buffer, then append a newline.void
reindentToPreviousLevel()
Set the top-most indent to the column the cursor was before the lastreindentWithAlignToCursor()
call.void
reindentWithAlignToCursor()
Set the top-most indent to the column the cursor is currently in, can be undone withreindentToPreviousLevel()
.java.lang.String
toString()
SourcePrinter
unindent()
Pop the last indentation of the indentation stack.
-
-
-
Field Detail
-
endOfLineCharacter
private final java.lang.String endOfLineCharacter
-
indentation
private final java.lang.String indentation
-
tabWidth
private final int tabWidth
-
indentType
private final PrettyPrinterConfiguration.IndentType indentType
-
indents
private final java.util.Deque<java.lang.String> indents
-
reindentedIndents
private final java.util.Deque<java.lang.String> reindentedIndents
-
lastPrintedIndent
private java.lang.String lastPrintedIndent
-
buf
private final java.lang.StringBuilder buf
-
cursor
private Position cursor
-
indented
private boolean indented
-
-
Constructor Detail
-
SourcePrinter
SourcePrinter()
-
SourcePrinter
SourcePrinter(PrettyPrinterConfiguration configuration)
-
-
Method Detail
-
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.
-
calculateIndentWithAlignTo
private java.lang.String calculateIndentWithAlignTo(int column)
-
unindent
public SourcePrinter unindent()
Pop the last indentation of the indentation stack. Does not actually output anything.
-
append
private void append(java.lang.String arg)
-
print
public SourcePrinter print(java.lang.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 currentgetCursor()
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(java.lang.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)
andprintln(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 java.lang.String getSource()
Deprecated.use toString()- Returns:
- the currently printed source code.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
- Returns:
- the currently printed source code.
-
normalizeEolInTextBlock
public java.lang.String normalizeEolInTextBlock(java.lang.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 withreindentToPreviousLevel()
. Does not actually output anything.
-
reindentToPreviousLevel
public void reindentToPreviousLevel()
Set the top-most indent to the column the cursor was before the lastreindentWithAlignToCursor()
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.)
-
-