Search in sources :

Example 21 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 22 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 23 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)

Example 24 with PsiDocComment

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

the class JavaDocumentationProvider method generateParametersTakingDocFromSuperMethods.

public static void generateParametersTakingDocFromSuperMethods(Project project, StringBuilder builder, CodeDocumentationAwareCommenter commenter, PsiMethod psiMethod) {
    final PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
    final Map<String, String> param2Description = new HashMap<>();
    final PsiMethod[] superMethods = psiMethod.findSuperMethods();
    for (PsiMethod superMethod : superMethods) {
        final PsiDocComment comment = superMethod.getDocComment();
        if (comment != null) {
            final PsiDocTag[] params = comment.findTagsByName("param");
            for (PsiDocTag param : params) {
                final PsiElement[] dataElements = param.getDataElements();
                if (dataElements != null) {
                    String paramName = null;
                    for (PsiElement dataElement : dataElements) {
                        if (dataElement instanceof PsiDocParamRef) {
                            //noinspection ConstantConditions
                            paramName = dataElement.getReference().getCanonicalText();
                            break;
                        }
                    }
                    if (paramName != null) {
                        param2Description.put(paramName, param.getText());
                    }
                }
            }
        }
    }
    for (PsiParameter parameter : parameters) {
        String description = param2Description.get(parameter.getName());
        if (description != null) {
            builder.append(CodeDocumentationUtil.createDocCommentLine("", project, commenter));
            if (description.indexOf('\n') > -1)
                description = description.substring(0, description.lastIndexOf('\n'));
            builder.append(description);
        } else {
            builder.append(CodeDocumentationUtil.createDocCommentLine(PARAM_TAG, project, commenter));
            builder.append(parameter.getName());
        }
        builder.append(LINE_SEPARATOR);
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) HashMap(com.intellij.util.containers.HashMap) PsiDocParamRef(com.intellij.psi.impl.source.javadoc.PsiDocParamRef)

Example 25 with PsiDocComment

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

the class PsiFieldImpl method getDocComment.

@Override
public PsiDocComment getDocComment() {
    final PsiFieldStub stub = getGreenStub();
    if (stub != null && !stub.hasDocComment())
        return null;
    CompositeElement treeElement = getNode();
    if (getTypeElement() != null) {
        PsiElement element = treeElement.findChildByRoleAsPsiElement(ChildRole.DOC_COMMENT);
        return element instanceof PsiDocComment ? (PsiDocComment) element : null;
    } else {
        ASTNode prevField = treeElement.getTreePrev();
        while (prevField.getElementType() != JavaElementType.FIELD) {
            prevField = prevField.getTreePrev();
        }
        return ((PsiField) SourceTreeToPsiMap.treeElementToPsi(prevField)).getDocComment();
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiFieldStub(com.intellij.psi.impl.java.stubs.PsiFieldStub) ASTNode(com.intellij.lang.ASTNode)

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