1
1
package org .antlr .codebuff ;
2
2
3
- import com .google .common .base .CharMatcher ;
4
3
import org .antlr .v4 .runtime .CommonToken ;
5
4
import org .antlr .v4 .runtime .CommonTokenStream ;
6
5
import org .antlr .v4 .runtime .ParserRuleContext ;
@@ -103,7 +102,7 @@ public String format() {
103
102
104
103
105
104
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
107
106
int tokenIndexInStream = realTokens .get (i ).getTokenIndex ();
108
107
processToken (i , tokenIndexInStream );
109
108
}
@@ -130,13 +129,12 @@ public void processToken(int indexIntoRealTokens, int tokenIndexInStream) {
130
129
}
131
130
else if ( (injectNL_WS &0xFF )==CAT_INJECT_WS ) {
132
131
ws = CollectFeatures .unwscat (injectNL_WS );
132
+ if ( ws ==0 && cannotJoin (realTokens .get (indexIntoRealTokens -1 ), curToken ) ) { // failsafe!
133
+ ws = 1 ;
134
+ }
133
135
}
134
136
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 ;
140
138
141
139
if ( newlines >0 ) {
142
140
output .append (Tool .newlines (newlines ));
@@ -156,17 +154,17 @@ else if ( (injectNL_WS&0xFF)==CAT_INJECT_WS ) {
156
154
// if we decide to inject a newline, we better recompute this value before classifying alignment
157
155
features [INDEX_MATCHING_TOKEN_DIFF_LINE ] = getMatchingSymbolOnDiffLine (doc , node , line );
158
156
159
- align = alignClassifier .classify (k , features , corpus .align , MAX_CONTEXT_DIFF_THRESHOLD );
157
+ alignOrIndent = alignClassifier .classify (k , features , corpus .align , MAX_CONTEXT_DIFF_THRESHOLD );
160
158
161
- if ( align ==CAT_INDENT ) {
159
+ if ( alignOrIndent ==CAT_INDENT ) {
162
160
if ( firstTokenOnPrevLine !=null ) { // if not on first line, we cannot indent
163
161
int indentedCol = firstTokenOnPrevLine .getCharPositionInLine ()+INDENT_LEVEL ;
164
162
charPosInLine = indentedCol ;
165
163
output .append (Tool .spaces (indentedCol ));
166
164
}
167
165
}
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 );
170
168
int deltaFromAncestor = deltaChild [0 ];
171
169
int childIndex = deltaChild [1 ];
172
170
ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (node , curToken );
@@ -181,16 +179,16 @@ else if ( child instanceof TerminalNode ){
181
179
}
182
180
else {
183
181
// uh oh.
184
- System .err .println ("Whoops. Tried access invalid child" );
182
+ System .err .println ("Whoops. Tried to access invalid child" );
185
183
}
186
184
if ( start !=null ) {
187
185
int indentCol = start .getCharPositionInLine ();
188
186
charPosInLine = indentCol ;
189
187
output .append (Tool .spaces (indentCol ));
190
188
}
191
189
}
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 );
194
192
ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (node , curToken );
195
193
ParserRuleContext ancestor = CollectFeatures .getAncestor (earliestLeftAncestor , deltaFromAncestor );
196
194
Token start = ancestor .getStart ();
@@ -206,7 +204,7 @@ else if ( (align&0xFF)==CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ) {
206
204
}
207
205
208
206
TokenPositionAnalysis tokenPositionAnalysis =
209
- getTokenAnalysis (features , indexIntoRealTokens , tokenIndexInStream , newlines , align , ws );
207
+ getTokenAnalysis (features , indexIntoRealTokens , tokenIndexInStream , injectNL_WS , alignOrIndent );
210
208
analysis .setSize (tokenIndexInStream +1 );
211
209
analysis .set (tokenIndexInStream , tokenPositionAnalysis );
212
210
@@ -235,14 +233,7 @@ public void emitCommentsToTheLeft(int tokenIndexInStream) {
235
233
List <Token > hiddenTokensToLeft = tokens .getHiddenTokensToLeft (tokenIndexInStream );
236
234
if ( hiddenTokensToLeft !=null ) {
237
235
// 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 );
246
237
if ( hasComment ) {
247
238
// avoid whitespace at end of sequence as we'll inject that
248
239
int last = -1 ;
@@ -259,7 +250,7 @@ public void emitCommentsToTheLeft(int tokenIndexInStream) {
259
250
String hiddenText = hidden .getText ();
260
251
output .append (hiddenText );
261
252
if ( hiddenText .matches ("\\ n+" ) ) {
262
- line += CharMatcher . is ( '\n' ). countIn ( hiddenText );
253
+ line += Tool . count ( hiddenText , '\n' );
263
254
charPosInLine = 0 ;
264
255
}
265
256
else {
@@ -272,28 +263,20 @@ public void emitCommentsToTheLeft(int tokenIndexInStream) {
272
263
}
273
264
274
265
public TokenPositionAnalysis getTokenAnalysis (int [] features , int indexIntoRealTokens , int tokenIndexInStream ,
275
- int injectNewline ,
276
- int align ,
277
- int ws )
266
+ int injectNL_WS , int alignOrIndent )
278
267
{
279
268
CommonToken curToken = (CommonToken )tokens .get (tokenIndexInStream );
280
269
// compare prediction of newline against original, alert about any diffs
281
270
CommonToken prevToken = originalTokens .get (curToken .getTokenIndex ()-1 );
282
271
CommonToken originalCurToken = originalTokens .get (curToken .getTokenIndex ());
283
272
284
- boolean failsafeTriggered = false ;
285
- if ( ws ==0 && cannotJoin (realTokens .get (indexIntoRealTokens -1 ), curToken ) ) { // failsafe!
286
- ws = 1 ;
287
- failsafeTriggered = true ;
288
- }
289
-
290
273
boolean prevIsWS = prevToken .getChannel ()==Token .HIDDEN_CHANNEL ; // assume this means whitespace
291
274
int actualNL = Tool .count (prevToken .getText (), '\n' );
292
275
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" );
294
277
String alignPredictionString = String .format ("### line %d: predicted %d actual %s" ,
295
278
originalCurToken .getLine (),
296
- align ,
279
+ alignOrIndent ,
297
280
"?" );
298
281
299
282
String newlineAnalysis = newlinePredictionString +"\n " +
@@ -302,7 +285,7 @@ public TokenPositionAnalysis getTokenAnalysis(int[] features, int indexIntoRealT
302
285
String alignAnalysis =alignPredictionString +"\n " +
303
286
alignClassifier .getPredictionAnalysis (doc , k , features , corpus .align ,
304
287
MAX_CONTEXT_DIFF_THRESHOLD );
305
- return new TokenPositionAnalysis (newlineAnalysis , alignAnalysis , "n/a" );
288
+ return new TokenPositionAnalysis (curToken , injectNL_WS , newlineAnalysis , alignOrIndent , alignAnalysis );
306
289
}
307
290
308
291
/** Do not join two words like "finaldouble" or numbers like "3double",
0 commit comments