Search in sources :

Example 16 with ImportDeclarationNode

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

ImportDeclarationNode (org.ballerinalang.plugins.idea.psi.ImportDeclarationNode)16 PsiElement (com.intellij.psi.PsiElement)13 PackageDeclarationNode (org.ballerinalang.plugins.idea.psi.PackageDeclarationNode)9 AliasNode (org.ballerinalang.plugins.idea.psi.AliasNode)7 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)7 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)6 ArrayList (java.util.ArrayList)6 FullyQualifiedPackageNameNode (org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode)6 NotNull (org.jetbrains.annotations.NotNull)6 XmlAttribNode (org.ballerinalang.plugins.idea.psi.XmlAttribNode)5 PsiReference (com.intellij.psi.PsiReference)4 LinkedList (java.util.LinkedList)4 AnnotationAttachmentNode (org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode)3 BallerinaFile (org.ballerinalang.plugins.idea.psi.BallerinaFile)3 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)3 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)3 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)2 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 ResolveResult (com.intellij.psi.ResolveResult)2