Search in sources :

Example 41 with ModuleRootManager

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

the class InheritedJdkTest method test2.

public void test2() throws Exception {
    final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
    ModuleRootModificationUtil.setSdkInherited(myModule);
    assertTrue("JDK is inherited after inheritSdk()", rootManager.isSdkInherited());
    assertNull("No JDK assigned", rootManager.getSdk());
    final Sdk mockJdk = IdeaTestUtil.getMockJdk17("mock 1.4");
    ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(mockJdk));
    final ProjectRootManagerEx projectRootManager = ProjectRootManagerEx.getInstanceEx(myProject);
    ApplicationManager.getApplication().runWriteAction(() -> projectRootManager.setProjectSdk(mockJdk));
    assertTrue(rootManager.isSdkInherited());
    assertEquals("mockJdk inherited", mockJdk, rootManager.getSdk());
    ApplicationManager.getApplication().runWriteAction(() -> projectRootManager.setProjectSdkName("jdk1"));
    assertTrue(rootManager.isSdkInherited());
    Assert.assertEquals("Correct non-existing JDK inherited", "jdk1", rootManager.orderEntries().process(new RootPolicy<String>() {

        @Override
        public String visitInheritedJdkOrderEntry(InheritedJdkOrderEntry inheritedJdkOrderEntry, String s) {
            return inheritedJdkOrderEntry.getJdkName();
        }
    }, null));
    assertNull("Non-existing JDK", rootManager.getSdk());
    final Sdk jdk1 = IdeaTestUtil.getMockJdk17("jdk1");
    ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(jdk1));
    assertTrue(rootManager.isSdkInherited());
    assertNotNull("JDK appeared", rootManager.getSdk());
    assertEquals("jdk1 found", jdk1, rootManager.getSdk());
}
Also used : ProjectRootManagerEx(com.intellij.openapi.roots.ex.ProjectRootManagerEx) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Sdk(com.intellij.openapi.projectRoots.Sdk) RootPolicy(com.intellij.openapi.roots.RootPolicy) InheritedJdkOrderEntry(com.intellij.openapi.roots.InheritedJdkOrderEntry)

Example 42 with ModuleRootManager

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

the class MultiModuleEditingTest method testRenaming.

public void testRenaming() throws Exception {
    final ModuleManager moduleManager = ModuleManager.getInstance(myProject);
    final Module moduleA;
    final Module moduleB;
    {
        final ModifiableModuleModel moduleModel = moduleManager.getModifiableModel();
        moduleA = moduleModel.newModule("a.iml", StdModuleTypes.JAVA.getId());
        moduleB = moduleModel.newModule("b.iml", StdModuleTypes.JAVA.getId());
        final Module moduleC = moduleModel.newModule("c.iml", StdModuleTypes.JAVA.getId());
        final ModifiableRootModel rootModelB = ModuleRootManager.getInstance(moduleB).getModifiableModel();
        rootModelB.addModuleOrderEntry(moduleC);
        moduleModel.disposeModule(moduleC);
        ApplicationManager.getApplication().runWriteAction(() -> ModifiableModelCommitter.multiCommit(new ModifiableRootModel[] { rootModelB }, moduleModel));
    }
    final ModuleRootManager rootManagerB = ModuleRootManager.getInstance(moduleB);
    assertEquals(0, rootManagerB.getDependencies().length);
    final String[] dependencyModuleNames = rootManagerB.getDependencyModuleNames();
    assertEquals(1, dependencyModuleNames.length);
    assertEquals("c", dependencyModuleNames[0]);
    {
        final ModifiableModuleModel moduleModel = moduleManager.getModifiableModel();
        moduleModel.renameModule(moduleA, "c");
        assertSame(moduleA, moduleModel.findModuleByName("a"));
        assertSame(moduleA, moduleManager.findModuleByName("a"));
        assertEquals("c", moduleModel.getNewName(moduleA));
        assertSame(moduleA, moduleModel.getModuleToBeRenamed("c"));
        ApplicationManager.getApplication().runWriteAction(() -> moduleModel.commit());
    }
    assertEquals(1, rootManagerB.getDependencies().length);
    assertEquals(moduleA, rootManagerB.getDependencies()[0]);
    assertSame(moduleA, moduleManager.findModuleByName("c"));
    assertNull(moduleManager.findModuleByName("a"));
    assertEquals("c", moduleA.getName());
    moduleManager.disposeModule(moduleA);
    moduleManager.disposeModule(moduleB);
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module)

Example 43 with ModuleRootManager

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

the class FilePathReferenceProvider method getRoots.

@NotNull
public static Collection<PsiFileSystemItem> getRoots(@Nullable final Module thisModule, boolean includingClasses) {
    if (thisModule == null)
        return Collections.emptyList();
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(thisModule);
    Set<PsiFileSystemItem> result = ContainerUtil.newLinkedHashSet();
    final PsiManager psiManager = PsiManager.getInstance(thisModule.getProject());
    if (includingClasses) {
        VirtualFile[] libraryUrls = moduleRootManager.orderEntries().getAllLibrariesAndSdkClassesRoots();
        for (VirtualFile file : libraryUrls) {
            PsiDirectory directory = psiManager.findDirectory(file);
            if (directory != null) {
                result.add(directory);
            }
        }
    }
    VirtualFile[] sourceRoots = moduleRootManager.orderEntries().recursively().withoutSdk().withoutLibraries().sources().usingCache().getRoots();
    for (VirtualFile root : sourceRoots) {
        final PsiDirectory directory = psiManager.findDirectory(root);
        if (directory != null) {
            final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory);
            if (aPackage != null && aPackage.getName() != null) {
                // package prefix
                result.add(PackagePrefixFileSystemItemImpl.create(directory));
            } else {
                result.add(directory);
            }
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) NotNull(org.jetbrains.annotations.NotNull)

Example 44 with ModuleRootManager

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

the class PythonModuleBuilderBase method commitModule.

@Nullable
@Override
public Module commitModule(@NotNull Project project, @Nullable ModifiableModuleModel model) {
    Module module = super.commitModule(project, model);
    if (module != null && myGenerator != null) {
        ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
        VirtualFile[] contentRoots = moduleRootManager.getContentRoots();
        VirtualFile dir = module.getProject().getBaseDir();
        if (contentRoots.length > 0 && contentRoots[0] != null) {
            dir = contentRoots[0];
        }
        myGenerator.generateProject(project, dir, null, module);
    }
    return module;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with ModuleRootManager

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

the class PyPackageUtil method findRequirementsTxt.

@Nullable
public static VirtualFile findRequirementsTxt(@NotNull Module module) {
    final String requirementsPath = PyPackageRequirementsSettings.getInstance(module).getRequirementsPath();
    if (!requirementsPath.isEmpty()) {
        final VirtualFile file = LocalFileSystem.getInstance().findFileByPath(requirementsPath);
        if (file != null) {
            return file;
        }
        final ModuleRootManager manager = ModuleRootManager.getInstance(module);
        for (VirtualFile root : manager.getContentRoots()) {
            final VirtualFile fileInRoot = root.findFileByRelativePath(requirementsPath);
            if (fileInRoot != null) {
                return fileInRoot;
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Nullable(org.jetbrains.annotations.Nullable)

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