Search in sources :

Example 31 with PackageNameNode

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

Example 32 with PackageNameNode

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

the class AnnotationReference method getVariants.

@NotNull
public List<LookupElement> getVariants(boolean allAnnotations) {
    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(allAnnotations));
    } else {
        results.addAll(getVariantsFromPackage(packageNameNode, allAnnotations));
    }
    return results;
}
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 33 with PackageNameNode

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

the class AnnotationReference 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(false));
    } else {
        results.addAll(getVariantsFromPackage(packageNameNode, false));
    }
    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 34 with PackageNameNode

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

the class EnumReference 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());
    } else {
        results.addAll(getVariantsFromPackage(packageNameNode));
    }
    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 35 with PackageNameNode

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

the class BallerinaImportOptimizer method getUsedImportDeclarations.

/**
 * Used to get used imports in a file.
 *
 * @param file a {@link PsiFile} element
 * @return list of used {@link ImportDeclarationNode} in the file.
 */
private List<ImportDeclarationNode> getUsedImportDeclarations(@NotNull PsiFile file) {
    // This is used to track all packages used in the file.
    List<String> usedPackages = new LinkedList<>();
    Collection<PackageNameNode> packageNameNodes = PsiTreeUtil.findChildrenOfType(file, PackageNameNode.class);
    for (PackageNameNode packageNameNode : packageNameNodes) {
        ProgressManager.checkCanceled();
        if (packageNameNode == null) {
            continue;
        }
        PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.getParentOfType(packageNameNode, PackageDeclarationNode.class);
        if (packageDeclarationNode != null) {
            continue;
        }
        ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(packageNameNode, ImportDeclarationNode.class);
        if (importDeclarationNode != null) {
            continue;
        }
        XmlAttribNode xmlAttribNode = PsiTreeUtil.getParentOfType(packageNameNode, XmlAttribNode.class);
        if (xmlAttribNode != null) {
            continue;
        }
        PsiElement nameIdentifier = packageNameNode.getNameIdentifier();
        if (nameIdentifier == null) {
            continue;
        }
        usedPackages.add(nameIdentifier.getText());
    }
    List<ImportDeclarationNode> usedImportDeclarations = new LinkedList<>();
    List<String> fullyQualifiedImportedPackages = new LinkedList<>();
    List<String> importedPackages = new LinkedList<>();
    Collection<ImportDeclarationNode> importDeclarationNodes = PsiTreeUtil.findChildrenOfType(file, ImportDeclarationNode.class);
    for (ImportDeclarationNode importDeclarationNode : importDeclarationNodes) {
        ProgressManager.checkCanceled();
        if (importDeclarationNode == null) {
            continue;
        }
        // Check unused imports. No need to check for fully qualified path since we cant import packages of same
        // name.
        List<PackageNameNode> packageNames = new ArrayList<>(PsiTreeUtil.findChildrenOfType(importDeclarationNode, PackageNameNode.class));
        PackageNameNode lastPackage = ContainerUtil.getLastItem(packageNames);
        if (lastPackage == null) {
            continue;
        }
        String lastPackageName = lastPackage.getText();
        if (!usedPackages.contains(lastPackageName)) {
            continue;
        }
        // Check conflicting imports (which ends with same package name).
        if (importedPackages.contains(lastPackageName)) {
            continue;
        }
        importedPackages.add(lastPackageName);
        // Check redeclared imports.
        FullyQualifiedPackageNameNode fullyQualifiedPackageName = PsiTreeUtil.getChildOfType(importDeclarationNode, FullyQualifiedPackageNameNode.class);
        if (fullyQualifiedPackageName == null) {
            continue;
        }
        if (fullyQualifiedImportedPackages.contains(fullyQualifiedPackageName.getText())) {
            continue;
        }
        fullyQualifiedImportedPackages.add(fullyQualifiedPackageName.getText());
        usedImportDeclarations.add(importDeclarationNode);
    }
    return usedImportDeclarations;
}
Also used : ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) ArrayList(java.util.ArrayList) PackageDeclarationNode(org.ballerinalang.plugins.idea.psi.PackageDeclarationNode) LinkedList(java.util.LinkedList) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) XmlAttribNode(org.ballerinalang.plugins.idea.psi.XmlAttribNode) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)35 PsiElement (com.intellij.psi.PsiElement)30 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)20 LinkedList (java.util.LinkedList)17 NotNull (org.jetbrains.annotations.NotNull)14 LookupElement (com.intellij.codeInsight.lookup.LookupElement)11 Nullable (org.jetbrains.annotations.Nullable)11 PsiReference (com.intellij.psi.PsiReference)9 FullyQualifiedPackageNameNode (org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode)9 PsiDirectory (com.intellij.psi.PsiDirectory)8 ArrayList (java.util.ArrayList)7 ImportDeclarationNode (org.ballerinalang.plugins.idea.psi.ImportDeclarationNode)7 AliasNode (org.ballerinalang.plugins.idea.psi.AliasNode)5 PackageDeclarationNode (org.ballerinalang.plugins.idea.psi.PackageDeclarationNode)5 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)4 Module (com.intellij.openapi.module.Module)4 PsiFile (com.intellij.psi.PsiFile)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)4 BallerinaFile (org.ballerinalang.plugins.idea.psi.BallerinaFile)4 XmlAttribNode (org.ballerinalang.plugins.idea.psi.XmlAttribNode)4