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