Search in sources :

Example 6 with PsiDocComment

use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.

the class FormatCommentsProcessor method formatCommentsInner.

/**
   * Formats PsiDocComments of current ASTNode element and all his children PsiDocComments
   */
@NotNull
private static TextRange formatCommentsInner(@NotNull Project project, @NotNull ASTNode element, @NotNull final TextRange markedRange) {
    TextRange resultTextRange = markedRange;
    final PsiElement elementPsi = element.getPsi();
    boolean shouldFormat = markedRange.contains(element.getTextRange());
    if (shouldFormat) {
        final ASTNode rangeAnchor;
        // its parent.
        if (elementPsi instanceof PsiDocComment) {
            rangeAnchor = element.getTreeParent();
        } else {
            rangeAnchor = element;
        }
        TextRange before = rangeAnchor.getTextRange();
        new CommentFormatter(project).processComment(element);
        int deltaRange = rangeAnchor.getTextRange().getLength() - before.getLength();
        resultTextRange = new TextRange(markedRange.getStartOffset(), markedRange.getEndOffset() + deltaRange);
    }
    // If element is out of range its children are also out of range. So in both cases formatting is finished. It's just for optimization.
    if ((shouldFormat && (elementPsi instanceof PsiMethod || elementPsi instanceof PsiField || elementPsi instanceof PsiDocComment)) || markedRange.getEndOffset() < element.getStartOffset()) {
        return resultTextRange;
    }
    ASTNode current = element.getFirstChildNode();
    while (current != null) {
        // When element is PsiClass its PsiDocComment is formatted up to this moment, so we didn't need to format it again.
        if (!(shouldFormat && current.getPsi() instanceof PsiDocComment && elementPsi instanceof PsiClass)) {
            resultTextRange = formatCommentsInner(project, current, resultTextRange);
        }
        current = current.getTreeNext();
    }
    return resultTextRange;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiMethod(com.intellij.psi.PsiMethod) PsiField(com.intellij.psi.PsiField) ASTNode(com.intellij.lang.ASTNode) PsiClass(com.intellij.psi.PsiClass) TextRange(com.intellij.openapi.util.TextRange) CommentFormatter(com.intellij.psi.impl.source.codeStyle.javadoc.CommentFormatter) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PsiDocComment

use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.

the class JDParser method getElementsCommentInfo.

private static CommentInfo getElementsCommentInfo(@Nullable PsiElement psiElement) {
    CommentInfo info = null;
    if (psiElement instanceof PsiDocComment) {
        final PsiDocComment docComment = (PsiDocComment) psiElement;
        if (docComment.getOwner() == null && docComment.getParent() instanceof PsiJavaFile) {
            info = CommentFormatter.getCommentInfo(docComment);
            if (info != null) {
                info.setCommentOwner(docComment);
                info.setComment(docComment);
            }
        } else {
            return getElementsCommentInfo(psiElement.getParent());
        }
    } else if (psiElement instanceof PsiDocCommentOwner) {
        PsiDocCommentOwner owner = (PsiDocCommentOwner) psiElement;
        info = CommentFormatter.getOrigCommentInfo(owner);
        if (info != null) {
            info.setCommentOwner(owner);
            info.setComment(owner.getDocComment());
        }
    }
    return info;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment)

Example 8 with PsiDocComment

use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.

the class RefactoringUtil method fixJavadocsForParams.

public static void fixJavadocsForParams(PsiMethod method, Set<PsiParameter> newParameters, Condition<Pair<PsiParameter, String>> eqCondition, Condition<String> matchedToOldParam) throws IncorrectOperationException {
    final PsiDocComment docComment = method.getDocComment();
    if (docComment == null)
        return;
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    final PsiDocTag[] paramTags = docComment.findTagsByName("param");
    if (parameters.length > 0 && newParameters.size() < parameters.length && paramTags.length == 0)
        return;
    Map<PsiParameter, PsiDocTag> tagForParam = new HashMap<>();
    for (PsiParameter parameter : parameters) {
        boolean found = false;
        for (PsiDocTag paramTag : paramTags) {
            if (parameter.getName().equals(getNameOfReferencedParameter(paramTag))) {
                tagForParam.put(parameter, paramTag);
                found = true;
                break;
            }
        }
        if (!found) {
            for (PsiDocTag paramTag : paramTags) {
                final String paramName = getNameOfReferencedParameter(paramTag);
                if (eqCondition.value(Pair.create(parameter, paramName))) {
                    tagForParam.put(parameter, paramTag);
                    found = true;
                    break;
                }
            }
        }
        if (!found && !newParameters.contains(parameter)) {
            tagForParam.put(parameter, null);
        }
    }
    List<PsiDocTag> newTags = new ArrayList<>();
    for (PsiDocTag paramTag : paramTags) {
        final String paramName = getNameOfReferencedParameter(paramTag);
        if (!tagForParam.containsValue(paramTag) && !matchedToOldParam.value(paramName)) {
            newTags.add((PsiDocTag) paramTag.copy());
        }
    }
    for (PsiParameter parameter : parameters) {
        if (tagForParam.containsKey(parameter)) {
            final PsiDocTag psiDocTag = tagForParam.get(parameter);
            if (psiDocTag != null) {
                final PsiDocTag copy = (PsiDocTag) psiDocTag.copy();
                final PsiDocTagValue valueElement = copy.getValueElement();
                if (valueElement != null) {
                    valueElement.replace(createParamTag(parameter).getValueElement());
                }
                newTags.add(copy);
            }
        } else {
            newTags.add(createParamTag(parameter));
        }
    }
    PsiElement anchor = paramTags.length > 0 ? paramTags[0].getPrevSibling() : null;
    for (PsiDocTag paramTag : paramTags) {
        paramTag.delete();
    }
    for (PsiDocTag psiDocTag : newTags) {
        anchor = anchor != null && anchor.isValid() ? docComment.addAfter(psiDocTag, anchor) : docComment.add(psiDocTag);
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) HashMap(com.intellij.util.containers.HashMap) THashMap(gnu.trove.THashMap) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue)

Example 9 with PsiDocComment

use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.

the class JavaDocCommentFixer method fixComment.

@Override
public void fixComment(@NotNull Project project, @NotNull Editor editor, @NotNull PsiComment comment) {
    if (!(comment instanceof PsiDocComment)) {
        return;
    }
    PsiDocComment docComment = (PsiDocComment) comment;
    PsiJavaDocumentedElement owner = docComment.getOwner();
    if (owner == null) {
        return;
    }
    PsiFile file = comment.getContainingFile();
    if (file == null) {
        return;
    }
    JavaDocReferenceInspection referenceInspection = new JavaDocReferenceInspection();
    JavaDocLocalInspection localInspection = getDocLocalInspection();
    InspectionManager inspectionManager = InspectionManager.getInstance(project);
    ProblemDescriptor[] referenceProblems = null;
    ProblemDescriptor[] otherProblems = null;
    if (owner instanceof PsiClass) {
        referenceProblems = referenceInspection.checkClass(((PsiClass) owner), inspectionManager, false);
        otherProblems = localInspection.checkClass(((PsiClass) owner), inspectionManager, false);
    } else if (owner instanceof PsiField) {
        referenceProblems = referenceInspection.checkField(((PsiField) owner), inspectionManager, false);
        otherProblems = localInspection.checkField(((PsiField) owner), inspectionManager, false);
    } else if (owner instanceof PsiMethod) {
        referenceProblems = referenceInspection.checkMethod((PsiMethod) owner, inspectionManager, false);
        otherProblems = localInspection.checkMethod((PsiMethod) owner, inspectionManager, false);
    }
    if (referenceProblems != null) {
        fixReferenceProblems(referenceProblems, project);
    }
    if (otherProblems != null) {
        fixCommonProblems(otherProblems, comment, editor.getDocument(), project);
    }
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    ensureContentOrdered(docComment, editor.getDocument());
    locateCaret(docComment, editor, file);
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) JavaDocReferenceInspection(com.intellij.codeInspection.javaDoc.JavaDocReferenceInspection) InspectionManager(com.intellij.codeInspection.InspectionManager) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) JavaDocLocalInspection(com.intellij.codeInspection.javaDoc.JavaDocLocalInspection)

Example 10 with PsiDocComment

use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.

the class JavaDocumentationProvider method generateDocumentationContentStub.

@Override
public String generateDocumentationContentStub(PsiComment _comment) {
    final PsiJavaDocumentedElement commentOwner = ((PsiDocComment) _comment).getOwner();
    final Project project = _comment.getProject();
    final StringBuilder builder = new StringBuilder();
    final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter) LanguageCommenters.INSTANCE.forLanguage(_comment.getLanguage());
    if (commentOwner instanceof PsiMethod) {
        PsiMethod psiMethod = (PsiMethod) commentOwner;
        generateParametersTakingDocFromSuperMethods(project, builder, commenter, psiMethod);
        final PsiTypeParameterList typeParameterList = psiMethod.getTypeParameterList();
        if (typeParameterList != null) {
            createTypeParamsListComment(builder, project, commenter, typeParameterList);
        }
        if (psiMethod.getReturnType() != null && !PsiType.VOID.equals(psiMethod.getReturnType())) {
            builder.append(CodeDocumentationUtil.createDocCommentLine(RETURN_TAG, project, commenter));
            builder.append(LINE_SEPARATOR);
        }
        final PsiJavaCodeReferenceElement[] references = psiMethod.getThrowsList().getReferenceElements();
        for (PsiJavaCodeReferenceElement reference : references) {
            builder.append(CodeDocumentationUtil.createDocCommentLine(THROWS_TAG, project, commenter));
            builder.append(reference.getText());
            builder.append(LINE_SEPARATOR);
        }
    } else if (commentOwner instanceof PsiClass) {
        final PsiTypeParameterList typeParameterList = ((PsiClass) commentOwner).getTypeParameterList();
        if (typeParameterList != null) {
            createTypeParamsListComment(builder, project, commenter, typeParameterList);
        }
    }
    return builder.length() > 0 ? builder.toString() : null;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) Project(com.intellij.openapi.project.Project) CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter)

Aggregations

PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)54 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)14 Project (com.intellij.openapi.project.Project)8 TextRange (com.intellij.openapi.util.TextRange)6 PsiElement (com.intellij.psi.PsiElement)6 IElementType (com.intellij.psi.tree.IElementType)6 NotNull (org.jetbrains.annotations.NotNull)5 ASTNode (com.intellij.lang.ASTNode)4 PsiComment (com.intellij.psi.PsiComment)4 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)4 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 ArrayList (java.util.ArrayList)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiField (com.intellij.psi.PsiField)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 PsiDocParamRef (com.intellij.psi.impl.source.javadoc.PsiDocParamRef)2 HashMap (com.intellij.util.containers.HashMap)2 HashSet (java.util.HashSet)2 Matcher (java.util.regex.Matcher)2