private static String removeJavadoc( VisitorState state, int startPos, Comment danglingJavadoc, SuggestedFix.Builder builder) { int javadocStart = startPos + danglingJavadoc.getSourcePos(0); int javadocEnd = javadocStart + danglingJavadoc.getText().length(); // Capturing an extra newline helps the formatter. if (state.getSourceCode().charAt(javadocEnd) == '\n') { javadocEnd++; } builder.replace(javadocStart, javadocEnd, ""); return danglingJavadoc.getText(); }
private Description generateSeeFix(DocTreePath docTreePath, SeeTree seeTree, VisitorState state) { int pos = ((DCDocComment) docTreePath.getDocComment()).comment.getSourcePos(0); SuggestedFix fix = SuggestedFix.builder() .merge(Utils.replace(seeTree, "", state)) .replace( pos, pos, String.format( "See {@link %s}.\n", seeTree.getReference().stream().map(Object::toString).collect(joining(" ")))) .build(); return buildDescription(diagnosticPosition(docTreePath, state)) .setMessage(String.format(CONSIDER_USING_MESSAGE, "see")) .addFix(fix) .build(); }
private void handleDanglingParams(TextTree node) { Matcher matcher = PARAM_MATCHER.matcher(node.getBody()); Comment comment = ((DCDocComment) getCurrentPath().getDocComment()).comment; while (matcher.find()) { int startPos = comment.getSourcePos(((DCText) node).pos + matcher.start()); int endPos = comment.getSourcePos(((DCText) node).pos + matcher.end()); String paramName = matcher.group(1); SuggestedFix fix = SuggestedFix.replace(startPos, endPos, String.format("{@code %s}", paramName)); state.reportMatch( buildDescription( getDiagnosticPosition(startPos, getCurrentPath().getTreePath().getLeaf())) .addFix(fix) .build()); } }
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 long getSourcePosition(DCDocComment dc) { return dc.comment.getSourcePos(pos); }
private static int commentEnd(DCDocComment doc) { int length = doc.comment.getText().length(); return doc.comment.getSourcePos(length-1); }
public int getEndPos(DCDocComment dc) { return dc.comment.getSourcePos(endPos); }
@Override public int getSourcePos(int i) { return wrapped.getSourcePos(i) + offset; }
boolean isCommentOnPreviousLine(Comment c) { int tokenLine = lineMap.getLineNumber(c.getSourcePos(0)); return tokenLine == currentLineNumber - 1; }
private static String removeJavadoc( VisitorState state, int startPos, Comment danglingJavadoc, SuggestedFix.Builder builder) { int javadocStart = startPos + danglingJavadoc.getSourcePos(0); int javadocEnd = javadocStart + danglingJavadoc.getText().length(); // Capturing an extra newline helps the formatter. if (state.getSourceCode().charAt(javadocEnd) == '\n') { javadocEnd++; } builder.replace(javadocStart, javadocEnd, ""); return danglingJavadoc.getText(); }
private static void fixParamComment( SuggestedFix.Builder fix, Commented<ExpressionTree> commented, VarSymbol param, Comment c) { fix.prefixWith(commented.tree(), String.format("/* %s= */ ", param.getSimpleName())) .replace(c.getSourcePos(0), c.getSourcePos(0) + c.getText().length(), ""); } }
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);
.addFix( SuggestedFix.replace( start + match.getSourcePos(0), start + match.getSourcePos(match.getText().length() - 1) + 1, String.format("/* %s= */", formal.getSimpleName()))) .build());
while (matcher.find()) { Range<Integer> range = Range.closed(comment.getSourcePos(matcher.start()), comment.getSourcePos(matcher.end())); if (emittedFixes.intersects(range) || dontEmitCodeFix.intersects(range)) { continue;
private void handleMalformedTags(TextTree node) { Matcher matcher = misplacedCurly.matcher(node.getBody()); Comment comment = ((DCDocComment) getCurrentPath().getDocComment()).comment; while (matcher.find()) { int beforeAt = comment.getSourcePos(((DCText) node).pos + matcher.start()); int startOfCurly = comment.getSourcePos(((DCText) node).pos + matcher.end(1)); SuggestedFix fix = SuggestedFix.builder() .replace(beforeAt, beforeAt, "{") .replace(startOfCurly, startOfCurly + 1, " ") .build(); state.reportMatch( buildDescription( getDiagnosticPosition(beforeAt, getCurrentPath().getTreePath().getLeaf())) .addFix(fix) .build()); } }
private Description generateReturnFix( DocTreePath docTreePath, ReturnTree returnTree, VisitorState state) { int pos = ((DCDocComment) docTreePath.getDocComment()).comment.getSourcePos(0); String description = returnTree.toString().replaceAll("^@return ", ""); SuggestedFix fix = SuggestedFix.builder() .merge(Utils.replace(returnTree, "", state)) .replace( pos, pos, String.format( "Returns %s%s\n", lowerFirstLetter(description), description.endsWith(".") ? "" : ".")) .build(); return buildDescription(diagnosticPosition(docTreePath, state)) .setMessage(String.format(CONSIDER_USING_MESSAGE, "link")) .addFix(fix) .build(); }
boolean isCommentOnPreviousLine(Comment c) { int tokenLine = lineMap.getLineNumber(c.getSourcePos(0)); return tokenLine == currentLineNumber - 1; }
@Override public int getSourcePos(int i) { return wrapped.getSourcePos(i) + offset; }
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; }
private static void fixParamComment( SuggestedFix.Builder fix, Commented<ExpressionTree> commented, VarSymbol param, Comment c) { fix.prefixWith(commented.tree(), String.format("/* %s= */ ", param.getSimpleName())) .replace(c.getSourcePos(0), c.getSourcePos(0) + c.getText().length(), ""); } }