MCPcopy Create free account
hub / github.com/antlr/codebuff / emitCommentsToTheLeft

Method emitCommentsToTheLeft

src/org/antlr/codebuff/Formatter.java:383–424  ·  view source on GitHub ↗

Look into the originalTokens stream to get the comments to the left of current token. Emit all whitespace and comments except for whitespace at the end as we'll inject that per newline prediction. We able to see original input stream for comment purposes only. With all whitespace removed, we ca

(int tokenIndexInStream, int injectNL_WS)

Source from the content-addressed store, hash-verified

381 * is the only place that examines the original token stream during formatting.
382 */
383 public int emitCommentsToTheLeft(int tokenIndexInStream, int injectNL_WS) {
384 List<Token> hiddenTokensToLeft = originalTokens.getHiddenTokensToLeft(tokenIndexInStream);
385 if ( hiddenTokensToLeft!=null ) {
386 // if at least one is not whitespace, assume it's a comment and print all hidden stuff including whitespace
387 boolean hasComment = Trainer.hasCommentToken(hiddenTokensToLeft);
388 if ( hasComment ) {
389 // avoid whitespace at end of sequence as we'll inject that
390 int last = -1;
391 for (int i=hiddenTokensToLeft.size()-1; i>=0; i--) {
392 Token hidden = hiddenTokensToLeft.get(i);
393 String hiddenText = hidden.getText();
394 if ( !hiddenText.matches("\\s+") ) {
395 last = i;
396 break;
397 }
398 }
399 Token commentToken = hiddenTokensToLeft.get(last);
400 List<Token> truncated = hiddenTokensToLeft.subList(0, last+1);
401 for (Token hidden : truncated) {
402 String hiddenText = hidden.getText();
403 output.append(hiddenText);
404 if ( hiddenText.matches("\\n+") ) {
405 line += Tool.count(hiddenText, '\n');
406 charPosInLine = 0;
407 }
408 else {
409 // if a comment or plain ' ', must count char position
410 charPosInLine += hiddenText.length();
411 }
412 }
413 // failsafe. make sure single-line comments have \n on the end.
414 // If not predicted, must override and inject one
415 if ( commentToken.getType()==corpus.language.singleLineCommentType &&
416 (injectNL_WS&0xFF)!=CAT_INJECT_NL )
417 {
418 return nlcat(1); // force formatter to predict newline then trigger alignment
419 }
420 }
421 }
422
423 return injectNL_WS; // send same thing back out unless we trigger failsafe
424 }
425
426 public TokenPositionAnalysis getTokenAnalysis(int[] features, int[] featuresForAlign,
427 int tokenIndexInStream,

Callers 1

processTokenMethod · 0.95

Calls 11

hasCommentTokenMethod · 0.95
countMethod · 0.95
nlcatMethod · 0.80
sizeMethod · 0.65
getMethod · 0.65
getTextMethod · 0.65
matchesMethod · 0.45
subListMethod · 0.45
appendMethod · 0.45
lengthMethod · 0.45
getTypeMethod · 0.45

Tested by

no test coverage detected