Search in sources :

Example 16 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 ParameterNode parameterNode) {
    TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(parameterNode, TypeNameNode.class);
    if (typeNameNode == null) {
        return null;
    }
    PsiElement typeNode = getType(typeNameNode, true);
    if (typeNode == null) {
        return null;
    }
    return typeNode;
}
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) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with TypeNameNode

use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method isAttachedFunction.

public static boolean isAttachedFunction(@NotNull IdentifierPSINode function) {
    CodeBlockParameterNode codeBlockParameterNode = PsiTreeUtil.getChildOfType(function.getParent(), CodeBlockParameterNode.class);
    if (codeBlockParameterNode == null) {
        return false;
    }
    TypeNameNode typeNameNode = PsiTreeUtil.findChildOfType(codeBlockParameterNode, TypeNameNode.class);
    if (typeNameNode == null) {
        return false;
    }
    PsiReference reference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
    if (reference == null) {
        return false;
    }
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null) {
        return false;
    }
    return true;
}
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) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode)

Example 18 with TypeNameNode

use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method getReturnTypes.

/**
 * Generic function to get return types from a definition.
 *
 * @param definitionNode definition node
 * @return list of return values of the provided definition.
 */
@NotNull
public static List<TypeNameNode> getReturnTypes(@NotNull PsiElement definitionNode) {
    List<TypeNameNode> results = new LinkedList<>();
    // Parameters are in the ReturnParameterNode. So we first get the ReturnParameterNode from the definition
    // node.
    ReturnParameterNode node = PsiTreeUtil.findChildOfType(definitionNode, ReturnParameterNode.class);
    if (node == null) {
        return results;
    }
    // But there can be two possible scenarios. The actual return types can be in either ParameterTypeNameList or
    // ParameterListNode. This is because return types can be named parameters. In that case, ParameterListNode is
    // available.
    // First we check for ParameterTypeNameList.
    ParameterTypeNameList parameterTypeNameList = PsiTreeUtil.findChildOfType(node, ParameterTypeNameList.class);
    // If it is available, that means the return types are not named parameters.
    if (parameterTypeNameList != null) {
        // Each parameter will be of type TypeNameNode. So we get all return types.
        Collection<TypeNameNode> typeNameNodes = PsiTreeUtil.findChildrenOfType(parameterTypeNameList, TypeNameNode.class);
        // Add each TypeNameNode to the result list.
        results.addAll(typeNameNodes);
        // Return the results.
        return results;
    }
    // If there is no return type node, we check for ParameterListNode.
    ParameterListNode parameterListNode = PsiTreeUtil.findChildOfType(node, ParameterListNode.class);
    if (parameterListNode != null) {
        // Actual parameters are in ParameterNodes.
        Collection<TypeNameNode> parameterNodes = PsiTreeUtil.findChildrenOfType(parameterListNode, TypeNameNode.class);
        // Add each ParameterNode to the result list.
        results.addAll(parameterNodes);
        // Return the results.
        return results;
    }
    // Return empty list.
    return results;
}
Also used : ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) ParameterTypeNameList(org.ballerinalang.plugins.idea.psi.ParameterTypeNameList) 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) LinkedList(java.util.LinkedList) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with TypeNameNode

use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method getAttachedStruct.

@Nullable
public static StructDefinitionNode getAttachedStruct(@NotNull IdentifierPSINode function) {
    CodeBlockParameterNode codeBlockParameterNode = PsiTreeUtil.getChildOfType(function.getParent(), CodeBlockParameterNode.class);
    if (codeBlockParameterNode == null) {
        return null;
    }
    TypeNameNode typeNameNode = PsiTreeUtil.findChildOfType(codeBlockParameterNode, TypeNameNode.class);
    if (typeNameNode == null) {
        return null;
    }
    PsiReference reference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
    if (reference == null) {
        return null;
    }
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null) {
        return null;
    }
    PsiElement parent = resolvedElement.getParent();
    if (!(parent instanceof StructDefinitionNode)) {
        return null;
    }
    return (StructDefinitionNode) parent;
}
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) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with TypeNameNode

use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method getAnonymousStruct.

@Nullable
public static PsiElement getAnonymousStruct(@NotNull Object element, @NotNull List<PsiElement> parameterListNodes, int offset) {
    int index = BallerinaParameterInfoHandler.getCurrentParameterIndex(element, offset);
    PsiElement parameterListNode = parameterListNodes.get(0);
    if (parameterListNode instanceof ParameterListNode) {
        List<ParameterNode> parameterNodes = PsiTreeUtil.getChildrenOfTypeAsList(parameterListNode, ParameterNode.class);
        if (index < 0 || index >= parameterNodes.size()) {
            return null;
        }
        ParameterNode parameterNode = parameterNodes.get(index);
        TypeNameNode typeNameNode = PsiTreeUtil.findChildOfType(parameterNode, TypeNameNode.class);
        if (typeNameNode == null) {
            return null;
        }
        PsiReference reference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
        if (reference == null) {
            AnonStructTypeNameNode anonStructNode = PsiTreeUtil.findChildOfType(typeNameNode, AnonStructTypeNameNode.class);
            if (anonStructNode == null) {
                return null;
            }
            return anonStructNode;
        }
        PsiElement resolvedElement = reference.resolve();
        if (resolvedElement == null || !(resolvedElement.getParent() instanceof StructDefinitionNode)) {
            return null;
        }
        return resolvedElement.getParent();
    }
    return null;
}
Also used : ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) 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) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

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