Search in sources :

Example 11 with ParameterNode

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

the class BallerinaPsiImplUtil method getParameters.

/**
 * Returns parameter for provided definition node.
 *
 * @param definitionNode
 * @return
 */
@NotNull
public static List<IdentifierPSINode> getParameters(@NotNull PsiElement definitionNode) {
    List<IdentifierPSINode> parameters = new LinkedList<>();
    ParameterListNode parameterListNode = PsiTreeUtil.getChildOfType(definitionNode, ParameterListNode.class);
    if (parameterListNode == null) {
        return parameters;
    }
    // Get parameter nodes.
    Collection<ParameterNode> parameterNodes = PsiTreeUtil.getChildrenOfTypeAsList(parameterListNode, ParameterNode.class);
    for (ParameterNode parameterNode : parameterNodes) {
        IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(parameterNode, IdentifierPSINode.class);
        if (identifier == null) {
            continue;
        }
        parameters.add(identifier);
    }
    return parameters;
}
Also used : ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with ParameterNode

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

the class BallerinaPsiImplUtil method getAttachmentType.

/**
 * Returns the attachment type by checking the nodes.
 *
 * @param identifier identifier node of the annotation attachment node
 * @return attachment type.
 */
@Nullable
public static String getAttachmentType(@NotNull IdentifierPSINode identifier) {
    PsiElement parent = identifier.getParent();
    PsiElement superParent = parent.getParent();
    PsiElement nextSibling = PsiTreeUtil.skipSiblingsForward(superParent, PsiWhiteSpace.class, PsiComment.class, AnnotationAttachmentNode.class);
    String type = null;
    if (nextSibling == null) {
        AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getParentOfType(identifier, AnnotationAttachmentNode.class);
        if (annotationAttachmentNode != null) {
            PsiElement definitionNode = annotationAttachmentNode.getParent();
            type = getAnnotationAttachmentType(definitionNode);
        }
    } else if (nextSibling instanceof DefinitionNode) {
        PsiElement[] children = nextSibling.getChildren();
        if (children.length != 0) {
            PsiElement definitionNode = children[0];
            type = getAnnotationAttachmentType(definitionNode);
        }
    } else if (nextSibling.getParent() instanceof ResourceDefinitionNode) {
        type = "resource";
    } else if (nextSibling instanceof ActionDefinitionNode || nextSibling.getParent() instanceof ActionDefinitionNode || parent instanceof ActionDefinitionNode) {
        type = "action";
    } else if (nextSibling.getParent() instanceof ParameterNode) {
        type = "parameter";
    }
    return type;
}
Also used : ResourceDefinitionNode(org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode) ServiceDefinitionNode(org.ballerinalang.plugins.idea.psi.ServiceDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) ConstantDefinitionNode(org.ballerinalang.plugins.idea.psi.ConstantDefinitionNode) ConnectorDefinitionNode(org.ballerinalang.plugins.idea.psi.ConnectorDefinitionNode) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) TransformerDefinitionNode(org.ballerinalang.plugins.idea.psi.TransformerDefinitionNode) EnumDefinitionNode(org.ballerinalang.plugins.idea.psi.EnumDefinitionNode) ActionDefinitionNode(org.ballerinalang.plugins.idea.psi.ActionDefinitionNode) AnnotationDefinitionNode(org.ballerinalang.plugins.idea.psi.AnnotationDefinitionNode) DefinitionNode(org.ballerinalang.plugins.idea.psi.DefinitionNode) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) ResourceDefinitionNode(org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) ActionDefinitionNode(org.ballerinalang.plugins.idea.psi.ActionDefinitionNode) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with ParameterNode

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

the class BallerinaPsiImplUtil method getAnonymousStruct.

@Nullable
public static PsiElement getAnonymousStruct(@NotNull Object element, @NotNull List<PsiElement> parameterListNodes, int offset) {
    int index = BallerinaParameterInfoHandler.getCurrentParameterIndex(element, offset);
    PsiElement parameterListNode = parameterListNodes.get(0);
    if (parameterListNode instanceof ParameterListNode) {
        List<ParameterNode> parameterNodes = PsiTreeUtil.getChildrenOfTypeAsList(parameterListNode, ParameterNode.class);
        if (index < 0 || index >= parameterNodes.size()) {
            return null;
        }
        ParameterNode parameterNode = parameterNodes.get(index);
        TypeNameNode typeNameNode = PsiTreeUtil.findChildOfType(parameterNode, TypeNameNode.class);
        if (typeNameNode == null) {
            return null;
        }
        PsiReference reference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
        if (reference == null) {
            AnonStructTypeNameNode anonStructNode = PsiTreeUtil.findChildOfType(typeNameNode, AnonStructTypeNameNode.class);
            if (anonStructNode == null) {
                return null;
            }
            return anonStructNode;
        }
        PsiElement resolvedElement = reference.resolve();
        if (resolvedElement == null || !(resolvedElement.getParent() instanceof StructDefinitionNode)) {
            return null;
        }
        return resolvedElement.getParent();
    }
    return null;
}
Also used : ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) 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) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with ParameterNode

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

the class InvocationReference method resolve.

@Nullable
@Override
public PsiElement resolve() {
    IdentifierPSINode identifier = getElement();
    PsiElement parent = identifier.getParent();
    PsiElement prevSibling = parent.getPrevSibling();
    if (prevSibling == null) {
        return null;
    }
    PsiReference 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 || definitionNode instanceof ParameterNode || definitionNode instanceof GlobalVariableDefinitionNode) {
        StructDefinitionNode structDefinitionNode = BallerinaPsiImplUtil.resolveStructFromDefinitionNode(definitionNode);
        if (structDefinitionNode != null) {
            List<IdentifierPSINode> attachedFunctions = BallerinaPsiImplUtil.getAttachedFunctions(structDefinitionNode);
            for (IdentifierPSINode attachedFunction : attachedFunctions) {
                if (identifier.getText().equals(attachedFunction.getText())) {
                    return attachedFunction;
                }
            }
        }
    }
    return super.resolve();
}
Also used : VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with ParameterNode

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

the class RecordKeyReference method resolve.

private PsiElement resolve(@NotNull AssignmentStatementNode assignmentStatementNode) {
    IdentifierPSINode identifier = getElement();
    VariableReferenceListNode variableReferenceListNode = PsiTreeUtil.getChildOfType(assignmentStatementNode, VariableReferenceListNode.class);
    if (variableReferenceListNode == null) {
        return null;
    }
    PsiElement variableReferenceNode = variableReferenceListNode.getFirstChild();
    if (variableReferenceNode == null) {
        return null;
    }
    PsiReference reference = variableReferenceNode.findReferenceAt(0);
    if (reference == null) {
        return null;
    }
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null) {
        return null;
    }
    PsiElement parent = resolvedElement.getParent();
    if (parent instanceof VariableDefinitionNode || parent instanceof ParameterNode) {
        return resolve(parent);
    } else if (parent instanceof NameReferenceNode) {
        StructDefinitionNode structDefinitionNode = resolveStructDefinition(identifier);
        if (structDefinitionNode == null) {
            return null;
        }
        Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
        for (FieldDefinitionNode fieldDefinitionNode : fieldDefinitionNodes) {
            IdentifierPSINode fieldName = PsiTreeUtil.getChildOfType(fieldDefinitionNode, IdentifierPSINode.class);
            if (fieldName != null && identifier.getText().equals(fieldName.getText())) {
                return fieldName;
            }
        }
    }
    return null;
}
Also used : VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) Collection(java.util.Collection) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) VariableReferenceListNode(org.ballerinalang.plugins.idea.psi.VariableReferenceListNode) PsiElement(com.intellij.psi.PsiElement) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode)

Aggregations

ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)17 PsiElement (com.intellij.psi.PsiElement)12 PsiReference (com.intellij.psi.PsiReference)9 CodeBlockParameterNode (org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode)9 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)9 ReturnParameterNode (org.ballerinalang.plugins.idea.psi.ReturnParameterNode)8 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)7 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)7 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)7 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)7 Nullable (org.jetbrains.annotations.Nullable)7 LinkedList (java.util.LinkedList)6 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)6 ParameterListNode (org.ballerinalang.plugins.idea.psi.ParameterListNode)6 AssignmentStatementNode (org.ballerinalang.plugins.idea.psi.AssignmentStatementNode)5 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)5 EnumDefinitionNode (org.ballerinalang.plugins.idea.psi.EnumDefinitionNode)4 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)4 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)4 NotNull (org.jetbrains.annotations.NotNull)4