Search in sources :

Example 1 with TypeCastNode

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

the class BallerinaPsiImplUtil method getType.

@Nullable
private static PsiElement getType(@NotNull AssignmentStatementNode assignmentStatementNode, @NotNull IdentifierPSINode identifier) {
    if (!BallerinaPsiImplUtil.isVarAssignmentStatement(assignmentStatementNode)) {
        return null;
    }
    ExpressionNode expressionNode = PsiTreeUtil.getChildOfType(assignmentStatementNode, ExpressionNode.class);
    if (expressionNode == null) {
        return null;
    }
    PsiElement expressionNodeFirstChild = expressionNode.getFirstChild();
    if (expressionNodeFirstChild instanceof VariableReferenceNode) {
        PsiElement typeNode = getTypeFromVariableReference(assignmentStatementNode, identifier, expressionNodeFirstChild);
        if (typeNode != null) {
            return typeNode;
        }
    } else if (expressionNodeFirstChild instanceof TypeCastNode) {
        int index = getVariableIndexFromVarAssignment(assignmentStatementNode, identifier);
        if (index == 0) {
            TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(expressionNodeFirstChild, TypeNameNode.class);
            if (typeNameNode == null) {
                return null;
            }
            return getType(typeNameNode, false);
        }
        StructDefinitionNode errorStruct = BallerinaPsiImplUtil.getErrorStruct(assignmentStatementNode, identifier, true, false);
        if (errorStruct == null) {
            return null;
        }
        return errorStruct.getNameIdentifier();
    } else if (expressionNodeFirstChild instanceof TypeConversionNode) {
        int index = getVariableIndexFromVarAssignment(assignmentStatementNode, identifier);
        if (index == 0) {
            TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(expressionNodeFirstChild, TypeNameNode.class);
            if (typeNameNode == null) {
                return null;
            }
            return getType(typeNameNode, false);
        }
        StructDefinitionNode errorStruct = BallerinaPsiImplUtil.getErrorStruct(assignmentStatementNode, identifier, false, true);
        if (errorStruct == null) {
            return null;
        }
        return errorStruct.getNameIdentifier();
    }
    PsiReference firstChildReference = expressionNodeFirstChild.findReferenceAt(expressionNodeFirstChild.getTextLength());
    if (firstChildReference == null) {
        return null;
    }
    PsiElement resolvedElement = firstChildReference.resolve();
    if (resolvedElement == null) {
        return null;
    }
    return getType(((IdentifierPSINode) resolvedElement));
}
Also used : StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) 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) TypeConversionNode(org.ballerinalang.plugins.idea.psi.TypeConversionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) TypeCastNode(org.ballerinalang.plugins.idea.psi.TypeCastNode) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with TypeCastNode

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

the class BallerinaPsiImplUtil method getStructDefinition.

/**
 * Checks the RHS of the provided {@link AssignmentStatementNode} and returns the corresponding
 * {@link StructDefinitionNode}.
 *
 * @param assignmentStatementNode an assignment statement node
 * @param structReferenceNode     an identifier which is a var variable in the assignment node
 * @return {@code null} if cannot suggest a {@link StructDefinitionNode}, otherwise returns the corresponding
 * {@link StructDefinitionNode}.
 */
@Nullable
public static StructDefinitionNode getStructDefinition(@NotNull AssignmentStatementNode assignmentStatementNode, @NotNull IdentifierPSINode structReferenceNode) {
    if (!BallerinaPsiImplUtil.isVarAssignmentStatement(assignmentStatementNode)) {
        return null;
    }
    ExpressionNode expressionNode = PsiTreeUtil.getChildOfType(assignmentStatementNode, ExpressionNode.class);
    if (expressionNode == null) {
        return null;
    }
    PsiElement expressionNodeFirstChild = expressionNode.getFirstChild();
    if (expressionNodeFirstChild instanceof VariableReferenceNode) {
        return getStructDefinition((VariableReferenceNode) expressionNodeFirstChild, assignmentStatementNode, structReferenceNode);
    } else if (expressionNodeFirstChild instanceof TypeCastNode) {
        int index = getVariableIndexFromVarAssignment(assignmentStatementNode, structReferenceNode);
        if (index == 0) {
            return resolveTypeNodeStruct((expressionNodeFirstChild));
        } else if (index == 1) {
            return getErrorStruct(assignmentStatementNode, structReferenceNode, true, false);
        }
    } else if (expressionNodeFirstChild instanceof TypeConversionNode) {
        int index = getVariableIndexFromVarAssignment(assignmentStatementNode, structReferenceNode);
        if (index == 0) {
            return resolveTypeNodeStruct((expressionNodeFirstChild));
        } else if (index == 1) {
            return getErrorStruct(assignmentStatementNode, structReferenceNode, false, true);
        }
    }
    return null;
}
Also used : ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) TypeConversionNode(org.ballerinalang.plugins.idea.psi.TypeConversionNode) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) TypeCastNode(org.ballerinalang.plugins.idea.psi.TypeCastNode) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 ExpressionNode (org.ballerinalang.plugins.idea.psi.ExpressionNode)2 TypeCastNode (org.ballerinalang.plugins.idea.psi.TypeCastNode)2 TypeConversionNode (org.ballerinalang.plugins.idea.psi.TypeConversionNode)2 VariableReferenceNode (org.ballerinalang.plugins.idea.psi.VariableReferenceNode)2 Nullable (org.jetbrains.annotations.Nullable)2 PsiReference (com.intellij.psi.PsiReference)1 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)1 BuiltInReferenceTypeNameNode (org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode)1 FunctionTypeNameNode (org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode)1 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)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