Search in sources :

Example 1 with ActionInvocationReference

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

the class IdentifierPSINode method checkAndSuggestReferenceAfterDot.

@Nullable
private PsiReference checkAndSuggestReferenceAfterDot() {
    PsiReference reference = null;
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(getParent());
    if (prevVisibleLeaf != null) {
        if (".".equals(prevVisibleLeaf.getText())) {
            PsiElement prevSibling = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
            if (prevSibling != null) {
                reference = prevSibling.findReferenceAt(prevSibling.getTextLength());
            }
        } else {
            reference = prevVisibleLeaf.findReferenceAt(prevVisibleLeaf.getTextLength());
        }
    }
    if (reference == null) {
        PsiElement prevSibling = getParent().getPrevSibling();
        if (prevSibling == null) {
            return null;
        }
        InvocationNode invocationNode = PsiTreeUtil.getChildOfType(prevSibling, InvocationNode.class);
        if (invocationNode == null) {
            return null;
        }
        AnyIdentifierNameNode anyIdentifierNameNode = PsiTreeUtil.getChildOfType(invocationNode, AnyIdentifierNameNode.class);
        if (anyIdentifierNameNode == null) {
            return null;
        }
        IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(anyIdentifierNameNode, IdentifierPSINode.class);
        if (identifier == null) {
            return null;
        }
        reference = identifier.findReferenceAt(identifier.getTextLength());
    }
    if (reference == null) {
        return null;
    }
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null) {
        return null;
    }
    // Get the return type of the function definition.
    PsiElement parent = resolvedElement.getParent();
    if (parent instanceof FunctionDefinitionNode) {
        List<TypeNameNode> returnTypes = BallerinaPsiImplUtil.getReturnTypes(parent);
        if (returnTypes.size() == 1) {
            return new TypeReference(this, returnTypes.get(0));
        }
    }
    if (resolvedElement.getParent() instanceof EndpointDeclarationNode) {
        return new ActionInvocationReference(this);
    }
    if (resolvedElement.getParent() instanceof EnumDefinitionNode) {
        return new EnumFieldReference(this);
    }
    PsiElement type = BallerinaPsiImplUtil.getType(((IdentifierPSINode) resolvedElement));
    if (type == null || (!(type instanceof BuiltInReferenceTypeNameNode) && !(type instanceof ReferenceTypeNameNode) && !(type instanceof ValueTypeNameNode))) {
        return null;
    }
    return new TypeReference(this, type);
}
Also used : EnumFieldReference(org.ballerinalang.plugins.idea.psi.references.EnumFieldReference) ActionInvocationReference(org.ballerinalang.plugins.idea.psi.references.ActionInvocationReference) PsiReference(com.intellij.psi.PsiReference) TypeReference(org.ballerinalang.plugins.idea.psi.references.TypeReference) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ActionInvocationReference

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

the class IdentifierPSINode method suggestReferenceTypeForInvocation.

@Nullable
private PsiReference suggestReferenceTypeForInvocation() {
    PsiElement parent = getParent();
    PsiElement prevSibling = parent.getPrevSibling();
    if (prevSibling == null) {
        return null;
    }
    PsiReference reference = null;
    if (parent instanceof AnyIdentifierNameNode) {
        InvocationNode invocationNode = PsiTreeUtil.getParentOfType(parent, InvocationNode.class);
        if (invocationNode != null) {
            PsiElement invocationNodePrevSibling = invocationNode.getPrevSibling();
            if (invocationNodePrevSibling != null) {
                if (invocationNodePrevSibling instanceof VariableReferenceNode) {
                    InvocationNode node = PsiTreeUtil.getChildOfType(invocationNodePrevSibling, InvocationNode.class);
                    if (node != null) {
                        IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(node, IdentifierPSINode.class);
                        if (identifier != null) {
                            reference = identifier.findReferenceAt(identifier.getTextLength());
                        }
                    } else {
                        NameReferenceNode nameReferenceNode = PsiTreeUtil.getChildOfType(invocationNodePrevSibling, NameReferenceNode.class);
                        if (nameReferenceNode != null) {
                            IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(nameReferenceNode, IdentifierPSINode.class);
                            if (identifier != null) {
                                reference = identifier.findReferenceAt(identifier.getTextLength());
                            }
                        }
                    }
                } else {
                    reference = invocationNodePrevSibling.findReferenceAt(invocationNodePrevSibling.getTextLength());
                }
            }
        }
    } else if (prevSibling instanceof VariableReferenceNode) {
        InvocationNode invocationNode = PsiTreeUtil.getChildOfType(prevSibling, InvocationNode.class);
        if (invocationNode != null) {
            IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(invocationNode, IdentifierPSINode.class);
            if (identifier == null) {
                return null;
            }
            reference = identifier.findReferenceAt(identifier.getTextLength());
        }
        FieldNode fieldNode = PsiTreeUtil.getChildOfType(prevSibling, FieldNode.class);
        if (fieldNode != null) {
            IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(fieldNode, IdentifierPSINode.class);
            if (identifier == null) {
                return null;
            }
            reference = identifier.findReferenceAt(identifier.getTextLength());
        }
        FunctionInvocationNode functionInvocationNode = PsiTreeUtil.getChildOfType(prevSibling, FunctionInvocationNode.class);
        if (functionInvocationNode != null) {
            FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getChildOfType(functionInvocationNode, FunctionReferenceNode.class);
            if (functionReferenceNode != null) {
                return new NameReference(this);
            }
        }
        if (reference == null) {
            reference = prevSibling.findReferenceAt(prevSibling.getTextLength());
        }
    } else {
        reference = prevSibling.findReferenceAt(prevSibling.getTextLength());
    }
    if (reference == null) {
        return null;
    }
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null) {
        return null;
    }
    PsiElement definitionNode = resolvedElement.getParent();
    if (definitionNode instanceof VariableDefinitionNode) {
        ConnectorDefinitionNode connectorDefinitionNode = BallerinaPsiImplUtil.resolveConnectorFromVariableDefinitionNode((definitionNode));
        if (connectorDefinitionNode != null) {
            return new ActionInvocationReference(this);
        }
    } else if (definitionNode instanceof EndpointDeclarationNode) {
        ConnectorReferenceNode connectorReferenceNode = PsiTreeUtil.getChildOfType(definitionNode, ConnectorReferenceNode.class);
        if (connectorReferenceNode != null) {
            return new ActionInvocationReference(this);
        }
    } else if (definitionNode instanceof FunctionDefinitionNode) {
        return new NameReference(this);
    }
    reference = checkAndSuggestReferenceAfterDot();
    if (reference != null) {
        return reference;
    }
    return new InvocationReference(this);
}
Also used : PackageNameReference(org.ballerinalang.plugins.idea.psi.references.PackageNameReference) NameReference(org.ballerinalang.plugins.idea.psi.references.NameReference) ActionInvocationReference(org.ballerinalang.plugins.idea.psi.references.ActionInvocationReference) PsiReference(com.intellij.psi.PsiReference) ActionInvocationReference(org.ballerinalang.plugins.idea.psi.references.ActionInvocationReference) InvocationReference(org.ballerinalang.plugins.idea.psi.references.InvocationReference) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ActionInvocationReference

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

the class IdentifierPSINode method getReference.

/**
 * Create and return a PsiReference object associated with this ID
 * node. The reference object will be asked to resolve this ref
 * by using the text of this node to identify the appropriate definition
 * site. The definition site is typically a subtree for a function
 * or variable definition whereas this reference is just to this ID
 * leaf node.
 * <p>
 * As the AST factory has no context and cannot create different kinds
 * of PsiNamedElement nodes according to context, every ID node
 * in the tree will be of this type. So, we distinguish references
 * from definitions or other uses by looking at context in this method
 * as we have parent (context) information.
 */
@Override
public PsiReference getReference() {
    PsiElement parent = getParent();
    IElementType elType = parent.getNode().getElementType();
    PsiElement prevVisibleLeaf;
    // do not return a reference for the ID nodes in a definition
    if (elType instanceof RuleIElementType) {
        XmlAttribNode xmlAttribNode = PsiTreeUtil.getParentOfType(parent, XmlAttribNode.class);
        switch(((RuleIElementType) elType).getRuleIndex()) {
            case RULE_packageName:
                if (xmlAttribNode != null) {
                    return new NameSpaceReference(this);
                }
                AliasNode aliasNode = PsiTreeUtil.getParentOfType(parent, AliasNode.class);
                if (aliasNode != null) {
                    return null;
                }
                return new PackageNameReference(this);
            case RULE_nameReference:
                // If we are currently resolving a var variable, we don't need to resolve it since it is the
                // definition.
                PsiElement element = getPsi();
                if (element instanceof IdentifierPSINode) {
                    AssignmentStatementNode assignmentStatementNode = PsiTreeUtil.getParentOfType(element, AssignmentStatementNode.class);
                    if (assignmentStatementNode != null && BallerinaPsiImplUtil.isVarAssignmentStatement(assignmentStatementNode)) {
                        IdentifierPSINode identifier = (IdentifierPSINode) element;
                        if (BallerinaPsiImplUtil.isRedeclaredVar(identifier)) {
                            return new NameReference(this);
                        }
                        if (BallerinaPsiImplUtil.isValidVarVariable(assignmentStatementNode, identifier)) {
                            return null;
                        }
                    }
                }
                if (xmlAttribNode != null) {
                    return new NameSpaceReference(this);
                }
                RecordKeyNode recordKeyNode = PsiTreeUtil.getParentOfType(parent, RecordKeyNode.class);
                if (recordKeyNode != null) {
                    return new RecordKeyReference(this);
                }
                RecordValueNode recordValueNode = PsiTreeUtil.getParentOfType(parent, RecordValueNode.class);
                if (recordValueNode != null) {
                    return new RecordValueReference(this);
                }
                prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(parent);
                if (prevVisibleLeaf != null) {
                    if (".".equals(prevVisibleLeaf.getText())) {
                        // Todo - update logic
                        PsiReference reference = checkAndSuggestReferenceAfterDot();
                        if (reference != null) {
                            return reference;
                        }
                        return new FieldReference(this);
                    }
                    // Todo - remove ","
                    if ((prevVisibleLeaf instanceof IdentifierPSINode) && prevVisibleLeaf.getParent() instanceof AnnotationDefinitionNode) {
                        return null;
                    } else if (("attach".equals(prevVisibleLeaf.getText()) || ",".equals(prevVisibleLeaf.getText())) && prevVisibleLeaf.getParent() instanceof AnnotationDefinitionNode) {
                        return new AttachmentPointReference(this);
                    }
                }
                return new NameReference(this);
            case RULE_field:
                prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(parent);
                PsiReference variableReference = null;
                if (prevVisibleLeaf != null && ".".equals(prevVisibleLeaf.getText())) {
                    PsiElement connectorVariable = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
                    if (connectorVariable != null) {
                        variableReference = connectorVariable.findReferenceAt(connectorVariable.getTextLength());
                    }
                } else {
                    PsiElement prevSibling = parent.getPrevSibling();
                    if (prevSibling != null) {
                        PsiReference reference = checkAndSuggestReferenceAfterDot();
                        if (reference != null) {
                            return reference;
                        }
                        return new FieldReference(this);
                    }
                }
                if (variableReference == null) {
                    return null;
                }
                PsiElement variableDefinition = variableReference.resolve();
                if (variableDefinition == null) {
                    return new FieldReference(this);
                }
                ConnectorDefinitionNode connectorDefinitionNode = BallerinaPsiImplUtil.resolveConnectorFromVariableDefinitionNode(variableDefinition.getParent());
                if (connectorDefinitionNode != null) {
                    return new ActionInvocationReference(this);
                }
                return new FieldReference(this);
            case RULE_functionReference:
                return new FunctionReference(this);
            case RULE_workerReference:
                return new WorkerReference(this);
            case RULE_attachmentPoint:
                return new AttachmentPointReference(this);
            case RULE_statement:
                // Todo - update logic
                PsiReference reference = checkAndSuggestReferenceAfterDot();
                if (reference != null) {
                    return reference;
                }
                prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(getParent());
                if (prevVisibleLeaf != null) {
                    if (".".equals(prevVisibleLeaf.getText())) {
                        PsiElement psiElement = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
                        if (psiElement == null) {
                            return new FieldReference(this);
                        }
                        reference = psiElement.findReferenceAt(psiElement.getTextLength());
                        if (reference == null) {
                            return new FieldReference(this);
                        }
                        PsiElement resolvedElement = reference.resolve();
                        if (resolvedElement == null) {
                            return new FieldReference(this);
                        }
                        PsiElement resolvedElementParent = resolvedElement.getParent();
                        if (resolvedElementParent instanceof ConnectorDefinitionNode) {
                            return new ActionInvocationReference(this);
                        }
                        if (resolvedElementParent instanceof VariableDefinitionNode) {
                            connectorDefinitionNode = BallerinaPsiImplUtil.resolveConnectorFromVariableDefinitionNode((resolvedElementParent));
                            if (connectorDefinitionNode != null) {
                                return new ActionInvocationReference(this);
                            }
                        }
                        return new FieldReference(this);
                    } else if (prevVisibleLeaf.getText().matches("[,]")) {
                        return new NameReference(this);
                    } else if (prevVisibleLeaf.getText().matches("[{]")) {
                        return new RecordKeyReference(this);
                    }
                }
                PsiElement nextVisibleLeaf = PsiTreeUtil.nextVisibleLeaf(getPsi());
                if (nextVisibleLeaf != null && ":".equals(nextVisibleLeaf.getText())) {
                    return new PackageNameReference(this);
                }
                return new StatementReference(this);
            case RULE_invocation:
                return suggestReferenceTypeForInvocation();
            case RULE_structReference:
                return new StructReference(this);
            case RULE_transformerReference:
                return new TransformerReference(this);
            case RULE_recordKey:
                return new RecordKeyReference(this);
            case RULE_anyIdentifierName:
                return suggestReferenceTypeForInvocation();
            // return new DocVariableReference(this);
            default:
                return null;
        }
    }
    if (parent instanceof PsiErrorElement) {
        return suggestReferenceType(parent);
    }
    return null;
}
Also used : TransformerReference(org.ballerinalang.plugins.idea.psi.references.TransformerReference) PackageNameReference(org.ballerinalang.plugins.idea.psi.references.PackageNameReference) NameReference(org.ballerinalang.plugins.idea.psi.references.NameReference) PackageNameReference(org.ballerinalang.plugins.idea.psi.references.PackageNameReference) StatementReference(org.ballerinalang.plugins.idea.psi.references.StatementReference) NameSpaceReference(org.ballerinalang.plugins.idea.psi.references.NameSpaceReference) FunctionReference(org.ballerinalang.plugins.idea.psi.references.FunctionReference) RecordValueReference(org.ballerinalang.plugins.idea.psi.references.RecordValueReference) WorkerReference(org.ballerinalang.plugins.idea.psi.references.WorkerReference) PsiElement(com.intellij.psi.PsiElement) FieldReference(org.ballerinalang.plugins.idea.psi.references.FieldReference) EnumFieldReference(org.ballerinalang.plugins.idea.psi.references.EnumFieldReference) StructReference(org.ballerinalang.plugins.idea.psi.references.StructReference) ActionInvocationReference(org.ballerinalang.plugins.idea.psi.references.ActionInvocationReference) RuleIElementType(org.antlr.jetbrains.adaptor.lexer.RuleIElementType) PsiReference(com.intellij.psi.PsiReference) IElementType(com.intellij.psi.tree.IElementType) RuleIElementType(org.antlr.jetbrains.adaptor.lexer.RuleIElementType) PsiErrorElement(com.intellij.psi.PsiErrorElement) AttachmentPointReference(org.ballerinalang.plugins.idea.psi.references.AttachmentPointReference) RecordKeyReference(org.ballerinalang.plugins.idea.psi.references.RecordKeyReference)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3 PsiReference (com.intellij.psi.PsiReference)3 ActionInvocationReference (org.ballerinalang.plugins.idea.psi.references.ActionInvocationReference)3 EnumFieldReference (org.ballerinalang.plugins.idea.psi.references.EnumFieldReference)2 NameReference (org.ballerinalang.plugins.idea.psi.references.NameReference)2 PackageNameReference (org.ballerinalang.plugins.idea.psi.references.PackageNameReference)2 Nullable (org.jetbrains.annotations.Nullable)2 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 IElementType (com.intellij.psi.tree.IElementType)1 RuleIElementType (org.antlr.jetbrains.adaptor.lexer.RuleIElementType)1 AttachmentPointReference (org.ballerinalang.plugins.idea.psi.references.AttachmentPointReference)1 FieldReference (org.ballerinalang.plugins.idea.psi.references.FieldReference)1 FunctionReference (org.ballerinalang.plugins.idea.psi.references.FunctionReference)1 InvocationReference (org.ballerinalang.plugins.idea.psi.references.InvocationReference)1 NameSpaceReference (org.ballerinalang.plugins.idea.psi.references.NameSpaceReference)1 RecordKeyReference (org.ballerinalang.plugins.idea.psi.references.RecordKeyReference)1 RecordValueReference (org.ballerinalang.plugins.idea.psi.references.RecordValueReference)1 StatementReference (org.ballerinalang.plugins.idea.psi.references.StatementReference)1 StructReference (org.ballerinalang.plugins.idea.psi.references.StructReference)1 TransformerReference (org.ballerinalang.plugins.idea.psi.references.TransformerReference)1