Search in sources :

Example 11 with PerlNamespaceDefinitionElement

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

the class PerlSubReference method resolveInner.

@NotNull
@Override
protected ResolveResult[] resolveInner(boolean incompleteCode) {
    PsiElement myElement = getElement();
    assert myElement instanceof PerlSubNameElement;
    PsiElement parent = myElement.getParent();
    if (parent instanceof PerlSubDeclarationElement || parent instanceof PerlSubDefinitionElement) {
        return ResolveResult.EMPTY_ARRAY;
    }
    PerlSubNameElement subNameElement = (PerlSubNameElement) myElement;
    List<PsiElement> relatedItems = new ArrayList<>();
    String packageName = subNameElement.getPackageName();
    String subName = subNameElement.getName();
    Project project = subNameElement.getProject();
    PerlNamespaceElement expliclitPackageElement = null;
    if (parent instanceof PerlNamespaceElementContainer) {
        expliclitPackageElement = ((PerlNamespaceElementContainer) parent).getNamespaceElement();
    }
    if (!subName.isEmpty()) {
        if (parent instanceof PerlMethod && ((PerlMethod) parent).isObjectMethod()) {
            boolean isSuper = expliclitPackageElement != null && expliclitPackageElement.isSUPER();
            relatedItems.addAll(PerlMro.resolveSub(project, isSuper ? PerlPackageUtil.getContextPackageName(subNameElement) : packageName, subName, isSuper));
        } else // static resolution
        {
            if (PerlSharedSettings.getInstance(project).SIMPLE_MAIN_RESOLUTION && // fixme this is a dirty hack until proper names resolution implemented
            PerlPackageUtil.isMain(packageName)) {
                PsiFile file = subNameElement.getContainingFile();
                GlobalSearchScope fileScope = GlobalSearchScope.fileScope(file);
                collectRelatedItems(packageName + PerlPackageUtil.PACKAGE_SEPARATOR + subName, project, parent, relatedItems, fileScope);
            // if (file instanceof PerlFile)
            // ((PerlFile) file).getElementsResolveScope();
            // System.err.println("Checking for " + subName);
            }
            if (relatedItems.isEmpty()) {
                GlobalSearchScope globalSearchScope = GlobalSearchScope.allScope(project);
                // check indexes for defined subs
                collectRelatedItems(packageName + PerlPackageUtil.PACKAGE_SEPARATOR + subName, project, parent, relatedItems, globalSearchScope);
                if (expliclitPackageElement == null) {
                    // check for imports to the current file
                    PerlNamespaceDefinitionElement namespaceContainer = PerlPackageUtil.getNamespaceContainerForElement(subNameElement);
                    if (namespaceContainer != null) {
                        for (PerlExportDescriptor exportDescriptor : namespaceContainer.getImportedSubsDescriptors()) {
                            if (exportDescriptor.getImportedName().equals(subName)) {
                                int currentSize = relatedItems.size();
                                collectRelatedItems(exportDescriptor.getTargetCanonicalName(), project, parent, relatedItems, globalSearchScope);
                                if (// imported, but not found, attempting autoload
                                relatedItems.size() == currentSize) {
                                    collectRelatedItems(exportDescriptor.getRealPackage() + PerlSubUtil.SUB_AUTOLOAD_WITH_PREFIX, project, parent, relatedItems, globalSearchScope);
                                }
                            }
                        }
                    }
                } else // check imports to target namespace
                {
                    String targetPackageName = expliclitPackageElement.getCanonicalName();
                    if (targetPackageName != null) {
                        // fixme partially not DRY with previous block
                        for (PerlNamespaceDefinitionElement namespaceDefinition : PerlPackageUtil.getNamespaceDefinitions(project, targetPackageName)) {
                            for (PerlExportDescriptor exportDescriptor : namespaceDefinition.getImportedSubsDescriptors()) {
                                if (exportDescriptor.getImportedName().equals(subName)) {
                                    collectRelatedItems(exportDescriptor.getTargetCanonicalName(), project, parent, relatedItems, globalSearchScope);
                                }
                            }
                        }
                    }
                }
                // check for builtins
                if (relatedItems.isEmpty()) {
                    PerlSubDefinitionElement builtInSub = PerlBuiltInSubsService.getInstance(project).findSub(subName);
                    if (builtInSub != null) {
                        relatedItems.add(builtInSub);
                    }
                }
                // check for autoload
                if (relatedItems.isEmpty() && // don't check for UNIVERSAL::AUTOLOAD
                !PerlPackageUtil.isUNIVERSAL(packageName)) {
                    collectRelatedItems(packageName + PerlSubUtil.SUB_AUTOLOAD_WITH_PREFIX, project, parent, relatedItems, globalSearchScope);
                }
            }
        }
    }
    List<ResolveResult> result = getResolveResults(relatedItems);
    return result.toArray(new ResolveResult[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) PerlExportDescriptor(com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PerlNamespaceElementContainer(com.perl5.lang.perl.psi.properties.PerlNamespaceElementContainer) PsiFile(com.intellij.psi.PsiFile) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with PerlNamespaceDefinitionElement

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

the class PerlMroC3 method getLinearISA.

/**
 * Builds list of inheritance path for C3 mro (Dylan, Python, Perl6): http://perldoc.perl.org/mro.html#The-C3-MRO
 *
 * @param project              project
 * @param namespaceDefinitions list of package names to add
 * @param recursionMap         recursion protection map
 * @param result               list to populate
 */
@Override
public void getLinearISA(Project project, List<PerlNamespaceDefinitionElement> namespaceDefinitions, HashSet<String> recursionMap, ArrayList<String> result) {
    // System.err.println("Resolving C3 for " + packageNames);
    ProgressManager.checkCanceled();
    Collection<PerlNamespaceDefinitionElement> nextIterationDefinitions = new ArrayList<>();
    for (PerlNamespaceDefinitionElement namespaceDefinition : namespaceDefinitions) {
        String packageName = namespaceDefinition.getPackageName();
        if (!recursionMap.contains(packageName)) {
            recursionMap.add(packageName);
            result.add(packageName);
            nextIterationDefinitions.add(namespaceDefinition);
        }
    }
    for (PerlNamespaceDefinitionElement namespaceDefinition : nextIterationDefinitions) {
        namespaceDefinition.getLinearISA(recursionMap, result);
    }
}
Also used : PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) ArrayList(java.util.ArrayList)

Example 13 with PerlNamespaceDefinitionElement

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

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

the class PerlMoveFileHandler method updateMovedFile.

@Override
public void updateMovedFile(PsiFile file) throws IncorrectOperationException {
    String originalPackageName = file.getUserData(ORIGINAL_PACKAGE_NAME);
    Project project = file.getProject();
    VirtualFile virtualFile = file.getVirtualFile();
    VirtualFile newInnermostRoot = PerlUtil.getFileClassRoot(project, virtualFile);
    if (newInnermostRoot != null && originalPackageName != null) {
        String newRelativePath = VfsUtil.getRelativePath(virtualFile, newInnermostRoot);
        String newPackageName = PerlPackageUtil.getPackageNameByPath(newRelativePath);
        final RenameRefactoring[] refactoring = { null };
        for (PerlNamespaceDefinitionElement namespaceDefinition : PsiTreeUtil.findChildrenOfType(file, PerlNamespaceDefinitionElement.class)) {
            if (originalPackageName.equals(namespaceDefinition.getPackageName())) {
                if (refactoring[0] == null) {
                    refactoring[0] = RefactoringFactory.getInstance(file.getProject()).createRename(namespaceDefinition, newPackageName);
                } else {
                    refactoring[0].addElement(namespaceDefinition, newPackageName);
                }
            }
        }
        if (refactoring[0] != null) {
            ApplicationManager.getApplication().invokeLater(refactoring[0]::run);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) RenameRefactoring(com.intellij.refactoring.RenameRefactoring)

Example 15 with PerlNamespaceDefinitionElement

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

the class PerlRenameFileProcessor method getPostRenameCallback.

@Nullable
@Override
public Runnable getPostRenameCallback(final PsiElement element, String newName, RefactoringElementListener elementListener) {
    if (newName.endsWith(".pm")) {
        final Project project = element.getProject();
        final String currentPackageName = ((PerlFileImpl) element).getFilePackageName();
        if (currentPackageName != null) {
            String[] nameChunks = currentPackageName.split(PerlPackageUtil.PACKAGE_SEPARATOR);
            nameChunks[nameChunks.length - 1] = newName.replaceFirst("\\.pm$", "");
            final String newPackageName = StringUtils.join(nameChunks, PerlPackageUtil.PACKAGE_SEPARATOR);
            final String newFileName = ((PerlFileImpl) element).getVirtualFile().getParent().getPath() + '/' + newName;
            return () -> {
                VirtualFile newFile = LocalFileSystem.getInstance().findFileByPath(newFileName);
                if (newFile != null) {
                    PsiFile psiFile = PsiManager.getInstance(project).findFile(newFile);
                    if (psiFile != null) {
                        final RenameRefactoring[] refactoring = { null };
                        for (PerlNamespaceDefinitionElement namespaceDefinition : PsiTreeUtil.findChildrenOfType(psiFile, PerlNamespaceDefinitionElement.class)) {
                            if (currentPackageName.equals(namespaceDefinition.getPackageName())) {
                                if (refactoring[0] == null) {
                                    refactoring[0] = RefactoringFactory.getInstance(psiFile.getProject()).createRename(namespaceDefinition, newPackageName);
                                } else {
                                    refactoring[0].addElement(namespaceDefinition, newPackageName);
                                }
                            }
                        }
                        if (refactoring[0] != null) {
                            ApplicationManager.getApplication().invokeLater(refactoring[0]::run);
                        }
                    }
                }
            };
        }
    }
    return super.getPostRenameCallback(element, newName, elementListener);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PerlNamespaceDefinitionElement (com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)17 PsiElement (com.intellij.psi.PsiElement)12 NotNull (org.jetbrains.annotations.NotNull)10 Project (com.intellij.openapi.project.Project)8 ArrayList (java.util.ArrayList)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 PsiFile (com.intellij.psi.PsiFile)6 PerlExportDescriptor (com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor)6 ResolveResult (com.intellij.psi.ResolveResult)4 PerlImportsCollector (com.perl5.lang.perl.util.processors.PerlImportsCollector)4 PsiElementResolveResult (com.intellij.psi.PsiElementResolveResult)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 MasonSettings (com.perl5.lang.mason2.idea.configuration.MasonSettings)2 PerlSubDefinitionElement (com.perl5.lang.perl.psi.PerlSubDefinitionElement)2 PerlUseStatement (com.perl5.lang.perl.psi.PerlUseStatement)2 PerlFileImpl (com.perl5.lang.perl.psi.impl.PerlFileImpl)2 PerlNamespaceDefinitionStub (com.perl5.lang.perl.psi.stubs.namespaces.PerlNamespaceDefinitionStub)2 THashSet (gnu.trove.THashSet)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 StructureViewTreeElement (com.intellij.ide.structureView.StructureViewTreeElement)1