Search in sources :

Example 11 with PsiElementResolveResult

use of com.intellij.psi.PsiElementResolveResult 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)

Example 12 with PsiElementResolveResult

use of com.intellij.psi.PsiElementResolveResult in project Perl5-IDEA by Camelcade.

the class PerlSubReferenceSimple method getResolveResults.

@NotNull
public List<ResolveResult> getResolveResults(List<PsiElement> relatedItems) {
    List<ResolveResult> result = new ArrayList<>();
    resetFlags();
    for (PsiElement element : relatedItems) {
        if (!isAutoloaded() && element instanceof PerlIdentifierOwner && PerlSubUtil.SUB_AUTOLOAD.equals(((PerlIdentifierOwner) element).getName())) {
            setAutoloaded();
        }
        if (!isConstant() && element instanceof PerlLightConstantDefinitionElement) {
            setConstant();
        }
        if (!isDeclared() && element instanceof PerlSubDeclarationElement) {
            setDeclared();
        }
        if (!isDefined() && element instanceof PerlSubDefinitionElement) {
            setDefined();
        }
        if (!isXSub() && element instanceof PerlSubElement && ((PerlSubElement) element).isXSub()) {
            setXSub();
        }
        if (!isAliased() && element instanceof PerlGlobVariable) {
            setAliased();
        }
        result.add(new PsiElementResolveResult(element));
    }
    return result;
}
Also used : PerlLightConstantDefinitionElement(com.perl5.lang.perl.parser.constant.psi.light.PerlLightConstantDefinitionElement) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) ArrayList(java.util.ArrayList) PerlGlobVariable(com.perl5.lang.perl.psi.PerlGlobVariable) PerlSubDeclarationElement(com.perl5.lang.perl.psi.PerlSubDeclarationElement) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) PerlSubDefinitionElement(com.perl5.lang.perl.psi.PerlSubDefinitionElement) PsiElement(com.intellij.psi.PsiElement) PerlIdentifierOwner(com.perl5.lang.perl.psi.properties.PerlIdentifierOwner) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with PsiElementResolveResult

use of com.intellij.psi.PsiElementResolveResult in project Perl5-IDEA by Camelcade.

the class PerlVariableReference method resolveInner.

@NotNull
@Override
protected ResolveResult[] resolveInner(boolean incompleteCode) {
    PsiElement elementParent = myElement.getParent();
    assert elementParent instanceof PerlVariable;
    PerlVariable perlVariable = (PerlVariable) elementParent;
    List<ResolveResult> result = new ArrayList<>();
    PerlVariableDeclarationElement lexicalDeclaration = PerlResolveUtil.getLexicalDeclaration(perlVariable);
    if (lexicalDeclaration == null || lexicalDeclaration.isGlobalDeclaration() && !(lexicalDeclaration instanceof PerlImplicitVariableDeclaration)) {
        // not found explicit lexically visible declarations
        // imports
        PerlVariableType actualType = perlVariable.getActualType();
        Project project = perlVariable.getProject();
        PerlNamespaceDefinitionElement namespaceContainer = PerlPackageUtil.getNamespaceContainerForElement(perlVariable);
        if (// not true if LPE in TemplateToolkit
        namespaceContainer != null) {
            String variableName = perlVariable.getName();
            if (actualType == PerlVariableType.SCALAR) {
                for (PerlExportDescriptor importEntry : namespaceContainer.getImportedScalarDescriptors()) {
                    if (importEntry.getImportedName().equals(variableName)) {
                        for (PerlVariableDeclarationElement targetVariable : PerlScalarUtil.getGlobalScalarDefinitions(project, importEntry.getTargetCanonicalName())) {
                            result.add(new PsiElementResolveResult(targetVariable));
                        }
                    }
                }
            } else if (actualType == PerlVariableType.ARRAY) {
                for (PerlExportDescriptor importEntry : namespaceContainer.getImportedArrayDescriptors()) {
                    if (importEntry.getImportedName().equals(variableName)) {
                        for (PerlVariableDeclarationElement targetVariable : PerlArrayUtil.getGlobalArrayDefinitions(project, importEntry.getTargetCanonicalName())) {
                            result.add(new PsiElementResolveResult(targetVariable));
                        }
                    }
                }
            } else if (actualType == PerlVariableType.HASH) {
                for (PerlExportDescriptor importEntry : namespaceContainer.getImportedHashDescriptors()) {
                    if (importEntry.getImportedName().equals(variableName)) {
                        for (PerlVariableDeclarationElement targetVariable : PerlHashUtil.getGlobalHashDefinitions(project, importEntry.getTargetCanonicalName())) {
                            result.add(new PsiElementResolveResult(targetVariable));
                        }
                    }
                }
            }
        }
        // our variable declaration
        for (PerlGlobVariable glob : perlVariable.getRelatedGlobs()) {
            result.add(new PsiElementResolveResult(glob));
        }
        // globs
        for (PerlVariableDeclarationElement globalDeclaration : perlVariable.getGlobalDeclarations()) {
            result.add(new PsiElementResolveResult(globalDeclaration));
        }
    } else {
        result.add(new PsiElementResolveResult(lexicalDeclaration));
    }
    return result.toArray(new ResolveResult[result.size()]);
}
Also used : PerlVariableDeclarationElement(com.perl5.lang.perl.psi.PerlVariableDeclarationElement) PerlVariableType(com.perl5.lang.perl.psi.utils.PerlVariableType) ArrayList(java.util.ArrayList) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) Project(com.intellij.openapi.project.Project) PerlExportDescriptor(com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PerlGlobVariable(com.perl5.lang.perl.psi.PerlGlobVariable) PerlVariable(com.perl5.lang.perl.psi.PerlVariable) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) PerlImplicitVariableDeclaration(com.perl5.lang.perl.psi.impl.PerlImplicitVariableDeclaration) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with PsiElementResolveResult

use of com.intellij.psi.PsiElementResolveResult in project Perl5-IDEA by Camelcade.

the class PodSubReference method resolveInner.

@NotNull
@Override
protected ResolveResult[] resolveInner(boolean incompleteCode) {
    PsiElement element = getElement();
    if (element != null) {
        final Project project = element.getProject();
        String subName = element.getText();
        if (StringUtil.isNotEmpty(subName)) {
            final PsiFile containingFile = element.getContainingFile();
            String packageName = PodFileUtil.getPackageName(containingFile);
            List<ResolveResult> results = new ArrayList<>();
            if (StringUtil.isNotEmpty(packageName)) {
                String canonicalName = packageName + PerlPackageUtil.PACKAGE_SEPARATOR + subName;
                for (PerlSubDefinitionElement target : PerlSubUtil.getSubDefinitions(project, canonicalName)) {
                    results.add(new PsiElementResolveResult(target));
                }
                for (PerlSubDeclarationElement target : PerlSubUtil.getSubDeclarations(project, canonicalName)) {
                    results.add(new PsiElementResolveResult(target));
                }
            }
            if (results.isEmpty()) {
                final PsiFile perlFile = containingFile.getViewProvider().getStubBindingRoot();
                for (PerlSubElement subBase : PsiTreeUtil.findChildrenOfType(perlFile, PerlSubElement.class)) {
                    String subBaseName = subBase.getName();
                    if (subBaseName != null && StringUtil.equals(subBaseName, subName)) {
                        results.add(new PsiElementResolveResult(subBase));
                    }
                }
            }
            return results.toArray(new ResolveResult[results.size()]);
        }
    }
    return ResolveResult.EMPTY_ARRAY;
}
Also used : Project(com.intellij.openapi.project.Project) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) PerlSubDeclarationElement(com.perl5.lang.perl.psi.PerlSubDeclarationElement) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) PerlSubDefinitionElement(com.perl5.lang.perl.psi.PerlSubDefinitionElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with PsiElementResolveResult

use of com.intellij.psi.PsiElementResolveResult in project Perl5-IDEA by Camelcade.

the class TemplateToolkitBlockReference method resolveInner.

@NotNull
@Override
protected ResolveResult[] resolveInner(boolean incompleteCode) {
    PsiElement element = getElement();
    if (element == null) {
        return ResolveResult.EMPTY_ARRAY;
    }
    TextRange range = ElementManipulators.getValueTextRange(element);
    if (range == null) {
        return ResolveResult.EMPTY_ARRAY;
    }
    final CharSequence targetName = range.subSequence(element.getText());
    if (StringUtil.isEmpty(targetName)) {
        return ResolveResult.EMPTY_ARRAY;
    }
    final List<ResolveResult> result = new ArrayList<>();
    PsiTreeUtil.processElements(element.getContainingFile(), element1 -> {
        if (element1 instanceof TemplateToolkitNamedBlock && StringUtil.equals(((TemplateToolkitNamedBlock) element1).getName(), targetName)) {
            result.add(new PsiElementResolveResult(element1));
        }
        return true;
    });
    return result.toArray(new ResolveResult[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) TemplateToolkitNamedBlock(com.perl5.lang.tt2.psi.TemplateToolkitNamedBlock) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElementResolveResult (com.intellij.psi.PsiElementResolveResult)17 PsiElement (com.intellij.psi.PsiElement)13 ResolveResult (com.intellij.psi.ResolveResult)13 NotNull (org.jetbrains.annotations.NotNull)12 ArrayList (java.util.ArrayList)8 Project (com.intellij.openapi.project.Project)5 PsiFile (com.intellij.psi.PsiFile)5 PerlGlobVariable (com.perl5.lang.perl.psi.PerlGlobVariable)2 PerlNamespaceDefinitionElement (com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)2 PerlSubDeclarationElement (com.perl5.lang.perl.psi.PerlSubDeclarationElement)2 PerlSubDefinitionElement (com.perl5.lang.perl.psi.PerlSubDefinitionElement)2 PerlSubElement (com.perl5.lang.perl.psi.PerlSubElement)2 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)1 PsiReference (com.intellij.psi.PsiReference)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 XmlFile (com.intellij.psi.xml.XmlFile)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1