11package org .antlr .codebuff ;
22
3- import com .google .common .base .CharMatcher ;
43import org .antlr .v4 .runtime .CommonToken ;
54import org .antlr .v4 .runtime .CommonTokenStream ;
65import org .antlr .v4 .runtime .ParserRuleContext ;
@@ -103,7 +102,7 @@ public String format() {
103102
104103
105104 realTokens = getRealTokens (tokens );
106- for (int i = 2 ; i <realTokens .size (); i ++) { // can't process first 2 tokens
105+ for (int i = CollectFeatures . ANALYSIS_START_TOKEN_INDEX ; i <realTokens .size (); i ++) { // can't process first 2 tokens
107106 int tokenIndexInStream = realTokens .get (i ).getTokenIndex ();
108107 processToken (i , tokenIndexInStream );
109108 }
@@ -130,13 +129,12 @@ public void processToken(int indexIntoRealTokens, int tokenIndexInStream) {
130129 }
131130 else if ( (injectNL_WS &0xFF )==CAT_INJECT_WS ) {
132131 ws = CollectFeatures .unwscat (injectNL_WS );
132+ if ( ws ==0 && cannotJoin (realTokens .get (indexIntoRealTokens -1 ), curToken ) ) { // failsafe!
133+ ws = 1 ;
134+ }
133135 }
134136
135- if ( ws ==0 && cannotJoin (realTokens .get (indexIntoRealTokens -1 ), curToken ) ) { // failsafe!
136- ws = 1 ;
137- }
138-
139- int align = CAT_NO_ALIGNMENT ;
137+ int alignOrIndent = CAT_NO_ALIGNMENT ;
140138
141139 if ( newlines >0 ) {
142140 output .append (Tool .newlines (newlines ));
@@ -156,17 +154,17 @@ else if ( (injectNL_WS&0xFF)==CAT_INJECT_WS ) {
156154 // if we decide to inject a newline, we better recompute this value before classifying alignment
157155 features [INDEX_MATCHING_TOKEN_DIFF_LINE ] = getMatchingSymbolOnDiffLine (doc , node , line );
158156
159- align = alignClassifier .classify (k , features , corpus .align , MAX_CONTEXT_DIFF_THRESHOLD );
157+ alignOrIndent = alignClassifier .classify (k , features , corpus .align , MAX_CONTEXT_DIFF_THRESHOLD );
160158
161- if ( align ==CAT_INDENT ) {
159+ if ( alignOrIndent ==CAT_INDENT ) {
162160 if ( firstTokenOnPrevLine !=null ) { // if not on first line, we cannot indent
163161 int indentedCol = firstTokenOnPrevLine .getCharPositionInLine ()+INDENT_LEVEL ;
164162 charPosInLine = indentedCol ;
165163 output .append (Tool .spaces (indentedCol ));
166164 }
167165 }
168- else if ( (align &0xFF )==CAT_ALIGN_WITH_ANCESTOR_CHILD ) {
169- int [] deltaChild = CollectFeatures .unaligncat (align );
166+ else if ( (alignOrIndent &0xFF )==CAT_ALIGN_WITH_ANCESTOR_CHILD ) {
167+ int [] deltaChild = CollectFeatures .unaligncat (alignOrIndent );
170168 int deltaFromAncestor = deltaChild [0 ];
171169 int childIndex = deltaChild [1 ];
172170 ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (node , curToken );
@@ -181,16 +179,16 @@ else if ( child instanceof TerminalNode ){
181179 }
182180 else {
183181 // uh oh.
184- System .err .println ("Whoops. Tried access invalid child" );
182+ System .err .println ("Whoops. Tried to access invalid child" );
185183 }
186184 if ( start !=null ) {
187185 int indentCol = start .getCharPositionInLine ();
188186 charPosInLine = indentCol ;
189187 output .append (Tool .spaces (indentCol ));
190188 }
191189 }
192- else if ( (align &0xFF )==CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ) {
193- int deltaFromAncestor = CollectFeatures .unindentcat (align );
190+ else if ( (alignOrIndent &0xFF )==CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ) {
191+ int deltaFromAncestor = CollectFeatures .unindentcat (alignOrIndent );
194192 ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (node , curToken );
195193 ParserRuleContext ancestor = CollectFeatures .getAncestor (earliestLeftAncestor , deltaFromAncestor );
196194 Token start = ancestor .getStart ();
@@ -206,7 +204,7 @@ else if ( (align&0xFF)==CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ) {
206204 }
207205
208206 TokenPositionAnalysis tokenPositionAnalysis =
209- getTokenAnalysis (features , indexIntoRealTokens , tokenIndexInStream , newlines , align , ws );
207+ getTokenAnalysis (features , indexIntoRealTokens , tokenIndexInStream , injectNL_WS , alignOrIndent );
210208 analysis .setSize (tokenIndexInStream +1 );
211209 analysis .set (tokenIndexInStream , tokenPositionAnalysis );
212210
@@ -235,14 +233,7 @@ public void emitCommentsToTheLeft(int tokenIndexInStream) {
235233 List <Token > hiddenTokensToLeft = tokens .getHiddenTokensToLeft (tokenIndexInStream );
236234 if ( hiddenTokensToLeft !=null ) {
237235 // if at least one is not whitespace, assume it's a comment and print all hidden stuff including whitespace
238- boolean hasComment = false ;
239- for (Token hidden : hiddenTokensToLeft ) {
240- String hiddenText = hidden .getText ();
241- if ( !hiddenText .matches ("\\ s+" ) ) {
242- hasComment = true ;
243- break ;
244- }
245- }
236+ boolean hasComment = CollectFeatures .hasCommentToken (hiddenTokensToLeft );
246237 if ( hasComment ) {
247238 // avoid whitespace at end of sequence as we'll inject that
248239 int last = -1 ;
@@ -259,7 +250,7 @@ public void emitCommentsToTheLeft(int tokenIndexInStream) {
259250 String hiddenText = hidden .getText ();
260251 output .append (hiddenText );
261252 if ( hiddenText .matches ("\\ n+" ) ) {
262- line += CharMatcher . is ( '\n' ). countIn ( hiddenText );
253+ line += Tool . count ( hiddenText , '\n' );
263254 charPosInLine = 0 ;
264255 }
265256 else {
@@ -272,28 +263,20 @@ public void emitCommentsToTheLeft(int tokenIndexInStream) {
272263 }
273264
274265 public TokenPositionAnalysis getTokenAnalysis (int [] features , int indexIntoRealTokens , int tokenIndexInStream ,
275- int injectNewline ,
276- int align ,
277- int ws )
266+ int injectNL_WS , int alignOrIndent )
278267 {
279268 CommonToken curToken = (CommonToken )tokens .get (tokenIndexInStream );
280269 // compare prediction of newline against original, alert about any diffs
281270 CommonToken prevToken = originalTokens .get (curToken .getTokenIndex ()-1 );
282271 CommonToken originalCurToken = originalTokens .get (curToken .getTokenIndex ());
283272
284- boolean failsafeTriggered = false ;
285- if ( ws ==0 && cannotJoin (realTokens .get (indexIntoRealTokens -1 ), curToken ) ) { // failsafe!
286- ws = 1 ;
287- failsafeTriggered = true ;
288- }
289-
290273 boolean prevIsWS = prevToken .getChannel ()==Token .HIDDEN_CHANNEL ; // assume this means whitespace
291274 int actualNL = Tool .count (prevToken .getText (), '\n' );
292275 String newlinePredictionString = String .format ("### line %d: predicted %d \\ n actual ?" ,
293- originalCurToken .getLine (), injectNewline , prevIsWS ? actualNL : "none" );
276+ originalCurToken .getLine (), injectNL_WS , prevIsWS ? actualNL : "none" );
294277 String alignPredictionString = String .format ("### line %d: predicted %d actual %s" ,
295278 originalCurToken .getLine (),
296- align ,
279+ alignOrIndent ,
297280 "?" );
298281
299282 String newlineAnalysis = newlinePredictionString +"\n " +
@@ -302,7 +285,7 @@ public TokenPositionAnalysis getTokenAnalysis(int[] features, int indexIntoRealT
302285 String alignAnalysis =alignPredictionString +"\n " +
303286 alignClassifier .getPredictionAnalysis (doc , k , features , corpus .align ,
304287 MAX_CONTEXT_DIFF_THRESHOLD );
305- return new TokenPositionAnalysis (newlineAnalysis , alignAnalysis , "n/a" );
288+ return new TokenPositionAnalysis (curToken , injectNL_WS , newlineAnalysis , alignOrIndent , alignAnalysis );
306289 }
307290
308291 /** Do not join two words like "finaldouble" or numbers like "3double",
0 commit comments