Class AutoIndentWriter

java.lang.Object
org.antlr.stringtemplate.AutoIndentWriter
All Implemented Interfaces:
StringTemplateWriter
Direct Known Subclasses:
NoIndentWriter

public class AutoIndentWriter extends 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 Details

    • indents

      protected 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 String newline
      \n or \r\n?
    • out

      protected 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 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

      public AutoIndentWriter(Writer out, String newline)
    • AutoIndentWriter

      public AutoIndentWriter(Writer out)
  • Method Details