public void unregisterAll(String path, boolean under, boolean unregisterSources) { Url url = toUrl(path); for (ContentEntry eachEntry : myRootModel.getContentEntries()) { if (unregisterSources) { for (SourceFolder eachFolder : eachEntry.getSourceFolders()) { String ancestor = under ? url.getUrl() : eachFolder.getUrl(); String child = under ? eachFolder.getUrl() : url.getUrl(); if (isEqualOrAncestor(ancestor, child)) { eachEntry.removeSourceFolder(eachFolder); } } } for (ExcludeFolder eachFolder : eachEntry.getExcludeFolders()) { String ancestor = under ? url.getUrl() : eachFolder.getUrl(); String child = under ? eachFolder.getUrl() : url.getUrl(); if (isEqualOrAncestor(ancestor, child)) { if (eachFolder.isSynthetic()) { getCompilerExtension().setExcludeOutput(false); } else { eachEntry.removeExcludeFolder(eachFolder); } } } } }
@Override public void setSelected(final AnActionEvent e, final boolean isSelected) { final VirtualFile[] selectedFiles = getSelectedFiles(); assert selectedFiles.length != 0; final ContentEntryEditor contentEntryEditor = myEntryTreeEditor.getContentEntryEditor(); for (VirtualFile selectedFile : selectedFiles) { final SourceFolder sourceFolder = contentEntryEditor.getSourceFolder(selectedFile); if (isSelected) { if (sourceFolder == null) { // not marked yet contentEntryEditor.addSourceFolder(selectedFile, myEditTestSources, ""); } else { if (myEditTestSources != sourceFolder.isTestSource()) { final String packagePrefix = sourceFolder.getPackagePrefix(); contentEntryEditor.removeSourceFolder(sourceFolder); contentEntryEditor.addSourceFolder(selectedFile, myEditTestSources, packagePrefix); } } } else { if (sourceFolder != null) { // already marked contentEntryEditor.removeSourceFolder(sourceFolder); } } } }
public boolean hasRegisteredSourceSubfolder(File f) { String url = toUrl(f.getPath()).getUrl(); for (ContentEntry eachEntry : myRootModel.getContentEntries()) { for (SourceFolder eachFolder : eachEntry.getSourceFolders()) { if (isEqualOrAncestor(url, eachFolder.getUrl())) return true; } } return false; }
protected void assertGeneratedSources(String moduleName, String... expectedSources) { ContentEntry contentRoot = getContentRoot(moduleName); List<ContentFolder> folders = new ArrayList<ContentFolder>(); for (SourceFolder folder : contentRoot.getSourceFolders(JavaSourceRootType.SOURCE)) { JavaSourceRootProperties properties = folder.getJpsElement().getProperties(JavaSourceRootType.SOURCE); assertNotNull(properties); if (properties.isForGeneratedSources()) { folders.add(folder); } } doAssertContentFolders(contentRoot, folders, expectedSources); }
public static void removeSourceRoot(Module module, VirtualFile root) { ModuleRootModificationUtil.updateModel( module, model -> { ContentEntry entry = findContentEntryWithAssertion(model, root); for (SourceFolder sourceFolder : entry.getSourceFolders()) { if (root.equals(sourceFolder.getFile())) { entry.removeSourceFolder(sourceFolder); break; } } }); }
private static boolean isTestDirectory(final Module module, final PsiElement element) { final PsiDirectory dir = (PsiDirectory) element; final ModuleRootManager manager = ModuleRootManager.getInstance(module); final ContentEntry[] entries = manager.getContentEntries(); for (ContentEntry entry : entries) { for (SourceFolder folder : entry.getSourceFolders()) { if (folder.isTestSource() && folder.getFile() == dir.getVirtualFile()) { return true; } } } return false; }
private static void removeSourceRoot(@NotNull Module module, @NotNull final VirtualFile root) { final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); final ContentEntry contentEntry = findContentEntryForRoot(model, root); if (contentEntry != null) { for (SourceFolder sourceFolder : contentEntry.getSourceFolders()) { if (sourceFolder.getFile() == root) { contentEntry.removeSourceFolder(sourceFolder); } } } ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { model.commit(); } }); }
private static boolean changePackagePrefixes( PsiPackage psiPackage, final String oldQualifiedName, final String newQualifiedName) { final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules(); List<ModifiableRootModel> modelsToCommit = new ArrayList<ModifiableRootModel>(); for (final Module module : modules) { boolean anyChange = false; final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); for (final ContentEntry contentEntry : rootModel.getContentEntries()) { for (final SourceFolder sourceFolder : contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES)) { final String packagePrefix = sourceFolder.getPackagePrefix(); if (packagePrefix.startsWith(oldQualifiedName)) { sourceFolder.setPackagePrefix( newQualifiedName + packagePrefix.substring(oldQualifiedName.length())); anyChange = true; } } } if (anyChange) { modelsToCommit.add(rootModel); } else { rootModel.dispose(); } } if (!modelsToCommit.isEmpty()) { ModifiableRootModel[] rootModels = modelsToCommit.toArray(new ModifiableRootModel[modelsToCommit.size()]); if (rootModels.length > 0) { ModifiableModelCommitter.multiCommit( rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel()); } return true; } else { return false; } }
@Override public void findRelatedStepDefsRoots( @NotNull final Module module, @NotNull final PsiFile featureFile, @NotNull final List<PsiDirectory> newStepDefinitionsRoots, @NotNull final Set<String> processedStepDirectories) { final ContentEntry[] contentEntries = ModuleRootManager.getInstance(module).getContentEntries(); for (final ContentEntry contentEntry : contentEntries) { final SourceFolder[] sourceFolders = contentEntry.getSourceFolders(); for (SourceFolder sf : sourceFolders) { // ToDo: check if inside test folder VirtualFile sfDirectory = sf.getFile(); if (sfDirectory != null && sfDirectory.isDirectory()) { PsiDirectory sourceRoot = PsiDirectoryFactory.getInstance(module.getProject()).createDirectory(sfDirectory); if (!processedStepDirectories.contains(sourceRoot.getVirtualFile().getPath())) { newStepDefinitionsRoots.add(sourceRoot); } } } } }
public boolean hasCollision(String sourceRootPath) { Url url = toUrl(sourceRootPath); for (ContentEntry eachEntry : myRootModel.getContentEntries()) { for (SourceFolder eachFolder : eachEntry.getSourceFolders()) { String ancestor = url.getUrl(); String child = eachFolder.getUrl(); if (isEqualOrAncestor(ancestor, child) || isEqualOrAncestor(child, ancestor)) { return true; } } for (ExcludeFolder eachFolder : eachEntry.getExcludeFolders()) { String ancestor = url.getUrl(); String child = eachFolder.getUrl(); if (isEqualOrAncestor(ancestor, child) || isEqualOrAncestor(child, ancestor)) { return true; } } } return false; }
@Override public VirtualFile[] occursInPackagePrefixes(PsiPackage psiPackage) { List<VirtualFile> result = new ArrayList<VirtualFile>(); final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules(); for (final Module module : modules) { for (final ContentEntry contentEntry : ModuleRootManager.getInstance(module).getContentEntries()) { final List<SourceFolder> sourceFolders = contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES); for (final SourceFolder sourceFolder : sourceFolders) { final String packagePrefix = sourceFolder.getPackagePrefix(); if (packagePrefix.startsWith(psiPackage.getQualifiedName())) { final VirtualFile file = sourceFolder.getFile(); if (file != null) { result.add(file); } } } } } return VfsUtil.toVirtualFileArray(result); }
@NotNull private RootInfo buildRootInfo(@NotNull Project project) { final RootInfo info = new RootInfo(); for (final Module module : ModuleManager.getInstance(project).getModules()) { final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); for (final VirtualFile contentRoot : moduleRootManager.getContentRoots()) { if (!info.contentRootOf.containsKey(contentRoot)) { info.contentRootOf.put(contentRoot, module); } } for (ContentEntry contentEntry : moduleRootManager.getContentEntries()) { if (!(contentEntry instanceof ContentEntryImpl) || !((ContentEntryImpl) contentEntry).isDisposed()) { for (VirtualFile excludeRoot : contentEntry.getExcludeFolderFiles()) { info.excludedFromModule.put(excludeRoot, module); } } // Init module sources for (final SourceFolder sourceFolder : contentEntry.getSourceFolders()) { final VirtualFile sourceFolderRoot = sourceFolder.getFile(); if (sourceFolderRoot != null) { info.rootTypeId.put(sourceFolderRoot, getRootTypeId(sourceFolder.getRootType())); info.classAndSourceRoots.add(sourceFolderRoot); info.sourceRootOf.putValue(sourceFolderRoot, module); info.packagePrefix.put(sourceFolderRoot, sourceFolder.getPackagePrefix()); } } } for (OrderEntry orderEntry : moduleRootManager.getOrderEntries()) { if (orderEntry instanceof LibraryOrSdkOrderEntry) { final LibraryOrSdkOrderEntry entry = (LibraryOrSdkOrderEntry) orderEntry; final VirtualFile[] sourceRoots = entry.getRootFiles(OrderRootType.SOURCES); final VirtualFile[] classRoots = entry.getRootFiles(OrderRootType.CLASSES); // Init library sources for (final VirtualFile sourceRoot : sourceRoots) { info.classAndSourceRoots.add(sourceRoot); info.libraryOrSdkSources.add(sourceRoot); info.packagePrefix.put(sourceRoot, ""); } // init library classes for (final VirtualFile classRoot : classRoots) { info.classAndSourceRoots.add(classRoot); info.libraryOrSdkClasses.add(classRoot); info.packagePrefix.put(classRoot, ""); } if (orderEntry instanceof LibraryOrderEntry) { Library library = ((LibraryOrderEntry) orderEntry).getLibrary(); if (library != null) { for (VirtualFile root : ((LibraryEx) library).getExcludedRoots()) { info.excludedFromLibraries.putValue(root, library); } for (VirtualFile root : sourceRoots) { info.sourceOfLibraries.putValue(root, library); } for (VirtualFile root : classRoots) { info.classOfLibraries.putValue(root, library); } } } } } } for (DirectoryIndexExcludePolicy policy : Extensions.getExtensions(DirectoryIndexExcludePolicy.EP_NAME, project)) { Collections.addAll(info.excludedFromProject, policy.getExcludeRootsForProject()); } return info; }