Search in sources :

Example 46 with PsiDocComment

use of com.intellij.psi.javadoc.PsiDocComment 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 47 with PsiDocComment

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

the class PsiJavaCodeReferenceElementImpl method getKind.

public int getKind(@NotNull PsiFile containingFile) {
    if (!containingFile.isValid()) {
        // optimization to avoid relatively expensive this.isValid check
        // but still provide diagnostics for this element and not its containing DummyHolder file
        PsiUtilCore.ensureValid(this);
    }
    CompositeElement treeParent = getTreeParent();
    IElementType i = treeParent.getElementType();
    if (isDummy(i)) {
        return myKindWhenDummy;
    }
    if (i == JavaElementType.TYPE) {
        return treeParent.getTreeParent().getPsi() instanceof PsiTypeCodeFragment ? CLASS_OR_PACKAGE_NAME_KIND : CLASS_NAME_KIND;
    }
    if (i == JavaElementType.EXTENDS_LIST || i == JavaElementType.IMPLEMENTS_LIST || i == JavaElementType.EXTENDS_BOUND_LIST || i == JavaElementType.THROWS_LIST || i == JavaElementType.THIS_EXPRESSION || i == JavaElementType.SUPER_EXPRESSION || i == JavaDocElementType.DOC_METHOD_OR_FIELD_REF || i == JavaDocElementType.DOC_TAG_VALUE_ELEMENT || i == JavaElementType.REFERENCE_PARAMETER_LIST || i == JavaElementType.ANNOTATION) {
        if (isQualified()) {
            return CLASS_OR_PACKAGE_NAME_KIND;
        }
        return CLASS_NAME_KIND;
    }
    if (i == JavaElementType.NEW_EXPRESSION) {
        final ASTNode qualifier = treeParent.findChildByRole(ChildRole.QUALIFIER);
        return qualifier != null ? CLASS_IN_QUALIFIED_NEW_KIND : CLASS_NAME_KIND;
    }
    if (i == JavaElementType.ANONYMOUS_CLASS) {
        if (treeParent.getChildRole(this) == ChildRole.BASE_CLASS_REFERENCE) {
            LOG.assertTrue(treeParent.getTreeParent().getElementType() == JavaElementType.NEW_EXPRESSION);
            final ASTNode qualifier = treeParent.getTreeParent().findChildByRole(ChildRole.QUALIFIER);
            return qualifier != null ? CLASS_IN_QUALIFIED_NEW_KIND : CLASS_NAME_KIND;
        } else {
            // incomplete code
            return CLASS_OR_PACKAGE_NAME_KIND;
        }
    }
    if (i == JavaElementType.PACKAGE_STATEMENT || i == JavaElementType.EXPORTS_STATEMENT || i == JavaElementType.OPENS_STATEMENT) {
        return PACKAGE_NAME_KIND;
    }
    if (i == JavaElementType.IMPORT_STATEMENT) {
        boolean isOnDemand = SourceTreeToPsiMap.<PsiImportStatement>treeToPsiNotNull(treeParent).isOnDemand();
        return isOnDemand ? CLASS_FQ_OR_PACKAGE_NAME_KIND : CLASS_FQ_NAME_KIND;
    }
    if (i == JavaElementType.IMPORT_STATIC_STATEMENT) {
        return CLASS_FQ_OR_PACKAGE_NAME_KIND;
    }
    if (i == JavaElementType.JAVA_CODE_REFERENCE) {
        int parentKind = ((PsiJavaCodeReferenceElementImpl) treeParent).getKind(containingFile);
        if (parentKind == CLASS_NAME_KIND) {
            return CLASS_OR_PACKAGE_NAME_KIND;
        }
        if (parentKind == CLASS_FQ_NAME_KIND) {
            return CLASS_FQ_OR_PACKAGE_NAME_KIND;
        }
        return parentKind;
    }
    if (i == JavaElementType.CLASS || i == JavaElementType.PARAMETER_LIST || i == TokenType.ERROR_ELEMENT) {
        return CLASS_OR_PACKAGE_NAME_KIND;
    }
    if (i == JavaElementType.IMPORT_STATIC_REFERENCE) {
        return CLASS_FQ_OR_PACKAGE_NAME_KIND;
    }
    if (i == JavaDocElementType.DOC_TAG || i == JavaDocElementType.DOC_INLINE_TAG || i == JavaDocElementType.DOC_REFERENCE_HOLDER || i == JavaDocElementType.DOC_TYPE_HOLDER) {
        PsiDocComment docComment = PsiTreeUtil.getParentOfType(this, PsiDocComment.class);
        if (JavaDocUtil.isInsidePackageInfo(docComment)) {
            return CLASS_FQ_OR_PACKAGE_NAME_KIND;
        }
        return CLASS_OR_PACKAGE_NAME_KIND;
    }
    if (isCodeFragmentType(i)) {
        PsiJavaCodeReferenceCodeFragment fragment = (PsiJavaCodeReferenceCodeFragment) treeParent.getPsi();
        return fragment.isClassesAccepted() ? CLASS_FQ_OR_PACKAGE_NAME_KIND : PACKAGE_NAME_KIND;
    }
    if (i == JavaElementType.USES_STATEMENT || i == JavaElementType.PROVIDES_STATEMENT || i == JavaElementType.PROVIDES_WITH_LIST) {
        return CLASS_NAME_KIND;
    }
    diagnoseUnknownParent();
    return CLASS_NAME_KIND;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) ASTNode(com.intellij.lang.ASTNode)

Example 48 with PsiDocComment

use of com.intellij.psi.javadoc.PsiDocComment 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)

Example 49 with PsiDocComment

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

the class JavadocParamTagsTest method testDeleteTag1.

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

Example 50 with PsiDocComment

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

the class JavadocParamTagsTest method testDeleteTag3.

public void testDeleteTag3() throws Exception {
    final PsiElementFactory factory = getFactory();
    final PsiMethod method = factory.createMethodFromText("/**\n" + " * Javadoc\n" + " * @param p1\n" + " * @param p2\n" + " * @param p3\n" + " */" + "  void m() {}", null);
    final PsiDocComment docComment = method.getDocComment();
    assertNotNull(docComment);
    final PsiDocTag[] tags = docComment.getTags();
    ApplicationManager.getApplication().runWriteAction(() -> tags[1].delete());
    assertEquals("/**\n" + " * Javadoc\n" + " * @param p1\n" + " * @param p3\n" + " */", docComment.getText());
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag)

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