Search in sources :

Example 1 with EnumFieldReference

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

the class IdentifierPSINode method checkAndSuggestReferenceAfterDot.

@Nullable
private PsiReference checkAndSuggestReferenceAfterDot() {
    PsiReference reference = null;
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(getParent());
    if (prevVisibleLeaf != null) {
        if (".".equals(prevVisibleLeaf.getText())) {
            PsiElement prevSibling = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
            if (prevSibling != null) {
                reference = prevSibling.findReferenceAt(prevSibling.getTextLength());
            }
        } else {
            reference = prevVisibleLeaf.findReferenceAt(prevVisibleLeaf.getTextLength());
        }
    }
    if (reference == null) {
        PsiElement prevSibling = getParent().getPrevSibling();
        if (prevSibling == null) {
            return null;
        }
        InvocationNode invocationNode = PsiTreeUtil.getChildOfType(prevSibling, InvocationNode.class);
        if (invocationNode == null) {
            return null;
        }
        AnyIdentifierNameNode anyIdentifierNameNode = PsiTreeUtil.getChildOfType(invocationNode, AnyIdentifierNameNode.class);
        if (anyIdentifierNameNode == null) {
            return null;
        }
        IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(anyIdentifierNameNode, IdentifierPSINode.class);
        if (identifier == null) {
            return null;
        }
        reference = identifier.findReferenceAt(identifier.getTextLength());
    }
    if (reference == null) {
        return null;
    }
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null) {
        return null;
    }
    // Get the return type of the function definition.
    PsiElement parent = resolvedElement.getParent();
    if (parent instanceof FunctionDefinitionNode) {
        List<TypeNameNode> returnTypes = BallerinaPsiImplUtil.getReturnTypes(parent);
        if (returnTypes.size() == 1) {
            return new TypeReference(this, returnTypes.get(0));
        }
    }
    if (resolvedElement.getParent() instanceof EndpointDeclarationNode) {
        return new ActionInvocationReference(this);
    }
    if (resolvedElement.getParent() instanceof EnumDefinitionNode) {
        return new EnumFieldReference(this);
    }
    PsiElement type = BallerinaPsiImplUtil.getType(((IdentifierPSINode) resolvedElement));
    if (type == null || (!(type instanceof BuiltInReferenceTypeNameNode) && !(type instanceof ReferenceTypeNameNode) && !(type instanceof ValueTypeNameNode))) {
        return null;
    }
    return new TypeReference(this, type);
}
Also used : EnumFieldReference(org.ballerinalang.plugins.idea.psi.references.EnumFieldReference) ActionInvocationReference(org.ballerinalang.plugins.idea.psi.references.ActionInvocationReference) PsiReference(com.intellij.psi.PsiReference) TypeReference(org.ballerinalang.plugins.idea.psi.references.TypeReference) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 ActionInvocationReference (org.ballerinalang.plugins.idea.psi.references.ActionInvocationReference)1 EnumFieldReference (org.ballerinalang.plugins.idea.psi.references.EnumFieldReference)1 TypeReference (org.ballerinalang.plugins.idea.psi.references.TypeReference)1 Nullable (org.jetbrains.annotations.Nullable)1