Search in sources :

Example 11 with ImportDeclarationNode

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

the class BallerinaPsiImplUtil method getPackagesImportedAsAliasInCurrentFile.

@NotNull
public static List<PsiElement> getPackagesImportedAsAliasInCurrentFile(@NotNull PsiFile file) {
    Collection<ImportDeclarationNode> allImports = PsiTreeUtil.findChildrenOfType(file, ImportDeclarationNode.class);
    ArrayList<PsiElement> filteredPackages = new ArrayList<>();
    for (ImportDeclarationNode importDeclaration : allImports) {
        Collection<AliasNode> aliasNodes = PsiTreeUtil.findChildrenOfType(importDeclaration, AliasNode.class);
        if (aliasNodes.isEmpty()) {
            continue;
        }
        PsiElement aliasNode = aliasNodes.iterator().next();
        PsiElement firstChild = aliasNode.getFirstChild();
        if (firstChild != null) {
            filteredPackages.add(firstChild);
        }
    }
    return filteredPackages;
}
Also used : ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) AliasNode(org.ballerinalang.plugins.idea.psi.AliasNode) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with ImportDeclarationNode

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

the class BallerinaPsiImplUtil method resolveAliasNode.

/**
 * Resolves the given alias node to the corresponding directory.
 *
 * @param aliasNode an alias node
 * @return {@link PsiDirectory} which is the definition of the alias node.
 */
@Nullable
public static PsiDirectory resolveAliasNode(@NotNull AliasNode aliasNode) {
    ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(aliasNode, ImportDeclarationNode.class);
    FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.getChildOfType(importDeclarationNode, FullyQualifiedPackageNameNode.class);
    if (fullyQualifiedPackageNameNode == null) {
        return null;
    }
    PackageNameNode[] packageNameNodes = PsiTreeUtil.getChildrenOfType(fullyQualifiedPackageNameNode, PackageNameNode.class);
    if (packageNameNodes == null) {
        return null;
    }
    PackageNameNode lastElement = ArrayUtil.getLastElement(packageNameNodes);
    if (lastElement == null) {
        return null;
    }
    PsiElement packageName = lastElement.getNameIdentifier();
    if (!(packageName instanceof IdentifierPSINode)) {
        return null;
    }
    List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(((IdentifierPSINode) packageName));
    if (directories.isEmpty()) {
        return null;
    }
    return directories.get(0);
}
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) PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with ImportDeclarationNode

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

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

the class BallerinaPsiImplUtil method addImport.

/**
 * Adds an import declaration node to the file.
 *
 * @param file       file which is to be used to insert the import declaration node
 * @param importPath import path to be used in the import declaration node
 * @param alias      alias if needed. If this is {@code null}, it will be ignored
 * @return import declaration node which is added
 */
@NotNull
public static ImportDeclarationNode addImport(@NotNull PsiFile file, @NotNull String importPath, @Nullable String alias) {
    PsiElement addedNode;
    Collection<ImportDeclarationNode> importDeclarationNodes = PsiTreeUtil.findChildrenOfType(file, ImportDeclarationNode.class);
    Project project = file.getProject();
    ImportDeclarationNode importDeclaration = BallerinaElementFactory.createImportDeclaration(project, importPath, alias);
    if (importDeclarationNodes.isEmpty()) {
        Collection<PackageDeclarationNode> packageDeclarationNodes = PsiTreeUtil.findChildrenOfType(file, PackageDeclarationNode.class);
        if (packageDeclarationNodes.isEmpty()) {
            PsiElement[] children = file.getChildren();
            // Children cannot be empty since the IDEA adds placeholder string
            PsiElement nonEmptyElement = PsiTreeUtil.skipSiblingsForward(children[0], PsiWhiteSpace.class, PsiComment.class);
            if (nonEmptyElement == null) {
                nonEmptyElement = children[0];
            }
            addedNode = file.addBefore(importDeclaration, children[0]);
            file.addBefore(BallerinaElementFactory.createDoubleNewLine(project), nonEmptyElement);
        } else {
            PackageDeclarationNode packageDeclarationNode = packageDeclarationNodes.iterator().next();
            PsiElement parent = packageDeclarationNode.getParent();
            addedNode = parent.addAfter(importDeclaration, packageDeclarationNode);
            parent.addAfter(BallerinaElementFactory.createDoubleNewLine(project), packageDeclarationNode);
        }
    } else {
        LinkedList<ImportDeclarationNode> importDeclarations = new LinkedList<>(importDeclarationNodes);
        ImportDeclarationNode lastImport = importDeclarations.getLast();
        addedNode = lastImport.getParent().addAfter(importDeclaration, lastImport);
        lastImport.getParent().addAfter(BallerinaElementFactory.createNewLine(project), lastImport);
    }
    return ((ImportDeclarationNode) addedNode);
}
Also used : Project(com.intellij.openapi.project.Project) ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) PackageDeclarationNode(org.ballerinalang.plugins.idea.psi.PackageDeclarationNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with ImportDeclarationNode

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

the class PackageNameReference method multiResolve.

@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
    IdentifierPSINode identifier = getElement();
    if (identifier == null) {
        return new ResolveResult[0];
    }
    AliasNode aliasNode = PsiTreeUtil.getParentOfType(identifier, AliasNode.class);
    if (aliasNode != null) {
        return new ResolveResult[0];
    }
    List<ResolveResult> results = new ArrayList<>();
    ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(identifier, ImportDeclarationNode.class);
    if (importDeclarationNode != null) {
        List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(identifier);
        for (PsiDirectory directory : directories) {
            results.add(new PsiElementResolveResult(directory));
        }
        // Return the results.
        return results.toArray(new ResolveResult[results.size()]);
    }
    PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.getParentOfType(identifier, PackageDeclarationNode.class);
    if (packageDeclarationNode != null) {
        // If this is a package declaration, resolve the directory.
        List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(identifier);
        for (PsiDirectory directory : directories) {
            results.add(new PsiElementResolveResult(directory));
        }
        // Return the results.
        return results.toArray(new ResolveResult[results.size()]);
    }
    PsiFile containingFile = identifier.getContainingFile();
    if (containingFile == null) {
        return new ResolveResult[0];
    }
    List<PsiElement> importedPackages = BallerinaPsiImplUtil.getImportedPackagesInCurrentFile(containingFile);
    for (PsiElement importedPackage : importedPackages) {
        String packageName = importedPackage.getText();
        if (packageName == null || packageName.isEmpty()) {
            continue;
        }
        if (packageName.equals(identifier.getText())) {
            PsiReference reference = importedPackage.findReferenceAt(0);
            if (reference != null) {
                PsiElement resolvedElement = reference.resolve();
                if (resolvedElement != null) {
                    results.add(new PsiElementResolveResult(resolvedElement));
                }
            }
        }
    }
    importedPackages = BallerinaPsiImplUtil.getPackagesImportedAsAliasInCurrentFile(containingFile);
    for (PsiElement importedPackage : importedPackages) {
        String packageName = importedPackage.getText();
        if (packageName == null || packageName.isEmpty()) {
            continue;
        }
        if (packageName.equals(identifier.getText())) {
            IdentifierPSINode nameNode = PsiTreeUtil.findChildOfType(importedPackage, IdentifierPSINode.class);
            if (nameNode != null) {
                results.add(new PsiElementResolveResult(nameNode));
            }
        }
    }
    return results.toArray(new ResolveResult[results.size()]);
}
Also used : ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) PackageDeclarationNode(org.ballerinalang.plugins.idea.psi.PackageDeclarationNode) PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) AliasNode(org.ballerinalang.plugins.idea.psi.AliasNode) NotNull(org.jetbrains.annotations.NotNull)

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