use of org.ballerinalang.plugins.idea.psi.StatementNode in project ballerina by ballerina-lang.
the class BallerinaParameterInfoHandler method getCurrentParameterIndex.
public static int getCurrentParameterIndex(@NotNull Object o, int offset) {
// This method updates parameter index node. This will be used to highlight the current parameter in the
// parameter popup.
PsiElement element;
// If the object is a type of ExpressionListNode, cast the object.
if (o instanceof ExpressionListNode) {
element = ((ExpressionListNode) o);
} else if (o instanceof FunctionReferenceNode) {
PsiElement parent = ((FunctionReferenceNode) o).getParent();
ExpressionListNode expressionListNode = PsiTreeUtil.getChildOfType(parent, ExpressionListNode.class);
if (expressionListNode == null) {
return 0;
}
PsiElement[] children = expressionListNode.getChildren();
return children.length / 2;
} else if (o instanceof ConnectorReferenceNode) {
PsiElement parent = ((ConnectorReferenceNode) o).getParent();
ExpressionListNode expressionListNode = PsiTreeUtil.getChildOfType(parent, ExpressionListNode.class);
if (expressionListNode == null) {
return 0;
}
PsiElement[] children = expressionListNode.getChildren();
return children.length / 2;
} else if (o instanceof IdentifierPSINode) {
StatementNode statementNode = PsiTreeUtil.getParentOfType((IdentifierPSINode) o, StatementNode.class);
if (statementNode == null) {
return 0;
}
PsiFile containingFile = statementNode.getContainingFile();
PsiElement elementAtOffset = containingFile.findElementAt(offset);
if (elementAtOffset == null) {
return 0;
}
int count = 0;
int commas = 0;
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(elementAtOffset);
do {
if (prevVisibleLeaf != null) {
if (prevVisibleLeaf.getText().matches("[,]")) {
count++;
commas++;
} else if (prevVisibleLeaf.getText().matches("[}]")) {
count++;
} else if (prevVisibleLeaf.getText().matches("[{]")) {
count = count - commas;
} else if ("(".equals(prevVisibleLeaf.getText())) {
break;
}
prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
}
} while (prevVisibleLeaf != null);
return count;
} else {
return 0;
}
if (!(o instanceof PsiElement)) {
return -1;
}
// Get the element at offset.
PsiElement psiElement = ((PsiElement) o).getContainingFile().findElementAt(offset);
if (psiElement == null) {
return -1;
}
// Get the child nodes of element.
PsiElement[] children = element.getChildren();
// If there are no child nodes, set current parameter to 0 and return.
if (children.length == 0) {
return 0;
}
// If the number of children are not 0, we need to calculate the correct index.
int index = 0;
// Iterate through all children.
for (PsiElement child : children) {
// Get the offset of the child.
int childTextOffset = child.getTextOffset();
// following condition is true. So we set the current parameter and return.
if (psiElement.getTextOffset() <= childTextOffset) {
return index;
}
// If the child is a LeafPsiElement, increment the index.
if (child instanceof LeafPsiElement) {
index++;
}
}
// the calculated current parameter.
return index;
}
Aggregations