@Override public PsiDocCommentOwner getOwner() { final PsiElement parent = getParent(); if (parent instanceof PsiDocCommentOwner) { final PsiDocCommentOwner owner = (PsiDocCommentOwner) parent; if (owner.getDocComment() == this) { return owner; } } return null; }
@NotNull @Override public Comparable weigh(@NotNull LookupElement element) { final Object object = element.getObject(); if (object instanceof PsiDocCommentOwner) { final PsiDocCommentOwner member = (PsiDocCommentOwner) object; if (!JavaPsiFacade.getInstance(member.getProject()) .getResolveHelper() .isAccessible(member, myPosition, null)) return MyEnum.INACCESSIBLE; if (member.isDeprecated()) return MyEnum.DEPRECATED; } return MyEnum.NORMAL; }
@Nullable private ProblemDescriptor[] checkMember( final PsiDocCommentOwner docCommentOwner, final InspectionManager manager, final boolean isOnTheFly) { final ArrayList<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>(); final PsiDocComment docComment = docCommentOwner.getDocComment(); if (docComment == null) return null; final Set<PsiJavaCodeReferenceElement> references = new HashSet<PsiJavaCodeReferenceElement>(); docComment.accept(getVisitor(references, docCommentOwner, problems, manager, isOnTheFly)); for (PsiJavaCodeReferenceElement reference : references) { final List<PsiClass> classesToImport = new ImportClassFix(reference).getClassesToImport(); final PsiElement referenceNameElement = reference.getReferenceNameElement(); problems.add( manager.createProblemDescriptor( referenceNameElement != null ? referenceNameElement : reference, cannotResolveSymbolMessage("<code>" + reference.getText() + "</code>"), !isOnTheFly || classesToImport.isEmpty() ? null : new AddImportFix(classesToImport), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, isOnTheFly)); } return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]); }
private void replaceDocComment( @Nullable String newCommentText, @NotNull final PsiDocCommentOwner psiDocCommentOwner) { final PsiDocComment oldComment = psiDocCommentOwner.getDocComment(); if (newCommentText != null) newCommentText = stripSpaces(newCommentText); if (newCommentText == null || oldComment == null || newCommentText.equals(oldComment.getText())) { return; } try { PsiComment newComment = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createCommentFromText(newCommentText, null); final ASTNode oldNode = oldComment.getNode(); final ASTNode newNode = newComment.getNode(); assert oldNode != null && newNode != null; final ASTNode parent = oldNode.getTreeParent(); parent.replaceChild( oldNode, newNode); // important to replace with tree operation to avoid resolve and repository // update } catch (IncorrectOperationException e) { LOG.error(e); } }
/** * Returns the original comment info of the specified element or null * * @param element the specified element * @return text chunk */ @Nullable private static String getOrigCommentInfo(PsiDocCommentOwner element) { StringBuilder sb = new StringBuilder(); PsiElement e = element.getFirstChild(); if (!(e instanceof PsiComment)) { // no comments for this element return null; } else { boolean first = true; while (true) { if (e instanceof PsiDocComment) { PsiComment cm = (PsiComment) e; String text = cm.getText(); if (text.startsWith("//")) { if (!first) sb.append('\n'); sb.append(text.substring(2).trim()); } else if (text.startsWith("/*")) { if (text.charAt(2) == '*') { text = text.substring(3, Math.max(3, text.length() - 2)); } else { text = text.substring(2, Math.max(2, text.length() - 2)); } sb.append(text); } } else if (!(e instanceof PsiWhiteSpace) && !(e instanceof PsiComment)) { break; } first = false; e = e.getNextSibling(); } return sb.toString(); } }
private static PsiDocTag getTextJavaDoc(@NotNull final PsiDocCommentOwner element) { final PsiDocComment docComment = element.getDocComment(); if (docComment != null) { return docComment.findTagByName("testng.test"); } return null; }
@Override public void invoke( @NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException { PsiDocCommentOwner container = getContainer(element); assert container != null; if (!CodeInsightUtilBase.preparePsiElementForWrite(container)) return; if (use15Suppressions(container)) { final PsiModifierList modifierList = container.getModifierList(); if (modifierList != null) { addSuppressAnnotation(project, editor, container, container, getID(container)); } } else { PsiDocComment docComment = container.getDocComment(); PsiManager manager = PsiManager.getInstance(project); if (docComment == null) { String commentText = "/** @" + SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME + " " + getID(container) + "*/"; docComment = JavaPsiFacade.getInstance(manager.getProject()) .getElementFactory() .createDocCommentFromText(commentText); PsiElement firstChild = container.getFirstChild(); container.addBefore(docComment, firstChild); } else { PsiDocTag noInspectionTag = docComment.findTagByName(SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME); if (noInspectionTag != null) { String tagText = noInspectionTag.getText() + ", " + getID(container); noInspectionTag.replace( JavaPsiFacade.getInstance(manager.getProject()) .getElementFactory() .createDocTagFromText(tagText)); } else { String tagText = "@" + SuppressionUtil.SUPPRESS_INSPECTIONS_TAG_NAME + " " + getID(container); docComment.add( JavaPsiFacade.getInstance(manager.getProject()) .getElementFactory() .createDocTagFromText(tagText)); } } } DaemonCodeAnalyzer.getInstance(project).restart(); }
public static boolean isDeprecatedByDocTag(@NotNull PsiDocCommentOwner owner) { PsiDocComment docComment = owner.getDocComment(); return docComment != null && docComment.findTagByName("deprecated") != null; }