Search in sources :

Example 6 with ParameterListNode

use of org.ballerinalang.plugins.idea.psi.ParameterListNode 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 7 with ParameterListNode

use of org.ballerinalang.plugins.idea.psi.ParameterListNode 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)

Example 8 with ParameterListNode

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

the class BallerinaParameterInfoHandler method getItemsToShow.

/**
 * Returns an list of {@link ParameterListNode} which corresponds to the given element and parent element.
 *
 * @param element
 * @param parent
 * @return
 */
private static List<PsiElement> getItemsToShow(PsiElement element, PsiElement parent) {
    // This method can be a overloaded method. So there can be multiple signatures. To store all of these, we
    // create a list.
    List<PsiElement> list = new ArrayList<>();
    // Function name will be at NameReferenceNode. So we search for this child node.
    PsiElement namedIdentifierDefNode = getNameIdentifierDefinitionNode(parent);
    PsiElement nameIdentifier = getNameIdentifier(element, namedIdentifierDefNode);
    if (nameIdentifier == null || !(nameIdentifier instanceof IdentifierPSINode)) {
        return list;
    }
    PsiReference reference = nameIdentifier.findReferenceAt(nameIdentifier.getTextLength());
    if (reference != null) {
        // Resolve the reference
        PsiElement resolvedElement = reference.resolve();
        if (resolvedElement != null) {
            // Resolved element will be the identifier of the function node. So we get the parent
            // node (FunctionDefinitionNode).
            PsiElement definitionNode = resolvedElement.getParent();
            if (definitionNode instanceof ParameterNode) {
                boolean isLambda = BallerinaPsiImplUtil.isALambdaFunction(resolvedElement);
                if (isLambda) {
                    ParameterTypeNameList parameterTypeNameList = PsiTreeUtil.findChildOfType(definitionNode, ParameterTypeNameList.class);
                    if (parameterTypeNameList != null) {
                        list.add(parameterTypeNameList);
                    }
                }
            } else {
                // Since we need the ParameterListNode, search for ParameterListNode child node.
                ParameterListNode parameterListNode = PsiTreeUtil.findChildOfType(definitionNode, ParameterListNode.class);
                // Add to the list if the result is not null.
                if (parameterListNode != null) {
                    list.add(parameterListNode);
                }
            }
        }
    }
    return list;
}
Also used : ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) ParameterTypeNameList(org.ballerinalang.plugins.idea.psi.ParameterTypeNameList) ArrayList(java.util.ArrayList) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 9 with ParameterListNode

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

the class BallerinaDocumentationProvider method getParameterString.

/**
 * Returns parameters as a single string concatenated with ",". Leading and Trailering parenthesis will be added
 * if the {@literal withParenthesis} is true.
 *
 * @param definitionNode  parent element (definition element)
 * @param withParenthesis indicate whether we want to enclose the parameters with parenthesis
 * @return a string containing the parameters.
 */
@NotNull
public static String getParameterString(PsiElement definitionNode, boolean withParenthesis) {
    ParameterListNode parameterListNode = PsiTreeUtil.getChildOfType(definitionNode, ParameterListNode.class);
    List<String> presentations = BallerinaParameterInfoHandler.getParameterPresentations(parameterListNode);
    StringBuilder stringBuilder = new StringBuilder();
    if (withParenthesis) {
        stringBuilder.append("(");
    }
    stringBuilder.append(StringUtil.join(presentations, ", "));
    if (withParenthesis) {
        stringBuilder.append(")");
    }
    return stringBuilder.toString();
}
Also used : ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ParameterListNode (org.ballerinalang.plugins.idea.psi.ParameterListNode)9 ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)6 ParameterTypeNameList (org.ballerinalang.plugins.idea.psi.ParameterTypeNameList)5 ReturnParameterNode (org.ballerinalang.plugins.idea.psi.ReturnParameterNode)5 LinkedList (java.util.LinkedList)4 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)4 PsiElement (com.intellij.psi.PsiElement)3 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)3 BuiltInReferenceTypeNameNode (org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode)3 CodeBlockParameterNode (org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode)3 NotNull (org.jetbrains.annotations.NotNull)3 PsiReference (com.intellij.psi.PsiReference)2 ArrayList (java.util.ArrayList)2 AnnotationAttachmentNode (org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode)2 AnnotationAttributeValueNode (org.ballerinalang.plugins.idea.psi.AnnotationAttributeValueNode)2 FunctionTypeNameNode (org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode)2 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)2 ResourceDefinitionNode (org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode)2 SimpleLiteralNode (org.ballerinalang.plugins.idea.psi.SimpleLiteralNode)2