Search in sources :

Example 11 with VariableDefinitionNode

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

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

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

Example 14 with VariableDefinitionNode

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

VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)14 PsiElement (com.intellij.psi.PsiElement)12 PsiReference (com.intellij.psi.PsiReference)10 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)10 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)9 AssignmentStatementNode (org.ballerinalang.plugins.idea.psi.AssignmentStatementNode)8 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)8 ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)8 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)8 CodeBlockParameterNode (org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode)6 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (org.jetbrains.annotations.Nullable)6 LinkedList (java.util.LinkedList)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)4 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)4 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)4 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)4 VariableReferenceNode (org.ballerinalang.plugins.idea.psi.VariableReferenceNode)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 Collection (java.util.Collection)3