Top Description Fields Constructors Methods
org.antlr.stringtemplate

public Class AutoIndentWriter

extends Object
implements StringTemplateWriter
Class Inheritance
All Implemented Interfaces
org.antlr.stringtemplate.StringTemplateWriter
Known Direct Subclasses
org.antlr.stringtemplate.NoIndentWriter
Imports
java.io.IOException, .Writer, java.util.ArrayList, .List

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

Modifier and TypeField and Description
protected int[]
anchors

Stack of integer anchors (char positions in line); avoid Integer creation overhead.

protected int
protected boolean
protected int
charPosition

Track char position in the line (later we can think about tabs).

protected int
protected List<E>
indents

stack of indents; use List as it's much faster than Stack.

protected int
protected String
newline

\n or \r\n?

protected Writer

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
public int
protected StringBuffer
getIndentString(int spaces)

public int
public int
indent(int spaces)

public void
public String
public void
public void
pushIndentation(String indent)

Implements org.antlr.stringtemplate.StringTemplateWriter.pushIndentation.

Push even blank (null) indents as they are like scopes; must be able to pop them back off stack.
public void
public int
write(String str)

Implements org.antlr.stringtemplate.StringTemplateWriter.write.

Write out a string literal or attribute expression or expression element.
public int
write(String str, String wrap)

Implements org.antlr.stringtemplate.StringTemplateWriter.write.

Write out a string literal or attribute expression or expression element.
public int
public int
writeWrapSeparator(String wrap)

Implements org.antlr.stringtemplate.StringTemplateWriter.writeWrapSeparator.

Because we might need to wrap at a non-atomic string boundary (such as when we wrap in between template applications <data:{v|[) we need to expose the wrap string writing just like for the separator.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

anchorsback to summary
protected int[] anchors

Stack of integer anchors (char positions in line); avoid Integer creation overhead.

anchors_spback to summary
protected int anchors_sp
atStartOfLineback to summary
protected boolean atStartOfLine
charPositionback to summary
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.

charPositionOfStartOfExprback to summary
protected int charPositionOfStartOfExpr
indentsback to summary
protected List<E> indents

stack of indents; use List as it's much faster than Stack. Grows from 0..n-1. List

lineWidthback to summary
protected int lineWidth
newlineback to summary
protected String newline

\n or \r\n?

outback to summary
protected Writer out

Constructor Detail

AutoIndentWriterback to summary
public AutoIndentWriter(Writer out, String newline)
AutoIndentWriterback to summary
public AutoIndentWriter(Writer out)

Method Detail

getIndentationWidthback to summary
public int getIndentationWidth()
getIndentStringback to summary
protected StringBuffer getIndentString(int spaces)
indentback to summary
public int indent() throws IOException
indentback to summary
public int indent(int spaces) throws IOException
popAnchorPointback to summary
public void popAnchorPoint()

Implements org.antlr.stringtemplate.StringTemplateWriter.popAnchorPoint.

popIndentationback to summary
public String popIndentation()

Implements org.antlr.stringtemplate.StringTemplateWriter.popIndentation.

pushAnchorPointback to summary
public void pushAnchorPoint()

Implements org.antlr.stringtemplate.StringTemplateWriter.pushAnchorPoint.

pushIndentationback to summary
public void pushIndentation(String indent)

Implements org.antlr.stringtemplate.StringTemplateWriter.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.

setLineWidthback to summary
public void setLineWidth(int lineWidth)

Implements org.antlr.stringtemplate.StringTemplateWriter.setLineWidth.

writeback to summary
public int write(String str) throws IOException

Implements org.antlr.stringtemplate.StringTemplateWriter.write.

Write out a string literal or attribute expression or expression element.

writeback to summary
public int write(String str, String wrap) throws IOException

Implements org.antlr.stringtemplate.StringTemplateWriter.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.

writeSeparatorback to summary
public int writeSeparator(String str) throws IOException

Implements org.antlr.stringtemplate.StringTemplateWriter.writeSeparator.

Doc from org.antlr.stringtemplate.StringTemplateWriter.writeSeparator.

Write a separator. Same as write() except that a \n cannot be inserted before emitting a separator.

writeWrapSeparatorback to summary
public int writeWrapSeparator(String wrap) throws IOException

Implements org.antlr.stringtemplate.StringTemplateWriter.writeWrapSeparator.

Doc from org.antlr.stringtemplate.StringTemplateWriter.writeWrapSeparator.

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.