Comparator.<Comment>comparingInt(c -> c.getSourcePos(0)).reversed(), tokens.get(0).comments()); int startPos = ((JCTree) tree).getStartPosition() - startTokenization; int endOfCommentPos = comment.getSourcePos(comment.getText().length() - 1); String stringBetweenComments = source.substring(endOfCommentPos, startPos); if (stringBetweenComments.chars().filter(c -> c == '\n').count() > 1) { break; startPos = comment.getSourcePos(0);
List<Comment> matches = new ArrayList<>(); for (Comment comment : token.comments()) { if (comment.getStyle() != BLOCK) { continue; String.format( "`%s` does not match formal parameter name `%s`", match.getText(), formal.getSimpleName())) .addFix( SuggestedFix.replace( start + match.getSourcePos(0), start + match.getSourcePos(match.getText().length() - 1) + 1, String.format("/* %s= */", formal.getSimpleName()))) .build());
DCDocComment parse() { String c = comment.getText(); buf = new char[c.length() + 1]; c.getChars(0, c.length(), buf, 0);
if (c.getSourcePos(0) <= invocationEnd || lineMap.getLineNumber(c.getSourcePos(0)) <= lineMap.getLineNumber(argumentTracker.currentArgumentEndPosition)) { argumentTracker.addCommentToCurrentArgument(c, Position.ANY);
DCDocComment parse() { String c = comment.getText(); buf = new char[c.length() + 1]; c.getChars(0, c.length(), buf, 0);
DCText text = (DCText) tree; return dcComment.comment.getSourcePos(text.pos + text.text.length()); case ERRONEOUS: DCErroneous err = (DCErroneous) tree; return dcComment.comment.getSourcePos(err.pos + err.body.length()); case IDENTIFIER: DCIdentifier ident = (DCIdentifier) tree; return dcComment.comment.getSourcePos(ident.pos + (ident.name != names.error ? ident.name.length() : 0)); case PARAM: DCParam param = (DCParam) tree; return dcComment.comment.getSourcePos(block.pos + block.getTagName().length() + 1);
DCText text = (DCText) tree; return dcComment.comment.getSourcePos(text.pos + text.text.length()); case ERRONEOUS: DCErroneous err = (DCErroneous) tree; return dcComment.comment.getSourcePos(err.pos + err.body.length()); case IDENTIFIER: DCIdentifier ident = (DCIdentifier) tree; return dcComment.comment.getSourcePos(ident.pos + (ident.name != names.error ? ident.name.length() : 0)); case PARAM: DCParam param = (DCParam) tree; return dcComment.comment.getSourcePos(block.pos + block.getTagName().length() + 1);
List<Comment> matches = new ArrayList<>(); for (Comment comment : token.comments()) { if (comment.getStyle() != BLOCK) { continue; String.format( "`%s` does not match formal parameter name `%s`", match.getText(), formal.getSimpleName())) .addFix( SuggestedFix.replace( start + match.getSourcePos(0), start + match.getSourcePos(match.getText().length() - 1) + 1, String.format("/* %s= */", formal.getSimpleName()))) .build());
Optional<Comment> lastBlockComment = Streams.findLast( actual.beforeComments().stream().filter(c -> c.getStyle() == CommentStyle.BLOCK));
private static boolean isApproximateMatchingComment(Comment comment, String formal) { switch (comment.getStyle()) { case BLOCK: case LINE: // sometimes people use comments around arguments for higher level structuring - such as // dividing two separate blocks of arguments. In these cases we want to avoid concluding // that its a match. Therefore we also check to make sure that the comment is not really // long and that it doesn't contain acsii-art style markup. String commentText = Comments.getTextFromComment(comment); boolean textMatches = Arrays.asList(commentText.split("[^a-zA-Z0-9_]+", -1)).contains(formal); boolean tooLong = commentText.length() > formal.length() + 5 && commentText.length() > 50; boolean tooMuchMarkup = CharMatcher.anyOf("-*!@<>").countIn(commentText) > 5; return textMatches && !tooLong && !tooMuchMarkup; default: return false; } }
/** * Extract the text body from a comment. * * <p>This currently includes asterisks that start lines in the body of block comments. Do not * rely on this behaviour. * * <p>TODO(andrewrice) Update this method to handle block comments properly if we find the need */ public static String getTextFromComment(Comment comment) { switch (comment.getStyle()) { case BLOCK: return comment.getText().replaceAll("^\\s*/\\*\\s*(.*?)\\s*\\*/\\s*", "$1"); case LINE: return comment.getText().replaceAll("^\\s*//\\s*", ""); default: return comment.getText(); } }
public int getEndPos(DCDocComment dc) { return dc.comment.getSourcePos(endPos); }
private List<Comment> getComments(Comment.CommentStyle style) { if (comments == null) { return List.nil(); } else { ListBuffer<Comment> buf = new ListBuffer<>(); for (Comment c : comments) { if (c.getStyle() == style) { buf.add(c); } } return buf.toList(); } } }
Builder<T> addComment( Comment comment, int nodePosition, int tokenizingOffset, Position position) { OffsetComment offsetComment = new OffsetComment(comment, tokenizingOffset); if (comment.getSourcePos(0) < nodePosition) { if (position.equals(Position.BEFORE) || position.equals(Position.ANY)) { beforeCommentsBuilder().add(offsetComment); } } else { if (position.equals(Position.AFTER) || position.equals(Position.ANY)) { afterCommentsBuilder().add(offsetComment); } } return this; }
public JCDiagnostic.DiagnosticPosition pos(DCDocComment dc) { return new SimpleDiagnosticPosition(dc.comment.getSourcePos(pos)); }
private List<Comment> getComments(Comment.CommentStyle style) { if (comments == null) { return List.nil(); } else { ListBuffer<Comment> buf = new ListBuffer<>(); for (Comment c : comments) { if (c.getStyle() == style) { buf.add(c); } } return buf.toList(); } } }
@Nullable private static Comment findOrphanedJavadoc(Name name, List<ErrorProneToken> tokens) { for (ErrorProneToken token : tokens) { for (Comment comment : token.comments()) { if (comment.getText().startsWith("/**")) { return comment; } } if (token.kind() == TokenKind.IDENTIFIER && token.name().equals(name)) { return null; } } return null; } }
public JCDiagnostic.DiagnosticPosition pos(DCDocComment dc) { return new SimpleDiagnosticPosition(dc.comment.getSourcePos(pos)); }
public long getSourcePosition(DCDocComment dc) { return dc.comment.getSourcePos(pos); }
public String getCommentText(JCTree tree) { Comment c = getComment(tree); return (c == null) ? null : c.getText(); }