Search in sources :

Example 16 with StructDefinitionNode

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

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

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

Example 19 with StructDefinitionNode

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

the class RecordKeyReference method resolve.

@Nullable
private PsiElement resolve(@NotNull PsiElement definitionNode) {
    IdentifierPSINode identifier = getElement();
    TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(definitionNode, TypeNameNode.class);
    if (typeNameNode == null) {
        return null;
    } else {
        // Todo - Add getType util method
        PsiReference reference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
        if (reference == null) {
            TypeNameNode actualType = PsiTreeUtil.getChildOfType(typeNameNode, TypeNameNode.class);
            if (actualType == null) {
                return null;
            }
            reference = actualType.findReferenceAt(actualType.getTextLength());
            if (reference == null) {
                return null;
            }
        }
        PsiElement resolvedElement = reference.resolve();
        if (resolvedElement == null) {
            return null;
        }
        PsiElement resolvedElementParent = resolvedElement.getParent();
        if (resolvedElementParent instanceof StructDefinitionNode) {
            // Todo - use an util method
            Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(resolvedElementParent, 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 : StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with StructDefinitionNode

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

the class StatementReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    List<LookupElement> results = new LinkedList<>();
    IdentifierPSINode identifier = getElement();
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
    if (prevVisibleLeaf == null) {
        return new LookupElement[0];
    }
    if (":".equals(prevVisibleLeaf.getText())) {
        PsiElement packageNameNode = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
        if (packageNameNode == null || !(packageNameNode instanceof IdentifierPSINode)) {
            return new LookupElement[0];
        }
        results.addAll(getVariantsFromPackage(packageNameNode));
    } else if (".".equals(prevVisibleLeaf.getText())) {
        // Todo - suggest length field
        PsiElement previousField = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
        if (previousField == null) {
            return new LookupElement[0];
        }
        PsiReference reference = previousField.findReferenceAt(0);
        if (reference == null) {
            return new Object[0];
        }
        PsiElement resolvedElement = reference.resolve();
        if (resolvedElement == null) {
            return new LookupElement[0];
        }
        // Todo - use util method
        PsiElement resolvedElementParent = resolvedElement.getParent();
        StructDefinitionNode structDefinitionNode = null;
        // Resolve the corresponding resolvedElementParent to get the struct definition.
        if (resolvedElementParent instanceof VariableDefinitionNode) {
            structDefinitionNode = BallerinaPsiImplUtil.resolveStructFromDefinitionNode(((VariableDefinitionNode) resolvedElementParent));
        } else if (resolvedElementParent instanceof FieldDefinitionNode) {
            structDefinitionNode = BallerinaPsiImplUtil.resolveTypeNodeStruct((resolvedElementParent));
        }
        if (structDefinitionNode == null) {
            return results.toArray(new LookupElement[results.size()]);
        }
        Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
        List<LookupElement> fields = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
        results.addAll(fields);
    } else {
        results.addAll(getVariantsFromCurrentPackage());
    }
    return results.toArray(new LookupElement[results.size()]);
}
Also used : VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) Collection(java.util.Collection) LinkedList(java.util.LinkedList) List(java.util.List) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) LinkedList(java.util.LinkedList) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)21 PsiElement (com.intellij.psi.PsiElement)17 PsiReference (com.intellij.psi.PsiReference)13 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)12 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)11 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)10 Nullable (org.jetbrains.annotations.Nullable)10 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)9 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)8 NotNull (org.jetbrains.annotations.NotNull)8 BuiltInReferenceTypeNameNode (org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode)7 FunctionTypeNameNode (org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode)7 ValueTypeNameNode (org.ballerinalang.plugins.idea.psi.ValueTypeNameNode)7 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)6 LinkedList (java.util.LinkedList)6 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)6 ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)6 LookupElement (com.intellij.codeInsight.lookup.LookupElement)5 CodeBlockParameterNode (org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode)5 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)5