use of org.ballerinalang.plugins.idea.psi.NameReferenceNode in project ballerina by ballerina-lang.
the class FieldReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
IdentifierPSINode identifier = getElement();
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
// Todo - remove hard coded "."
if (prevVisibleLeaf == null || !".".equals(prevVisibleLeaf.getText())) {
return new LookupElement[0];
}
PsiElement previousField = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
if (previousField == null) {
return new LookupElement[0];
}
PsiReference reference = previousField.findReferenceAt(0);
if (reference == null) {
PsiElement prevSibling = identifier.getParent().getPrevSibling();
if (prevSibling == null) {
return new LookupElement[0];
}
if (prevSibling instanceof VariableReferenceNode) {
PsiElement[] children = prevSibling.getChildren();
if (children.length <= 0) {
return new LookupElement[0];
}
PsiElement firstChild = children[0].getFirstChild();
if (firstChild == null) {
return new LookupElement[0];
}
PsiReference functionReference = firstChild.findReferenceAt(firstChild.getTextLength());
if (functionReference == null) {
return new LookupElement[0];
}
PsiElement resolvedElement = functionReference.resolve();
if (resolvedElement == null) {
return new LookupElement[0];
}
PsiElement parent = resolvedElement.getParent();
if (parent instanceof FunctionDefinitionNode) {
List<TypeNameNode> returnTypes = BallerinaPsiImplUtil.getReturnTypes(((FunctionDefinitionNode) parent));
if (returnTypes.size() == 1) {
TypeNameNode typeNameNode = returnTypes.get(0);
List<LookupElement> functions = Arrays.asList((LookupElement[]) TypeReference.getVariants(typeNameNode));
return functions.toArray(new LookupElement[functions.size()]);
}
}
}
}
if (reference == null) {
return new LookupElement[0];
}
// Todo - use util method
PsiElement resolvedElement = reference.resolve();
if (resolvedElement == null || !(resolvedElement instanceof IdentifierPSINode)) {
return new LookupElement[0];
}
PsiElement resolvedElementParent = resolvedElement.getParent();
StructDefinitionNode structDefinitionNode = null;
List<LookupElement> results = new LinkedList<>();
// Resolve the corresponding resolvedElementParent to get the struct definition.
if (resolvedElementParent instanceof VariableDefinitionNode || resolvedElementParent instanceof CodeBlockParameterNode || resolvedElementParent instanceof ParameterNode) {
structDefinitionNode = BallerinaPsiImplUtil.resolveStructFromDefinitionNode(resolvedElementParent);
} else if (resolvedElementParent instanceof FieldDefinitionNode) {
structDefinitionNode = BallerinaPsiImplUtil.resolveTypeNodeStruct((resolvedElementParent));
} else if (resolvedElementParent instanceof NameReferenceNode) {
AssignmentStatementNode assignmentStatementNode = PsiTreeUtil.getParentOfType(resolvedElement, AssignmentStatementNode.class);
if (assignmentStatementNode != null) {
structDefinitionNode = BallerinaPsiImplUtil.getStructDefinition(assignmentStatementNode, ((IdentifierPSINode) resolvedElement));
} else {
structDefinitionNode = BallerinaPsiImplUtil.findStructDefinition((IdentifierPSINode) resolvedElement);
}
if (structDefinitionNode != null) {
IdentifierPSINode structName = PsiTreeUtil.findChildOfType(structDefinitionNode, IdentifierPSINode.class);
if (structName != null) {
resolvedElement = structName;
}
}
} else if (resolvedElementParent instanceof EnumDefinitionNode) {
Collection<EnumFieldNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(resolvedElementParent, EnumFieldNode.class);
results.addAll(BallerinaCompletionUtils.createEnumFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement));
return results.toArray(new LookupElement[results.size()]);
} else if (resolvedElementParent instanceof StructDefinitionNode) {
structDefinitionNode = ((StructDefinitionNode) resolvedElementParent);
}
if (structDefinitionNode == null) {
return new LookupElement[0];
}
Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
results.addAll(BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement, null));
List<IdentifierPSINode> attachedFunctions = BallerinaPsiImplUtil.getAttachedFunctions(structDefinitionNode);
results.addAll(BallerinaCompletionUtils.createAttachedFunctionsLookupElements(attachedFunctions));
return results.toArray(new LookupElement[results.size()]);
}
use of org.ballerinalang.plugins.idea.psi.NameReferenceNode in project ballerina by ballerina-lang.
the class BallerinaParameterInfoHandler method showParameterInfo.
@Override
public void showParameterInfo(@NotNull Object element, @NotNull CreateParameterInfoContext context) {
// This method will be called with the return object of the findElementForParameterInfo(). If it is null,
// this method will not be called.
// Since we know the type, we check and cast the object.
PsiElement currentElement = null;
PsiElement parentElement = null;
List<PsiElement> list = getParameters(element);
if (element instanceof ExpressionListNode) {
ExpressionListNode expressionListNode = (ExpressionListNode) element;
// We need to get the ExpressionListNode parent of current ExpressionListNode.
// Current ExpressionListNode - "WSO2"
// Parent ExpressionListNode - setName("WSO2")
// By doing this, we get the function name because setName("WSO2") is also a ExpressionNode.
PsiElement parent = PsiTreeUtil.getParentOfType(expressionListNode, FunctionInvocationNode.class);
// node is a FunctionInvocationNode.
if (parent == null) {
// So if the parent is null, we consider the FunctionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionNode.class);
}
if (parent == null) {
parent = PsiTreeUtil.getParentOfType(expressionListNode, InvocationNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ActionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ActionInvocationNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ActionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ConnectorInitNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ExpressionListNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionListNode.class);
}
if (parent == null) {
parent = expressionListNode;
}
currentElement = expressionListNode;
parentElement = parent;
} else if (element instanceof FunctionInvocationNode) {
FunctionInvocationNode functionInvocationNode = (FunctionInvocationNode) element;
currentElement = functionInvocationNode;
parentElement = functionInvocationNode;
} else if (element instanceof ActionInvocationNode) {
ActionInvocationNode actionInvocationNode = (ActionInvocationNode) element;
currentElement = actionInvocationNode;
parentElement = actionInvocationNode;
} else if (element instanceof ConnectorInitNode) {
ConnectorInitNode connectorInitNode = (ConnectorInitNode) element;
currentElement = connectorInitNode;
parentElement = connectorInitNode;
} else if (element instanceof ExpressionNode) {
ExpressionNode expressionNode = (ExpressionNode) element;
currentElement = expressionNode;
parentElement = expressionNode;
} else if (element instanceof NameReferenceNode) {
NameReferenceNode nameReferenceNode = (NameReferenceNode) element;
currentElement = nameReferenceNode;
parentElement = nameReferenceNode;
} else if (element instanceof FunctionReferenceNode) {
FunctionReferenceNode functionReferenceNode = (FunctionReferenceNode) element;
currentElement = functionReferenceNode;
parentElement = functionReferenceNode;
} else if (element instanceof ConnectorReferenceNode) {
ConnectorReferenceNode connectorReferenceNode = (ConnectorReferenceNode) element;
currentElement = connectorReferenceNode;
parentElement = connectorReferenceNode;
} else if (element instanceof IdentifierPSINode) {
IdentifierPSINode identifier = (IdentifierPSINode) element;
currentElement = identifier;
parentElement = identifier;
} else if (element instanceof InvocationNode) {
InvocationNode invocationNode = (InvocationNode) element;
currentElement = invocationNode;
parentElement = invocationNode;
}
PsiElement namedIdentifierDefNode = getNameIdentifierDefinitionNode(parentElement);
PsiElement nameIdentifier = getNameIdentifier(currentElement, namedIdentifierDefNode);
if (currentElement == null || nameIdentifier == null || !(nameIdentifier instanceof IdentifierPSINode)) {
return;
}
if (list == null) {
list = new LinkedList<>();
}
// If there are no items to show, set a custom object. Otherwise set the list as an array.
if (list.isEmpty() && canResolve(nameIdentifier)) {
// Todo - change how to identify no parameter situation
context.setItemsToShow(new Object[] { "Empty" });
} else {
context.setItemsToShow(list.toArray(new PsiElement[list.size()]));
}
context.showHint(currentElement, currentElement.getTextRange().getStartOffset(), this);
}
use of org.ballerinalang.plugins.idea.psi.NameReferenceNode in project ballerina by ballerina-lang.
the class BallerinaParameterInfoHandler method getParameters.
/**
* Returns the parameter list for the given element.
*
* @param element
* @return
*/
@NotNull
public static List<PsiElement> getParameters(@NotNull Object element) {
List<PsiElement> list = new LinkedList<>();
if (element instanceof ExpressionListNode) {
ExpressionListNode expressionListNode = (ExpressionListNode) element;
// We need to get the ExpressionListNode parent of current ExpressionListNode.
// Current ExpressionListNode - "WSO2"
// Parent ExpressionListNode - setName("WSO2")
PsiElement parent = PsiTreeUtil.getParentOfType(expressionListNode, FunctionInvocationNode.class);
if (parent == null) {
// So if the parent is null, we consider the ActionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ActionInvocationNode.class);
}
if (parent == null) {
parent = PsiTreeUtil.getParentOfType(expressionListNode, InvocationNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ActionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ConnectorInitNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the FunctionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ExpressionListNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionListNode.class);
}
if (parent == null) {
parent = expressionListNode;
}
list = getItemsToShow(expressionListNode, parent);
} else if (element instanceof FunctionInvocationNode) {
FunctionInvocationNode functionInvocationNode = (FunctionInvocationNode) element;
list = getItemsToShow(functionInvocationNode, functionInvocationNode);
} else if (element instanceof ActionInvocationNode) {
ActionInvocationNode actionInvocationNode = (ActionInvocationNode) element;
list = getItemsToShow(actionInvocationNode, actionInvocationNode);
} else if (element instanceof ConnectorInitNode) {
ConnectorInitNode connectorInitNode = (ConnectorInitNode) element;
list = getItemsToShow(connectorInitNode, connectorInitNode);
} else if (element instanceof ExpressionNode) {
ExpressionNode expressionNode = (ExpressionNode) element;
list = getItemsToShow(expressionNode, expressionNode);
} else if (element instanceof NameReferenceNode) {
NameReferenceNode nameReferenceNode = (NameReferenceNode) element;
list = getItemsToShow(nameReferenceNode, nameReferenceNode);
} else if (element instanceof FunctionReferenceNode) {
FunctionReferenceNode functionReferenceNode = (FunctionReferenceNode) element;
list = getItemsToShow(functionReferenceNode, functionReferenceNode);
} else if (element instanceof ConnectorReferenceNode) {
ConnectorReferenceNode connectorReferenceNode = (ConnectorReferenceNode) element;
list = getItemsToShow(connectorReferenceNode, connectorReferenceNode);
} else if (element instanceof IdentifierPSINode) {
IdentifierPSINode identifier = (IdentifierPSINode) element;
list = getItemsToShow(identifier, identifier);
} else if (element instanceof InvocationNode) {
InvocationNode invocationNode = (InvocationNode) element;
list = getItemsToShow(invocationNode, invocationNode);
}
return list;
}
Aggregations