Search in sources :

Example 11 with PerlVariableDeclarationElement

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

the class PerlVariableDeclarationSearcher method processBuiltIns.

public boolean processBuiltIns() {
    PerlVariableDeclarationElement variableDeclaration = PerlBuiltInVariablesService.getInstance(myVariable.getProject()).getVariableDeclaration(myVariableType, myName);
    if (variableDeclaration == null) {
        return true;
    }
    myResult = variableDeclaration;
    return true;
}
Also used : PerlVariableDeclarationElement(com.perl5.lang.perl.psi.PerlVariableDeclarationElement)

Example 12 with PerlVariableDeclarationElement

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

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

the class PerlUseVarsQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    Collection<PerlVariableDeclarationElement> declarations = myVariablesProvider.getValue();
    if (declarations.isEmpty()) {
        startElement.delete();
        return;
    }
    StringBuilder newCode = new StringBuilder("our");
    if (declarations.size() > 1) {
        newCode.append("(");
    }
    newCode.append(StringUtil.join(ContainerUtil.map(declarations, PsiElement::getText), ","));
    if (declarations.size() > 1) {
        newCode.append(")");
    }
    newCode.append(";");
    PerlFileImpl fakeFile = PerlElementFactory.createFile(myStartElement.getProject(), newCode.toString());
    PsiPerlStatement newElement = PsiTreeUtil.findChildOfType(fakeFile, PsiPerlStatement.class);
    if (newElement != null) {
        startElement.replace(newElement);
    }
}
Also used : PerlVariableDeclarationElement(com.perl5.lang.perl.psi.PerlVariableDeclarationElement) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PsiPerlStatement(com.perl5.lang.perl.psi.PsiPerlStatement) LocalQuickFixOnPsiElement(com.intellij.codeInspection.LocalQuickFixOnPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 14 with PerlVariableDeclarationElement

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

the class PerlUnresolvedVariableInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PerlVisitor() {

        @Override
        public void visitPerlVariable(@NotNull final PerlVariable variable) {
            PsiElement parent = variable.getParent();
            if (parent instanceof PerlVariableDeclarationElement || variable.isBuiltIn()) {
                return;
            }
            PerlVariableNameElement variableNameElement = variable.getVariableNameElement();
            if (variableNameElement != null) {
                for (PsiReference reference : variableNameElement.getReferences()) {
                    if (reference instanceof PsiPolyVariantReference && ((PsiPolyVariantReference) reference).multiResolve(false).length > 0 || reference.resolve() != null) {
                        return;
                    }
                }
                registerProblem(holder, variableNameElement, "Unable to find variable declaration.");
            }
        }
    };
}
Also used : PerlVariableDeclarationElement(com.perl5.lang.perl.psi.PerlVariableDeclarationElement) PerlVariableNameElement(com.perl5.lang.perl.psi.PerlVariableNameElement) PsiReference(com.intellij.psi.PsiReference) PerlVisitor(com.perl5.lang.perl.psi.PerlVisitor) PerlVariable(com.perl5.lang.perl.psi.PerlVariable) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with PerlVariableDeclarationElement

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

the class PerlGotoVariableContributor method getItemsByName.

@NotNull
@Override
public NavigationItem[] getItemsByName(String name, String pattern, Project project, boolean includeNonProjectItems) {
    if (name.length() > 0) {
        Collection<PerlVariableDeclarationElement> result;
        GlobalSearchScope scope = includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project);
        char firstChar = name.charAt(0);
        if (firstChar == '$') {
            result = PerlScalarUtil.getGlobalScalarDefinitions(project, name.substring(1), scope);
        } else if (firstChar == '@') {
            result = PerlArrayUtil.getGlobalArrayDefinitions(project, name.substring(1), scope);
        } else if (firstChar == '%') {
            result = PerlHashUtil.getGlobalHashDefinitions(project, name.substring(1), scope);
        } else if (firstChar == '*') {
            Collection<PsiPerlGlobVariable> globResult = PerlGlobUtil.getGlobsDefinitions(project, name.substring(1), scope);
            // noinspection SuspiciousToArrayCall
            return globResult.toArray(new NavigationItem[globResult.size()]);
        } else {
            return NavigationItem.EMPTY_NAVIGATION_ITEM_ARRAY;
        }
        // noinspection SuspiciousToArrayCall
        return result.toArray(new NavigationItem[result.size()]);
    }
    return NavigationItem.EMPTY_NAVIGATION_ITEM_ARRAY;
}
Also used : PerlVariableDeclarationElement(com.perl5.lang.perl.psi.PerlVariableDeclarationElement) NavigationItem(com.intellij.navigation.NavigationItem) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Collection(java.util.Collection) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)12 PerlVariableDeclarationElement (com.perl5.lang.perl.psi.PerlVariableDeclarationElement)10 NotNull (org.jetbrains.annotations.NotNull)10 PerlVariable (com.perl5.lang.perl.psi.PerlVariable)6 PerlVariableType (com.perl5.lang.perl.psi.utils.PerlVariableType)6 PerlLexicalScope (com.perl5.lang.perl.psi.properties.PerlLexicalScope)5 Project (com.intellij.openapi.project.Project)4 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)3 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 PerlExportDescriptor (com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor)3 com.perl5.lang.perl.psi (com.perl5.lang.perl.psi)3 PerlImplicitVariableDeclaration (com.perl5.lang.perl.psi.impl.PerlImplicitVariableDeclaration)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 PsiFile (com.intellij.psi.PsiFile)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)2 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)2 PerlIcons (com.perl5.PerlIcons)2