Search in sources :

Example 6 with ConnectorReferenceNode

use of org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode 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;
}
Also used : ActionInvocationNode(org.ballerinalang.plugins.idea.psi.ActionInvocationNode) ExpressionListNode(org.ballerinalang.plugins.idea.psi.ExpressionListNode) ConnectorInitNode(org.ballerinalang.plugins.idea.psi.ConnectorInitNode) ActionInvocationNode(org.ballerinalang.plugins.idea.psi.ActionInvocationNode) InvocationNode(org.ballerinalang.plugins.idea.psi.InvocationNode) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) LinkedList(java.util.LinkedList) ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ConnectorReferenceNode(org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with ConnectorReferenceNode

use of org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode 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;
}
Also used : ExpressionListNode(org.ballerinalang.plugins.idea.psi.ExpressionListNode) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ConnectorReferenceNode(org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) StatementNode(org.ballerinalang.plugins.idea.psi.StatementNode) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)7 ConnectorReferenceNode (org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode)7 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)5 FunctionReferenceNode (org.ballerinalang.plugins.idea.psi.FunctionReferenceNode)5 ActionInvocationNode (org.ballerinalang.plugins.idea.psi.ActionInvocationNode)4 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)4 PsiReference (com.intellij.psi.PsiReference)3 ExpressionListNode (org.ballerinalang.plugins.idea.psi.ExpressionListNode)3 ExpressionNode (org.ballerinalang.plugins.idea.psi.ExpressionNode)3 FunctionInvocationNode (org.ballerinalang.plugins.idea.psi.FunctionInvocationNode)3 InvocationNode (org.ballerinalang.plugins.idea.psi.InvocationNode)3 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)3 Nullable (org.jetbrains.annotations.Nullable)3 LinkedList (java.util.LinkedList)2 ConnectorInitNode (org.ballerinalang.plugins.idea.psi.ConnectorInitNode)2 StatementNode (org.ballerinalang.plugins.idea.psi.StatementNode)2 NotNull (org.jetbrains.annotations.NotNull)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1