Search in sources :

Example 1 with PerlNamespaceDefinitionWithIdentifier

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

the class PerlPsiUtil method processNamespaceStatements.

public static boolean processNamespaceStatements(@NotNull PsiElement rootElement, Processor<PsiElement> processor) {
    PsiElement run = rootElement.getFirstChild();
    boolean result = true;
    while (run != null && result) {
        if (!(run instanceof PerlNamespaceDefinitionWithIdentifier)) {
            if (run instanceof PerlCompositeElement) {
                result = processor.process(run);
            }
            if (result && run instanceof PerlStatementsContainer) {
                result = processNamespaceStatements(run, processor);
            }
        }
        run = run.getNextSibling();
    }
    return result;
}
Also used : PerlStatementsContainer(com.perl5.lang.perl.psi.properties.PerlStatementsContainer) ASTWrapperPsiElement(com.intellij.extapi.psi.ASTWrapperPsiElement)

Example 2 with PerlNamespaceDefinitionWithIdentifier

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

the class PerlRenameNamespaceDefinitionProcessor method getPostRenameCallback.

@Nullable
@Override
public Runnable getPostRenameCallback(PsiElement element, final String newName, RefactoringElementListener elementListener) {
    if (element instanceof PerlNamespaceDefinitionWithIdentifier && isFileToBeRenamed((PerlNamespaceDefinitionWithIdentifier) element)) {
        final PsiFile file = element.getContainingFile();
        return new Runnable() {

            @Override
            public void run() {
                for (PsiReference reference : ReferencesSearch.search(file, file.getUseScope()).findAll()) {
                    PerlPsiUtil.renameElement(reference.getElement(), newName);
                }
                // rename file
                String newPackageName = PerlPackageUtil.getCanonicalPackageName(newName);
                List<String> newPackageChunks = Arrays.asList(newPackageName.split(PerlPackageUtil.PACKAGE_SEPARATOR));
                String newFileName = newPackageChunks.get(newPackageChunks.size() - 1) + ".pm";
                file.setName(newFileName);
                // move file
                VirtualFile containingDir = file.getVirtualFile().getParent();
                VirtualFile newContainingDir = PerlUtil.getFileClassRoot(file.getProject(), containingDir);
                for (int i = 0; i < newPackageChunks.size() - 1; i++) {
                    String subDirName = newPackageChunks.get(i);
                    assert subDirName != null && !subDirName.isEmpty();
                    assert newContainingDir != null;
                    VirtualFile subDir = newContainingDir.findChild(subDirName);
                    try {
                        newContainingDir = subDir != null ? subDir : newContainingDir.createChildDirectory(null, subDirName);
                    } catch (IOException e) {
                        throw new IncorrectOperationException("Could not create subdirectory: " + newContainingDir.getPath() + "/" + subDirName);
                    }
                }
                if (newContainingDir != null && !newContainingDir.equals(containingDir)) {
                    try {
                        file.getVirtualFile().move(this, newContainingDir);
                    } catch (IOException e) {
                        throw new IncorrectOperationException("Could not move package file to the: " + newContainingDir.getPath());
                    }
                }
            }
        };
    }
    return super.getPostRenameCallback(element, newName, elementListener);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) PerlNamespaceDefinitionWithIdentifier(com.perl5.lang.perl.psi.PerlNamespaceDefinitionWithIdentifier) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PerlNamespaceDefinitionWithIdentifier

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

the class PerlMultipleNamespaceDefinitionsInspection method buildVisitor.

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

        @Override
        public void visitPerlNamespaceDefinitionWithIdentifier(@NotNull PerlNamespaceDefinitionWithIdentifier o) {
            Project project = o.getProject();
            String packageName = o.getPackageName();
            if (packageName != null && !PerlPackageUtil.MAIN_PACKAGE.equals(packageName) && PerlPackageUtil.getNamespaceDefinitions(project, o.getPackageName(), GlobalSearchScope.projectScope(project)).size() > 1 && o.getNameIdentifier() != null) {
                registerProblem(holder, o.getNameIdentifier(), "Multiple namespace definitions found");
            }
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) PerlVisitor(com.perl5.lang.perl.psi.PerlVisitor) NotNull(org.jetbrains.annotations.NotNull) PerlNamespaceDefinitionWithIdentifier(com.perl5.lang.perl.psi.PerlNamespaceDefinitionWithIdentifier) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PerlNamespaceDefinitionWithIdentifier

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

the class PerlNamespaceRecursiveInheritanceInspection method buildVisitor.

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

        @Override
        public void visitPerlNamespaceDefinitionWithIdentifier(@NotNull PerlNamespaceDefinitionWithIdentifier o) {
            PsiElement nameIdentifier = o.getNameIdentifier();
            if (nameIdentifier == null) {
                return;
            }
            String packageName = o.getPackageName();
            if (PerlPackageUtil.MAIN_PACKAGE.equals(packageName)) {
                return;
            }
            if (hasRecursiveInheritance(o, new THashSet<>())) {
                registerError(holder, o.getContainingFile(), "Namespace " + packageName + " has recursive inheritance");
                registerError(holder, nameIdentifier, "Namespace " + packageName + " has recursive inheritance");
            }
        }
    };
}
Also used : PerlVisitor(com.perl5.lang.perl.psi.PerlVisitor) NotNull(org.jetbrains.annotations.NotNull) PerlNamespaceDefinitionWithIdentifier(com.perl5.lang.perl.psi.PerlNamespaceDefinitionWithIdentifier) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PerlNamespaceDefinitionWithIdentifier

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

Aggregations

PerlNamespaceDefinitionWithIdentifier (com.perl5.lang.perl.psi.PerlNamespaceDefinitionWithIdentifier)5 PsiElement (com.intellij.psi.PsiElement)4 NotNull (org.jetbrains.annotations.NotNull)4 PerlVisitor (com.perl5.lang.perl.psi.PerlVisitor)3 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2 PsiReference (com.intellij.psi.PsiReference)2 ASTWrapperPsiElement (com.intellij.extapi.psi.ASTWrapperPsiElement)1 StructureViewTreeElement (com.intellij.ide.structureView.StructureViewTreeElement)1 SortableTreeElement (com.intellij.ide.util.treeView.smartTree.SortableTreeElement)1 TreeElement (com.intellij.ide.util.treeView.smartTree.TreeElement)1 Language (com.intellij.lang.Language)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 FileViewProvider (com.intellij.psi.FileViewProvider)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 IElementType (com.intellij.psi.tree.IElementType)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1