Search in sources :

Example 1 with PerlNamespaceDefinitionElement

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

the class MasonNamespaceDefinitionImpl method getParentNamespaceDefinitions.

@Override
public List<PerlNamespaceDefinitionElement> getParentNamespaceDefinitions() {
    List<String> parentsPaths;
    PerlNamespaceDefinitionStub stub = getStub();
    if (stub != null) {
        parentsPaths = stub.getParentNamespacesNames();
    } else {
        parentsPaths = getParentNamespacesNamesFromPsi();
    }
    VirtualFile containingFile = MasonCoreUtil.getContainingVirtualFile(getContainingFile());
    List<PerlNamespaceDefinitionElement> parentsNamespaces;
    if (!parentsPaths.isEmpty() && containingFile != null) {
        parentsNamespaces = Mason2Util.collectComponentNamespacesByPaths(getProject(), parentsPaths, containingFile.getParent());
    } else {
        String autobaseParent = getParentNamespaceFromAutobase();
        if (autobaseParent != null) {
            parentsNamespaces = Mason2Util.getMasonNamespacesByAbsolutePath(getProject(), autobaseParent);
        } else {
            parentsNamespaces = new ArrayList<>();
        }
    }
    if (parentsNamespaces.isEmpty()) {
        parentsNamespaces.addAll(PerlPackageUtil.getNamespaceDefinitions(getProject(), MASON_DEFAULT_COMPONENT_PARENT));
    }
    return parentsNamespaces;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PerlNamespaceDefinitionStub(com.perl5.lang.perl.psi.stubs.namespaces.PerlNamespaceDefinitionStub)

Example 2 with PerlNamespaceDefinitionElement

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

the class Mason2Util method collectComponentNamespacesByPaths.

@NotNull
public static List<PerlNamespaceDefinitionElement> collectComponentNamespacesByPaths(@NotNull Project project, @NotNull List<String> componentPaths, @NotNull VirtualFile anchorDir) {
    List<PerlNamespaceDefinitionElement> result = new ArrayList<>();
    MasonSettings masonSettings = MasonSettings.getInstance(project);
    for (String componentPath : componentPaths) {
        VirtualFile componentFile = null;
        if (componentPath.startsWith(// abs path relative to mason roots, see the Mason::Interp::_determine_parent_compc
        "" + VfsUtil.VFS_SEPARATOR_CHAR)) {
            for (VirtualFile componentRoot : masonSettings.getComponentsRoots()) {
                componentFile = componentRoot.findFileByRelativePath(componentPath.substring(1));
                if (componentFile != null) {
                    break;
                }
            }
        } else // relative path
        {
            componentFile = anchorDir.findFileByRelativePath(componentPath);
        }
        if (componentFile != null) {
            String absolutePath = VfsUtil.getRelativePath(componentFile, project.getBaseDir());
            if (absolutePath != null) {
                result.addAll(Mason2Util.getMasonNamespacesByAbsolutePath(project, absolutePath));
            }
        }
    }
    return result;
}
Also used : MasonSettings(com.perl5.lang.mason2.idea.configuration.MasonSettings) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PerlNamespaceDefinitionElement

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

the class PerlPackageUtil method getContextPackageName.

/**
 * Searching of namespace element is in. If no explicit namespaces defined, main is returned
 *
 * @param element psi element to find definition for
 * @return canonical package name
 */
@Contract("null -> null")
public static String getContextPackageName(@Nullable PsiElement element) {
    if (element == null) {
        return null;
    }
    PerlNamespaceDefinitionElement namespaceDefinition = getContainingNamespace(element);
    if (// checking that definition is valid and got namespace
    namespaceDefinition != null && namespaceDefinition.getPackageName() != null) {
        String name = namespaceDefinition.getPackageName();
        assert name != null;
        return name;
    }
    // default value
    PsiFile file = element.getContainingFile();
    if (file instanceof PerlFileImpl) {
        PsiElement contextParent = file.getContext();
        PsiElement realParent = file.getParent();
        if (contextParent != null && !contextParent.equals(realParent)) {
            return getContextPackageName(contextParent);
        }
        return ((PerlFileImpl) file).getPackageName();
    } else {
        return MAIN_PACKAGE;
    }
}
Also used : PerlFileImpl(com.perl5.lang.perl.psi.impl.PerlFileImpl) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Contract(org.jetbrains.annotations.Contract)

Example 4 with PerlNamespaceDefinitionElement

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

the class PerlPackageUtil method collectNestedPackageDefinitionsFromFile.

/**
 * Adds to queue netsted namespaces, which names should be adjusted to the new package name/path
 *
 * @param queue   - RenameRefactoringQueue
 * @param file    - file has been moved
 * @param oldPath - previous filepath
 */
public static void collectNestedPackageDefinitionsFromFile(@NotNull RenameRefactoringQueue queue, VirtualFile file, String oldPath) {
    Project project = queue.getProject();
    VirtualFile newInnermostRoot = PerlUtil.getFileClassRoot(project, file);
    if (newInnermostRoot != null) {
        String newRelativePath = VfsUtil.getRelativePath(file, newInnermostRoot);
        String newPackageName = getPackageNameByPath(newRelativePath);
        VirtualFile oldInnermostRoot = PerlUtil.getFileClassRoot(project, oldPath);
        if (oldInnermostRoot != null) {
            String oldRelativePath = oldPath.substring(oldInnermostRoot.getPath().length());
            String oldPackageName = getPackageNameByPath(oldRelativePath);
            if (!oldPackageName.equals(newPackageName)) {
                PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
                if (psiFile != null) {
                    for (PerlNamespaceDefinitionElement namespaceDefinition : PsiTreeUtil.findChildrenOfType(psiFile, PerlNamespaceDefinitionElement.class)) {
                        if (oldPackageName.equals(namespaceDefinition.getPackageName())) {
                            queue.addElement(namespaceDefinition, newPackageName);
                        }
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PsiFile(com.intellij.psi.PsiFile)

Example 5 with PerlNamespaceDefinitionElement

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

the class PerlPackageUtil method processParentClassesSubs.

public static void processParentClassesSubs(PerlNamespaceDefinitionElement childClass, Set<String> processedSubsNames, Set<PerlNamespaceDefinitionElement> recursionMap, Processor<PerlSubElement> processor) {
    if (childClass == null || recursionMap.contains(childClass)) {
        return;
    }
    recursionMap.add(childClass);
    for (PerlNamespaceDefinitionElement parentNamespace : childClass.getParentNamespaceDefinitions()) {
        for (PsiElement subDefinitionBase : collectNamespaceSubs(parentNamespace)) {
            ProgressManager.checkCanceled();
            String subName = ((PerlSubElement) subDefinitionBase).getSubName();
            if (subDefinitionBase.isValid() && ((PerlSubElement) subDefinitionBase).isMethod() && !processedSubsNames.contains(subName)) {
                processedSubsNames.add(subName);
                processor.process(((PerlSubElement) subDefinitionBase));
            }
        }
        processParentClassesSubs(parentNamespace, processedSubsNames, recursionMap, processor);
    }
}
Also used : PerlNamespaceDefinitionElement(com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) PsiElement(com.intellij.psi.PsiElement)

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