@Override public Void visitStartElement(StartElementTree startElementTree, Void aVoid) { if (startElementTree.getName().toString().equalsIgnoreCase("p")) { DCStartElement node = (DCStartElement) startElementTree; DocTreePath path = getCurrentPath(); int start = (int) node.getSourcePosition((DCDocComment) path.getDocComment()) + node.pos; int end = node.getEndPos((DCDocComment) getCurrentPath().getDocComment()); fixes.add(Optional.of(SuggestedFix.replace(start, end, ""))); // int endPos = startPos + node.getSignature().length(); // String oldName = node.getSignature(); // int idx = oldName.indexOf('#'); // if (idx != -1) { // startPos += idx + 1; // } // state.reportMatch( // describeMatch( // state.getPath().getLeaf(), SuggestedFix.replace(startPos, endPos, with))); // int start = element.pos; // fixes.add(Optional.of(SuggestedFix.replace(start, element.getEndPos()))); } return super.visitStartElement(startElementTree, aVoid); }
/** Replaces the leaf doctree in the given path with {@code replacement}. */ public static void replaceDocTree( SuggestedFix.Builder fix, DocTreePath docPath, String replacement) { DocTree leaf = docPath.getLeaf(); checkArgument( leaf instanceof DCTree.DCEndPosTree, "no end position information for %s", leaf.getKind()); DCTree.DCEndPosTree<?> node = (DCTree.DCEndPosTree<?>) leaf; DCTree.DCDocComment comment = (DCTree.DCDocComment) docPath.getDocComment(); fix.replace((int) node.getSourcePosition(comment), node.getEndPos(comment), replacement); }
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()); } }
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 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(); }
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()); } }
VisitorState state) { RangeSet<Integer> emittedFixes = TreeRangeSet.create(); Comment comment = ((DCDocComment) path.getDocComment()).comment; Matcher matcher = GENERIC.matcher(comment.getText()); while (matcher.find()) {
return Description.NO_MATCH; List<? extends DocTree> firstSentence = docTreePath.getDocComment().getFirstSentence(); if (!firstSentence.isEmpty()) { return NO_MATCH;
/** Replaces the leaf doctree in the given path with {@code replacement}. */ public static void replaceDocTree( SuggestedFix.Builder fix, DocTreePath docPath, String replacement) { DocTree leaf = docPath.getLeaf(); checkArgument( leaf instanceof DCTree.DCEndPosTree, "no end position information for %s", leaf.getKind()); DCTree.DCEndPosTree<?> node = (DCTree.DCEndPosTree<?>) leaf; DCTree.DCDocComment comment = (DCTree.DCDocComment) docPath.getDocComment(); fix.replace((int) node.getSourcePosition(comment), node.getEndPos(comment), replacement); }
if (path.getLeaf() == path.getDocComment()) return path;
/** * Factory method for creating {@link DocTreePathHandle}. * * @param docTreePath for which the {@link DocTreePathHandle} should be created. * @param javac * @return a new {@link DocTreePathHandle} * @throws java.lang.IllegalArgumentException if arguments are not supported */ public static DocTreePathHandle create(final DocTreePath docTreePath, CompilationInfo javac) throws IllegalArgumentException { Parameters.notNull("docTreePath", docTreePath); Parameters.notNull("javac", javac); TreePathHandle treePathHandle = TreePathHandle.create(docTreePath.getTreePath(), javac); if(treePathHandle.getFileObject() == null) { return null; } int position = (int) ((DCTree) docTreePath.getLeaf()).getSourcePosition((DCTree.DCDocComment)docTreePath.getDocComment()); if (position == (-1)) { DocTree docTree = docTreePath.getLeaf(); if(docTree == docTreePath.getDocComment()) { return new DocTreePathHandle(new DocCommentDelegate(treePathHandle)); } int index = listChildren(docTreePath.getParentPath().getLeaf()).indexOf(docTree); assert index != (-1); return new DocTreePathHandle(new CountingDelegate(treePathHandle, index, docTreePath.getLeaf().getKind())); } Position pos = createPositionRef(treePathHandle.getFileObject(), position, Bias.Forward); return new DocTreePathHandle(new DocTreeDelegate(pos, new DocTreeDelegate.KindPath(docTreePath), treePathHandle)); }