Commits

Tim Vernum committed 1cffb13

Make source printer do some indenting for readability

  • Participants
  • Parent commits d41f97c

Comments (0)

Files changed (1)

File parser/source/java/parser/org/adjective/syntactic/parser/util/PrintSourceVisitor.java

 import org.adjective.syntactic.parser.node.HasAnnotations;
 import org.adjective.syntactic.parser.node.HasModifiers;
 import org.adjective.syntactic.parser.node.HasTokens;
-import org.adjective.syntactic.parser.util.*;
 
+import java.io.IOException;
 import java.io.PrintStream;
 
 public class PrintSourceVisitor implements JavaParserVisitor<Object, PrintStream>
 {
+    private long _indent = 0;
+
+    private CharSequence getIndentString()
+    {
+        final StringBuilder builder = new StringBuilder();
+        return printIndent(builder);
+    }
+
+    private <T extends Appendable> T printIndent(T out)
+    {
+        for (int i = 0; i < _indent; i++)
+        {
+            try
+            {
+                out.append("  ");
+            }
+            catch (IOException e)
+            {
+                // swallow it...
+            }
+        }
+        return out;
+    }
+
     protected Object visitNode(Node node, PrintStream out)
     {
         return node.jjtAccept(this, out);
         }
     }
 
-    private Object printListSource(final PrintStream out, final Iterable<? extends Node> list, final String separator)
+    private Object printListSource(final PrintStream out, final Iterable<? extends Node> list, final CharSequence separator)
     {
         boolean first = true;
         for (Node node : list)
     @Override
     public Object visit(final ASTClassOrInterfaceBody node, final PrintStream out)
     {
+        _indent++;
         out.println("{");
+        printIndent(out);
         printListSource(out, node.getElements(), "");
+        _indent--;
+        printIndent(out);
         out.println("}");
         return null;
     }
     public Object visit(final ASTBlock node, final PrintStream out)
     {
         out.println("{");
-        printListSource(out, node.statements(), "");
+        _indent++;
+        printIndent(out);
+        printListSource(out, node.statements(), getIndentString());
+        _indent--;
+        printIndent(out);
         out.println("}");
         return null;
     }