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));
}
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;
}
Aggregations