use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getStructDefinition.
@Nullable
public static StructDefinitionNode getStructDefinition(@NotNull AssignmentStatementNode assignmentStatementNode, @NotNull IdentifierPSINode structReferenceNode, @NotNull PsiElement definitionNode) {
// Now we need to know the index of the provided struct reference node in the assignment statement node.
int index = getVariableIndexFromVarAssignment(assignmentStatementNode, structReferenceNode);
if (index < 0) {
return null;
}
// Now we get all of the return types from the function.
List<TypeNameNode> returnTypes = new LinkedList<>();
if (definitionNode instanceof FunctionDefinitionNode) {
returnTypes = getReturnTypes(((FunctionDefinitionNode) definitionNode));
}
if (definitionNode instanceof ActionDefinitionNode) {
returnTypes = getReturnTypes(((ActionDefinitionNode) definitionNode));
}
// at least 1, etc.
if (returnTypes.size() <= index) {
return null;
}
// Get the corresponding return type.
PsiElement elementType = returnTypes.get(index);
// If this is a struct, we need to resolve it to the definition.
PsiReference referenceAtElementType = elementType.findReferenceAt(elementType.getTextLength());
if (referenceAtElementType == null) {
return null;
}
PsiElement resolvedIdentifier = referenceAtElementType.resolve();
if (resolvedIdentifier != null) {
// If we can resolve it to a definition, parent should be a struct definition node.
PsiElement structDefinition = resolvedIdentifier.getParent();
if (structDefinition instanceof StructDefinitionNode) {
return (StructDefinitionNode) structDefinition;
}
}
return null;
}
use of org.ballerinalang.plugins.idea.psi.TypeNameNode 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.TypeNameNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method isArrayDefinition.
public static boolean isArrayDefinition(@NotNull PsiElement definitionNode) {
TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(definitionNode, TypeNameNode.class);
if (typeNameNode == null) {
return false;
}
String definitionText = typeNameNode.getText();
String definitionRegex = "\\[\\s*]";
int definitionDimension = getArrayDimension(definitionText, definitionRegex);
return definitionDimension != 0;
}
use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getType.
/**
* Returns the type of the provided identifier.
*
* @param identifier an identifier
* @return the type of the identifier. This can be one of {@link ValueTypeNameNode} (for value types like string),
* {@link BuiltInReferenceTypeNameNode} (for reference types like json) or {@link TypeNameNode} (for arrays).
*/
@Nullable
public static PsiElement getType(@NotNull IdentifierPSINode identifier) {
PsiElement parent = identifier.getParent();
// In case of lambda functions or functions attached to types.
if (parent instanceof FunctionDefinitionNode) {
CodeBlockParameterNode codeBlockParameterNode = PsiTreeUtil.getChildOfType(parent, CodeBlockParameterNode.class);
if (codeBlockParameterNode != null) {
TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(codeBlockParameterNode, TypeNameNode.class);
if (typeNameNode != null) {
return typeNameNode.getFirstChild();
}
}
}
PsiReference reference = identifier.findReferenceAt(0);
if (reference != null) {
// Todo - Do we need to consider this situation?
}
VariableDefinitionNode variableDefinitionNode = PsiTreeUtil.getParentOfType(identifier, VariableDefinitionNode.class);
if (variableDefinitionNode != null) {
PsiElement typeNode = getType(variableDefinitionNode);
if (typeNode != null) {
return typeNode;
}
}
AssignmentStatementNode assignmentStatementNode = PsiTreeUtil.getParentOfType(identifier, AssignmentStatementNode.class);
if (assignmentStatementNode != null) {
PsiElement typeNode = getType(assignmentStatementNode, identifier);
if (typeNode != null) {
return typeNode;
}
}
ParameterNode parameterNode = PsiTreeUtil.getParentOfType(identifier, ParameterNode.class);
if (parameterNode != null) {
PsiElement typeNode = getType(parameterNode);
if (typeNode != null) {
return typeNode;
}
}
FieldDefinitionNode fieldDefinitionNode = PsiTreeUtil.getParentOfType(identifier, FieldDefinitionNode.class);
if (fieldDefinitionNode != null) {
PsiElement typeNode = getType(fieldDefinitionNode);
if (typeNode != null) {
return typeNode;
}
}
return null;
}
use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method resolveTypeNodeStruct.
public static StructDefinitionNode resolveTypeNodeStruct(@NotNull PsiElement element) {
TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(element, TypeNameNode.class);
if (typeNameNode == null) {
return null;
}
PsiReference nameReference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
if (nameReference == null) {
return null;
}
PsiElement resolvedElement = nameReference.resolve();
if (resolvedElement == null) {
return null;
}
if (resolvedElement.getParent() instanceof StructDefinitionNode) {
return ((StructDefinitionNode) resolvedElement.getParent());
}
return null;
}
Aggregations