Search in sources :

Example 6 with ModuleRootManager

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

the class SaveProjectAsTemplateAction method collectStructure.

@NotNull
private static List<LocalArchivedTemplate.RootDescription> collectStructure(Project project, Module moduleToSave) {
    List<LocalArchivedTemplate.RootDescription> result = new ArrayList<>();
    if (moduleToSave != null) {
        PathMacroManager macroManager = PathMacroManager.getInstance(moduleToSave);
        ModuleRootManager rootManager = ModuleRootManager.getInstance(moduleToSave);
        int i = 0;
        for (VirtualFile file : rootManager.getContentRoots()) {
            result.add(i, describeRoot(file, i, macroManager));
            i++;
        }
    } else {
        PathMacroManager macroManager = PathMacroManager.getInstance(project);
        ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
        int i = 0;
        for (VirtualFile file : rootManager.getContentRoots()) {
            result.add(i, describeRoot(file, i, macroManager));
            i++;
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) PathMacroManager(com.intellij.openapi.components.PathMacroManager) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with ModuleRootManager

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

the class WebModuleBuilder method doGenerate.

private static <T> void doGenerate(@NotNull WebProjectTemplate<T> template, @NotNull Module module) {
    WebProjectGenerator.GeneratorPeer<T> peer = template.getPeer();
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    VirtualFile[] contentRoots = moduleRootManager.getContentRoots();
    VirtualFile dir = module.getProject().getBaseDir();
    if (contentRoots.length > 0 && contentRoots[0] != null) {
        dir = contentRoots[0];
    }
    template.generateProject(module.getProject(), dir, peer.getSettings(), module);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WebProjectGenerator(com.intellij.platform.WebProjectGenerator) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager)

Example 8 with ModuleRootManager

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

the class CodeInsightTestCase method configureByFiles.

protected VirtualFile configureByFiles(@Nullable final File rawProjectRoot, @NotNull final VirtualFile... vFiles) throws IOException {
    myFile = null;
    myEditor = null;
    final File toDirIO = createTempDirectory();
    final VirtualFile toDir = getVirtualFile(toDirIO);
    ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
            final ModifiableRootModel rootModel = rootManager.getModifiableModel();
            if (clearModelBeforeConfiguring()) {
                rootModel.clear();
            }
            // auxiliary files should be copied first
            VirtualFile[] reversed = ArrayUtil.reverseArray(vFiles);
            Map<VirtualFile, EditorInfo> editorInfos;
            if (rawProjectRoot != null) {
                final File projectRoot = rawProjectRoot.getCanonicalFile();
                FileUtil.copyDir(projectRoot, toDirIO);
                VirtualFile fromDir = getVirtualFile(projectRoot);
                editorInfos = copyFilesFillingEditorInfos(fromDir, toDir, ContainerUtil.map2Array(reversed, String.class, s -> s.getPath().substring(projectRoot.getPath().length())));
                toDir.refresh(false, true);
            } else {
                editorInfos = new LinkedHashMap<>();
                for (final VirtualFile vFile : reversed) {
                    VirtualFile parent = vFile.getParent();
                    assert parent.isDirectory() : parent;
                    editorInfos.putAll(copyFilesFillingEditorInfos(parent, toDir, vFile.getName()));
                }
            }
            boolean sourceRootAdded = false;
            if (isAddDirToContentRoot()) {
                final ContentEntry contentEntry = rootModel.addContentEntry(toDir);
                if (isAddDirToSource()) {
                    sourceRootAdded = true;
                    contentEntry.addSourceFolder(toDir, isAddDirToTests());
                }
            }
            doCommitModel(rootModel);
            if (sourceRootAdded) {
                sourceRootAdded(toDir);
            }
            openEditorsAndActivateLast(editorInfos);
        } catch (IOException e) {
            LOG.error(e);
        }
    });
    return toDir;
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ContentEntry(com.intellij.openapi.roots.ContentEntry) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) IOException(java.io.IOException) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 9 with ModuleRootManager

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

the class TodoJavaTreeHelper method addPackagesToChildren.

@Override
public void addPackagesToChildren(final ArrayList<AbstractTreeNode> children, final Module module, final TodoTreeBuilder builder) {
    Project project = getProject();
    final PsiManager psiManager = PsiManager.getInstance(project);
    final List<VirtualFile> sourceRoots = new ArrayList<>();
    if (module == null) {
        final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
        ContainerUtil.addAll(sourceRoots, projectRootManager.getContentSourceRoots());
    } else {
        ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
        ContainerUtil.addAll(sourceRoots, moduleRootManager.getSourceRoots());
    }
    final Set<PsiPackage> topLevelPackages = new HashSet<>();
    for (final VirtualFile root : sourceRoots) {
        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);
                } else {
                    final Iterator<PsiFile> files = builder.getFiles(subdirectory);
                    if (!files.hasNext())
                        continue;
                    TodoDirNode dirNode = new TodoDirNode(project, subdirectory, builder);
                    if (!children.contains(dirNode)) {
                        children.add(dirNode);
                    }
                }
            }
            // add non-dir items
            final Iterator<PsiFile> filesUnderDirectory = builder.getFilesUnderDirectory(directory);
            for (; filesUnderDirectory.hasNext(); ) {
                final PsiFile file = filesUnderDirectory.next();
                TodoFileNode todoFileNode = new TodoFileNode(project, file, builder, false);
                if (!children.contains(todoFileNode)) {
                    children.add(todoFileNode);
                }
            }
        } else {
            // this is the case when a source root has pakage prefix assigned
            topLevelPackages.add(directoryPackage);
        }
    }
    GlobalSearchScope scope = module != null ? GlobalSearchScope.moduleScope(module) : GlobalSearchScope.projectScope(project);
    ArrayList<PsiPackage> packages = new ArrayList<>();
    for (PsiPackage psiPackage : topLevelPackages) {
        final PsiPackage aPackage = findNonEmptyPackage(psiPackage, module, project, builder, scope);
        if (aPackage != null) {
            packages.add(aPackage);
        }
    }
    for (PsiPackage psiPackage : packages) {
        if (!builder.getTodoTreeStructure().getIsFlattenPackages()) {
            PackageElement element = new PackageElement(module, psiPackage, false);
            TodoPackageNode packageNode = new TodoPackageNode(project, element, builder, psiPackage.getQualifiedName());
            if (!children.contains(packageNode)) {
                children.add(packageNode);
            }
        } else {
            Set<PsiPackage> allPackages = new HashSet<>();
            traverseSubPackages(psiPackage, module, builder, project, allPackages);
            for (PsiPackage aPackage : allPackages) {
                TodoPackageNode packageNode = new TodoPackageNode(project, new PackageElement(module, aPackage, false), builder);
                if (!children.contains(packageNode)) {
                    children.add(packageNode);
                }
            }
        }
    }
    final List<VirtualFile> roots = collectContentRoots(module);
    roots.removeAll(sourceRoots);
    addDirsToChildren(roots, children, builder);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PackageElement(com.intellij.ide.projectView.impl.nodes.PackageElement) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager)

Example 10 with ModuleRootManager

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

the class ModuleFixtureBuilderImpl method initModule.

protected void initModule(Module module) {
    final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
    final ModifiableRootModel rootModel = rootManager.getModifiableModel();
    for (String contentRoot : myContentRoots) {
        final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(contentRoot);
        Assert.assertNotNull("cannot find content root: " + contentRoot, virtualFile);
        final ContentEntry contentEntry = rootModel.addContentEntry(virtualFile);
        for (String sourceRoot : mySourceRoots) {
            String s = contentRoot + "/" + sourceRoot;
            VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByPath(s);
            if (vf == null) {
                final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(sourceRoot);
                if (file != null && VfsUtilCore.isAncestor(virtualFile, file, false))
                    vf = file;
            }
            //        assert vf != null : "cannot find source root: " + sourceRoot;
            if (vf != null) {
                contentEntry.addSourceFolder(vf, false);
            } else {
                // files are not created yet
                contentEntry.addSourceFolder(VfsUtilCore.pathToUrl(s), false);
            }
        }
    }
    setupRootModel(rootModel);
    rootModel.commit();
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentEntry(com.intellij.openapi.roots.ContentEntry) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager)

Aggregations

ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)47 VirtualFile (com.intellij.openapi.vfs.VirtualFile)26 Module (com.intellij.openapi.module.Module)19 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)13 ContentEntry (com.intellij.openapi.roots.ContentEntry)11 Sdk (com.intellij.openapi.projectRoots.Sdk)10 File (java.io.File)7 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)6 SourceFolder (com.intellij.openapi.roots.SourceFolder)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 ModuleManager (com.intellij.openapi.module.ModuleManager)2 OrderEntry (com.intellij.openapi.roots.OrderEntry)2 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)2 ProjectRootManagerEx (com.intellij.openapi.roots.ex.ProjectRootManagerEx)2 FilePaths.findParentContentEntry (com.android.tools.idea.gradle.util.FilePaths.findParentContentEntry)1 GradleUtil.getGradleBuildFile (com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile)1 CodeInsightTestCase (com.intellij.codeInsight.CodeInsightTestCase)1