Search in sources :

Example 1 with TypeNameNode

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;
}
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 TypeNameNode

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

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;
}
Also used : 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) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString)

Example 4 with TypeNameNode

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;
}
Also used : GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) 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) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with TypeNameNode

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

Aggregations

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