Search in sources :

Example 6 with AliasNode

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

the class BallerinaAnnotator method annotateImportDeclarations.

private void annotateImportDeclarations(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    AliasNode aliasNode = PsiTreeUtil.findChildOfType(element, AliasNode.class);
    if (aliasNode != null) {
        // Create the annotation.
        Annotation annotation = holder.createInfoAnnotation(aliasNode.getTextRange(), null);
        annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.PACKAGE);
    } else {
        Collection<PackageNameNode> packageNameNodes = PsiTreeUtil.findChildrenOfType(element, PackageNameNode.class);
        if (!packageNameNodes.isEmpty()) {
            PackageNameNode lastPackageName = (PackageNameNode) packageNameNodes.toArray()[packageNameNodes.size() - 1];
            // Create the annotation.
            Annotation annotation = holder.createInfoAnnotation(lastPackageName.getTextRange(), null);
            annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.PACKAGE);
        }
    }
}
Also used : PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) Annotation(com.intellij.lang.annotation.Annotation) AliasNode(org.ballerinalang.plugins.idea.psi.AliasNode)

Example 7 with AliasNode

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

the class BallerinaCompletionContributor method handlePackageNameNode.

/**
 * Add lookups for package declarations.
 *
 * @param parameters parameters which contain details of completion invocation
 * @param resultSet  result list which is used to add lookups
 */
private void handlePackageNameNode(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet resultSet) {
    PsiElement element = parameters.getPosition();
    PsiElement parent = element.getParent();
    PsiElement superParent = parent.getParent();
    // Check whether we are in a package declaration node
    if (superParent.getParent() instanceof PackageDeclarationNode) {
        // If we are in a package declaration node, suggest packages.
        addPackageSuggestions(resultSet, element);
    } else if (superParent.getParent() instanceof ImportDeclarationNode && !(superParent instanceof AliasNode)) {
        // If the parent is not an AliasNode and is inside the ImportDeclarationNode, we need to suggest
        // packages.
        addImportSuggestions(resultSet, element);
    }
}
Also used : ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) PackageDeclarationNode(org.ballerinalang.plugins.idea.psi.PackageDeclarationNode) PsiElement(com.intellij.psi.PsiElement) AliasNode(org.ballerinalang.plugins.idea.psi.AliasNode)

Example 8 with AliasNode

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

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

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

Aggregations

AliasNode (org.ballerinalang.plugins.idea.psi.AliasNode)11 PsiElement (com.intellij.psi.PsiElement)8 ImportDeclarationNode (org.ballerinalang.plugins.idea.psi.ImportDeclarationNode)7 NotNull (org.jetbrains.annotations.NotNull)6 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)5 ArrayList (java.util.ArrayList)5 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)5 PsiReference (com.intellij.psi.PsiReference)4 PackageDeclarationNode (org.ballerinalang.plugins.idea.psi.PackageDeclarationNode)4 PsiDirectory (com.intellij.psi.PsiDirectory)3 LinkedList (java.util.LinkedList)3 PsiFile (com.intellij.psi.PsiFile)2 ResolveResult (com.intellij.psi.ResolveResult)2 FullyQualifiedPackageNameNode (org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode)2 FunctionReferenceNode (org.ballerinalang.plugins.idea.psi.FunctionReferenceNode)2 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)2 XmlAttribNode (org.ballerinalang.plugins.idea.psi.XmlAttribNode)2 Nullable (org.jetbrains.annotations.Nullable)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1