Search in sources :

Example 21 with PackageNameNode

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

the class BallerinaPsiImplUtil method resolveDirectory.

/**
 * Resolves a package name to matching directories.
 *
 * @param identifier the package name identifier which we need to resolve to the directory
 * @return resolved directory
 */
@NotNull
public static List<PsiDirectory> resolveDirectory(@NotNull IdentifierPSINode identifier) {
    List<PsiDirectory> results = new LinkedList<>();
    PsiElement parent;
    PsiElement tempParent = identifier.getParent();
    if (tempParent == null) {
        return results;
    }
    PsiElement superParent = tempParent.getParent();
    AliasNode aliasNode = PsiTreeUtil.getParentOfType(superParent, AliasNode.class);
    if (aliasNode != null) {
        PsiElement temp = superParent;
        while (temp != null && !(temp instanceof FullyQualifiedPackageNameNode)) {
            temp = temp.getPrevSibling();
        }
        if (temp != null) {
            parent = temp.getLastChild();
        } else {
            return results;
        }
    } else {
        parent = tempParent;
    }
    // This is used to store all the packages which need to resolve the current package.
    List<PsiElement> packages = new ArrayList<>();
    packages.add(parent);
    // Find all previous PackageNameNode elements.
    PsiElement sibling = parent.getPrevSibling();
    while (sibling != null) {
        if (sibling instanceof PackageNameNode) {
            // Add the sibling to the index 0 because we are traversing backward.
            packages.add(0, sibling);
        }
        sibling = sibling.getPrevSibling();
    }
    Project project = identifier.getProject();
    PsiFile containingFile = identifier.getContainingFile();
    PsiFile originalFile = containingFile.getOriginalFile();
    VirtualFile virtualFile = originalFile.getVirtualFile();
    if (virtualFile == null) {
        return results;
    }
    Module module = ModuleUtilCore.findModuleForFile(virtualFile, project);
    if (module == null) {
        return results;
    }
    results.addAll(findAllMatchingPackages(module, packages));
    return results;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Project(com.intellij.openapi.project.Project) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) 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 22 with PackageNameNode

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

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

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

the class BallerinaPsiImplUtil method suggestCurrentPackagePath.

@NotNull
public static PsiDirectory[] suggestCurrentPackagePath(@NotNull PsiElement element) {
    List<PsiDirectory> results = new ArrayList<>();
    Project project = element.getProject();
    PsiElement parent = element.getParent();
    // This is used to store all the packages which need to resolve the current package.
    List<PsiElement> packages = new ArrayList<>();
    packages.add(parent);
    // Find all previous PackageNameNode elements.
    PsiElement sibling = parent.getPrevSibling();
    while (sibling != null) {
        if (sibling instanceof PackageNameNode) {
            // Add the sibling to the index 0 because we are traversing backward.
            packages.add(0, sibling);
        }
        sibling = sibling.getPrevSibling();
    }
    // We need to get the content roots from the project and find matching directories in each content root.
    VirtualFile[] contentRoots = ProjectRootManager.getInstance(project).getContentRoots();
    for (VirtualFile contentRoot : contentRoots) {
        // Get any matching directory from the content root.
        List<VirtualFile> matches = suggestDirectory(contentRoot, packages);
        // If there are matches, add it to the results.
        for (VirtualFile file : matches) {
            results.add(PsiManager.getInstance(project).findDirectory(file));
        }
    }
    return results.toArray(new PsiDirectory[results.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with PackageNameNode

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

the class LanguageInjectorUtils method isValid.

/**
 * Checks whether the provided host is suitable for language injecting.
 *
 * @param host          host string to be checked
 * @param packageNames  valid package names
 * @param functionNames valid function names
 * @return {@code true} if suitable for injecting language, {@code false} otherwise.
 */
public static boolean isValid(@NotNull PsiLanguageInjectionHost host, @NotNull Set<String> packageNames, @NotNull Set<String> functionNames) {
    if (!(host instanceof QuotedLiteralString)) {
        return false;
    }
    ExpressionListNode expressionListNode = PsiTreeUtil.getTopmostParentOfType(host, ExpressionListNode.class);
    if (expressionListNode == null) {
        return false;
    }
    PsiElement firstChild = expressionListNode.getFirstChild();
    if (firstChild.getChildren().length > 1) {
        return false;
    }
    FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getPrevSiblingOfType(expressionListNode, FunctionReferenceNode.class);
    if (functionReferenceNode == null) {
        return false;
    }
    PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(functionReferenceNode, PackageNameNode.class);
    if (packageNameNode == null) {
        return false;
    }
    IdentifierPSINode functionName = PsiTreeUtil.getChildOfType(functionReferenceNode, IdentifierPSINode.class);
    if (functionName == null) {
        return false;
    }
    ExpressionNode expressionNode = PsiTreeUtil.getParentOfType(host, ExpressionNode.class);
    if (expressionNode == null) {
        return false;
    }
    Collection<QuotedLiteralString> quotedLiteralStrings = PsiTreeUtil.findChildrenOfType(expressionNode, QuotedLiteralString.class);
    // We ignore this case since the string might not be identified correctly and will cause issues.
    if (quotedLiteralStrings.size() > 1) {
        return false;
    }
    return packageNames.contains(packageNameNode.getText()) && functionNames.contains(functionName.getText());
}
Also used : ExpressionListNode(org.ballerinalang.plugins.idea.psi.ExpressionListNode) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) 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