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 intprotected booleanprotected intTrack char position in the line (later we can think about tabs).protected intprotected Liststack of indents; use List as it's much faster than Stack.protected intprotected String\n or \r\n?protected WriterFields inherited from interface StringTemplateWriter
NO_WRAP -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintprotected StringBuffergetIndentString(int spaces) intindent()intindent(int spaces) voidvoidvoidpushIndentation(String indent) Push even blank (null) indents as they are like scopes; must be able to pop them back off stack.voidsetLineWidth(int lineWidth) intWrite out a string literal or attribute expression or expression element.intWrite out a string literal or attribute expression or expression element.intwriteSeparator(String str) Write a separator.intwriteWrapSeparator(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:
setLineWidthin 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:
pushIndentationin interfaceStringTemplateWriter
-
popIndentation
- Specified by:
popIndentationin interfaceStringTemplateWriter
-
pushAnchorPoint
public void pushAnchorPoint()- Specified by:
pushAnchorPointin interfaceStringTemplateWriter
-
popAnchorPoint
public void popAnchorPoint()- Specified by:
popAnchorPointin interfaceStringTemplateWriter
-
getIndentationWidth
public int getIndentationWidth() -
write
Write out a string literal or attribute expression or expression element.- Specified by:
writein interfaceStringTemplateWriter- Throws:
IOException
-
writeSeparator
Description copied from interface:StringTemplateWriterWrite a separator. Same as write() except that a \n cannot be inserted before emitting a separator.- Specified by:
writeSeparatorin 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:
writein interfaceStringTemplateWriter- Throws:
IOException
-
writeWrapSeparator
Description copied from interface:StringTemplateWriterBecause 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:
writeWrapSeparatorin interfaceStringTemplateWriter- Throws:
IOException
-
indent
- Throws:
IOException
-
indent
- Throws:
IOException
-
getIndentString
-