Search in sources :

Example 11 with AliasNode

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

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