@@ -82,43 +82,62 @@ public Unparser(Context context, ColorSetting color, Color terminalColor) {
8282 public int compare (ASTNode o1 , ASTNode o2 ) {
8383 Deque <Component > thisStack = new LinkedList <>();
8484 Deque <Component > thatStack = new LinkedList <>();
85- StringBuilder thisString = new StringBuilder ();
86- StringBuilder thatString = new StringBuilder ();
85+ Indenter thisString = new Indenter ();
86+ thisString .setWidth (-1 );
87+ Indenter thatString = new Indenter ();
88+ thisString .setWidth (-1 );
8789 thisStack .push (new TermComponent (o1 ));
8890 thatStack .push (new TermComponent (o2 ));
91+ int lastIdx = 0 ;
8992 while (true ) {
9093 processStack (thisStack , thisString );
9194 processStack (thatStack , thatString );
9295 int lim = Math .min (thisString .length (), thatString .length ());
93- String s1 = thisString .substring (0 , lim );
94- String s2 = thisString .substring (0 , lim );
96+ String s1 = thisString .stringBuilder .substring (lastIdx , lim );
97+ String s2 = thatString .stringBuilder .substring (lastIdx , lim );
98+ lastIdx = lim ;
9599 int result = comparator .compare (s1 , s2 );
96100 if (result != 0 ) return result ;
97- thisString .delete (0 , lim );
98- thatString .delete (0 , lim );
99101 if (thisStack .isEmpty () && thatStack .isEmpty ()) {
100- return comparator .compare (thisString .toString ( ), thatString .toString ( ));
102+ return comparator .compare (thisString .stringBuilder . substring ( lim ), thatString .stringBuilder . substring ( lim ));
101103 }
102104 }
103105 }
104106
105- public String print (ASTNode node ) {
107+ public String print (ASTNode node , IndentationOptions indent ) {
106108 Deque <Component > stack = new LinkedList <>();
107- StringBuilder string = new StringBuilder ( );
109+ Indenter string = new Indenter ( indent );
108110 stack .push (new TermComponent (node ));
109111 while (!stack .isEmpty ()) {
110112 processStack (stack , string );
111113 }
112114 return string .toString ();
113115 }
114116
115- private void processStack (Deque <Component > stack , StringBuilder string ) {
117+ private void processStack (Deque <Component > stack , Indenter string ) {
116118 if (stack .isEmpty ()) {
117119 return ;
118120 }
119121 Component comp = stack .pop ();
120122 if (comp instanceof StringComponent ) {
121- string .append (((StringComponent )comp ).s );
123+ string .write (((StringComponent )comp ).s );
124+ return ;
125+ }
126+ if (comp instanceof FormatComponent ) {
127+ switch (((FormatComponent ) comp ).format ) {
128+ case INDENT :
129+ string .indent (4 );
130+ break ;
131+ case DEDENT :
132+ string .unindent ();
133+ break ;
134+ case INDENT_TO_CURRENT :
135+ string .indentToCurrent ();
136+ break ;
137+ case NEWLINE :
138+ string .endLine ();
139+ break ;
140+ }
122141 return ;
123142 }
124143 ASTNode term = ((TermComponent ) comp ).term ;
@@ -138,6 +157,11 @@ private static class StringComponent implements Component {
138157 public StringComponent (String s ) {
139158 this .s = s ;
140159 }
160+
161+ @ Override
162+ public String toString () {
163+ return s ;
164+ }
141165 }
142166
143167 private static class TermComponent implements Component {
@@ -146,6 +170,11 @@ private static class TermComponent implements Component {
146170 public TermComponent (ASTNode term ) {
147171 this .term = term ;
148172 }
173+
174+ @ Override
175+ public String toString () {
176+ return term .toString ();
177+ }
149178 }
150179
151180 private static enum Format {
@@ -158,6 +187,11 @@ private static class FormatComponent implements Component {
158187 public FormatComponent (Format format ) {
159188 this .format = format ;
160189 }
190+
191+ @ Override
192+ public String toString () {
193+ return format .toString ();
194+ }
161195 }
162196
163197 private class ComponentVisitor extends NonCachingVisitor {
0 commit comments