Search in sources :

Example 1 with AnnotationReferenceNode

use of org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode in project ballerina by ballerina-lang.

the class AnnotationAttributeReference method resolve.

@Nullable
@Override
public PsiElement resolve() {
    IdentifierPSINode identifier = getElement();
    AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getParentOfType(identifier, AnnotationAttachmentNode.class);
    if (annotationAttachmentNode == null) {
        return null;
    }
    AnnotationReferenceNode annotationReferenceNode = PsiTreeUtil.getChildOfType(annotationAttachmentNode, AnnotationReferenceNode.class);
    if (annotationReferenceNode == null) {
        return null;
    }
    int textLength = annotationReferenceNode.getTextLength();
    PsiReference reference = annotationReferenceNode.findReferenceAt(textLength);
    if (reference == null) {
        return null;
    }
    PsiElement annotationName = reference.resolve();
    if (annotationName == null || !(annotationName instanceof IdentifierPSINode)) {
        return null;
    }
    PsiElement annotationDefinition = annotationName.getParent();
    Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(annotationDefinition, FieldDefinitionNode.class);
    return BallerinaPsiImplUtil.resolveReference(fieldDefinitionNodes, identifier);
}
Also used : IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) AnnotationReferenceNode(org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode) PsiElement(com.intellij.psi.PsiElement) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with AnnotationReferenceNode

use of org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode in project ballerina by ballerina-lang.

the class BallerinaDocumentationProvider method getAnnotationAttributeNode.

/**
 * Extract and return the {@link AnnotationAttributeValueNode} if the provided annotation matches the provided type.
 *
 * @param annotation     annotation element
 * @param annotationType annotation type
 * @return {@link AnnotationAttributeValueNode} of the given element if the annotation matches the given type.
 * {@code null}
 * otherwise.
 */
private static AnnotationAttributeValueNode getAnnotationAttributeNode(PsiElement annotation, String annotationType) {
    AnnotationReferenceNode annotationReferenceNode = PsiTreeUtil.findChildOfType(annotation, AnnotationReferenceNode.class);
    if (annotationReferenceNode == null) {
        return null;
    }
    PsiElement nameReferenceNodeIdentifier = annotationReferenceNode.getNameIdentifier();
    if (nameReferenceNodeIdentifier == null) {
        return null;
    }
    if (!annotationType.equals(nameReferenceNodeIdentifier.getText())) {
        return null;
    }
    return PsiTreeUtil.findChildOfType(annotation, AnnotationAttributeValueNode.class);
}
Also used : AnnotationReferenceNode(org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode) PsiElement(com.intellij.psi.PsiElement)

Example 3 with AnnotationReferenceNode

use of org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode in project ballerina by ballerina-lang.

the class BallerinaAnnotator method canHighlightParameters.

/**
 * Checks whether the query,path parameters can be highlighted in the given element.
 *
 * @param element element which needs to be checked
 * @return {@code true} if parameters can be highlighted, {@code false} otherwise.
 */
private boolean canHighlightParameters(@NotNull PsiElement element) {
    AnnotationAttributeNode annotationAttributeNode = PsiTreeUtil.getParentOfType(element, AnnotationAttributeNode.class);
    if (annotationAttributeNode == null) {
        return false;
    }
    AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getParentOfType(annotationAttributeNode, AnnotationAttachmentNode.class);
    if (annotationAttachmentNode == null) {
        return false;
    }
    AnnotationReferenceNode annotationReferenceNode = PsiTreeUtil.getChildOfType(annotationAttachmentNode, AnnotationReferenceNode.class);
    if (annotationReferenceNode == null) {
        return false;
    }
    IdentifierPSINode annotationName = PsiTreeUtil.getChildOfType(annotationReferenceNode, IdentifierPSINode.class);
    return annotationName != null && "resourceConfig".equals(annotationName.getText());
}
Also used : AnnotationAttributeNode(org.ballerinalang.plugins.idea.psi.AnnotationAttributeNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) AnnotationReferenceNode(org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode)

Example 4 with AnnotationReferenceNode

use of org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode in project ballerina by ballerina-lang.

the class BallerinaAnnotator method isMatchingParamAvailable.

private boolean isMatchingParamAvailable(@NotNull AnnotationAttributeNode annotationAttributeNode, @NotNull String value) {
    ResourceDefinitionNode resourceDefinitionNode = PsiTreeUtil.getParentOfType(annotationAttributeNode, ResourceDefinitionNode.class);
    if (resourceDefinitionNode == null) {
        return false;
    }
    ParameterListNode parameterListNode = PsiTreeUtil.getChildOfType(resourceDefinitionNode, ParameterListNode.class);
    if (parameterListNode == null) {
        return false;
    }
    ParameterNode[] parameterNodes = PsiTreeUtil.getChildrenOfType(parameterListNode, ParameterNode.class);
    if (parameterNodes == null) {
        return false;
    }
    for (ParameterNode parameterNode : parameterNodes) {
        AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getChildOfType(parameterNode, AnnotationAttachmentNode.class);
        if (annotationAttachmentNode == null) {
            continue;
        }
        AnnotationReferenceNode annotationReferenceNode = PsiTreeUtil.getChildOfType(annotationAttachmentNode, AnnotationReferenceNode.class);
        if (annotationReferenceNode == null) {
            continue;
        }
        PsiElement paramType = annotationReferenceNode.getNameIdentifier();
        if (paramType == null) {
            continue;
        }
        if (!"PathParam".equals(paramType.getText())) {
            continue;
        }
        Collection<AnnotationAttributeValueNode> annotationAttributeValueNodes = PsiTreeUtil.findChildrenOfType(annotationAttachmentNode, AnnotationAttributeValueNode.class);
        for (AnnotationAttributeValueNode annotationAttributeValueNode : annotationAttributeValueNodes) {
            SimpleLiteralNode simpleLiteralNode = PsiTreeUtil.getChildOfType(annotationAttributeValueNode, SimpleLiteralNode.class);
            if (simpleLiteralNode == null || simpleLiteralNode.getFirstChild() == null) {
                continue;
            }
            PsiElement firstChild = simpleLiteralNode.getFirstChild();
            if (!(firstChild instanceof LeafPsiElement)) {
                continue;
            }
            if (((LeafPsiElement) firstChild).getElementType() != BallerinaTypes.QUOTED_STRING) {
                continue;
            }
            String text = firstChild.getText();
            text = text.substring(1, text.length() - 1);
            if (value.equals(text)) {
                return true;
            }
        }
    }
    // does not have an annotation attachments.
    for (ParameterNode parameterNode : parameterNodes) {
        AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getChildOfType(parameterNode, AnnotationAttachmentNode.class);
        if (annotationAttachmentNode != null) {
            continue;
        }
        PsiElement nameIdentifier = parameterNode.getNameIdentifier();
        if (nameIdentifier == null) {
            continue;
        }
        if (value.equals(nameIdentifier.getText())) {
            return true;
        }
    }
    return false;
}
Also used : ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) ResourceDefinitionNode(org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) SimpleLiteralNode(org.ballerinalang.plugins.idea.psi.SimpleLiteralNode) AnnotationAttributeValueNode(org.ballerinalang.plugins.idea.psi.AnnotationAttributeValueNode) AnnotationReferenceNode(org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode)

Example 5 with AnnotationReferenceNode

use of org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode in project ballerina by ballerina-lang.

the class AnnotationAttributeReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    IdentifierPSINode identifier = getElement();
    AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getParentOfType(identifier, AnnotationAttachmentNode.class);
    if (annotationAttachmentNode == null) {
        return new Object[0];
    }
    AnnotationReferenceNode annotationReferenceNode = PsiTreeUtil.getChildOfType(annotationAttachmentNode, AnnotationReferenceNode.class);
    if (annotationReferenceNode == null) {
        return new Object[0];
    }
    int textLength = annotationReferenceNode.getTextLength();
    PsiReference reference = annotationReferenceNode.findReferenceAt(textLength);
    if (reference == null) {
        return new Object[0];
    }
    PsiElement annotationName = reference.resolve();
    if (annotationName == null || !(annotationName instanceof IdentifierPSINode)) {
        return new Object[0];
    }
    PsiElement annotationDefinition = annotationName.getParent();
    Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(annotationDefinition, FieldDefinitionNode.class);
    List<LookupElement> results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) annotationName, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
    return results.toArray(new LookupElement[results.size()]);
}
Also used : IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) AnnotationReferenceNode(org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode) PsiElement(com.intellij.psi.PsiElement) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AnnotationReferenceNode (org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode)5 PsiElement (com.intellij.psi.PsiElement)4 AnnotationAttachmentNode (org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode)4 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)3 PsiReference (com.intellij.psi.PsiReference)2 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 AnnotationAttributeNode (org.ballerinalang.plugins.idea.psi.AnnotationAttributeNode)1 AnnotationAttributeValueNode (org.ballerinalang.plugins.idea.psi.AnnotationAttributeValueNode)1 ParameterListNode (org.ballerinalang.plugins.idea.psi.ParameterListNode)1 ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)1 ResourceDefinitionNode (org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode)1 SimpleLiteralNode (org.ballerinalang.plugins.idea.psi.SimpleLiteralNode)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1