@@ -50,9 +50,11 @@ public final class JavaOutput extends Output {
5050 private final InputMetadata inputMetadata ;
5151 private int iLine = 0 ; // Closest corresponding line number on input.
5252 private int lastK = -1 ; // Last {@link Tok} index output.
53- private int spacesPending = 0 ;
5453 private int newlinesPending = 0 ;
5554 private StringBuilder lineBuilder = new StringBuilder ();
55+ // Spaces and tabs seen since the last non-blank character. Written out in front of the next one, so that a line
56+ // never ends in whitespace: trailing tabs inside a comment were kept before, and only a second run removed them.
57+ private StringBuilder spacesPending = new StringBuilder ();
5658
5759 /**
5860 * {@code JavaOutput} constructor.
@@ -99,23 +101,26 @@ public void append(State state, String text, Range<Integer> range) {
99101 if (newlinesPending == 0 ) {
100102 ++newlinesPending ;
101103 }
102- spacesPending = 0 ;
104+ spacesPending = new StringBuilder () ;
103105 } else {
104106 boolean rangesSet = false ;
105107 int textN = text .length ();
106108 for (int i = 0 ; i < textN ; i ++) {
107109 char c = text .charAt (i );
108110 switch (c ) {
109111 case ' ' :
110- ++spacesPending ;
112+ spacesPending .append (' ' );
113+ break ;
114+ case '\t' :
115+ spacesPending .append ('\t' );
111116 break ;
112117 case '\r' :
113118 if (i + 1 < text .length () && text .charAt (i + 1 ) == '\n' ) {
114119 i ++;
115120 }
116121 // falls through
117122 case '\n' :
118- spacesPending = 0 ;
123+ spacesPending = new StringBuilder () ;
119124 ++newlinesPending ;
120125 break ;
121126 default :
@@ -128,9 +133,9 @@ public void append(State state, String text, Range<Integer> range) {
128133 rangesSet = false ;
129134 --newlinesPending ;
130135 }
131- while (spacesPending > 0 ) {
132- lineBuilder .append (' ' );
133- -- spacesPending ;
136+ if (spacesPending . length () > 0 ) {
137+ lineBuilder .append (spacesPending );
138+ spacesPending = new StringBuilder () ;
134139 }
135140 lineBuilder .append (c );
136141 if (!range .isEmpty ()) {
@@ -152,7 +157,7 @@ public void append(State state, String text, Range<Integer> range) {
152157
153158 @ Override
154159 public void indent (int indent ) {
155- spacesPending = indent ;
160+ spacesPending . append ( " " . repeat ( indent )) ;
156161 }
157162
158163 /** Flush any incomplete last line, then add the EOF token into our data structures. */
@@ -354,7 +359,7 @@ public String toString() {
354359 return MoreObjects .toStringHelper (this )
355360 .add ("iLine" , iLine )
356361 .add ("lastK" , lastK )
357- .add ("spacesPending" , spacesPending )
362+ .add ("spacesPending" , spacesPending . toString (). replace ( " \t " , " \\ t" ) )
358363 .add ("newlinesPending" , newlinesPending )
359364 .add ("inputMetadata" , inputMetadata )
360365 .add ("super" , super .toString ())
0 commit comments