Search in sources :

Example 1 with StructDefinitionNode

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;
}
Also used : FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) 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) ActionDefinitionNode(org.ballerinalang.plugins.idea.psi.ActionDefinitionNode) PsiReference(com.intellij.psi.PsiReference) LinkedList(java.util.LinkedList) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with StructDefinitionNode

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));
}
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 3 with StructDefinitionNode

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;
}
Also used : 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) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 4 with StructDefinitionNode

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

Example 5 with StructDefinitionNode

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()]);
}
Also used : StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)21 PsiElement (com.intellij.psi.PsiElement)17 PsiReference (com.intellij.psi.PsiReference)13 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)12 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)11 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)10 Nullable (org.jetbrains.annotations.Nullable)10 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)9 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)8 NotNull (org.jetbrains.annotations.NotNull)8 BuiltInReferenceTypeNameNode (org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode)7 FunctionTypeNameNode (org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode)7 ValueTypeNameNode (org.ballerinalang.plugins.idea.psi.ValueTypeNameNode)7 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)6 LinkedList (java.util.LinkedList)6 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)6 ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)6 LookupElement (com.intellij.codeInsight.lookup.LookupElement)5 CodeBlockParameterNode (org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode)5 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)5