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