Search in sources :

Example 11 with SourceFolder

use of com.intellij.openapi.roots.SourceFolder in project android by JetBrains.

the class ContentEntriesSetup method addSourceFolder.

private static void addSourceFolder(@NotNull ContentEntry contentEntry, @NotNull File folderPath, @NotNull JpsModuleSourceRootType type, boolean generated) {
    String url = pathToIdeaUrl(folderPath);
    SourceFolder sourceFolder = contentEntry.addSourceFolder(url, type);
    if (generated) {
        JpsModuleSourceRoot sourceRoot = sourceFolder.getJpsElement();
        JpsElement properties = sourceRoot.getProperties();
        if (properties instanceof JavaSourceRootProperties) {
            ((JavaSourceRootProperties) properties).setForGeneratedSources(true);
        }
    }
}
Also used : SourceFolder(com.intellij.openapi.roots.SourceFolder) JpsModuleSourceRoot(org.jetbrains.jps.model.module.JpsModuleSourceRoot) JpsElement(org.jetbrains.jps.model.JpsElement) JavaSourceRootProperties(org.jetbrains.jps.model.java.JavaSourceRootProperties)

Example 12 with SourceFolder

use of com.intellij.openapi.roots.SourceFolder in project android by JetBrains.

the class IdeFrameFixture method getSourceFolderRelativePaths.

/**
   * Returns a list of system independent paths
   */
@NotNull
public Collection<String> getSourceFolderRelativePaths(@NotNull String moduleName, @NotNull JpsModuleSourceRootType<?> sourceType) {
    Set<String> paths = Sets.newHashSet();
    Module module = getModule(moduleName);
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    execute(new GuiTask() {

        @Override
        protected void executeInEDT() throws Throwable {
            ModifiableRootModel rootModel = moduleRootManager.getModifiableModel();
            try {
                for (ContentEntry contentEntry : rootModel.getContentEntries()) {
                    for (SourceFolder folder : contentEntry.getSourceFolders()) {
                        JpsModuleSourceRootType<?> rootType = folder.getRootType();
                        if (rootType.equals(sourceType)) {
                            String path = urlToPath(folder.getUrl());
                            String relativePath = getRelativePath(myProjectPath, new File(toSystemDependentName(path)));
                            paths.add(PathUtil.toSystemIndependentName(relativePath));
                        }
                    }
                }
            } finally {
                rootModel.dispose();
            }
        }
    });
    return paths;
}
Also used : GuiTask(org.fest.swing.edt.GuiTask) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) SourceFolder(com.intellij.openapi.roots.SourceFolder) JpsModuleSourceRootType(org.jetbrains.jps.model.module.JpsModuleSourceRootType) ContentEntry(com.intellij.openapi.roots.ContentEntry) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) GradleUtil.getGradleBuildFile(com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile) Assert.assertNotNull(junit.framework.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with SourceFolder

use of com.intellij.openapi.roots.SourceFolder in project intellij-community by JetBrains.

the class JavaDirectoryIconProvider method getIcon.

@Override
@Nullable
public Icon getIcon(@NotNull PsiElement element, int flags) {
    if (element instanceof PsiDirectory) {
        final PsiDirectory psiDirectory = (PsiDirectory) element;
        final VirtualFile vFile = psiDirectory.getVirtualFile();
        final Project project = psiDirectory.getProject();
        SourceFolder sourceFolder;
        Icon symbolIcon;
        if (vFile.getParent() == null && vFile.getFileSystem() instanceof ArchiveFileSystem) {
            symbolIcon = PlatformIcons.JAR_ICON;
        } else if (ProjectRootsUtil.isModuleContentRoot(vFile, project)) {
            Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(vFile);
            symbolIcon = module != null ? ModuleType.get(module).getIcon() : PlatformIcons.CONTENT_ROOT_ICON_CLOSED;
        } else if ((sourceFolder = ProjectRootsUtil.getModuleSourceRoot(vFile, project)) != null) {
            symbolIcon = SourceRootPresentation.getSourceRootIcon(sourceFolder);
        } else if (JrtFileSystem.isModuleRoot(vFile)) {
            symbolIcon = AllIcons.Nodes.JavaModuleRoot;
        } else if (JavaDirectoryService.getInstance().getPackage(psiDirectory) != null) {
            symbolIcon = PlatformIcons.PACKAGE_ICON;
        } else if (!Registry.is("ide.hide.excluded.files") && ProjectRootManager.getInstance(project).getFileIndex().isExcluded(vFile)) {
            symbolIcon = AllIcons.Modules.ExcludeRoot;
        } else {
            symbolIcon = PlatformIcons.DIRECTORY_CLOSED_ICON;
        }
        return ElementBase.createLayeredIcon(element, symbolIcon, 0);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) SourceFolder(com.intellij.openapi.roots.SourceFolder) PsiDirectory(com.intellij.psi.PsiDirectory) Module(com.intellij.openapi.module.Module) ArchiveFileSystem(com.intellij.openapi.vfs.newvfs.ArchiveFileSystem) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with SourceFolder

use of com.intellij.openapi.roots.SourceFolder in project intellij-community by JetBrains.

the class ContentRootDataService method createSourceRootIfAbsent.

private static void createSourceRootIfAbsent(@NotNull ContentEntry entry, @NotNull final SourceRoot root, @NotNull String moduleName, @NotNull JpsModuleSourceRootType<?> sourceRootType, boolean generated, boolean createEmptyContentRootDirectories) {
    SourceFolder[] folders = entry.getSourceFolders();
    for (SourceFolder folder : folders) {
        VirtualFile file = folder.getFile();
        if (file == null) {
            continue;
        }
        if (ExternalSystemApiUtil.getLocalFileSystemPath(file).equals(root.getPath())) {
            final JpsModuleSourceRootType<?> folderRootType = folder.getRootType();
            if (JavaSourceRootType.SOURCE.equals(folderRootType) || sourceRootType.equals(folderRootType)) {
                return;
            }
            if (JavaSourceRootType.TEST_SOURCE.equals(folderRootType) && JavaResourceRootType.TEST_RESOURCE.equals(sourceRootType)) {
                return;
            }
            entry.removeSourceFolder(folder);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Importing %s for content root '%s' of module '%s'", root, entry.getUrl(), moduleName));
    }
    SourceFolder sourceFolder = entry.addSourceFolder(toVfsUrl(root.getPath()), sourceRootType);
    if (!StringUtil.isEmpty(root.getPackagePrefix())) {
        sourceFolder.setPackagePrefix(root.getPackagePrefix());
    }
    if (generated) {
        JavaSourceRootProperties properties = sourceFolder.getJpsElement().getProperties(JavaModuleSourceRootTypes.SOURCES);
        if (properties != null) {
            properties.setForGeneratedSources(true);
        }
    }
    if (createEmptyContentRootDirectories) {
        ExternalSystemApiUtil.doWriteAction(() -> {
            try {
                VfsUtil.createDirectoryIfMissing(root.getPath());
            } catch (IOException e) {
                LOG.warn(String.format("Unable to create directory for the path: %s", root.getPath()), e);
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFolder(com.intellij.openapi.roots.SourceFolder) IOException(java.io.IOException) JavaSourceRootProperties(org.jetbrains.jps.model.java.JavaSourceRootProperties)

Example 15 with SourceFolder

use of com.intellij.openapi.roots.SourceFolder in project intellij-community by JetBrains.

the class IdeFrameFixture method getSourceFolderRelativePaths.

@NotNull
public Collection<String> getSourceFolderRelativePaths(@NotNull String moduleName, @NotNull final JpsModuleSourceRootType<?> sourceType) {
    final Set<String> paths = new HashSet<>();
    Module module = getModule(moduleName);
    final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    execute(new GuiTask() {

        @Override
        protected void executeInEDT() throws Throwable {
            ModifiableRootModel rootModel = moduleRootManager.getModifiableModel();
            try {
                for (ContentEntry contentEntry : rootModel.getContentEntries()) {
                    for (SourceFolder folder : contentEntry.getSourceFolders()) {
                        JpsModuleSourceRootType<?> rootType = folder.getRootType();
                        if (rootType.equals(sourceType)) {
                            String path = urlToPath(folder.getUrl());
                            String relativePath = getRelativePath(myProjectPath, new File(toSystemDependentName(path)));
                            paths.add(relativePath);
                        }
                    }
                }
            } finally {
                rootModel.dispose();
            }
        }
    });
    return paths;
}
Also used : ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) GuiTask(org.fest.swing.edt.GuiTask) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) SourceFolder(com.intellij.openapi.roots.SourceFolder) JpsModuleSourceRootType(org.jetbrains.jps.model.module.JpsModuleSourceRootType) ContentEntry(com.intellij.openapi.roots.ContentEntry) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Assert.assertNotNull(junit.framework.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

SourceFolder (com.intellij.openapi.roots.SourceFolder)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)12 ContentEntry (com.intellij.openapi.roots.ContentEntry)7 Module (com.intellij.openapi.module.Module)6 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)5 NotNull (org.jetbrains.annotations.NotNull)5 ExcludeFolder (com.intellij.openapi.roots.ExcludeFolder)3 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)3 JavaSourceRootProperties (org.jetbrains.jps.model.java.JavaSourceRootProperties)3 JpsModuleSourceRootType (org.jetbrains.jps.model.module.JpsModuleSourceRootType)3 Project (com.intellij.openapi.project.Project)2 ContentFolder (com.intellij.openapi.roots.ContentFolder)2 File (java.io.File)2 Assert.assertNotNull (junit.framework.Assert.assertNotNull)2 GuiTask (org.fest.swing.edt.GuiTask)2 Nullable (org.jetbrains.annotations.Nullable)2 JpsElement (org.jetbrains.jps.model.JpsElement)2 JpsModuleSourceRoot (org.jetbrains.jps.model.module.JpsModuleSourceRoot)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 GradleUtil.getGradleBuildFile (com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile)1