use of org.ballerinalang.plugins.idea.psi.StructDefinitionNode 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.StructDefinitionNode 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.StructDefinitionNode 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;
}
use of org.ballerinalang.plugins.idea.psi.StructDefinitionNode 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());
}
use of org.ballerinalang.plugins.idea.psi.StructDefinitionNode in project ballerina by ballerina-lang.
the class InvocationReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
StructDefinitionNode structDefinition = getStructDefinitionNode();
if (structDefinition == null) {
return new LookupElement[0];
}
List<IdentifierPSINode> attachedFunctions = BallerinaPsiImplUtil.getAttachedFunctions(structDefinition);
List<LookupElement> results = BallerinaCompletionUtils.createAttachedFunctionsLookupElements(attachedFunctions);
return results.toArray(new LookupElement[results.size()]);
}
Aggregations