Search in sources :

Example 6 with PerlGlobVariable

use of com.perl5.lang.perl.psi.PerlGlobVariable in project Perl5-IDEA by Camelcade.

the class PerlMro method getVariants.

/**
 * Returns collection of Sub Definitions of class and it's superclasses according perl's default MRO
 *
 * @param psiElement      anchorElement
 * @param basePackageName base project
 * @param isSuper         flag for SUPER resolutions
 * @return collection of definitions
 */
public static Collection<PsiElement> getVariants(@NotNull PsiElement psiElement, String basePackageName, boolean isSuper) {
    Project project = psiElement.getProject();
    HashMap<String, PsiElement> methods = new HashMap<>();
    if (basePackageName != null) {
        GlobalSearchScope searchScope = psiElement.getResolveScope();
        for (String packageName : getLinearISA(project, basePackageName, isSuper)) {
            PerlSubUtil.processSubDefinitionsInPackage(project, packageName, searchScope, subDefinition -> {
                if (!methods.containsKey(subDefinition.getSubName())) {
                    methods.put(subDefinition.getSubName(), subDefinition);
                }
                return true;
            });
            PerlSubUtil.processSubDeclarationsInPackage(project, packageName, searchScope, subDeclaration -> {
                if (!methods.containsKey(subDeclaration.getSubName())) {
                    methods.put(subDeclaration.getSubName(), subDeclaration);
                }
                return true;
            });
            for (PerlGlobVariable globVariable : PerlGlobUtil.getGlobsDefinitions(project, "*" + packageName)) {
                if (globVariable.isLeftSideOfAssignment() && !methods.containsKey(globVariable.getName())) {
                    methods.put(globVariable.getName(), globVariable);
                }
            }
        }
    }
    return new ArrayList<>(methods.values());
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PerlGlobVariable(com.perl5.lang.perl.psi.PerlGlobVariable) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PerlGlobVariable

use of com.perl5.lang.perl.psi.PerlGlobVariable 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 8 with PerlGlobVariable

use of com.perl5.lang.perl.psi.PerlGlobVariable in project Perl5-IDEA by Camelcade.

the class PerlAnnotator method annotate.

@Override
public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
    IElementType elementType = PsiUtilCore.getElementType(element);
    if (elementType == NYI_STATEMENT) {
        holder.createInfoAnnotation(element, "Unimplemented statement").setTextAttributes(CodeInsightColors.TODO_DEFAULT_ATTRIBUTES);
    } else if (element instanceof PerlGlobVariable && ((PerlGlobVariable) element).isBuiltIn()) {
        holder.createInfoAnnotation(element, null).setTextAttributes(PERL_GLOB_BUILTIN);
    } else if (element instanceof PerlVariable && ((PerlVariable) element).isBuiltIn()) {
        holder.createInfoAnnotation(element, null).setTextAttributes(VARIABLE_KEYS_MAP.get(element.getClass()));
    } else if (elementType == LABEL_DECLARATION || elementType == LABEL_EXPR) {
        holder.createInfoAnnotation(element.getFirstChild(), null).setTextAttributes(PerlSyntaxHighlighter.PERL_LABEL);
    } else if (elementType == PACKAGE) {
        assert element instanceof PerlNamespaceElement;
        PerlNamespaceElement namespaceElement = (PerlNamespaceElement) element;
        PsiElement parent = namespaceElement.getParent();
        if (parent instanceof PerlNamespaceDefinitionWithIdentifier) {
            decorateElement(namespaceElement, holder, PerlSyntaxHighlighter.PERL_PACKAGE_DEFINITION, false);
        } else {
            if (namespaceElement.isPragma()) {
                decorateElement(namespaceElement, holder, PerlSyntaxHighlighter.PERL_PACKAGE_PRAGMA, false);
            } else if (namespaceElement.isBuiltin()) {
                decorateElement(namespaceElement, holder, PerlSyntaxHighlighter.PERL_PACKAGE_CORE, false);
            }
        }
    } else if (element instanceof PerlPolyNamedElement) {
        TextAttributesKey subAttribute = PerlSyntaxHighlighter.PERL_SUB_DEFINITION;
        if (elementType == PerlConstantsWrapperElementType.CONSTANT_WRAPPER) {
            // fixme some interface?
            subAttribute = PerlSyntaxHighlighter.PERL_CONSTANT;
        }
        for (PerlDelegatingLightNamedElement lightNamedElement : ((PerlPolyNamedElement) element).getLightElements()) {
            TextAttributesKey currentKey = lightNamedElement instanceof PerlSubDefinition ? subAttribute : PerlSyntaxHighlighter.PERL_PACKAGE_DEFINITION;
            PsiElement navigationElement = lightNamedElement.getNavigationElement();
            holder.createInfoAnnotation(ElementManipulators.getValueTextRange(navigationElement).shiftRight(lightNamedElement.getTextOffset()), null).setEnforcedTextAttributes(adjustTextAttributes(currentScheme.getAttributes(currentKey), false));
        }
    } else if (// instanceof PerlSubNameElement
    elementType == SUB_NAME) {
        PsiElement parent = element.getParent();
        if (parent instanceof PsiPerlSubDeclaration) {
            holder.createInfoAnnotation(element, null).setTextAttributes(PerlSyntaxHighlighter.PERL_SUB_DECLARATION);
        } else if (parent instanceof PerlSubDefinitionElement) {
            if ("AUTOLOAD".equals(((PerlSubNameElement) element).getName())) {
                holder.createInfoAnnotation(element, null).setTextAttributes(PerlSyntaxHighlighter.PERL_AUTOLOAD);
            } else {
                holder.createInfoAnnotation(element, null).setTextAttributes(PerlSyntaxHighlighter.PERL_SUB_DEFINITION);
            }
        } else if (parent instanceof PerlMethod) {
            // fixme don't we need to take multiple references here?
            PsiElement grandParent = parent.getParent();
            PerlNamespaceElement methodNamespace = ((PerlMethod) parent).getNamespaceElement();
            if (// / not ...->method fixme shouldn't we use isObjectMethod here?
            !(grandParent instanceof PsiPerlNestedCall) && // no explicit NS or it's core
            (methodNamespace == null || methodNamespace.isCORE()) && ((PerlSubNameElement) element).isBuiltIn()) {
                decorateElement(element, holder, PerlSyntaxHighlighter.PERL_SUB_BUILTIN);
            } else {
                PsiReference reference = element.getReference();
                if (reference instanceof PerlSubReference) {
                    ((PerlSubReference) reference).multiResolve(false);
                    if (((PerlSubReference) reference).isConstant()) {
                        holder.createInfoAnnotation(element, "Constant").setTextAttributes(PerlSyntaxHighlighter.PERL_CONSTANT);
                    } else if (((PerlSubReference) reference).isAutoloaded()) {
                        holder.createInfoAnnotation(element, "Auto-loaded sub").setTextAttributes(PerlSyntaxHighlighter.PERL_AUTOLOAD);
                    } else if (((PerlSubReference) reference).isXSub()) {
                        holder.createInfoAnnotation(element, "XSub").setTextAttributes(PerlSyntaxHighlighter.PERL_XSUB);
                    }
                }
            }
        }
    }
}
Also used : PerlSubReference(com.perl5.lang.perl.psi.references.PerlSubReference) PsiReference(com.intellij.psi.PsiReference) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) IElementType(com.intellij.psi.tree.IElementType) PerlDelegatingLightNamedElement(com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)7 Project (com.intellij.openapi.project.Project)5 PerlGlobVariable (com.perl5.lang.perl.psi.PerlGlobVariable)5 NotNull (org.jetbrains.annotations.NotNull)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 PerlExportDescriptor (com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor)3 StringUtil (com.intellij.openapi.util.text.StringUtil)2 PsiElementResolveResult (com.intellij.psi.PsiElementResolveResult)2 ResolveResult (com.intellij.psi.ResolveResult)2 PerlLightConstantDefinitionElement (com.perl5.lang.perl.parser.constant.psi.light.PerlLightConstantDefinitionElement)2 PerlSubElement (com.perl5.lang.perl.psi.PerlSubElement)2 PerlIdentifierOwner (com.perl5.lang.perl.psi.properties.PerlIdentifierOwner)2 PodLanguage (com.perl5.lang.pod.PodLanguage)2 ArrayList (java.util.ArrayList)2 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)1 CompletionProvider (com.intellij.codeInsight.completion.CompletionProvider)1 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)1 PlainPrefixMatcher (com.intellij.codeInsight.completion.PlainPrefixMatcher)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 StructureViewTreeElement (com.intellij.ide.structureView.StructureViewTreeElement)1