Search in sources :

Example 6 with CodeBlockParameterNode

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

the class BallerinaPsiImplUtil method getAttachedStruct.

@Nullable
public static StructDefinitionNode getAttachedStruct(@NotNull IdentifierPSINode function) {
    CodeBlockParameterNode codeBlockParameterNode = PsiTreeUtil.getChildOfType(function.getParent(), CodeBlockParameterNode.class);
    if (codeBlockParameterNode == null) {
        return null;
    }
    TypeNameNode typeNameNode = PsiTreeUtil.findChildOfType(codeBlockParameterNode, TypeNameNode.class);
    if (typeNameNode == null) {
        return null;
    }
    PsiReference reference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
    if (reference == null) {
        return null;
    }
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null) {
        return null;
    }
    PsiElement parent = resolvedElement.getParent();
    if (!(parent instanceof StructDefinitionNode)) {
        return null;
    }
    return (StructDefinitionNode) parent;
}
Also used : StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) BuiltInReferenceTypeNameNode(org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode) FunctionTypeNameNode(org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) ValueTypeNameNode(org.ballerinalang.plugins.idea.psi.ValueTypeNameNode) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with CodeBlockParameterNode

use of org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode 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 8 with CodeBlockParameterNode

use of org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode 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()]);
}
Also used : AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) PsiReference(com.intellij.psi.PsiReference) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) LinkedList(java.util.LinkedList) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) EnumFieldNode(org.ballerinalang.plugins.idea.psi.EnumFieldNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) Collection(java.util.Collection) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) EnumDefinitionNode(org.ballerinalang.plugins.idea.psi.EnumDefinitionNode) PsiElement(com.intellij.psi.PsiElement) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CodeBlockParameterNode (org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode)8 PsiElement (com.intellij.psi.PsiElement)7 PsiReference (com.intellij.psi.PsiReference)6 ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)5 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)5 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)5 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)4 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)4 AssignmentStatementNode (org.ballerinalang.plugins.idea.psi.AssignmentStatementNode)4 BuiltInReferenceTypeNameNode (org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode)4 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)4 FunctionTypeNameNode (org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode)4 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)4 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)4 ValueTypeNameNode (org.ballerinalang.plugins.idea.psi.ValueTypeNameNode)4 NotNull (org.jetbrains.annotations.NotNull)4 EnumDefinitionNode (org.ballerinalang.plugins.idea.psi.EnumDefinitionNode)3 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)3 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)3 ReturnParameterNode (org.ballerinalang.plugins.idea.psi.ReturnParameterNode)3