Search in sources :

Example 1 with ConnectorReferenceNode

use of org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode in project ballerina by ballerina-lang.

the class BallerinaParameterInfoHandler method findElement.

@Nullable
public static Object findElement(@NotNull PsiElement element, @Nullable PsiElement prevElement) {
    // This will allow us to identify the correct ExpressionListNode element.
    if (")".equals(element.getText())) {
        PsiElement parent = element.getParent();
        // invocations will not show proper parameters.
        if (parent != null && !(parent instanceof ActionInvocationNode)) {
            FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getChildOfType(parent, FunctionReferenceNode.class);
            if (functionReferenceNode != null) {
                return functionReferenceNode;
            }
            ConnectorReferenceNode connectorReferenceNode = PsiTreeUtil.getChildOfType(parent, ConnectorReferenceNode.class);
            if (connectorReferenceNode != null) {
                return connectorReferenceNode;
            }
            if (parent instanceof StatementNode) {
                PsiElement firstChild = parent.getFirstChild();
                PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(firstChild);
                if (prevVisibleLeaf == null || !"(".equals(prevVisibleLeaf.getText())) {
                    if (firstChild instanceof PsiErrorElement) {
                        return PsiTreeUtil.findChildOfType(firstChild, IdentifierPSINode.class);
                    }
                    return prevVisibleLeaf;
                } else if ("(".equals(prevVisibleLeaf.getText())) {
                    return PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
                } else {
                    return prevVisibleLeaf.getParent().getFirstChild();
                }
            }
        }
        // contain "(".
        if (prevElement != null) {
            element = prevElement;
        }
    }
    PsiElement node = PsiTreeUtil.getParentOfType(element, ExpressionListNode.class);
    // ExpressionListNode can be null if there are no arguments provided.
    if (node != null) {
        return node;
    }
    // So we check return the FunctionInvocationNode in that case.
    node = PsiTreeUtil.getParentOfType(element, FunctionInvocationNode.class);
    if (node != null) {
        return node;
    }
    // If FunctionInvocationNode is null, we check return the ActionInvocationNode in that case.
    node = PsiTreeUtil.getParentOfType(element, ActionInvocationNode.class);
    if (node != null) {
        return node;
    }
    // If ActionInvocationNode is null, we check return the ConnectorInitNode in that case.
    node = PsiTreeUtil.getParentOfType(element, InvocationNode.class);
    if (node != null) {
        return node;
    }
    // any expression node parent.
    if (element != null && "(".equals(element.getText())) {
        node = PsiTreeUtil.getParentOfType(element, ExpressionNode.class);
    }
    if (node != null) {
        return node;
    }
    if (prevElement != null && prevElement.getText().matches("[{,]") || prevElement instanceof LeafPsiElement) {
        return resolve(prevElement);
    }
    return null;
}
Also used : ActionInvocationNode(org.ballerinalang.plugins.idea.psi.ActionInvocationNode) PsiErrorElement(com.intellij.psi.PsiErrorElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) ConnectorReferenceNode(org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) StatementNode(org.ballerinalang.plugins.idea.psi.StatementNode) ActionInvocationNode(org.ballerinalang.plugins.idea.psi.ActionInvocationNode) InvocationNode(org.ballerinalang.plugins.idea.psi.InvocationNode) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ConnectorReferenceNode

use of org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode in project ballerina by ballerina-lang.

the class BallerinaUnresolvedReferenceInspection method checkFile.

@Override
@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    // does not work in tests since CodeInsightTestCase copies file into temporary location
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return new ProblemDescriptor[0];
    }
    if (!(file instanceof BallerinaFile)) {
        return new ProblemDescriptor[0];
    }
    List<ProblemDescriptor> problemDescriptors = new LinkedList<>();
    Collection<PackageNameNode> packageNameNodes = PsiTreeUtil.findChildrenOfType(file, PackageNameNode.class);
    for (PackageNameNode packageNameNode : packageNameNodes) {
        ProgressManager.checkCanceled();
        if (packageNameNode == null) {
            continue;
        }
        AliasNode aliasNode = PsiTreeUtil.getParentOfType(packageNameNode, AliasNode.class);
        if (aliasNode != null) {
            continue;
        }
        PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.getParentOfType(packageNameNode, PackageDeclarationNode.class);
        if (packageDeclarationNode != null) {
            continue;
        }
        XmlAttribNode xmlAttribNode = PsiTreeUtil.getParentOfType(packageNameNode, XmlAttribNode.class);
        if (xmlAttribNode != null) {
            continue;
        }
        LocalQuickFix[] availableFixes;
        ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(packageNameNode, ImportDeclarationNode.class);
        if (importDeclarationNode != null) {
            availableFixes = new LocalQuickFix[0];
        } else {
            BallerinaImportPackageQuickFix quickFix = new BallerinaImportPackageQuickFix(packageNameNode);
            availableFixes = new LocalQuickFix[] { quickFix };
        }
        PsiElement nameIdentifier = packageNameNode.getNameIdentifier();
        if (nameIdentifier == null) {
            continue;
        }
        PsiReference reference = nameIdentifier.getReference();
        if (reference == null || reference.resolve() == null) {
            if (reference instanceof PackageNameReference) {
                ResolveResult[] resolveResults = ((PackageNameReference) reference).multiResolve(false);
                if (resolveResults.length > 0) {
                    continue;
                }
            }
            problemDescriptors.add(createProblemDescriptor(manager, packageNameNode, isOnTheFly, availableFixes));
        }
    }
    // Todo - Add new quick fixes.
    LocalQuickFix[] availableFixes = new LocalQuickFix[0];
    Collection<NameReferenceNode> nameReferenceNodes = PsiTreeUtil.findChildrenOfType(file, NameReferenceNode.class);
    problemDescriptors.addAll(getUnresolvedNameReferenceDescriptors(manager, isOnTheFly, availableFixes, nameReferenceNodes));
    Collection<AnnotationAttributeNode> annotationAttributeNodes = PsiTreeUtil.findChildrenOfType(file, AnnotationAttributeNode.class);
    problemDescriptors.addAll(getUnresolvedReferenceDescriptors(manager, isOnTheFly, availableFixes, annotationAttributeNodes));
    Collection<ConnectorReferenceNode> connectorReferenceNodes = PsiTreeUtil.findChildrenOfType(file, ConnectorReferenceNode.class);
    problemDescriptors.addAll(getUnresolvedReferenceDescriptors(manager, isOnTheFly, availableFixes, connectorReferenceNodes));
    Collection<ActionInvocationNode> actionInvocationNodes = PsiTreeUtil.findChildrenOfType(file, ActionInvocationNode.class);
    problemDescriptors.addAll(getUnresolvedReferenceDescriptors(manager, isOnTheFly, availableFixes, actionInvocationNodes));
    Collection<FunctionReferenceNode> functionReferenceNodes = PsiTreeUtil.findChildrenOfType(file, FunctionReferenceNode.class);
    problemDescriptors.addAll(getUnresolvedReferenceDescriptors(manager, isOnTheFly, availableFixes, functionReferenceNodes));
    return problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
}
Also used : BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PackageNameReference(org.ballerinalang.plugins.idea.psi.references.PackageNameReference) AnnotationAttributeNode(org.ballerinalang.plugins.idea.psi.AnnotationAttributeNode) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) XmlAttribNode(org.ballerinalang.plugins.idea.psi.XmlAttribNode) ConnectorReferenceNode(org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) AliasNode(org.ballerinalang.plugins.idea.psi.AliasNode) ActionInvocationNode(org.ballerinalang.plugins.idea.psi.ActionInvocationNode) PsiReference(com.intellij.psi.PsiReference) PackageDeclarationNode(org.ballerinalang.plugins.idea.psi.PackageDeclarationNode) LinkedList(java.util.LinkedList) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ConnectorReferenceNode

use of org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method getConnectorDefinition.

@Nullable
public static ConnectorDefinitionNode getConnectorDefinition(@NotNull EndpointDeclarationNode node) {
    ConnectorReferenceNode connectorReferenceNode = PsiTreeUtil.getChildOfType(node, ConnectorReferenceNode.class);
    if (connectorReferenceNode == null) {
        return null;
    }
    PsiReference reference = connectorReferenceNode.findReferenceAt(connectorReferenceNode.getTextLength());
    if (reference == null) {
        return null;
    }
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null) {
        return null;
    }
    if (resolvedElement.getParent() instanceof ConnectorDefinitionNode) {
        return ((ConnectorDefinitionNode) resolvedElement.getParent());
    }
    return null;
}
Also used : ConnectorReferenceNode(org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode) PsiReference(com.intellij.psi.PsiReference) ConnectorDefinitionNode(org.ballerinalang.plugins.idea.psi.ConnectorDefinitionNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ConnectorReferenceNode

use of org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode in project ballerina by ballerina-lang.

the class ActionInvocationReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    IdentifierPSINode identifier = getElement();
    PsiElement parent = identifier.getParent();
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
    PsiElement resolvedElement = null;
    if (prevVisibleLeaf != null && ".".equals(prevVisibleLeaf.getText())) {
        PsiElement prevSibling = prevVisibleLeaf.getPrevSibling();
        if (prevSibling != null) {
            PsiReference reference = prevSibling.findReferenceAt(prevSibling.getTextLength());
            if (reference == null) {
                return new Object[0];
            }
            resolvedElement = reference.resolve();
        } else {
            PsiElement variableReference = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
            if (variableReference != null) {
                PsiReference reference = variableReference.findReferenceAt(variableReference.getTextLength());
                if (reference == null) {
                    return new Object[0];
                }
                resolvedElement = reference.resolve();
            }
        }
    } else {
        ConnectorReferenceNode connectorReferenceNode = PsiTreeUtil.getChildOfType(parent, ConnectorReferenceNode.class);
        if (connectorReferenceNode == null) {
            return new Object[0];
        }
        PsiReference reference = connectorReferenceNode.findReferenceAt(connectorReferenceNode.getTextLength());
        if (reference == null) {
            return new Object[0];
        }
        resolvedElement = reference.resolve();
    }
    if (resolvedElement == null) {
        return new Object[0];
    }
    PsiElement connectorNode = resolvedElement.getParent();
    if (connectorNode instanceof VariableDefinitionNode || connectorNode instanceof CodeBlockParameterNode) {
        connectorNode = BallerinaPsiImplUtil.resolveConnectorFromVariableDefinitionNode(connectorNode);
    } else if (connectorNode instanceof EndpointDeclarationNode) {
        connectorNode = BallerinaPsiImplUtil.getConnectorDefinition(((EndpointDeclarationNode) connectorNode));
    }
    if (connectorNode == null) {
        return new Object[0];
    }
    List<IdentifierPSINode> actions = BallerinaPsiImplUtil.getAllActionsFromAConnector(connectorNode);
    List<LookupElement> results = BallerinaCompletionUtils.createActionLookupElements(actions);
    return results.toArray(new LookupElement[results.size()]);
}
Also used : VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) EndpointDeclarationNode(org.ballerinalang.plugins.idea.psi.EndpointDeclarationNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) ConnectorReferenceNode(org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ConnectorReferenceNode

use of org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode 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);
}
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) 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)

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