Search in sources :

Example 1 with ConnectorDeclarationStatementNode

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

the class BallerinaPsiImplUtil method resolveStructFromDefinitionNode.

@Nullable
public static StructDefinitionNode resolveStructFromDefinitionNode(@NotNull PsiElement definitionNode) {
    if (definitionNode instanceof ConnectorDeclarationStatementNode) {
        NameReferenceNode nameReferenceNode = PsiTreeUtil.getChildOfType(definitionNode, NameReferenceNode.class);
        if (nameReferenceNode != null) {
            PsiReference reference = nameReferenceNode.findReferenceAt(nameReferenceNode.getTextLength());
            if (reference != null) {
                PsiElement resolvedElement = reference.resolve();
                if (resolvedElement != null && resolvedElement.getParent() instanceof StructDefinitionNode) {
                    return ((StructDefinitionNode) resolvedElement.getParent());
                }
            }
        }
    }
    TypeNameNode typeNameNode = PsiTreeUtil.findChildOfType(definitionNode, TypeNameNode.class);
    if (typeNameNode == null) {
        return null;
    }
    NameReferenceNode nameReferenceNode = PsiTreeUtil.findChildOfType(typeNameNode, NameReferenceNode.class);
    if (nameReferenceNode == null) {
        return null;
    }
    PsiElement nameIdentifier = nameReferenceNode.getNameIdentifier();
    if (nameIdentifier == null) {
        return null;
    }
    PsiReference typeNameNodeReference = nameIdentifier.getReference();
    if (typeNameNodeReference == null) {
        return null;
    }
    PsiElement resolvedDefElement = typeNameNodeReference.resolve();
    if (resolvedDefElement == null) {
        return null;
    }
    if (!(resolvedDefElement.getParent() instanceof StructDefinitionNode)) {
        return null;
    }
    return ((StructDefinitionNode) resolvedDefElement.getParent());
}
Also used : ConnectorDeclarationStatementNode(org.ballerinalang.plugins.idea.psi.ConnectorDeclarationStatementNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) BuiltInReferenceTypeNameNode(org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode) FunctionTypeNameNode(org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) ValueTypeNameNode(org.ballerinalang.plugins.idea.psi.ValueTypeNameNode) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) PsiReference(com.intellij.psi.PsiReference) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)1 BuiltInReferenceTypeNameNode (org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode)1 ConnectorDeclarationStatementNode (org.ballerinalang.plugins.idea.psi.ConnectorDeclarationStatementNode)1 FunctionTypeNameNode (org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode)1 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)1 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)1 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)1 ValueTypeNameNode (org.ballerinalang.plugins.idea.psi.ValueTypeNameNode)1 Nullable (org.jetbrains.annotations.Nullable)1