Search in sources :

Example 1 with InvocationReference

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

the class IdentifierPSINode method suggestReferenceTypeForInvocation.

@Nullable
private PsiReference suggestReferenceTypeForInvocation() {
    PsiElement parent = getParent();
    PsiElement prevSibling = parent.getPrevSibling();
    if (prevSibling == null) {
        return null;
    }
    PsiReference reference = null;
    if (parent instanceof AnyIdentifierNameNode) {
        InvocationNode invocationNode = PsiTreeUtil.getParentOfType(parent, InvocationNode.class);
        if (invocationNode != null) {
            PsiElement invocationNodePrevSibling = invocationNode.getPrevSibling();
            if (invocationNodePrevSibling != null) {
                if (invocationNodePrevSibling instanceof VariableReferenceNode) {
                    InvocationNode node = PsiTreeUtil.getChildOfType(invocationNodePrevSibling, InvocationNode.class);
                    if (node != null) {
                        IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(node, IdentifierPSINode.class);
                        if (identifier != null) {
                            reference = identifier.findReferenceAt(identifier.getTextLength());
                        }
                    } else {
                        NameReferenceNode nameReferenceNode = PsiTreeUtil.getChildOfType(invocationNodePrevSibling, NameReferenceNode.class);
                        if (nameReferenceNode != null) {
                            IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(nameReferenceNode, IdentifierPSINode.class);
                            if (identifier != null) {
                                reference = identifier.findReferenceAt(identifier.getTextLength());
                            }
                        }
                    }
                } else {
                    reference = invocationNodePrevSibling.findReferenceAt(invocationNodePrevSibling.getTextLength());
                }
            }
        }
    } else if (prevSibling instanceof VariableReferenceNode) {
        InvocationNode invocationNode = PsiTreeUtil.getChildOfType(prevSibling, InvocationNode.class);
        if (invocationNode != null) {
            IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(invocationNode, IdentifierPSINode.class);
            if (identifier == null) {
                return null;
            }
            reference = identifier.findReferenceAt(identifier.getTextLength());
        }
        FieldNode fieldNode = PsiTreeUtil.getChildOfType(prevSibling, FieldNode.class);
        if (fieldNode != null) {
            IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(fieldNode, IdentifierPSINode.class);
            if (identifier == null) {
                return null;
            }
            reference = identifier.findReferenceAt(identifier.getTextLength());
        }
        FunctionInvocationNode functionInvocationNode = PsiTreeUtil.getChildOfType(prevSibling, FunctionInvocationNode.class);
        if (functionInvocationNode != null) {
            FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getChildOfType(functionInvocationNode, FunctionReferenceNode.class);
            if (functionReferenceNode != null) {
                return new NameReference(this);
            }
        }
        if (reference == null) {
            reference = prevSibling.findReferenceAt(prevSibling.getTextLength());
        }
    } else {
        reference = prevSibling.findReferenceAt(prevSibling.getTextLength());
    }
    if (reference == null) {
        return null;
    }
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null) {
        return null;
    }
    PsiElement definitionNode = resolvedElement.getParent();
    if (definitionNode instanceof VariableDefinitionNode) {
        ConnectorDefinitionNode connectorDefinitionNode = BallerinaPsiImplUtil.resolveConnectorFromVariableDefinitionNode((definitionNode));
        if (connectorDefinitionNode != null) {
            return new ActionInvocationReference(this);
        }
    } else if (definitionNode instanceof EndpointDeclarationNode) {
        ConnectorReferenceNode connectorReferenceNode = PsiTreeUtil.getChildOfType(definitionNode, ConnectorReferenceNode.class);
        if (connectorReferenceNode != null) {
            return new ActionInvocationReference(this);
        }
    } else if (definitionNode instanceof FunctionDefinitionNode) {
        return new NameReference(this);
    }
    reference = checkAndSuggestReferenceAfterDot();
    if (reference != null) {
        return reference;
    }
    return new InvocationReference(this);
}
Also used : PackageNameReference(org.ballerinalang.plugins.idea.psi.references.PackageNameReference) NameReference(org.ballerinalang.plugins.idea.psi.references.NameReference) ActionInvocationReference(org.ballerinalang.plugins.idea.psi.references.ActionInvocationReference) PsiReference(com.intellij.psi.PsiReference) ActionInvocationReference(org.ballerinalang.plugins.idea.psi.references.ActionInvocationReference) InvocationReference(org.ballerinalang.plugins.idea.psi.references.InvocationReference) 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 InvocationReference (org.ballerinalang.plugins.idea.psi.references.InvocationReference)1 NameReference (org.ballerinalang.plugins.idea.psi.references.NameReference)1 PackageNameReference (org.ballerinalang.plugins.idea.psi.references.PackageNameReference)1 Nullable (org.jetbrains.annotations.Nullable)1