Essentially a char filter that knows how to auto-indent output by maintaining a stack of indent levels. 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 wo
| 55 | |
| 56 | |
| 57 | public class AutoIndentWriter implements STWriter { |
| 58 | /** Stack of indents. Use {@link List} as it's much faster than {@link Stack}. Grows |
| 59 | * from 0..n-1. |
| 60 | */ |
| 61 | public List<String> indents = new ArrayList<String>(); |
| 62 | |
| 63 | /** Stack of integer anchors (char positions in line); avoid {@link Integer} |
| 64 | * creation overhead. |
| 65 | */ |
| 66 | public int[] anchors = new int[10]; |
| 67 | public int anchors_sp = -1; |
| 68 | |
| 69 | /** {@code \n} or {@code \r\n}? */ |
| 70 | public String newline; |
| 71 | public Writer out = null; |
| 72 | public boolean atStartOfLine = true; |
| 73 | |
| 74 | /** |
| 75 | * Track char position in the line (later we can think about tabs). Indexed |
| 76 | * from 0. We want to keep {@code charPosition <= }{@link #lineWidth}. |
| 77 | * This is the position we are <em>about</em> to write, not the position |
| 78 | * last written to. |
| 79 | */ |
| 80 | public int charPosition = 0; |
| 81 | |
| 82 | /** The absolute char index into the output of the next char to be written. */ |
| 83 | public int charIndex = 0; |
| 84 | public int lineWidth = NO_WRAP; |
| 85 | public AutoIndentWriter(Writer out, String newline) { |
| 86 | this.out = out; |
| 87 | indents.add(null); // s oftart with no indent |
| 88 | this.newline = newline; |
| 89 | } |
| 90 | |
| 91 | public AutoIndentWriter(Writer out) { |
| 92 | this(out, System.getProperty("line.separator")); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void setLineWidth(int lineWidth) { |
| 97 | this.lineWidth = lineWidth; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void pushIndentation(String indent) { |
| 102 | indents.add(indent); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public String popIndentation() { |
| 107 | return indents.remove(indents.size()-1); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void pushAnchorPoint() { |
| 112 | if ( (anchors_sp+1) >= anchors.length ) { |
| 113 | int[] a = new int[anchors.length*2]; |
| 114 | System.arraycopy(anchors, 0, a, 0, anchors.length-1); |
nothing calls this directly
no outgoing calls
no test coverage detected