Search in sources :

Example 1 with ParameterTypeName

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

the class BallerinaDocumentationProvider method getReturnTypes.

/**
 * Returns the return types of the given definition node. This return types are later used to generate the
 * signature.
 *
 * @param definitionNode definition node which we want to get the return types
 * @return list of return type strings.
 */
private static List<String> getReturnTypes(PsiElement definitionNode) {
    List<String> 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<ParameterTypeName> parameterTypeNames = PsiTreeUtil.getChildrenOfTypeAsList(parameterTypeNameList, ParameterTypeName.class);
        // Add each TypeNameNode to the result list.
        parameterTypeNames.forEach(parameterTypeName -> {
            TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(parameterTypeName, TypeNameNode.class);
            if (typeNameNode != null) {
                results.add(BallerinaParameterInfoHandler.formatParameter(typeNameNode.getText()));
            }
        });
        // 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<ParameterNode> parameterNodes = PsiTreeUtil.findChildrenOfType(parameterListNode, ParameterNode.class);
        // Add each ParameterNode to the result list.
        parameterNodes.forEach(parameterNode -> results.add(BallerinaParameterInfoHandler.formatParameter(parameterNode.getText())));
        // Return the results.
        return results;
    }
    // Return empty list.
    return results;
}
Also used : ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) ParameterTypeNameList(org.ballerinalang.plugins.idea.psi.ParameterTypeNameList) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) ParameterTypeName(org.ballerinalang.plugins.idea.psi.ParameterTypeName) LinkedList(java.util.LinkedList) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode)

Example 2 with ParameterTypeName

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

the class BallerinaParameterInfoHandler method getParameterPresentations.

public static List<String> getParameterPresentations(ParameterTypeNameList node) {
    List<String> params = new LinkedList<>();
    if (node == null) {
        return params;
    }
    // Get type name nodes.
    Collection<ParameterTypeName> parameterTypeNames = PsiTreeUtil.findChildrenOfType(node, ParameterTypeName.class);
    for (ParameterTypeName parameterTypeName : parameterTypeNames) {
        TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(parameterTypeName, TypeNameNode.class);
        if (typeNameNode != null) {
            params.add(parameterTypeName.getText());
        }
    }
    return params;
}
Also used : TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) ParameterTypeName(org.ballerinalang.plugins.idea.psi.ParameterTypeName) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)2 ParameterTypeName (org.ballerinalang.plugins.idea.psi.ParameterTypeName)2 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)2 ParameterListNode (org.ballerinalang.plugins.idea.psi.ParameterListNode)1 ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)1 ParameterTypeNameList (org.ballerinalang.plugins.idea.psi.ParameterTypeNameList)1 ReturnParameterNode (org.ballerinalang.plugins.idea.psi.ReturnParameterNode)1