Search in sources :

Example 21 with PsiDocTag

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

the class ConstructorJavadocUsageInfo method fixUsage.

@Override
public void fixUsage() throws IncorrectOperationException {
    final PsiDocComment docComment = myMethod.getDocComment();
    if (docComment != null) {
        final List<PsiDocTag> mergedTags = new ArrayList<>();
        final PsiDocTag[] paramTags = docComment.findTagsByName("param");
        for (PsiDocTag paramTag : paramTags) {
            final PsiElement[] dataElements = paramTag.getDataElements();
            if (dataElements.length > 0) {
                if (dataElements[0] instanceof PsiDocParamRef) {
                    final PsiReference reference = dataElements[0].getReference();
                    if (reference != null) {
                        final PsiElement resolve = reference.resolve();
                        if (resolve instanceof PsiParameter) {
                            final int parameterIndex = myMethod.getParameterList().getParameterIndex((PsiParameter) resolve);
                            if (myDescriptor.getParameterInfo(parameterIndex) == null)
                                continue;
                        }
                    }
                }
                mergedTags.add((PsiDocTag) paramTag.copy());
            }
        }
        PsiMethod compatibleParamObjectConstructor = null;
        final PsiMethod existingConstructor = myDescriptor.getExistingClassCompatibleConstructor();
        if (existingConstructor != null && existingConstructor.getDocComment() == null) {
            compatibleParamObjectConstructor = existingConstructor;
        } else if (!myDescriptor.isUseExistingClass()) {
            compatibleParamObjectConstructor = myDescriptor.getExistingClass().getConstructors()[0];
        }
        if (compatibleParamObjectConstructor != null) {
            PsiDocComment psiDocComment = JavaPsiFacade.getElementFactory(myMethod.getProject()).createDocCommentFromText("/**\n*/");
            psiDocComment = (PsiDocComment) compatibleParamObjectConstructor.addBefore(psiDocComment, compatibleParamObjectConstructor.getFirstChild());
            for (PsiDocTag tag : mergedTags) {
                psiDocComment.add(tag);
            }
        }
    }
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ArrayList(java.util.ArrayList) PsiDocParamRef(com.intellij.psi.impl.source.javadoc.PsiDocParamRef)

Example 22 with PsiDocTag

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

the class PsiDocCommentImpl method findTagsByName.

@Override
@NotNull
public PsiDocTag[] findTagsByName(String name) {
    ArrayList<PsiDocTag> array = new ArrayList<>();
    PsiDocTag[] tags = getTags();
    name = "@" + name;
    for (PsiDocTag tag : tags) {
        if (tag.getNameElement().getText().equals(name)) {
            array.add(tag);
        }
    }
    return array.toArray(new PsiDocTag[array.size()]);
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with PsiDocTag

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

the class PsiDocParamRef method getReference.

@Override
public PsiReference getReference() {
    final PsiDocComment comment = PsiTreeUtil.getParentOfType(this, PsiDocComment.class);
    if (comment == null)
        return null;
    final PsiJavaDocumentedElement owner = comment.getOwner();
    if (!(owner instanceof PsiMethod) && !(owner instanceof PsiClass))
        return null;
    final ASTNode valueToken = findChildByType(JavaDocTokenType.DOC_TAG_VALUE_TOKEN);
    if (valueToken == null)
        return null;
    final String name = valueToken.getText();
    PsiElement reference = null;
    final PsiElement firstChild = getFirstChild();
    if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
        final PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
        for (PsiTypeParameter typeParameter : typeParameters) {
            if (typeParameter.getName().equals(name)) {
                reference = typeParameter;
            }
        }
    } else if (owner instanceof PsiMethod) {
        final PsiParameter[] parameters = ((PsiMethod) owner).getParameterList().getParameters();
        for (PsiParameter parameter : parameters) {
            if (parameter.getName().equals(name)) {
                reference = parameter;
            }
        }
    }
    final PsiElement resultReference = reference;
    return new PsiJavaReference() {

        @Override
        public PsiElement resolve() {
            return resultReference;
        }

        @Override
        @NotNull
        public String getCanonicalText() {
            return valueToken.getText();
        }

        @Override
        public PsiElement handleElementRename(String newElementName) {
            final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(getNode());
            LeafElement newElement = Factory.createSingleLeafElement(JavaDocTokenType.DOC_TAG_VALUE_TOKEN, newElementName, charTableByTree, getManager());
            replaceChild(valueToken, newElement);
            return PsiDocParamRef.this;
        }

        @Override
        public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
            if (isReferenceTo(element))
                return PsiDocParamRef.this;
            if (!(element instanceof PsiParameter)) {
                throw new IncorrectOperationException("Unsupported operation");
            }
            return handleElementRename(((PsiParameter) element).getName());
        }

        @Override
        public boolean isReferenceTo(PsiElement element) {
            if (!(element instanceof PsiNamedElement))
                return false;
            PsiNamedElement namedElement = (PsiNamedElement) element;
            if (!getCanonicalText().equals(namedElement.getName()))
                return false;
            return getManager().areElementsEquivalent(resolve(), element);
        }

        @Override
        @NotNull
        public PsiElement[] getVariants() {
            final PsiElement firstChild = getFirstChild();
            Set<String> usedNames = new HashSet<>();
            for (PsiDocTag tag : comment.getTags()) {
                if (tag.getName().equals("param")) {
                    PsiDocTagValue valueElement = tag.getValueElement();
                    if (valueElement != null) {
                        usedNames.add(valueElement.getText());
                    }
                }
            }
            PsiNamedElement[] result = PsiNamedElement.EMPTY_ARRAY;
            if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
                result = ((PsiTypeParameterListOwner) owner).getTypeParameters();
            } else if (owner instanceof PsiMethod) {
                result = ((PsiMethod) owner).getParameterList().getParameters();
            }
            List<PsiElement> filtered = new ArrayList<>();
            for (PsiNamedElement namedElement : result) {
                if (!usedNames.contains(namedElement.getName())) {
                    filtered.add(namedElement);
                }
            }
            return filtered.toArray(new PsiElement[filtered.size()]);
        }

        @Override
        public boolean isSoft() {
            return false;
        }

        @Override
        public TextRange getRangeInElement() {
            final int startOffsetInParent = valueToken.getPsi().getStartOffsetInParent();
            return new TextRange(startOffsetInParent, startOffsetInParent + valueToken.getTextLength());
        }

        @Override
        public PsiElement getElement() {
            return PsiDocParamRef.this;
        }

        @Override
        public void processVariants(@NotNull PsiScopeProcessor processor) {
            for (final PsiElement element : getVariants()) {
                if (!processor.execute(element, ResolveState.initial())) {
                    return;
                }
            }
        }

        @Override
        @NotNull
        public JavaResolveResult advancedResolve(boolean incompleteCode) {
            return resultReference == null ? JavaResolveResult.EMPTY : new CandidateInfo(resultReference, PsiSubstitutor.EMPTY);
        }

        @Override
        @NotNull
        public JavaResolveResult[] multiResolve(boolean incompleteCode) {
            return resultReference == null ? JavaResolveResult.EMPTY_ARRAY : new JavaResolveResult[] { new CandidateInfo(resultReference, PsiSubstitutor.EMPTY) };
        }
    };
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) CandidateInfo(com.intellij.psi.infos.CandidateInfo) ArrayList(java.util.ArrayList) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) CharTable(com.intellij.util.CharTable) NotNull(org.jetbrains.annotations.NotNull) ASTNode(com.intellij.lang.ASTNode) HashSet(java.util.HashSet) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) TextRange(com.intellij.openapi.util.TextRange) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 24 with PsiDocTag

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

the class JavadocParamTagsTest method createAndTestTag.

private static void createAndTestTag(String expectedText, String parameterName, String description) throws IncorrectOperationException {
    PsiElementFactory factory = getFactory();
    final PsiDocTag paramTag = factory.createParamTag(parameterName, description);
    assertEquals(expectedText, paramTag.getText());
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

Example 25 with PsiDocTag

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

the class JavadocParamTagsTest method testAddTag1.

public void testAddTag1() throws Exception {
    final PsiElementFactory factory = getFactory();
    final PsiMethod method = factory.createMethodFromText("/**\n" + " * Javadoc\n" + " * @param p1\n" + " */\n" + "void m();", null);
    final PsiDocComment docComment = method.getDocComment();
    assertNotNull(docComment);
    final PsiDocTag[] tags = docComment.getTags();
    final PsiDocTag tag2 = factory.createParamTag("p2", "");
    docComment.addAfter(tag2, tags[0]);
    assertEquals("/**\n" + " * Javadoc\n" + " * @param p1\n" + " * @param p2\n" + " */", docComment.getText());
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

Aggregations

PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)40 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)14 PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)7 ASTNode (com.intellij.lang.ASTNode)5 ArrayList (java.util.ArrayList)5 MethodJavaDocHelper (com.intellij.refactoring.util.javadoc.MethodJavaDocHelper)4 NotNull (org.jetbrains.annotations.NotNull)4 PsiDocParamRef (com.intellij.psi.impl.source.javadoc.PsiDocParamRef)3 PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 TextRange (com.intellij.openapi.util.TextRange)2 PsiInlineDocTag (com.intellij.psi.javadoc.PsiInlineDocTag)2 HashMap (com.intellij.util.containers.HashMap)2 TIntProcedure (gnu.trove.TIntProcedure)2 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 QuickFix (com.intellij.codeInspection.QuickFix)1 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1