Search in sources :

Example 11 with FullyQualifiedPackageNameNode

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

the class BallerinaPsiImplUtil method getAllImportsInAFile.

/**
 * Returns all imports as a map. Key is the last Package Name node or Alias node. Value is the import path.
 *
 * @param file file to get imports
 * @return map of imports
 */
public static Map<String, String> getAllImportsInAFile(@NotNull PsiFile file) {
    Map<String, String> results = new HashMap<>();
    Collection<ImportDeclarationNode> importDeclarationNodes = PsiTreeUtil.findChildrenOfType(file, ImportDeclarationNode.class);
    for (ImportDeclarationNode importDeclarationNode : importDeclarationNodes) {
        // There can be two possible values for the imported package name. If there is no alias node in the
        // import declaration, the imported package name is the last package name in the import path node.
        // Otherwise the package name is the alias node.
        FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.findChildOfType(importDeclarationNode, FullyQualifiedPackageNameNode.class);
        AliasNode aliasNode = PsiTreeUtil.findChildOfType(importDeclarationNode, AliasNode.class);
        if (aliasNode != null) {
            if (fullyQualifiedPackageNameNode != null) {
                // Key is the alias name node text. Value is the package path node text.
                // Eg:  import ballerina.utils as builtin;
                // Map: builtin -> ballerina.utils
                results.put(aliasNode.getText(), fullyQualifiedPackageNameNode.getText());
            }
        } else {
            if (fullyQualifiedPackageNameNode != null) {
                // We need to get all package name nodes from the package path node.
                List<PackageNameNode> packageNameNodes = new ArrayList<>(PsiTreeUtil.findChildrenOfType(importDeclarationNode, PackageNameNode.class));
                // If there is no package path node, return empty map.
                if (packageNameNodes.isEmpty()) {
                    return results;
                }
                // The package node is the last package name in the package path.
                // Eg:  import ballerina.utils;
                // Map: utils -> ballerina.utils
                PackageNameNode lastNode = packageNameNodes.get(packageNameNodes.size() - 1);
                results.put(lastNode.getText(), fullyQualifiedPackageNameNode.getText());
            }
        }
    }
    return results;
}
Also used : FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) AliasNode(org.ballerinalang.plugins.idea.psi.AliasNode)

Example 12 with FullyQualifiedPackageNameNode

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

FullyQualifiedPackageNameNode (org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode)12 PackageDeclarationNode (org.ballerinalang.plugins.idea.psi.PackageDeclarationNode)7 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)7 PsiElement (com.intellij.psi.PsiElement)6 ArrayList (java.util.ArrayList)6 ImportDeclarationNode (org.ballerinalang.plugins.idea.psi.ImportDeclarationNode)6 Module (com.intellij.openapi.module.Module)4 PsiDirectory (com.intellij.psi.PsiDirectory)4 PsiFile (com.intellij.psi.PsiFile)4 LinkedList (java.util.LinkedList)4 AliasNode (org.ballerinalang.plugins.idea.psi.AliasNode)3 BallerinaFile (org.ballerinalang.plugins.idea.psi.BallerinaFile)3 XmlAttribNode (org.ballerinalang.plugins.idea.psi.XmlAttribNode)3 NotNull (org.jetbrains.annotations.NotNull)3 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)2 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 DefinitionNode (org.ballerinalang.plugins.idea.psi.DefinitionNode)2 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)2 ServiceDefinitionNode (org.ballerinalang.plugins.idea.psi.ServiceDefinitionNode)2