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();
}
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;
}
}
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);
}
}
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();
}
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;
}
Aggregations