Search in sources :

Example 81 with IdentifierPSINode

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

the class RecordKeyReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    List<LookupElement> results = new LinkedList<>();
    IdentifierPSINode identifier = getElement();
    PsiElement parent = identifier.getParent();
    PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(parent, PackageNameNode.class);
    if (packageNameNode == null) {
        results.addAll(getVariantsFromCurrentPackage());
    }
    return results.toArray(new LookupElement[results.size()]);
}
Also used : PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) LinkedList(java.util.LinkedList) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 82 with IdentifierPSINode

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

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

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

the class RecordValueReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    List<LookupElement> results = new LinkedList<>();
    IdentifierPSINode identifier = getElement();
    PsiElement prevSibling = identifier.getPrevSibling();
    if (prevSibling != null && prevSibling.getPrevSibling() != null && prevSibling.getPrevSibling() instanceof PackageNameNode) {
        PsiReference reference = prevSibling.getPrevSibling().findReferenceAt(0);
        if (reference != null) {
            PsiElement resolvedElement = reference.resolve();
            if (resolvedElement instanceof PsiDirectory) {
                PsiDirectory currentPackage = (PsiDirectory) resolvedElement;
                List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(currentPackage, true, true);
                results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
                List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(currentPackage, true, true);
                results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
                List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(currentPackage, true, true);
                results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
                List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(currentPackage, true, true);
                results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
                if (!results.isEmpty()) {
                    return results.toArray(new LookupElement[results.size()]);
                }
            }
        }
    }
    PsiFile containingFile = identifier.getContainingFile();
    PsiFile originalFile = containingFile.getOriginalFile();
    List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP, true, AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP);
    results.addAll(packages);
    // Todo - improve suggesting elements from a package in StructValueNode since it will not be identified properly
    RecordKeyValueNode recordKeyValueNode = PsiTreeUtil.getParentOfType(identifier, RecordKeyValueNode.class);
    if (recordKeyValueNode != null) {
        RecordKeyNode mapStructKeyNode = PsiTreeUtil.getChildOfType(recordKeyValueNode, RecordKeyNode.class);
        if (mapStructKeyNode != null) {
            // Todo - use util?
            List<PsiElement> importedPackages = BallerinaPsiImplUtil.getImportedPackages(containingFile);
            for (PsiElement importedPackage : importedPackages) {
                PsiReference reference = importedPackage.findReferenceAt(0);
                if (reference == null) {
                    continue;
                }
                PsiElement resolvedElement = reference.resolve();
                if (resolvedElement == null) {
                    continue;
                }
                PsiDirectory resolvedPackage = (PsiDirectory) resolvedElement;
                if (mapStructKeyNode.getText().equals(resolvedPackage.getName())) {
                    List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(resolvedPackage, false, false);
                    results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
                    Collection<EnumFieldNode> enums = PsiTreeUtil.findChildrenOfType(resolvedPackage, EnumFieldNode.class);
                    results.addAll(BallerinaCompletionUtils.createEnumFieldLookupElements(enums, (IdentifierPSINode) resolvedElement));
                    List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(resolvedPackage, false, false);
                    results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
                    List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(resolvedPackage, false, false);
                    results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
                }
            }
        }
    }
    PsiDirectory currentPackage = originalFile.getParent();
    if (currentPackage != null) {
        List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(currentPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
        List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(currentPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
        List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(currentPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
        List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(currentPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
    }
    // Todo - use a util method
    ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
    if (scope != null) {
        int caretOffset = identifier.getStartOffset();
        List<IdentifierPSINode> variables = BallerinaPsiImplUtil.getAllLocalVariablesInResolvableScope(scope, caretOffset);
        results.addAll(BallerinaCompletionUtils.createVariableLookupElements(variables));
        List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getAllParametersInResolvableScope(scope, caretOffset);
        results.addAll(BallerinaCompletionUtils.createParameterLookupElements(parameters));
        List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope);
        results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
        List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
        results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
    }
    return results.toArray(new LookupElement[results.size()]);
}
Also used : RecordKeyValueNode(org.ballerinalang.plugins.idea.psi.RecordKeyValueNode) PsiReference(com.intellij.psi.PsiReference) LookupElement(com.intellij.codeInsight.lookup.LookupElement) LinkedList(java.util.LinkedList) RecordKeyNode(org.ballerinalang.plugins.idea.psi.RecordKeyNode) EnumFieldNode(org.ballerinalang.plugins.idea.psi.EnumFieldNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 85 with IdentifierPSINode

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

the class RecordValueReference method resolve.

@Nullable
@Override
public PsiElement resolve() {
    IdentifierPSINode identifier = getElement();
    PsiElement prevSibling = identifier.getPrevSibling();
    if (prevSibling != null && prevSibling.getPrevSibling() != null && prevSibling.getPrevSibling() instanceof PackageNameNode) {
        PsiReference reference = prevSibling.getPrevSibling().findReferenceAt(0);
        if (reference != null) {
            PsiElement resolvedElement = reference.resolve();
            if (resolvedElement instanceof PsiDirectory) {
                PsiDirectory psiDirectory = (PsiDirectory) resolvedElement;
                // First we try to resolve the reference to following definitions.
                PsiElement element = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, true, true);
                if (element != null) {
                    return element;
                }
            }
        }
    } else {
        PsiFile containingFile = identifier.getContainingFile();
        if (containingFile == null) {
            return null;
        }
        PsiFile originalFile = containingFile.getOriginalFile();
        PsiDirectory psiDirectory = originalFile.getParent();
        if (psiDirectory == null) {
            return null;
        }
        // First we try to resolve the reference to following definitions.
        PsiElement element = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, true, true);
        if (element != null) {
            return element;
        }
    }
    // If the reference is not resolved to an above definition, we try to resolve it to below definition.
    return BallerinaPsiImplUtil.resolveElementInScope(identifier, true, true, true, true, true);
}
Also used : PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)109 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)63 LinkedList (java.util.LinkedList)58 LookupElement (com.intellij.codeInsight.lookup.LookupElement)48 Nullable (org.jetbrains.annotations.Nullable)32 PsiDirectory (com.intellij.psi.PsiDirectory)29 PsiReference (com.intellij.psi.PsiReference)25 PsiFile (com.intellij.psi.PsiFile)24 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)20 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)17 ScopeNode (org.antlr.jetbrains.adaptor.psi.ScopeNode)15 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)12 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)12 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)12 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)10 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)9 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)9 ArrayList (java.util.ArrayList)8 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)8