1818
1919import com .google .common .base .CharMatcher ;
2020import com .google .common .base .MoreObjects ;
21+ import com .google .common .base .Strings ;
2122import com .google .common .collect .DiscreteDomain ;
2223import com .google .common .collect .ImmutableList ;
2324import com .google .common .collect .Range ;
@@ -55,9 +56,9 @@ public final class JavaOutput extends Output {
5556 private final int kN ; // The number of tokens or comments in the input, excluding the EOF.
5657 private int iLine = 0 ; // Closest corresponding line number on input.
5758 private int lastK = -1 ; // Last {@link Tok} index output.
58- private int spacesPending = 0 ;
5959 private int newlinesPending = 0 ;
6060 private StringBuilder lineBuilder = new StringBuilder ();
61+ private StringBuilder spacesPending = new StringBuilder ();
6162
6263 /**
6364 * {@code JavaOutput} constructor.
@@ -121,23 +122,26 @@ public void append(String text, Range<Integer> range) {
121122 if (newlinesPending == 0 ) {
122123 ++newlinesPending ;
123124 }
124- spacesPending = 0 ;
125+ spacesPending = new StringBuilder () ;
125126 } else {
126127 boolean rangesSet = false ;
127128 int textN = text .length ();
128129 for (int i = 0 ; i < textN ; i ++) {
129130 char c = text .charAt (i );
130131 switch (c ) {
131132 case ' ' :
132- ++spacesPending ;
133+ spacesPending .append (' ' );
134+ break ;
135+ case '\t' :
136+ spacesPending .append ('\t' );
133137 break ;
134138 case '\r' :
135139 if (i + 1 < text .length () && text .charAt (i + 1 ) == '\n' ) {
136140 i ++;
137141 }
138142 // falls through
139143 case '\n' :
140- spacesPending = 0 ;
144+ spacesPending = new StringBuilder () ;
141145 ++newlinesPending ;
142146 break ;
143147 default :
@@ -150,9 +154,9 @@ public void append(String text, Range<Integer> range) {
150154 rangesSet = false ;
151155 --newlinesPending ;
152156 }
153- while (spacesPending > 0 ) {
154- lineBuilder .append (' ' );
155- -- spacesPending ;
157+ if (spacesPending . length () > 0 ) {
158+ lineBuilder .append (spacesPending );
159+ spacesPending = new StringBuilder () ;
156160 }
157161 lineBuilder .append (c );
158162 if (!range .isEmpty ()) {
@@ -174,7 +178,7 @@ public void append(String text, Range<Integer> range) {
174178
175179 @ Override
176180 public void indent (int indent ) {
177- spacesPending = indent ;
181+ spacesPending . append ( Strings . repeat ( " " , indent )) ;
178182 }
179183
180184 /** Flush any incomplete last line, then add the EOF token into our data structures. */
@@ -387,7 +391,7 @@ public String toString() {
387391 return MoreObjects .toStringHelper (this )
388392 .add ("iLine" , iLine )
389393 .add ("lastK" , lastK )
390- .add ("spacesPending" , spacesPending )
394+ .add ("spacesPending" , spacesPending . toString (). replace ( " \t " , " \\ t" ) )
391395 .add ("newlinesPending" , newlinesPending )
392396 .add ("blankLines" , blankLines )
393397 .add ("super" , super .toString ())
0 commit comments