Search in sources :

Example 61 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class PackageViewPane method getSelectedDirectories.

@NotNull
@Override
public PsiDirectory[] getSelectedDirectories() {
    List<PsiDirectory> directories = ContainerUtil.newArrayList();
    for (PackageElementNode node : getSelectedNodes(PackageElementNode.class)) {
        PackageElement packageElement = node.getValue();
        PsiPackage aPackage = packageElement != null ? packageElement.getPackage() : null;
        final Module module = packageElement != null ? packageElement.getModule() : null;
        if (aPackage != null && module != null) {
            GlobalSearchScope scope = GlobalSearchScope.moduleScope(module);
            Collections.addAll(directories, aPackage.getDirectories(scope));
            if (Registry.is("projectView.choose.directory.on.compacted.middle.packages")) {
                Object parentValue = node.getParent().getValue();
                PsiPackage parentNodePackage = parentValue instanceof PackageElement ? ((PackageElement) parentValue).getPackage() : null;
                while (true) {
                    aPackage = aPackage.getParentPackage();
                    if (aPackage == null || aPackage.getQualifiedName().isEmpty() || aPackage.equals(parentNodePackage)) {
                        break;
                    }
                    Collections.addAll(directories, aPackage.getDirectories(scope));
                }
            }
        }
    }
    if (!directories.isEmpty()) {
        return directories.toArray(new PsiDirectory[directories.size()]);
    }
    return super.getSelectedDirectories();
}
Also used : PackageElementNode(com.intellij.ide.projectView.impl.nodes.PackageElementNode) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiDirectory(com.intellij.psi.PsiDirectory) PsiPackage(com.intellij.psi.PsiPackage) PackageElement(com.intellij.ide.projectView.impl.nodes.PackageElement) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class PackageViewProjectNode method getChildren.

@Override
@NotNull
public Collection<AbstractTreeNode> getChildren() {
    if (getSettings().isShowModules()) {
        final List<Module> allModules = new ArrayList<>(Arrays.asList(ModuleManager.getInstance(getProject()).getModules()));
        for (Iterator<Module> it = allModules.iterator(); it.hasNext(); ) {
            final Module module = it.next();
            final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
            if (sourceRoots.length == 0) {
                // do not show modules with no source roots configured
                it.remove();
            }
        }
        return modulesAndGroups(allModules.toArray(new Module[allModules.size()]));
    } else {
        final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
        final PsiManager psiManager = PsiManager.getInstance(myProject);
        final List<AbstractTreeNode> children = new ArrayList<>();
        final Set<PsiPackage> topLevelPackages = new HashSet<>();
        for (final VirtualFile root : projectRootManager.getContentSourceRoots()) {
            final PsiDirectory directory = psiManager.findDirectory(root);
            if (directory == null) {
                continue;
            }
            final PsiPackage directoryPackage = JavaDirectoryService.getInstance().getPackage(directory);
            if (directoryPackage == null || PackageUtil.isPackageDefault(directoryPackage)) {
                // add subpackages
                final PsiDirectory[] subdirectories = directory.getSubdirectories();
                for (PsiDirectory subdirectory : subdirectories) {
                    final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(subdirectory);
                    if (aPackage != null && !PackageUtil.isPackageDefault(aPackage)) {
                        topLevelPackages.add(aPackage);
                    }
                }
                // add non-dir items
                children.addAll(ProjectViewDirectoryHelper.getInstance(myProject).getDirectoryChildren(directory, getSettings(), false));
            } else {
                // this is the case when a source root has pakage prefix assigned
                topLevelPackages.add(directoryPackage);
            }
        }
        for (final PsiPackage psiPackage : topLevelPackages) {
            PackageUtil.addPackageAsChild(children, psiPackage, null, getSettings(), false);
        }
        if (getSettings().isShowLibraryContents()) {
            children.add(new PackageViewLibrariesNode(getProject(), null, getSettings()));
        }
        return children;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiPackage(com.intellij.psi.PsiPackage) PsiDirectory(com.intellij.psi.PsiDirectory) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull)

Example 63 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class PackageElementNode method updateValidData.

private void updateValidData(PresentationData presentation, PackageElement value) {
    final PsiPackage aPackage = value.getPackage();
    if (!getSettings().isFlattenPackages() && getSettings().isHideEmptyMiddlePackages() && PackageUtil.isPackageEmpty(aPackage, value.getModule(), true, isLibraryElement())) {
        setValue(null);
        return;
    }
    PsiPackage parentPackage;
    Object parentValue = getParentValue();
    if (parentValue instanceof PackageElement) {
        parentPackage = ((PackageElement) parentValue).getPackage();
    } else {
        parentPackage = null;
    }
    String qName = aPackage.getQualifiedName();
    String name = PackageUtil.getNodeName(getSettings(), aPackage, parentPackage, qName, showFQName(aPackage));
    presentation.setPresentableText(name);
    presentation.setIcon(PlatformIcons.PACKAGE_ICON);
    for (ProjectViewNodeDecorator decorator : Extensions.getExtensions(ProjectViewNodeDecorator.EP_NAME, myProject)) {
        decorator.decorate(this, presentation);
    }
}
Also used : ProjectViewNodeDecorator(com.intellij.ide.projectView.ProjectViewNodeDecorator) PsiPackage(com.intellij.psi.PsiPackage)

Example 64 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class MovePackageTest method performAction.

private void performAction(String[] packageNames, String newPackageName) throws Exception {
    final PsiPackage[] packages = new PsiPackage[packageNames.length];
    for (int i = 0; i < packages.length; i++) {
        String packageName = packageNames[i];
        packages[i] = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage(packageName);
        assertNotNull("Package " + packageName + " not found", packages[i]);
    }
    PsiPackage newParentPackage = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage(newPackageName);
    assertNotNull(newParentPackage);
    final PsiDirectory[] dirs = newParentPackage.getDirectories();
    assertEquals(dirs.length, 1);
    new MoveClassesOrPackagesProcessor(myProject, packages, new SingleSourceRootMoveDestination(PackageWrapper.create(newParentPackage), dirs[0]), true, false, null).run();
    FileDocumentManager.getInstance().saveAllDocuments();
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) SingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination) PsiPackage(com.intellij.psi.PsiPackage) MoveClassesOrPackagesProcessor(com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor)

Example 65 with PsiPackage

use of com.intellij.psi.PsiPackage in project intellij-community by JetBrains.

the class ResetConfigurationModuleAdapter method tryWithAnotherModule.

public static <T extends ModuleBasedConfiguration<JavaRunConfigurationModule> & CommonJavaRunConfigurationParameters> boolean tryWithAnotherModule(T configuration, boolean isDebug) {
    final String packageName = configuration.getPackage();
    if (packageName == null)
        return false;
    final Project project = configuration.getProject();
    final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName);
    if (aPackage == null)
        return false;
    final Module module = configuration.getConfigurationModule().getModule();
    if (module == null)
        return false;
    final Set<Module> modulesWithPackage = new HashSet<>();
    final PsiDirectory[] directories = aPackage.getDirectories();
    for (PsiDirectory directory : directories) {
        final Module currentModule = ModuleUtilCore.findModuleForFile(directory.getVirtualFile(), project);
        if (module != currentModule && currentModule != null) {
            modulesWithPackage.add(currentModule);
        }
    }
    if (!modulesWithPackage.isEmpty()) {
        final String testRunDebugId = isDebug ? ToolWindowId.DEBUG : ToolWindowId.RUN;
        final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        final Function<Module, String> moduleNameRef = module1 -> {
            final String moduleName = module1.getName();
            return "<a href=\"" + moduleName + "\">" + moduleName + "</a>";
        };
        String message = "Tests were not found in module \"" + module.getName() + "\".\n" + "Use ";
        if (modulesWithPackage.size() == 1) {
            message += "module \"" + moduleNameRef.fun(modulesWithPackage.iterator().next()) + "\" ";
        } else {
            message += "one of\n" + StringUtil.join(modulesWithPackage, moduleNameRef, "\n") + "\n";
        }
        message += "instead";
        toolWindowManager.notifyByBalloon(testRunDebugId, MessageType.WARNING, message, null, new ResetConfigurationModuleAdapter(configuration, project, isDebug, toolWindowManager, testRunDebugId));
        return true;
    }
    return false;
}
Also used : PsiPackage(com.intellij.psi.PsiPackage) ToolWindowManager(com.intellij.openapi.wm.ToolWindowManager) MessageType(com.intellij.openapi.ui.MessageType) HyperlinkEvent(javax.swing.event.HyperlinkEvent) ExecutionException(com.intellij.execution.ExecutionException) ModuleManager(com.intellij.openapi.module.ModuleManager) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) ModuleBasedConfiguration(com.intellij.execution.configurations.ModuleBasedConfiguration) Balloon(com.intellij.openapi.ui.popup.Balloon) HashSet(java.util.HashSet) ToolWindowId(com.intellij.openapi.wm.ToolWindowId) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) DefaultDebugExecutor(com.intellij.execution.executors.DefaultDebugExecutor) HyperlinkAdapter(com.intellij.ui.HyperlinkAdapter) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) StringUtil(com.intellij.openapi.util.text.StringUtil) Set(java.util.Set) Executor(com.intellij.execution.Executor) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) JavaRunConfigurationModule(com.intellij.execution.configurations.JavaRunConfigurationModule) CommonJavaRunConfigurationParameters(com.intellij.execution.CommonJavaRunConfigurationParameters) Function(com.intellij.util.Function) PsiDirectory(com.intellij.psi.PsiDirectory) Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) ToolWindowManager(com.intellij.openapi.wm.ToolWindowManager) PsiPackage(com.intellij.psi.PsiPackage) Module(com.intellij.openapi.module.Module) JavaRunConfigurationModule(com.intellij.execution.configurations.JavaRunConfigurationModule) HashSet(java.util.HashSet)

Aggregations

PsiPackage (com.intellij.psi.PsiPackage)79 PsiDirectory (com.intellij.psi.PsiDirectory)17 Module (com.intellij.openapi.module.Module)16 PsiClass (com.intellij.psi.PsiClass)16 JavaAnalysisScope (com.intellij.analysis.JavaAnalysisScope)12 Project (com.intellij.openapi.project.Project)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)10 NotNull (org.jetbrains.annotations.NotNull)10 CyclicDependenciesBuilder (com.intellij.cyclicDependencies.CyclicDependenciesBuilder)9 PsiFile (com.intellij.psi.PsiFile)9 AnalysisScope (com.intellij.analysis.AnalysisScope)8 PsiElement (com.intellij.psi.PsiElement)8 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)6 HashSet (java.util.HashSet)6 Nullable (org.jetbrains.annotations.Nullable)6 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)4 PackageElement (com.intellij.ide.projectView.impl.nodes.PackageElement)3 List (java.util.List)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2