Package org.antlr.stringtemplate
Class AutoIndentWriter
java.lang.Object
org.antlr.stringtemplate.AutoIndentWriter
- All Implemented Interfaces:
StringTemplateWriter
- Direct Known Subclasses:
NoIndentWriter
Essentially a char filter that knows how to auto-indent output
by maintaining a stack of indent levels. I set a flag upon newline
and then next nonwhitespace char resets flag and spits out indention.
The indent stack is a stack of strings so we can repeat original indent
not just the same number of columns (don't have to worry about tabs vs
spaces then).
Anchors are char positions (tabs won't work) that indicate where all
future wraps should justify to. The wrap position is actually the
larger of either the last anchor or the indentation level.
This is a filter on a Writer.
\n is the proper way to say newline for options and templates.
Templates can mix them but use \n for sure and options like
wrap="\n". ST will generate the right thing. Override the default (locale)
newline by passing in a string to the constructor.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]
Stack of integer anchors (char positions in line); avoid Integer creation overhead.protected int
protected boolean
protected int
Track char position in the line (later we can think about tabs).protected int
protected List
stack of indents; use List as it's much faster than Stack.protected int
protected String
\n or \r\n?protected Writer
Fields inherited from interface org.antlr.stringtemplate.StringTemplateWriter
NO_WRAP
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
protected StringBuffer
getIndentString
(int spaces) int
indent()
int
indent
(int spaces) void
void
void
pushIndentation
(String indent) Push even blank (null) indents as they are like scopes; must be able to pop them back off stack.void
setLineWidth
(int lineWidth) int
Write out a string literal or attribute expression or expression element.int
Write out a string literal or attribute expression or expression element.int
writeSeparator
(String str) Write a separator.int
writeWrapSeparator
(String wrap) Because we might need to wrap at a non-atomic string boundary (such as when we wrap in between template applications invalid input: '<'data:{v|[]}; wrap>) we need to expose the wrap string writing just like for the separator.
-
Field Details
-
indents
stack of indents; use List as it's much faster than Stack. Grows from 0..n-1. List -
anchors
protected int[] anchorsStack of integer anchors (char positions in line); avoid Integer creation overhead. -
anchors_sp
protected int anchors_sp -
newline
\n or \r\n? -
out
-
atStartOfLine
protected boolean atStartOfLine -
charPosition
protected int charPositionTrack char position in the line (later we can think about tabs). Indexed from 0. We want to keep charPosition invalid input: '<'= lineWidth. This is the position we are *about* to write not the position last written to. -
lineWidth
protected int lineWidth -
charPositionOfStartOfExpr
protected int charPositionOfStartOfExpr
-
-
Constructor Details
-
AutoIndentWriter
-
AutoIndentWriter
-
-
Method Details
-
setLineWidth
public void setLineWidth(int lineWidth) - Specified by:
setLineWidth
in interfaceStringTemplateWriter
-
pushIndentation
Push even blank (null) indents as they are like scopes; must be able to pop them back off stack. To deal with combined anchors and indentation, force indents to include any current anchor point. If current anchor is beyond current indent width, add the difference to the indent to be added. This prevents a check later to deal with anchors when starting new line.- Specified by:
pushIndentation
in interfaceStringTemplateWriter
-
popIndentation
- Specified by:
popIndentation
in interfaceStringTemplateWriter
-
pushAnchorPoint
public void pushAnchorPoint()- Specified by:
pushAnchorPoint
in interfaceStringTemplateWriter
-
popAnchorPoint
public void popAnchorPoint()- Specified by:
popAnchorPoint
in interfaceStringTemplateWriter
-
getIndentationWidth
public int getIndentationWidth() -
write
Write out a string literal or attribute expression or expression element.- Specified by:
write
in interfaceStringTemplateWriter
- Throws:
IOException
-
writeSeparator
Description copied from interface:StringTemplateWriter
Write a separator. Same as write() except that a \n cannot be inserted before emitting a separator.- Specified by:
writeSeparator
in interfaceStringTemplateWriter
- Throws:
IOException
-
write
Write out a string literal or attribute expression or expression element. If doing line wrap, then check wrap before emitting this str. If at or beyond desired line width then emit a \n and any indentation before spitting out this str.- Specified by:
write
in interfaceStringTemplateWriter
- Throws:
IOException
-
writeWrapSeparator
Description copied from interface:StringTemplateWriter
Because we might need to wrap at a non-atomic string boundary (such as when we wrap in between template applications invalid input: '<'data:{v|[]}; wrap>) we need to expose the wrap string writing just like for the separator. - Specified by:
writeWrapSeparator
in interfaceStringTemplateWriter
- Throws:
IOException
-
indent
- Throws:
IOException
-
indent
- Throws:
IOException
-
getIndentString
-