Search in sources :

Example 6 with PerlNamespaceDefinitionElement

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

the class PerlMooseInnerReference method resolveInner.

@NotNull
@Override
protected ResolveResult[] resolveInner(boolean incompleteCode) {
    List<ResolveResult> result = new ArrayList<>();
    PsiElement element = getElement();
    String subName = null;
    PerlSubDefinitionElement subDefinitionBase = PsiTreeUtil.getParentOfType(element, PerlSubDefinitionElement.class);
    if (subDefinitionBase != null) {
        subName = subDefinitionBase.getSubName();
    }
    PerlMooseAugmentStatement augmentStatement = PsiTreeUtil.getParentOfType(element, PerlMooseAugmentStatement.class);
    if (augmentStatement != null) {
        subName = augmentStatement.getSubName();
    }
    if (subName != null) {
        PerlNamespaceDefinitionElement namespaceDefinition = PsiTreeUtil.getParentOfType(element, PerlNamespaceDefinitionElement.class);
        Set<PerlNamespaceDefinitionElement> recursionSet = new THashSet<>();
        if (StringUtil.isNotEmpty(subName) && namespaceDefinition != null) {
            collectNamespaceMethodsAugmentations(namespaceDefinition, subName, recursionSet, result);
        }
    }
    return result.toArray(new ResolveResult[result.size()]);
}
Also used : PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) ArrayList(java.util.ArrayList) PerlMooseAugmentStatement(com.perl5.lang.perl.parser.moose.psi.PerlMooseAugmentStatement) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) PerlSubDefinitionElement(com.perl5.lang.perl.psi.PerlSubDefinitionElement) PsiElement(com.intellij.psi.PsiElement) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PerlNamespaceDefinitionElement

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

the class PerlStructureViewElement method getChildren.

@NotNull
@Override
public TreeElement[] getChildren() {
    List<TreeElement> result = new ArrayList<>();
    Set<String> implementedMethods = new HashSet<>();
    if (myElement instanceof PerlFile) {
        FileViewProvider viewProvider = ((PerlFile) myElement).getViewProvider();
        PsiFile podFile = viewProvider.getPsi(PodLanguage.INSTANCE);
        if (podFile != null && podFile.getChildren().length > 1) {
            result.add(new PodStructureViewElement(podFile));
        }
        Language targetLanguage = null;
        for (Language language : viewProvider.getLanguages()) {
            if (language == PerlLanguage.INSTANCE) {
                targetLanguage = language;
                break;
            } else if (targetLanguage == null && language.isKindOf(PerlLanguage.INSTANCE)) {
                targetLanguage = language;
            }
        }
        if (targetLanguage != null) {
            viewProvider.getPsi(targetLanguage).accept(new PerlRecursiveVisitor() {

                @Override
                public void visitNamespaceDefinitionElement(@NotNull PerlNamespaceDefinitionElement o) {
                    result.add(new PerlNamespaceStructureViewElement(o));
                    super.visitNamespaceDefinitionElement(o);
                }
            });
        }
    }
    if (myElement instanceof PerlNamespaceDefinitionElement) {
        // global variables
        for (PerlVariableDeclarationElement child : PsiTreeUtil.findChildrenOfType(myElement, PerlVariableDeclarationElement.class)) {
            if (child.isGlobalDeclaration() && myElement.isEquivalentTo(PerlPackageUtil.getNamespaceContainerForElement(child))) {
                result.add(new PerlVariableDeclarationStructureViewElement(child));
            }
        }
        Project project = myElement.getProject();
        GlobalSearchScope projectScope = GlobalSearchScope.projectScope(project);
        // imported scalars
        for (PerlExportDescriptor exportDescritptor : ((PerlNamespaceDefinitionElement) myElement).getImportedScalarDescriptors()) {
            String canonicalName = exportDescritptor.getTargetCanonicalName();
            Collection<PerlVariableDeclarationElement> variables = PerlScalarUtil.getGlobalScalarDefinitions(project, canonicalName);
            for (PerlVariableDeclarationElement variable : variables) {
                result.add(new PerlVariableDeclarationStructureViewElement(variable).setImported(exportDescritptor));
            }
            // globs
            Collection<PsiPerlGlobVariable> items = PerlGlobUtil.getGlobsDefinitions(project, canonicalName, projectScope);
            if (items.isEmpty()) {
                items = PerlGlobUtil.getGlobsDefinitions(project, canonicalName);
            }
            for (PerlGlobVariable item : items) {
                result.add(new PerlGlobStructureViewElement(item).setImported(exportDescritptor));
            }
        }
        // imported arrays
        for (PerlExportDescriptor exportDescritptor : ((PerlNamespaceDefinitionElement) myElement).getImportedArrayDescriptors()) {
            String canonicalName = exportDescritptor.getTargetCanonicalName();
            Collection<PerlVariableDeclarationElement> variables = PerlArrayUtil.getGlobalArrayDefinitions(project, canonicalName);
            for (PerlVariableDeclarationElement variable : variables) {
                result.add(new PerlVariableDeclarationStructureViewElement(variable).setImported(exportDescritptor));
            }
            // globs
            Collection<PsiPerlGlobVariable> items = PerlGlobUtil.getGlobsDefinitions(project, canonicalName, projectScope);
            if (items.isEmpty()) {
                items = PerlGlobUtil.getGlobsDefinitions(project, canonicalName);
            }
            for (PerlGlobVariable item : items) {
                result.add(new PerlGlobStructureViewElement(item).setImported(exportDescritptor));
            }
        }
        // imported hashes
        for (PerlExportDescriptor exportDescritptor : ((PerlNamespaceDefinitionElement) myElement).getImportedHashDescriptors()) {
            String canonicalName = exportDescritptor.getTargetCanonicalName();
            Collection<PerlVariableDeclarationElement> variables = PerlHashUtil.getGlobalHashDefinitions(project, canonicalName);
            for (PerlVariableDeclarationElement variable : variables) {
                result.add(new PerlVariableDeclarationStructureViewElement(variable).setImported(exportDescritptor));
            }
            // globs
            Collection<PsiPerlGlobVariable> items = PerlGlobUtil.getGlobsDefinitions(project, canonicalName, projectScope);
            if (items.isEmpty()) {
                items = PerlGlobUtil.getGlobsDefinitions(project, canonicalName);
            }
            for (PerlGlobVariable item : items) {
                result.add(new PerlGlobStructureViewElement(item).setImported(exportDescritptor));
            }
        }
        // Imported subs
        for (PerlExportDescriptor exportDescritptor : ((PerlNamespaceDefinitionElement) myElement).getImportedSubsDescriptors()) {
            String canonicalName = exportDescritptor.getTargetCanonicalName();
            // declarations
            Collection<PerlSubDeclarationElement> subDeclarations = PerlSubUtil.getSubDeclarations(project, canonicalName, projectScope);
            if (subDeclarations.isEmpty()) {
                subDeclarations = PerlSubUtil.getSubDeclarations(project, canonicalName);
            }
            for (PerlSubDeclarationElement item : subDeclarations) {
                result.add(new PerlSubStructureViewElement(item).setImported(exportDescritptor));
            }
            // definitions
            Collection<PerlSubDefinitionElement> subDefinitions = PerlSubUtil.getSubDefinitions(project, canonicalName, projectScope);
            if (subDefinitions.isEmpty()) {
                subDefinitions = PerlSubUtil.getSubDefinitions(project, canonicalName);
            }
            for (PerlSubDefinitionElement item : subDefinitions) {
                result.add(new PerlSubStructureViewElement(item).setImported(exportDescritptor));
            }
            // globs
            Collection<PsiPerlGlobVariable> items = PerlGlobUtil.getGlobsDefinitions(project, canonicalName, projectScope);
            if (items.isEmpty()) {
                items = PerlGlobUtil.getGlobsDefinitions(project, canonicalName);
            }
            for (PerlGlobVariable item : items) {
                result.add(new PerlGlobStructureViewElement(item).setImported(exportDescritptor));
            }
        }
        myElement.accept(new PerlRecursiveVisitor() {

            @Override
            public void visitPerlSubDefinitionElement(@NotNull PerlSubDefinitionElement child) {
                if (myElement.isEquivalentTo(PerlPackageUtil.getNamespaceContainerForElement(child))) {
                    implementedMethods.add(child.getName());
                    result.add(new PerlSubStructureViewElement(child));
                }
                super.visitPerlSubDefinitionElement(child);
            }

            @Override
            public void visitSubDeclarationElement(@NotNull PerlSubDeclarationElement child) {
                if (myElement.isEquivalentTo(PerlPackageUtil.getNamespaceContainerForElement(child))) {
                    result.add(new PerlSubStructureViewElement(child));
                }
                super.visitSubDeclarationElement(child);
            }

            @Override
            public void visitGlobVariable(@NotNull PsiPerlGlobVariable child) {
                if (child.isLeftSideOfAssignment() && myElement.isEquivalentTo(PerlPackageUtil.getNamespaceContainerForElement(child))) {
                    implementedMethods.add(child.getName());
                    result.add(new PerlGlobStructureViewElement(child));
                }
                super.visitGlobVariable(child);
            }
        });
    }
    // inherited elements
    if (myElement instanceof PerlNamespaceDefinitionWithIdentifier) {
        List<TreeElement> inheritedResult = new ArrayList<>();
        String packageName = ((PerlNamespaceDefinitionElement) myElement).getPackageName();
        if (packageName != null) {
            for (PsiElement element : PerlMro.getVariants(myElement, packageName, true)) {
                if (element instanceof PerlIdentifierOwner && !implementedMethods.contains(((PerlIdentifierOwner) element).getName())) {
                    if (element instanceof PerlLightConstantDefinitionElement) {
                        inheritedResult.add(new PerlSubStructureViewElement((PerlSubDefinitionElement) element).setInherited());
                    } else if (element instanceof PerlSubDefinitionElement) {
                        inheritedResult.add(new PerlSubStructureViewElement((PerlSubDefinitionElement) element).setInherited());
                    } else if (element instanceof PerlSubDeclarationElement) {
                        inheritedResult.add(new PerlSubStructureViewElement((PerlSubDeclarationElement) element).setInherited());
                    } else if (element instanceof PerlGlobVariable && ((PerlGlobVariable) element).isLeftSideOfAssignment() && ((PerlGlobVariable) element).getName() != null) {
                        inheritedResult.add(new PerlGlobStructureViewElement((PerlGlobVariable) element).setInherited());
                    }
                }
            }
        }
        if (!inheritedResult.isEmpty()) {
            result.addAll(0, inheritedResult);
        }
    }
    return result.toArray(new TreeElement[result.size()]);
}
Also used : PerlExportDescriptor(com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) PerlLanguage(com.perl5.lang.perl.PerlLanguage) PodLanguage(com.perl5.lang.pod.PodLanguage) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) TreeElement(com.intellij.ide.util.treeView.smartTree.TreeElement) StructureViewTreeElement(com.intellij.ide.structureView.StructureViewTreeElement) SortableTreeElement(com.intellij.ide.util.treeView.smartTree.SortableTreeElement) Project(com.intellij.openapi.project.Project) PerlLightConstantDefinitionElement(com.perl5.lang.perl.parser.constant.psi.light.PerlLightConstantDefinitionElement) PodStructureViewElement(com.perl5.lang.pod.idea.structureView.PodStructureViewElement) PerlIdentifierOwner(com.perl5.lang.perl.psi.properties.PerlIdentifierOwner) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with PerlNamespaceDefinitionElement

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

the class PerlMroDfs method getLinearISA.

/**
 * Builds list of inheritance path for DFS mro (Perl5 default): http://perldoc.perl.org/mro.html
 *
 * @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 DFS for " + packageNames);
    ProgressManager.checkCanceled();
    for (PerlNamespaceDefinitionElement namespaceDefinition : namespaceDefinitions) {
        String packageName = namespaceDefinition.getPackageName();
        if (!recursionMap.contains(packageName)) {
            recursionMap.add(packageName);
            result.add(packageName);
            namespaceDefinition.getLinearISA(recursionMap, result);
        }
    }
}
Also used : PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)

Example 9 with PerlNamespaceDefinitionElement

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

the class MasonNamespaceDefinitionImpl method getChildNamespaceDefinitions.

@NotNull
@Override
public List<PerlNamespaceDefinitionElement> getChildNamespaceDefinitions() {
    MasonSettings masonSettings = MasonSettings.getInstance(getProject());
    final List<PerlNamespaceDefinitionElement> childNamespaces = new ArrayList<>();
    // collect psi children
    final Project project = getProject();
    final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(project);
    final String componentPath = getComponentPath();
    if (componentPath != null) {
        final List<String> relativePaths = new ArrayList<>();
        final List<String> exactPaths = new ArrayList<>();
        StubIndex.getInstance().processAllKeys(MasonParentNamespacesStubIndex.KEY, project, parentPath -> {
            if (// absolute path, should be equal
            parentPath.charAt(0) == VfsUtil.VFS_SEPARATOR_CHAR) {
                if (componentPath.equals(parentPath.substring(1))) {
                    exactPaths.add(parentPath);
                }
            } else if (// relative path
            componentPath.endsWith(parentPath)) {
                relativePaths.add(parentPath);
            }
            return true;
        });
        for (String parentPath : exactPaths) {
            childNamespaces.addAll(StubIndex.getElements(MasonParentNamespacesStubIndex.KEY, parentPath, project, projectScope, MasonNamespaceDefinition.class));
        }
        for (String parentPath : relativePaths) {
            for (MasonNamespaceDefinition masonNamespaceDefinition : StubIndex.getElements(MasonParentNamespacesStubIndex.KEY, parentPath, project, projectScope, MasonNamespaceDefinition.class)) {
                if (masonNamespaceDefinition.getParentNamespaceDefinitions().contains(MasonNamespaceDefinitionImpl.this)) {
                    childNamespaces.add(masonNamespaceDefinition);
                }
            }
        }
    }
    // collect autobased children
    if (masonSettings.autobaseNames.contains(getContainingFile().getName())) {
        VirtualFile containingFile = MasonCoreUtil.getContainingVirtualFile(getContainingFile());
        if (containingFile != null) {
            final String basePath = VfsUtil.getRelativePath(containingFile.getParent(), getProject().getBaseDir());
            if (basePath != null) {
                final List<String> componentPaths = new ArrayList<>();
                StubIndex.getInstance().processAllKeys(MasonNamespaceDefitnitionsStubIndex.KEY, getProject(), componentPath1 -> {
                    if (componentPath1.startsWith(basePath)) {
                        componentPaths.add(componentPath1);
                    }
                    return true;
                });
                for (String compoPath : componentPaths) {
                    for (MasonNamespaceDefinition namespaceDefinition : StubIndex.getElements(MasonNamespaceDefitnitionsStubIndex.KEY, compoPath, project, projectScope, MasonNamespaceDefinition.class)) {
                        if (namespaceDefinition.getParentNamespaceDefinitions().contains(MasonNamespaceDefinitionImpl.this) && !childNamespaces.contains(namespaceDefinition)) {
                            childNamespaces.add(namespaceDefinition);
                        }
                    }
                }
            }
        }
    }
    return childNamespaces;
}
Also used : MasonSettings(com.perl5.lang.mason2.idea.configuration.MasonSettings) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) MasonNamespaceDefinition(com.perl5.lang.mason2.psi.MasonNamespaceDefinition) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with PerlNamespaceDefinitionElement

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

the class PerlPackageProcessorBase method addExports.

public void addExports(@NotNull PerlUseStatement useStatement, @NotNull Set<String> export, @NotNull Set<String> exportOk) {
    String packageName = useStatement.getPackageName();
    if (StringUtil.isEmpty(packageName)) {
        return;
    }
    // fixme handle tags
    for (PerlNamespaceDefinitionElement namespaceDefinition : PerlPackageUtil.getNamespaceDefinitions(useStatement.getProject(), packageName)) {
        export.addAll(namespaceDefinition.getEXPORT());
        exportOk.addAll(namespaceDefinition.getEXPORT_OK());
    }
    exportOk.addAll(export);
}
Also used : PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement)

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