AutoIndentWriter
org.antlr.stringtemplate
Class AutoIndentWriter
java.lang.Object
org.antlr.stringtemplate.AutoIndentWriter
- All Implemented Interfaces:
- StringTemplateWriter
- Direct Known Subclasses:
- NoIndentWriter
- public class AutoIndentWriter
- extends java.lang.Object
- implements StringTemplateWriter
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 |
protected int[] |
anchors
Stack of integer anchors (char positions in line); avoid Integer
creation overhead. |
protected int |
anchors_sp
|
protected boolean |
atStartOfLine
|
protected int |
charPosition
Track char position in the line (later we can think about tabs). |
protected int |
charPositionOfStartOfExpr
|
protected java.util.List |
indents
stack of indents; use List as it's much faster than Stack. |
protected int |
lineWidth
|
protected java.lang.String |
newline
\n or \r\n? |
protected java.io.Writer |
out
|
|
Method Summary |
int |
getIndentationWidth()
|
int |
indent()
|
int |
indent(int spaces)
|
void |
popAnchorPoint()
|
java.lang.String |
popIndentation()
|
void |
pushAnchorPoint()
|
void |
pushIndentation(java.lang.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(java.lang.String str)
Write out a string literal or attribute expression or expression element. |
int |
write(java.lang.String str,
java.lang.String wrap)
Write out a string literal or attribute expression or expression element. |
int |
writeSeparator(java.lang.String str)
Write a separator. |
int |
writeWrapSeparator(java.lang.String wrap)
Because we might need to wrap at a non-atomic string boundary
(such as when we wrap in between template applications
]}; wrap>) we need to expose the wrap string
writing just like for the separator. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
indents
protected java.util.List indents
- stack of indents; use List as it's much faster than Stack. Grows
from 0..n-1. List
anchors
protected int[] anchors
- Stack of integer anchors (char positions in line); avoid Integer
creation overhead.
anchors_sp
protected int anchors_sp
newline
protected java.lang.String newline
- \n or \r\n?
out
protected java.io.Writer out
atStartOfLine
protected boolean atStartOfLine
charPosition
protected int charPosition
- Track char position in the line (later we can think about tabs).
Indexed from 0. We want to keep charPosition <= lineWidth.
This is the position we are *about* to write not the position
last written to.
lineWidth
protected int lineWidth
charPositionOfStartOfExpr
protected int charPositionOfStartOfExpr
AutoIndentWriter
public AutoIndentWriter(java.io.Writer out,
java.lang.String newline)
AutoIndentWriter
public AutoIndentWriter(java.io.Writer out)
setLineWidth
public void setLineWidth(int lineWidth)
- Specified by:
setLineWidth in interface StringTemplateWriter
pushIndentation
public void pushIndentation(java.lang.String indent)
- Push even blank (null) indents as they are like scopes; must
be able to pop them back off stack.
- Specified by:
pushIndentation in interface StringTemplateWriter
popIndentation
public java.lang.String popIndentation()
- Specified by:
popIndentation in interface StringTemplateWriter
pushAnchorPoint
public void pushAnchorPoint()
- Specified by:
pushAnchorPoint in interface StringTemplateWriter
popAnchorPoint
public void popAnchorPoint()
- Specified by:
popAnchorPoint in interface StringTemplateWriter
getIndentationWidth
public int getIndentationWidth()
write
public int write(java.lang.String str)
throws java.io.IOException
- Write out a string literal or attribute expression or expression element.
- Specified by:
write in interface StringTemplateWriter
- Throws:
java.io.IOException
writeSeparator
public int writeSeparator(java.lang.String str)
throws java.io.IOException
- 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 interface StringTemplateWriter
- Throws:
java.io.IOException
write
public int write(java.lang.String str,
java.lang.String wrap)
throws java.io.IOException
- 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 interface StringTemplateWriter
- Throws:
java.io.IOException
writeWrapSeparator
public int writeWrapSeparator(java.lang.String wrap)
throws java.io.IOException
- 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
]}; wrap>) we need to expose the wrap string
writing just like for the separator.
- Specified by:
writeWrapSeparator in interface StringTemplateWriter
- Throws:
java.io.IOException
indent
public int indent()
throws java.io.IOException
- Throws:
java.io.IOException
indent
public int indent(int spaces)
throws java.io.IOException
- Throws:
java.io.IOException