PsiDirectory (@NotNull PsiDirectory directory, @Nullable Set<? extends JpsModuleSourceRootType<?>> rootTypes) { ProjectFileIndex index = ProjectRootManager.getInstance(directory.getProject()).getFileIndex(); if (rootTypes != null && !index.isUnderSourceRootOfType(directory.getVirtualFile(), rootTypes)) { Module module = ModuleUtilCore.findModuleForPsiElement(directory); if (module == null) return directory; ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel(); ContentEntry contentEntry = ContainerUtil.find(modifiableModel.getContentEntries(), entry -> entry.getFile() != null && VfsUtilCore.isAncestor(entry.getFile(), directory.getVirtualFile(), false)); if (contentEntry == null || !Objects.equals(contentEntry.getFile(), directory.getVirtualFile()) || contentEntry.getSourceFolders().length > 0) { return directory; } contentEntry.addSourceFolder(directory.getVirtualFile(), false); WriteAction.run(() -> modifiableModel.commit()); } return directory; }